Skip to content
← All posts

Nov 29, 2026

Your migration locked the table — and the dashboard went with it

A one-line ALTER TABLE runs in milliseconds in staging and freezes the live dashboard in production. The lock isn't the problem — the queue behind it is.

PostgreSQLmigrationsNode.js

A one-line migration runs in milliseconds in staging. In production it freezes the dashboard. Not an error page, not a crash — frozen. Every page that touches the table just hangs, and then the whole app starts timing out.

The migration is still running. It has been running for minutes, and it will keep running until the table is rewritten. That is the part nobody tells you about migrations: the statement is not the dangerous part. The lock is.

The lock is exclusive, and the queue is the killer

Most forms of ALTER TABLE take an ACCESS EXCLUSIVE lock on the table. That is the strongest lock Postgres has. While it is held, nothing else can touch the table — not a SELECT, not an INSERT, not the dashboard's little count query.

For a simple ADD COLUMN that lock is held for milliseconds, because on Postgres 11 and later, adding a column with a constant default is a metadata change — the default is stored in the catalog, no rows are rewritten. So the lock itself is rarely the problem. The queue is.

Here is how a two-millisecond lock freezes a site for minutes. A long-running report query is already holding a lightweight lock on the table. Your ALTER TABLE asks for the exclusive lock and has to wait for the report to finish. That is fine on its own. But while your ALTER waits, it sits at the front of the lock queue — and every new query that arrives now queues behind it, even though the ALTER has not started yet. The dashboard's queries pile up, the connection pool fills — the same exhaustion from the November 20 post — and the app stops answering.

The fix is one line at the top of every migration:

SET lock_timeout = '3s';
ALTER TABLE orders ADD COLUMN status_code text;

lock_timeout tells Postgres to give up if it cannot get the lock within three seconds. A blocked migration fails, the queue behind it drains at once, and you retry the migration when the table is quiet instead of taking the site down with it. A failed migration is a retry. A queued migration is an outage.

Indexes: never build one on a live table without CONCURRENTLY

A plain CREATE INDEX lets reads continue but makes every INSERT, UPDATE and DELETE on the table wait until the build finishes. On a table with millions of rows that is minutes or hours of frozen writes.

CREATE INDEX CONCURRENTLY idx_orders_status ON orders (status);

CONCURRENTLY builds the index in the background while reads and writes continue. It costs two passes over the table instead of one, so it takes longer overall — that is the trade, and it is worth it every time.

Two rules. First, it cannot run inside a transaction block, so if your migration tool wraps every file in a transaction, you have to tell it not to for this one — golang-migrate has a NO TRANSACTION annotation, Flyway has executeInTransaction=false. Second, a concurrent build can fail and leave behind an invalid index that Postgres will not use. Check for it after the build and drop it before retrying:

SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid;

The safe way to change a column

Adding a column is safe. Changing one is where the rewrites hide. ALTER COLUMN TYPE rewrites the whole table. A volatile default — now(), clock_timestamp() — rewrites the whole table even on a plain ADD COLUMN, because Postgres cannot store a value it has to compute per row in the catalog. The safe versions are boring on purpose:

  • Add the column nullable, with no default, or with a constant default.
  • Backfill old rows in batches, never one giant UPDATE — one transaction holding row locks on the whole table and bloating the write-ahead log is a second outage wearing a different name.
  • Add the NOT NULL constraint in two steps: a CHECK constraint marked NOT VALID first, which takes only a brief lock and does not scan existing rows, then VALIDATE CONSTRAINT, which scans under a light lock that does not block reads or writes. On Postgres 12 and later, SET NOT NULL after that skips its own scan entirely, because the valid CHECK already proves there are no NULLs.
SET lock_timeout = '2s';
ALTER TABLE users ADD CONSTRAINT users_full_name_nn
  CHECK (full_name IS NOT NULL) NOT VALID;

ALTER TABLE users VALIDATE CONSTRAINT users_full_name_nn;
ALTER TABLE users ALTER COLUMN full_name SET NOT NULL;
ALTER TABLE users DROP CONSTRAINT users_full_name_nn;

Renames take three deploys, not one

Renaming a column looks like a one-line change, and the lock is only held for milliseconds. The lock is not the problem here — the running code is. During a rolling deploy, the old version of the app and the new version run side by side, and one of them is always querying a column that no longer exists.

The fix is expand and contract, and it takes three deploys:

  1. Expand. Add the new column. Old code does not know it exists, so nothing breaks.
  2. Migrate. Have the code write to both columns and still read the old one. Backfill the old rows into the new column in small batches, committing between batches, stepping around rows live traffic is writing with FOR UPDATE SKIP LOCKED.
  3. Contract. Switch reads to the new column, stop writing the old one, and drop it in a later deploy — only after nothing uses it anymore.
-- backfill, run repeatedly until zero rows are affected
WITH batch AS (
  SELECT id FROM orders
  WHERE amount_cents IS NULL
  LIMIT 5000
  FOR UPDATE SKIP LOCKED
)
UPDATE orders o
SET amount_cents = o.amount
FROM batch
WHERE o.id = batch.id;

Every step is compatible with the code running before and after it, so every step can be rolled back by redeploying the previous version. A rename done in one step cannot be rolled back without another rename.

The honest limits

None of this matters on a small table. If your table has a few thousand rows, a plain migration finishes before anyone notices, and three deploys for a rename is ceremony you do not need. Check the table size first — pg_total_relation_size will tell you in one query — and save the discipline for the tables that actually hurt.

And lock_timeout does not help a statement that already holds the lock. It only stops a migration from waiting forever to get one. A migration that is already rewriting a million-row table will keep rewriting it; the timeout that saves you there is the one you set before you ran it, by choosing a pattern that never rewrites in the first place.

A migration is a deploy. It changes the thing every request depends on, while requests are in flight. Treat it with the same care you would treat a deploy to the code that serves those requests — small steps, each one safe on its own, and nothing that can queue the whole site behind a lock.