Skip to main content
Interactive pgtune Configurator →Open Architecture Blueprint ↗
P0 INCIDENTL6 BARM02 · 5 MIN READ

PostgreSQL ALTER TABLE Lock-Queue Outage & Zero-Downtime DDL

Understand why a waiting ALTER TABLE ACCESS EXCLUSIVE lock freezes every SELECT and UPDATE behind it, and ship safe zero-downtime Postgres migrations.

PRODUCTION SYMPTOM & ALERT SIGNATURE

Seconds after deploying a seemingly tiny ALTER TABLE ADD COLUMN or ADD CONSTRAINT migration, all unrelated SELECT and UPDATE queries on that table hang and API pods exhaust their connection pools.

pg_locks_count{mode="AccessExclusiveLock",granted="false"} > 0 AND pg_stat_activity_count{wait_event_type="Lock"} > 20
01 · ROOT-CAUSE MECHANICS (WHY IT HAPPENS)

Every SELECT acquires an ACCESS SHARE lock; ALTER TABLE requires an ACCESS EXCLUSIVE lock, which conflicts with every other lock mode including ACCESS SHARE.

Even if the ALTER TABLE itself would execute in 2ms, it cannot start until an existing 45-second analytics SELECT finishes. While ALTER TABLE sits in the wait queue, PostgreSQL enforces FIFO fairness and blocks EVERY new incoming SELECT and UPDATE behind the waiting DDL.

Without a strict lock_timeout (1-2s), one slow background query plus one unguarded ALTER TABLE turns into a full-site outage.

02 · LIVE TRIAGE COMMANDS (COPY & RUN)
Find the blocking PID and blocked lock chain immediately
$ psql -c "SELECT blocked.pid AS blocked_pid, blocked.query AS blocked_query, blocking.pid AS blocking_pid, blocking.state, now() - blocking.query_start AS age, left(blocking.query, 80) FROM pg_stat_activity blocked JOIN pg_stat_activity blocking ON blocking.pid = ANY(pg_blocking_pids(blocked.pid)) LIMIT 15;"
Smoking gun: ALTER TABLE waiting on a long SELECT, with dozens of app queries queued behind the ALTER TABLE PID.
Cancel the waiting migration safely to restore live traffic in < 1s
$ psql -c "SELECT pg_cancel_backend(<alter_table_pid>);"
Smoking gun: Blocked app SELECT/UPDATE queries drain immediately.
Verify no active AccessExclusiveLock waits remain
$ psql -c "SELECT pid, relation::regclass, mode, granted FROM pg_locks WHERE mode = 'AccessExclusiveLock' AND NOT granted;"
Smoking gun: 0 rows returned.
03 · PRODUCTION DIFF (BROKEN VS HARDENED)
BROKEN · Broken Migration SQL (unbounded lock wait + full table scan under lock)
-- Runs with default lock_timeout = 0 (infinite wait!)
ALTER TABLE invoices
  ADD CONSTRAINT invoices_amount_positive CHECK (amount_cents > 0);
-- Scans entire table while holding ACCESS EXCLUSIVE lock!
HARDENED · Zero-Downtime Migration Pattern (lock_timeout + NOT VALID + VALIDATE)
-- Step 1: Bound lock acquisition to 2s (retry with backoff in migrator if busy)
SET lock_timeout = '2s';
SET statement_timeout = '5s';

-- Instant metadata-only check (no full table scan under ACCESS EXCLUSIVE)
ALTER TABLE invoices
  ADD CONSTRAINT invoices_amount_positive CHECK (amount_cents > 0) NOT VALID;

-- Step 2: Validate existing rows under SHARE UPDATE EXCLUSIVE (does NOT block reads/writes!)
SET statement_timeout = '0';
ALTER TABLE invoices VALIDATE CONSTRAINT invoices_amount_positive;
04 · MINUTE-24 BAR-RAISER CHECKPOINT (L6 · M02)TEST YOUR INSTINCT · ZERO SIGNUP

Why does adding a nullable column (a 1ms metadata-only DDL in Postgres 16) still freeze production traffic if a 60-second SELECT is already running on the table?

05 · POST-FIX VERIFICATION CHECKLIST
  • Confirm migration runner wraps every DDL statement in SET lock_timeout = '2s' with exponential backoff retries.
  • Use CREATE INDEX CONCURRENTLY (never plain CREATE INDEX) and ADD CONSTRAINT ... NOT VALID followed by VALIDATE CONSTRAINT.
  • Confirm zero elevated lock wait spikes in pg_stat_activity during deployment.
06 · FREQUENTLY ASKED ENGINEERING QUESTIONS

Does ADD COLUMN with a DEFAULT still rewrite tables in PostgreSQL 16?

Not for constant/non-volatile defaults since PostgreSQL 11. However, it still requires a brief ACCESS EXCLUSIVE lock, so lock_timeout <= 2s is still mandatory to prevent lock-queue storms.

What lock does VALIDATE CONSTRAINT acquire?

SHARE UPDATE EXCLUSIVE, which allows concurrent SELECT, INSERT, UPDATE, and DELETE traffic to run completely unblocked.

Can CREATE INDEX CONCURRENTLY run inside a transaction block?

No. Migration tools like Flyway, Prisma, or Alembic must disable the implicit transaction wrapper for concurrent index creation.