The relationship between automatically generated sequences (such as those created for SERIAL columns) and the parent table is modeled by the sequence owner attribute.
You can change this ratio using the OWNED BY ALTER SEQUENCE commmand clause
eg. ALTER SEQUENCE foo_id OWNED by foo_schema.foo_table
to bind it to foo_table
or ALTER SEQUENCE foo_id ASSEMBLED NONE
to break the join between a sequence and any table
Information about this link is stored in the pg_depend directory table .
the join relation is the relationship between pg_depend.objid → pg_class.oid WHERE relkind = 'S' - which associates the sequence with the union record, and then pg_depend.refobjid → pg_class.oid WHERE relkind = 'r', which associates the connection record with the owner relation (table)
This query returns all the dependencies of the sequence -> tables in the database. The where clause filters it only to include automatically generated relationships, which limits it to displaying only sequences created by columns printed by SERIAL.
WITH fq_objects AS (SELECT c.oid,n.nspname || '.' ||c.relname AS fqname , c.relkind, c.relname AS relation FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ), sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'), tables AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' ) SELECT s.fqname AS sequence, '->' as depends, t.fqname AS table FROM pg_depend d JOIN sequences s ON s.oid = d.objid JOIN tables t ON t.oid = d.refobjid WHERE d.deptype = 'a' ;
cms Jul 24 '12 at 11:35 2012-07-24 11:35
source share