Most PostgreSQL outages during routine deploys aren't caused by slow SELECT queries — they happen because a 3-line ALTER TABLE or CREATE INDEX acquires an AccessExclusiveLock (or ShareLock), blocks behind one long-running reporting query, and backs up every normal SELECT and UPDATE in the queue behind it.
Here are the 5 PostgreSQL migration patterns that silently cause production lockups — and exact zero-downtime patterns to replace them.
1. Running CREATE INDEX Without CONCURRENTLY
Standard CREATE INDEX acquires a ShareLock, blocking every INSERT, UPDATE, and DELETE against that table until the full index build finishes. On a 20M-row orders or events table, that can stall writes for minutes.
-- Safe (must run outside an explicit BEGIN/COMMIT transaction block) SET lock_timeout = '2s'; CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_org_created ON public.orders (organization_id, created_at DESC); 2. Adding CHECK or FOREIGN KEY Constraints Without NOT VALID
ALTER TABLE ... ADD CONSTRAINT validates every existing row while holding locks that block concurrent writes. Splitting it into two steps removes the write outage:
SET lock_timeout = '2s'; ALTER TABLE public.orders ADD CONSTRAINT chk_orders_amount_nonnegative CHECK (amount_cents >= 0) NOT VALID; -- Scans rows later with ShareUpdateExclusiveLock (normal reads & writes continue) ALTER TABLE public.orders VALIDATE CONSTRAINT chk_orders_amount_nonnegative; 3. Skipping SET lock_timeout = '2s' Before DDL
Even a metadata-only change (that normally takes < 5 ms) will queue for an AccessExclusiveLock. While it waits in line behind one 30-second analytics query, every incoming SELECT waits behind the DDL lock request, taking down your API pool. Setting SET lock_timeout = '2s'; aborts safely so you can retry cleanly instead of cascading an outage.
4. Rewriting Tables With ALTER COLUMN ... TYPE or VACUUM FULL
Changing an integer to bigint or varchar(50) directly, or running VACUUM FULL / CLUSTER inside a live window, rewrites the entire heap under an AccessExclusiveLock. Use expand-and-contract (dual-write new column -> backfill in batches -> swap) or pg_repack instead.
5. Treating Untested pg_dump Files as Disaster Recovery
pg_dump without continuous WAL archiving leaves hours of data loss between nightly cron runs — and backups you haven't tested in an isolated restore environment often fail silently on missing extensions or corrupted permissions during an actual emergency.
Want the Complete PostgreSQL Backup, PITR & Zero-Downtime Migration Kit?
We packaged our production-tested PostgreSQL 15–17 setup into a single zero-subscription bundle ($14 one-time, lifetime updates):
-
pgbackrest/pgbackrest.conf&postgresql-pitr.conf: AES-256 encrypted S3pgBackRestsetup (zstd, async WAL archiving, 5-min RPOarchive_timeout) -
scripts/pg_backup_verify.sh+ systemd timer: Automated backup + ephemeral isolated restore smoke-test drill every week -
scripts/migration_lint.py: Zero-dependency Python 3 CI SQL migration linter catching missingCONCURRENTLY,NOT VALID, andlock_timeout -
scripts/backup_rpo_rto_calc.py+rpo_rto_matrix.csv: Estimate S3 storage & restore RTO across5 GBto2 TBclusters
👉 Get it on Ko-fi Direct (Stripe / Apple Pay): Postgres Backup, PITR & Zero-Downtime Migration Kit 2026 ($14)
Also check out our free browser-local DevOps & SRE calculators (Solo Founder Toolkit) and local AI hardware planner (localaihardware.dev).
Which lock mistake bit your team hardest — missing CONCURRENTLY, missing lock_timeout, or an untested backup?

