9

I'm using "ALTER SEQUENCE sequence OWNED BY table.column" to change sequence association in Postgres. Is there a way to view this new information using \ds or something similar? \ds still shows the table owner as the sequence owner.

0

4 Answers 4

11
SELECT c.relname,u.usename 
  FROM pg_class c, pg_user u
 WHERE c.relowner = u.usesysid and c.relkind = 'S'
   AND relnamespace IN (
                        SELECT oid
                          FROM pg_namespace
                         WHERE nspname NOT LIKE 'pg_%'
                           AND nspname != 'information_schema'
                        );
7

OWNED BY table.column changes the associated column; OWNER TO newowner changes the role which owns the sequence. The owner role is what is displayed in \ds. I don't know a psql command to see the linked column of a sequence. OWNED BY is primary used to cascade a delete of the associated sequence when the referent column is removed.

You can get visibility into the owned by column by constructing a query against the system catalog tables pg_class, pg_depend, and pg_attribute. I'm not sure of all the exact semantics of pg_depend, but this query should show you a sequence's column dependency:

select tab.relname as tabname, attr.attname as column
from pg_class as seq
join pg_depend as dep on (seq.relfilenode = dep.objid)
join pg_class as tab on (dep.refobjid = tab.relfilenode)
join pg_attribute as attr on (attr.attnum = dep.refobjsubid and attr.attrelid = dep.refobjid)
where seq.relname = 'sequence';
1
  • this query is not 100% correct - there should be appended a constraint - dep.classid = 1259 Nov 10, 2017 at 10:44
0

Yuri Levinsky's answer modified to include the namespace column:

SELECT n.nspname, c.relname, u.usename
FROM pg_class c
INNER JOIN pg_user u ON c.relowner = u.usesysid
INNER JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE c.relkind = 'S'
  AND n.nspname NOT LIKE 'pg_%'
  AND n.nspname != 'information_schema'
ORDER BY 1, 2, 3;
0

Would you also be able to change the owner on the sequences to zeus_eucus where the owner is postgres ? Otherwise I’m unable to modify them.

--list all the database sequences:

SELECT * FROM pg_sequences;

--Changing sequenceowner of a sequence:

$ ALTER SEQUENCE <schema_name>.<sequence_name> OWNERTO <new_name>;

1
  • pg_sequences does not exist on older postgresql.
    – Eric
    Jul 12 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.