Postgres Zero-Downtime Schema Migration Planner Prompt
Turn a risky schema change into a lock-aware, multi-step migration plan that avoids long ACCESS EXCLUSIVE locks — with the exact statements, lock_timeout guards, and a back-out path for each phase.
- Target user
- Engineers shipping schema changes to a live, high-traffic Postgres database
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior PostgreSQL DBA who has shipped schema changes to busy databases without downtime. You know exactly which DDL takes a brief lock and which rewrites the whole table, and you decompose dangerous changes into safe, reversible phases. I will paste: - The desired schema change (DDL or plain description): [DESIRED CHANGE] - The target table: size, write rate, and Postgres version: [TABLE / VERSION] - Any app constraints (zero downtime required, deploy can't be paused, ORM in use): [CONSTRAINTS] - Existing constraints/indexes/FKs on the table: [CURRENT SCHEMA] Produce a phased plan: 1. **Classify the operation's lock cost** — for each piece of the change, state the lock it takes and whether it rewrites the table or scans it. Call out the dangerous ones (adding a column with a volatile default on old versions, changing a column type, adding a NOT NULL or a validated FK/CHECK in one shot, building an index without CONCURRENTLY). 2. **Decompose into safe phases** — rewrite the change as small steps that each take only a brief lock. Canonical patterns: add nullable column → backfill in batches → add NOT VALID constraint → VALIDATE CONSTRAINT separately; CREATE INDEX CONCURRENTLY; expand-then- contract for type changes (new column + dual-write + backfill + swap). Give exact SQL. 3. **Guard every locking statement** — wrap DDL with a short `lock_timeout` (and statement_timeout for backfills) so a blocked DDL fails fast instead of queueing behind it and stalling all traffic; show the SET LOCAL pattern and a retry approach. 4. **Provide a back-out for each phase** — the exact reverse step, and note which phases are point-of-no-return (e.g. after dropping the old column). Output format: a numbered phase plan, each phase with [SQL | lock taken | duration risk | back-out], a separate batched-backfill snippet, and a final go/no-go checklist. Guardrails: rehearse the full plan on a snapshot/replica with production-like volume and measure lock duration before touching prod. Always set lock_timeout before locking DDL. Never run a table-rewriting statement against prod during peak. Validate constraints in a separate VALIDATE step, not inline. Keep the human reviewing each phase — the plan is a runbook, not an auto-applied migration.
Why this prompt works
The danger in schema migrations isn’t usually the change itself — it’s that an innocent-looking ALTER takes an ACCESS EXCLUSIVE lock and then queues behind a long-running query, freezing every write to the table until it clears. This prompt front-loads lock classification so each operation is labeled by the lock it takes and whether it rewrites the table, turning hidden risk into something you can plan around.
Decomposition is where zero-downtime actually happens. Adding a validated constraint or changing a column type in one statement scans or rewrites the whole table; splitting into nullable-add, batched backfill, NOT VALID, and a separate VALIDATE — or full expand-then-contract — keeps every lock brief. Emitting the exact SQL for these patterns means the plan is runnable, not aspirational.
The lock_timeout guard and per-phase back-out are the safety rails that make it production-grade: a blocked DDL fails fast instead of cascading, and every step has a reverse with point-of-no-return phases flagged. Rehearsing on a production-volume snapshot keeps the human approving each phase rather than discovering the lock cost live.
Related prompts
-
Postgres Declarative Partitioning Design Prompt
Design a declarative partitioning scheme for a large table — pick the strategy and key, plan indexes and constraints, and lay out a safe migration from the existing monolith with automated partition maintenance.
-
Postgres Lock Contention & Deadlock Investigation Prompt
Untangle blocking chains and deadlocks from pg_locks, pg_stat_activity, and log output — pinpoint the blocker, explain the lock conflict, and fix the access pattern so it stops recurring.