What this thread does
Do first: set a finite migration-session lock_timeout shorter than statement_timeout, then separate CREATE INDEX CONCURRENTLY from every transaction block.
Short answer
As of November 2026, the baseline is PostgreSQL 18, released September 25, 2025. Preserve writes with an expand/backfill/switch/contract rollout.
Choose / don't choose
- Ordinary transactional DDL: choose for small additive changes whose lock is brief; don't mix long backfills into that transaction.
- Concurrent index: choose for a live table that must accept writes; don't let the migration runner automatically wrap it in a transaction (CREATE INDEX).
- Staged constraint: choose
NOT VALIDthenVALIDATE CONSTRAINTfor supported constraints; don't assume validation eliminates the initial DDL lock (ALTER TABLE).
Not for: ORM migration generation; see drizzle-kit-push-or-generate-migrate-which-one-for-my-project.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
lock_timeout | Default 0 disables; choose a migration-specific lock-wait budget | Migration connection | operating report, timeouts |
statement_timeout | Default 0 disables; a nonzero statement timeout should exceed the lock budget | Each migration statement | timeouts |
CREATE INDEX CONCURRENTLY | Standalone command outside a transaction block | Live-table index build | indexing |
ADD CONSTRAINT ... NOT VALID | Skip initial historical-row scan, still enforce new writes; validate separately | Supported CHECK/foreign-key constraints | ALTER TABLE |
SET LOCAL | Inside BEGIN only; use session SET on the dedicated standalone-index connection and reset afterward | Setting lifetime | SET |
A queued DDL lock can delay later traffic. Marco Slot's February 22, 2018 operating report motivates short acquisition waits; use the current command reference rather than the report's historical default-column advice.
Order of operations
1. Add backward-compatible columns/tables in a short transaction. Commit promptly; retain old columns and compatible application code. 2. Backfill in bounded independently committed batches with application-specific protection against overwriting newer writes. Establish dual-write/reconciliation behavior before switching reads; DDL alone cannot provide it. 3. On a dedicated connection, issue timeout settings separately, then the standalone concurrent-index command. Do not send them with the build as one simple-query batch. Only one concurrent build per table can proceed at a time (indexing). 4. After build failure, inspect pg_index.indisvalid for the named target, for example:
SELECT indexrelid::regclass, indisvalid
FROM pg_index WHERE indrelid = 'app.items'::regclass;
A false value identifies an invalid index (catalog). Coordinate with other builders, remove only the failed migration's index using DROP INDEX CONCURRENTLY app.items_new_idx as a standalone command, fix the original cause, then retry. Concurrent dropping has dependency/partition restrictions (DROP INDEX). 5. Validate constraints, check the new read/write path, then switch application reads. Defer destructive contraction until old application instances can no longer use the previous schema. 6. Before commit, ROLLBACK discards ordinary transactional changes. After commit, rollback cannot rewind them: use an explicit reverse migration that preserves new writes. (ROLLBACK).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
INVALID | Concurrent build failed after creating its catalog entry | Inspect indisvalid, remove the identified failed index, repair cause, rebuild | CREATE INDEX |
23505 unique_violation | New/old duplicates violate a concurrent unique build; an invalid unique index can continue enforcement | Resolve duplicates under the business rule; inspect/remove failed index before retry | SQLSTATE, indexing |
Missing
- The linked source does not confirm: the illustrative statement_timeout='30min' migration budget.
- Exact DDL, workload, rollback retention window and write-reconciliation rules are unspecified.
- Concurrent writes can still wait briefly on DDL; no zero-interruption guarantee follows from these commands.
Started from a post by @Marco Slot, packaged by the editor. Original post