skip to Main Content

Do we have any query providing the sequences which are NOT attached to any of the tables in Postgres?

I would like to attach them back to tables.

2

Answers


  1. Take all sequences that have no dependency on a column:

    SELECT oid::regclass
    FROM pg_class
    WHERE relkind = 'S'
    EXCEPT ALL
    SELECT objid::regclass
    FROM pg_depend
    WHERE classid = 'pg_class'::regclass
      AND refclassid = 'pg_class'::regclass
      AND refobjsubid <> 0;
    
    Login or Signup to reply.
  2. To attach ("bind") a sequence to a table column:

    ALTER SEQUENCE my_seq OWNED BY my_table.my_column;
    

    See:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search