Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:44:27+00:00 2026-05-27T01:44:27+00:00

We are using PostgreSQL. My requirement is to delete unused sequences from my database.

  • 0

We are using PostgreSQL. My requirement is to delete unused sequences from my database.
For example, if I create any table through my application, one sequence will be created, but for deleting the table we are not deleting the sequence, too. If want to create the same table another sequence is being created.

Example: table: file; automatically created sequence for id coumn: file_id_seq

When I delete the table file and create it with same name again, a new sequence is being created (i.e. file_id_seq1). I have accumulated a huge number of unused sequences in my application database this way.

How to delete these unused sequences?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:44:27+00:00Added an answer on May 27, 2026 at 1:44 am

    A sequence that is created automatically for a serial column is deleted automatically, when the column (or its table) is dropped. The problem you describe should not exist to begin with. Only very old versions of PostgreSQL did not do that. 7.4 or older?

    Solution for the problem

    This query will generate the DDL commands to delete all "unbound" sequences in the database it is executed in:

    SELECT string_agg('DROP SEQUENCE ' || c.oid::regclass, '; ') || ';' AS ddl
    FROM   pg_class       c
    LEFT   JOIN pg_depend d ON d.refobjid = c.oid
                           AND d.deptype <> 'i'
    WHERE  c.relkind = 'S'
    AND    d.refobjid IS NULL;
    

    The cast to regclass in c.oid::regclass automatically schema-qualifies sequence names where necessary according to the current search_path. See:

    • How to check if a table exists in a given schema
    • How does the search_path influence identifier resolution and the "current schema"

    Result:

    DROP SEQUENCE foo_id_seq;
    DROP SEQUENCE bar_id_seq;
    ...
    

    Execute the result to drop all sequences that are not bound to a serial column (or any other column). Study the meaning of columns and tables here.

    Careful! These sequences might be in use otherwise. There are use cases where sequences are created as standalone objects. For instance, if you want multiple columns to share one sequence. You should know exactly what you are doing.

    However, you cannot delete sequences bound to a serial column this way. So the operation is safe in this respect.

    DROP SEQUENCE test_id_seq;
    

    Result:

    ERROR:  cannot drop sequence test_id_seq because other objects depend on it
    DETAIL:  default for table test column id depends on sequence test_id_seq
    HINT:  Use DROP ... CASCADE to drop the dependent objects too.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I updated a database table using postgresql from python My code was import psycopg2
I am using PostgreSQL Database. I want to get a column values from a
I've been using PostgreSQL a little bit lately, and one of the things that
Right now we are using PostgreSQL 8.3 (on Linux) as a database backend to
I am using PostgreSQL in a project I am working on, and one of
I am switching from using SQLite3 to PostgreSQL, and hoped that I could populate
How can i change database encoding for a PostgreSQL database using sql or phpPgAdmin?
I'm debeloping a Java Swing application, which persists the information through Hibernate, currently using
Using PostgreSQL, supposing a table like the following: 12184 | 4 | 83 12183
I am using PostgreSQL database. I have the data like below. id name1 name2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.