Home > database >  Postgres list sequence dependencies
Postgres list sequence dependencies

Time:11-25

How to get a list of all columns (and tables) using a sequence in postgres? I am looking for a result that looks something like the dependencies report from pgAdmin 4 shown below.

Thank you.

(in pgAdmin 4 when clicking the sequence object and then the dependency tab).
pgAdmin dependencies report

CodePudding user response:

To find the table and column that belongs to a sequence s, run

SELECT d.objid::regclass AS sequence_name,
       a.attrelid::regclass AS table_name,
       a.attname AS column_name
FROM pg_depend AS d
   JOIN pg_attribute AS a
      ON d.refobjid = a.attrelid
        AND d.refobjsubid = a.attnum
WHERE d.objid = 's'::regclass
  AND d.refclassid = 'pg_class'::regclass;
  • Related