Hi there —
Welcome to Issue #12 of Vacuum & Analyze. Last issue ended with log_lock_waits turning on by default in PostgreSQL 19. This issue is about the kind of lock wait that setting is meant to catch, and the one that causes the most avoidable outages.
Picture a routine deploy. The migration adds one nullable column to a busy table. It is a metadata-only change and should finish in milliseconds. Instead, the application stops responding. Connections pile up, latency climbs, and the pool runs dry. Then the migration finally completes, and everything recovers as if nothing happened.
The migration was never slow. It was waiting, and while it waited, it blocked everyone else.
Locks are granted in line
Most ALTER TABLE forms need an ACCESS EXCLUSIVE lock. That lock conflicts with every other lock mode, including the ACCESS SHARE lock that a plain SELECT takes. So the ALTER TABLE cannot start until every transaction that has touched the table has finished.
Here is the part that surprises people. Lock requests queue up in order. Once the ALTER TABLE is waiting, any new query on that table, even a simple SELECT, has to line up behind it. Those queries do not conflict with the transaction that is blocking the ALTER TABLE. They conflict with the ALTER TABLE itself, which is waiting for its turn.
The sequence looks like this:
- A long-running transaction reads from the table. It might be a report, or a session sitting
idle in transactionafter aSELECT. - Your migration requests
ACCESS EXCLUSIVEand waits behind it. - Every new query on the table queues behind the migration.
- The application stalls until the first transaction ends.
Finding the blocker
During an incident, start with who is waiting and who they are waiting on. The locks command in pgtools shows current lock contention and blocking queries. It needs only bash and psql, and the scripts are designed to be read-only with short timeouts, so they are safe to run against a database that is already struggling:
./pgtools.sh locks "postgresql://user@host:5432/dbname"
Most of the sessions listed will be victims. The migration is the one the others are queued behind. Find the session at the head of the chain, the one the migration itself is waiting on, then check how long it has been open and what state it is in. The activity command reads pg_stat_activity for that:
./pgtools.sh activity "postgresql://user@host:5432/dbname"
A transaction that has been open for a long time and is sitting idle in transaction is the usual answer. Ending that session, or canceling the migration, clears the queue immediately.
Run DDL so that it gives up
The fix is not to make the migration faster. It is to make it unwilling to wait. Set a short lock_timeout in the same session before the DDL:
SET lock_timeout = '2s';ALTER TABLE orders ADD COLUMN shipped_at timestamptz;
If the lock is not available within two seconds, the statement fails with an error and leaves the queue. The cost to the application is two seconds of delay at worst, not an outage. Wrap the migration in a retry with backoff, and it will succeed at the first quiet moment.
Two companion settings are worth having. idle_in_transaction_session_timeout removes the sessions that most often cause this in the first place. And log_lock_waits records any wait longer than deadlock_timeout, so the next occurrence leaves evidence in the log.
Two migrations that hold the lock too long
Waiting is one failure mode. Holding the lock while scanning the whole table is the other. Two common cases have safer forms.
Adding a foreign key. A plain ADD FOREIGN KEY validates every existing row while holding a lock that blocks writes. Split it in two. The first step is quick, and the second validates under a weaker lock that allows normal reads and writes:
ALTER TABLE orders ADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id) NOT VALID;ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_fk;
Setting a column NOT NULL. This scans the table under ACCESS EXCLUSIVE. Since PostgreSQL 12, if a validated CHECK (col IS NOT NULL) constraint already exists, SET NOT NULL skips the scan. So add the check as NOT VALID, validate it, then set the column:
ALTER TABLE orders ADD CONSTRAINT orders_shipped_at_nn CHECK (shipped_at IS NOT NULL) NOT VALID;ALTER TABLE orders VALIDATE CONSTRAINT orders_shipped_at_nn;ALTER TABLE orders ALTER COLUMN shipped_at SET NOT NULL;ALTER TABLE orders DROP CONSTRAINT orders_shipped_at_nn;
Both patterns still take a brief lock for the quick step, so keep lock_timeout set for those as well.
The mental model
A lock request has a cost even before it is granted. A waiting ACCESS EXCLUSIVE request blocks everything behind it, so the length of the wait matters as much as the length of the work.
Before any migration on a busy table, ask two questions:
- What lock does this take, and what does it conflict with? If the answer is “everything,” it needs a timeout.
- Does it hold that lock while doing real work? If so, split it so the strong lock covers only the instant change.
Reply and tell me: what is the worst migration-day outage you have caused or inherited? I read every reply.
— Gio

Leave a Reply