On PostgreSQL 11 or later, adding a nullable column to a table with 200 million rows typically completes in a few milliseconds. The same statement can also freeze every query on that table for four minutes. Both outcomes come from one command, and the difference has almost nothing to do with the size of the table.

That is the reframe this memo argues for. Teams worry about ALTER TABLE ... ADD COLUMN rewriting a huge table, and on modern Postgres that is rarely the problem. The real risk is the lock it needs and the queue that forms behind it. Get the lock handling right and most column adds become boring.

ADD COLUMN always takes the strongest lock, but usually only for an instant

Almost every form of ALTER TABLE ... ADD COLUMN acquires an ACCESS EXCLUSIVE lock on the table. That lock conflicts with everything, including the ACCESS SHARE lock a plain SELECT holds. While it is held, nobody can read or write the table.

What changed in PostgreSQL 11, released in 2018, is how long the lock is held. Before version 11, adding a column with a DEFAULT forced Postgres to rewrite every row to store the new value, which on a large table meant minutes or hours under that lock. From 11 onward, a non volatile default is stored once in the system catalog, in pg_attribute, and existing rows return it on read without being touched. A nullable column with no default was always a catalog only change.

So for the common cases, the actual work is a catalog update that takes milliseconds. A few forms still rewrite the table or do heavy work under the lock, and those are worth knowing by name.

ADD COLUMN form Table rewrite or full scan? Notes
ADD COLUMN note text No Catalog only on every supported version
ADD COLUMN status text DEFAULT 'new' No on 11+, yes before 11 Constant default stored in the catalog
ADD COLUMN created_at timestamptz DEFAULT now() No on 11+ now() is stable, evaluated once for existing rows
ADD COLUMN token uuid DEFAULT gen_random_uuid() Yes Volatile default: every existing row needs its own value
ADD COLUMN id2 bigserial or GENERATED ... AS IDENTITY Yes Each existing row needs a sequence value
ADD COLUMN total numeric GENERATED ALWAYS AS (a + b) STORED Yes Stored generated columns are computed for every row
ADD COLUMN x int NOT NULL DEFAULT 0 No on 11+ The default satisfies NOT NULL without a scan
ADD COLUMN code text UNIQUE Index build Builds a unique index while holding the lock
ADD COLUMN user_id bigint REFERENCES users(id) Validation scan, plus a lock on users Use NOT VALID, then VALIDATE separately

The now() row surprises people. Existing rows all get the same timestamp, the moment of the migration, which is often not what the developer wanted. If each row should get a distinct value, that is a backfill job, not a default.

The outage comes from the lock queue, not the change

PostgreSQL grants locks in queue order. When your migration asks for ACCESS EXCLUSIVE and some other session already holds a conflicting lock, the migration waits. Every query that arrives after it and needs any lock on that table, even a simple SELECT, now waits behind the migration, because granting it would let it jump the queue.

That is the mechanism behind most "the column add took down production" stories. A reporting query or a forgotten transaction has held ACCESS SHARE on the table for twenty minutes. The migration starts, cannot get its lock, and sits in the queue.

Within seconds, every request from the application that touches that table is stuck behind it. The connection pool fills up, health checks time out and the load balancer starts returning errors. When the long query finally ends, the migration runs in 5 milliseconds and everything recovers, which makes the incident look mysterious in hindsight.

The usual culprits are easy to list: an analyst running a long query on the primary, a batch job inside one huge transaction, an ORM session left "idle in transaction" after an exception, or a logical replication or backup tool holding locks longer than expected. You can see them while it happens:

SELECT pid, state, now() - xact_start AS xact_age,
       pg_blocking_pids(pid) AS blocked_by, left(query, 60)
FROM pg_stat_activity
WHERE datname = current_database()
ORDER BY xact_age DESC NULLS LAST;

The blocked_by column, from pg_blocking_pids(), shows which session each waiting process is stuck behind. In a lock queue incident you typically see one old transaction at the top, the migration blocked by it, and dozens of application queries blocked by the migration. Cancelling the migration with pg_cancel_backend() on its pid is the fastest way out mid incident; killing the old transaction works too, but someone may lose an hour of analysis.

The fix: give up fast, retry, and keep transactions short

The single most effective change is a lock timeout on the migration session. If the lock is not granted quickly, the statement fails instead of queueing, and the application never notices.

SET lock_timeout = '3s';
SET statement_timeout = '30s';
ALTER TABLE orders ADD COLUMN gift_note text;

A failed attempt costs you at most 3 seconds of queued queries, which a well behaved application absorbs. Wrap the statement in a retry loop with backoff, for example five attempts spaced 10, 20, 40 and 80 seconds apart, and most migrations succeed on the first or second try once the blocking query ends. The statement_timeout is a second guard in case the statement turns out to be one of the rewriting forms from the table above.

Three habits make the retries rarely necessary. First, set idle_in_transaction_session_timeout on application roles, for example to 60 seconds, so forgotten transactions cannot hold locks indefinitely. Second, keep long analytical queries off the primary, on a replica or a warehouse. Third, run one DDL statement per transaction: a migration file that adds a column and then backfills ten million rows in the same transaction holds ACCESS EXCLUSIVE for the whole backfill.

For the heavier forms, split the work so the strong lock is held only for catalog changes:

  • Volatile default: add the column as nullable, backfill in batches of a few thousand rows, then set the default for new rows.
  • Foreign key: add the column, then ADD CONSTRAINT ... FOREIGN KEY ... NOT VALID, then VALIDATE CONSTRAINT in a separate step, which takes a weaker SHARE UPDATE EXCLUSIVE lock.
  • Unique: CREATE UNIQUE INDEX CONCURRENTLY, then ADD CONSTRAINT ... UNIQUE USING INDEX.
  • NOT NULL on an existing column: add a CHECK (col IS NOT NULL) NOT VALID constraint, validate it, and from PostgreSQL 12 a later SET NOT NULL skips the full scan.

Migration linters such as Squawk for SQL files, or the strong_migrations gem in Rails projects, flag these patterns in review before they reach production. They are worth adding to CI for any team that ships schema changes weekly.

An illustrative incident and what would have prevented it

This mini case is illustrative, a composite of a pattern that is common in production postmortems. A mid sized e-commerce company runs PostgreSQL 15 with an orders table of about 40 million rows. A developer ships a migration that adds a nullable gift_note column during working hours, confident that it is a catalog only change. They are right about that part.

At the same moment, a finance analyst is running a 25 minute revenue query on the primary. It has held ACCESS SHARE on orders for 18 minutes. The migration requests ACCESS EXCLUSIVE and waits. Checkout and order history requests start queueing behind it. The application's connection pool of 100 connections is full within roughly 30 seconds, and the API begins returning errors. Seven minutes later the analyst's query finishes, the column is added in about 4 milliseconds and the queue drains.

Nothing in the migration was slow. Three small things would each have prevented the outage: SET lock_timeout = '3s' with a retry loop, which would have failed silently and succeeded seven minutes later; a statement timeout or a replica for the analyst's query; and a deploy check that refuses DDL while any transaction on the target table is older than, say, 60 seconds.

Questions about this incident pattern are a common way for platform and backend interviewers to separate candidates who have run Postgres in production from those who have only read about it. The candidate who mentions the lock queue before the table size usually has the scar.

The verdict: trade a few failed attempts for no outages

The trade off is simple. Without a lock timeout, migrations always succeed on the first attempt, and occasionally they take production down. With a short lock timeout and retries, some migrations fail and need another try, deploys can take a few minutes longer, and the database never builds a queue behind DDL. For any table on a request path, the second option wins easily; the cost is a little patience and a retry wrapper.

The strongest objection comes from teams whose deploy pipeline runs migrations and code release as one atomic step. A migration that gives up after 3 seconds can leave the pipeline half done: the new code expects a column that does not exist yet. That objection is real, and the answer is to decouple the two, not to drop the timeout.

Ship the column first, in its own deploy, with code that tolerates its absence; release the code that uses it in the next deploy. This expand and contract pattern costs one extra deploy per schema change and removes the ordering risk entirely. Some teams go further and schedule DDL for low traffic hours, which helps but does not remove the queue risk, because a long transaction can be running at 3 a.m. too.

Size still matters for the forms that rewrite the table, and on a version older than 11 a default on a large table remains a planned maintenance job. On a modern Postgres, though, treat the lock as the risk and the table size as a detail. Set the timeout, split the heavy forms, and keep long transactions off the primary.