Menu

1 / 3

PostgreSQL Unused Sequences: How to Find Them, and Which Ones Are Safe to Drop

DEV Community: sql·Son Tran·9 days ago
#fXmxsfp4
#devtosql#devto#dev#sequence#default
Long article (2359 words) — no AI summary yet.
Reading 0:00
15s threshold

Disclosure: I build Schemity , a desktop ERD tool - this post is from our blog and uses it for the examples.

TL;DR: The catalog answers half the question: a sequence attached to a column, used by a column default or by a BEGIN ATOMIC function cannot be dropped by accident, and PostgreSQL says so. A legacy default written as nextval('s'::text) , a function whose body is a string and application code record no dependency at all, so the drop succeeds and the next insert fails. Find those by searching defaults and function bodies, then watch last_value for a while before dropping anything.

You find unused sequences in PostgreSQL in two passes: the catalog tells you which sequences a column, a default or a function provably depends on, and a text search finds the callers the catalog never recorded. The second pass matters more than it looks.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.