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.
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"} > 20Every 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.
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?
- 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.
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.