r/Supabase • • 1d ago

database 3 Postgres migration patterns that silently lock production tables (and how to catch them in PRs)

We’ve all seen a migration pass staging in 50ms and then lock a production table for 20 seconds under real traffic. After reviewing dozens of incident post-mortems, almost all avoidable Postgres migration outages come down to three patterns: 1. Adding a NOT NULL column without a DEFAULT Running `ALTER TABLE users ADD COLUMN email text NOT NULL;` acquires an ACCESS EXCLUSIVE lock and rewrites/validates the table. On a busy table, every read and write queues up behind it. Fix: Add the column as nullable first, backfill in batches, then add a `NOT NULL` constraint using `NOT VALID` + `VALIDATE CONSTRAINT`. 2. Creating an index without CONCURRENTLY `CREATE INDEX idx_users_email ON users (email);` blocks all writes (`INSERT`, `UPDATE`, `DELETE`) on `users` until the index build finishes. Fix: Always use `CREATE INDEX CONCURRENTLY` (outside a transaction block). 3. Running ALTER TABLE without a lock_timeout Even a fast metadata-only `ALTER TABLE` needs an `ACCESS EXCLUSIVE` lock. If a long-running analytics query is holding a read lock on that table, your migration waits in the queue—and every subsequent query piles up behind your migration, causing a cascading outage. Fix: Always prepend `SET lock_timeout = '2s';` so the migration fails fast instead of taking down the app. To stop relying on human code review for this, I put together a lightweight static analyzer (`pgguard`) that parses SQL migrations in PRs and flags these exact lock risks before merge. Curious what other Postgres migration footguns you’ve hit in production that static checks should catch?

1 Upvotes

11 comments sorted by

1

u/Appropriate_Dare648 23h ago

Little detail but #1 doesn't rewrite the table, it just errors out on the first non-empty table. So your analyzer is flagging a migration that would've died in 3ms anyway, which is a nicer failure mode than the one you're describing.

1

u/Plus-Entertainer2699 23h ago

who even reads migration files

1

u/Delicious-Panda1217 23h ago

your first pattern doesn't rewrite anything, postgres just errors out on a non-empty table. rough look for a tool that parses migrations

1

u/Affectionate-Bit3278 22h ago

the first one is just wrong. ALTER TABLE users ADD COLUMN email text NOT NULL doesn't rewrite or validate anything, it fails instantly the second there's a single row in the table because postgres has no value to put in it. the rewrite was the DEFAULT case and even that's catalog-only since 11. so the scary lock you're describing never happens, the one that actually scans everything is the SET NOT NULL you mention two lines later, which is exactly the thing squawk already flags and you'd know that if you'd run it once.

and honestly, another linter? squawk covers the index and the column type, atlas covers the destructive stuff, and now pgguard parses the same migration file and prints the same three sentences into the same PR that nobody reads. the catalogue was never the problem. the lock_timeout at least fails fast on its own, you don't need a bot to tell you to type it in the migration file.

1

u/Weak_Particular_5799 17h ago

and the fix for that SET NOT NULL step as written (NOT NULL ... NOT VALID then VALIDATE) only exists on PG18+. On 17 and older you do it with CHECK (email IS NOT NULL) NOT VALID, validate it, then SET NOT NULL sees the check and skips the scan. On 3M rows that was 1 ms vs 1013 ms for me.

1

u/Significant_Cap1397 19h ago

Pattern 1 as written doesn't do what you say. ÀDD COLUMN email text NOT NULL`with no default errors out immediately on any table with rows, so there's no lock to hold and nothing to rewrite. The rewrite case is a volatile DEFAULT, and even a constant default has been metadata-only since 11. Do you have a post-mortem behind that first one or is it just the pattern everyone repeats?

1

u/NecessaryBitter6498 14h ago

adding a NOT NULL column without a default doesn't rewrite the table, it just fails on the first row.

1

u/Leading_Tour8056 13h ago

your #1 doesnt rewrite anything, it just errors out on the first non empty table. hard to trust the other two after that

1

u/812126 13h ago

Small thing: ADD COLUMN NOT NULL without a default doesn't rewrite anything, it just errors out on the first non-empty table. And the session holding the read lock in your point 3 is almost never analytics, it's a psql tab someone opened on Tuesday to check one row and never closed.

1

u/Signal_Notice_464 12h ago

your point 1 doesnt actually work the way you say. add column not null with no default doesnt rewrite and validate the table, it just dies on the first row because postgres has no value to put there. the thing that rewrites is a default that isnt constant. so the pattern you describe is the version that fails in 2ms and never reaches prod, its the safe failure.

and the lock_timeout thing, come on, every postgres migration post since forever says put SET lock_timeout at the top, squawk has been flagging create index without concurrently for years, strong_migrations too. saying 'stop relying on human review' when you've listed the three footguns that everyone already has a linter for is a bit rich. the one that actually eats people is alter column type on a table someone assumed was small, and that isnt in your three at all, it rewrites the whole thing under access exclusive and no diff review will tell you how long

1

u/Ill_Speed_6923 12h ago

Adding a NOT NULL column with no default doesn't rewrite or validate anything, it just fails outright on any table that already has rows. The rewrite case is a volatile default, which is a different pattern from the one you listed.