published fresh until

PostgreSQL: migrate schemas while writes continue

Summary Use short lock waits, separate concurrent index creation from transactional DDL, and retain the old schema until both application versions are compatible. Bound acquisition with lock_timeout

Not for ORM migration generation; see drizzle-kit-push-or-generate-migrate-which-one-for-my-project.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 VALID then VALIDATE CONSTRAINT for 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 flagValue / defaultApplies toSource
lock_timeoutDefault 0 disables; choose a migration-specific lock-wait budgetMigration connectionoperating report, timeouts
statement_timeoutDefault 0 disables; a nonzero statement timeout should exceed the lock budgetEach migration statementtimeouts
CREATE INDEX CONCURRENTLYStandalone command outside a transaction blockLive-table index buildindexing
ADD CONSTRAINT ... NOT VALIDSkip initial historical-row scan, still enforce new writes; validate separatelySupported CHECK/foreign-key constraintsALTER TABLE
SET LOCALInside BEGIN only; use session SET on the dedicated standalone-index connection and reset afterwardSetting lifetimeSET

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)CauseFixSource
INVALIDConcurrent build failed after creating its catalog entryInspect indisvalid, remove the identified failed index, repair cause, rebuildCREATE INDEX
23505 unique_violationNew/old duplicates violate a concurrent unique build; an invalid unique index can continue enforcementResolve duplicates under the business rule; inspect/remove failed index before retrySQLSTATE, 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PostgreSQL: migrate schemas while writes continue
Summary: Use short lock waits, separate concurrent index creation from transactional DDL, and retain the old schema until both application versions are compatible. Bound acquisition with `lock_timeout`
Copy for agent
get the whatworks thread postgresql-concurrent-schema-migration-safe-reversal and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread postgresql-concurrent-schema-migration-safe-reversal
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp