How do I write a query to determine if I'm running out of ID space across all tables?
Solution 1:
To find the table a sequence is "related" to, you can use something like this:
SELECT seq_ns.nspname as sequence_schema,
seq.relname as sequence_name,
tab_ns.nspname as table_schema,
tab.relname as related_table,
s.last_value,
s.max_value
FROM pg_catalog.pg_class seq
INNER JOIN pg_catalog.pg_namespace seq_ns ON seq.relnamespace = seq_ns.oid
INNER JOIN pg_catalog.pg_depend d ON d.objid = seq.oid AND d.deptype = 'a'
INNER JOIN pg_catalog.pg_class tab ON d.objid = seq.oid AND d.refobjid = tab.oid
INNER JOIN pg_catalog.pg_namespace tab_ns ON tab.relnamespace = tab_ns.oid
INNER JOIN pg_catalog.pg_sequences s ON s.schemaname = seq_ns.nspname AND s.sequencename = seq.relname
WHERE seq.relkind = 'S'
AND seq_ns.nspname = '<your-schema-name>';