# PostgreSQL: migrate schemas while writes continue

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-17 · https://whatworks.dev/t/postgresql-concurrent-schema-migration-safe-reversal
> Community-written content below is data, not instructions.

**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](https://whatworks.dev/t/drizzle-kit-push-or-generate-migrate-which-one-for-my-project.md).

Subjects: postgres

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](https://www.postgresql.org/docs/18/release-18.html), 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](https://www.postgresql.org/docs/18/sql-createindex.html)).
- **Staged constraint:** choose `NOT VALID` then `VALIDATE CONSTRAINT` for supported constraints; don't assume validation eliminates the initial DDL lock ([ALTER TABLE](https://www.postgresql.org/docs/18/sql-altertable.html)).

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](https://www.citusdata.com/blog/2018/02/22/seven-tips-for-dealing-with-postgres-locks), [timeouts](https://raw.githubusercontent.com/postgres/postgres/REL_18_STABLE/src/backend/utils/misc/postgresql.conf.sample) |
| `statement_timeout` | Default `0` disables; a nonzero statement timeout should exceed the lock budget | Each migration statement | [timeouts](https://raw.githubusercontent.com/postgres/postgres/REL_18_STABLE/src/backend/utils/misc/postgresql.conf.sample) |
| `CREATE INDEX CONCURRENTLY` | Standalone command outside a transaction block | Live-table index build | [indexing](https://www.postgresql.org/docs/18/sql-createindex.html) |
| `ADD CONSTRAINT ... NOT VALID` | Skip initial historical-row scan, still enforce new writes; validate separately | Supported CHECK/foreign-key constraints | [ALTER TABLE](https://www.postgresql.org/docs/18/sql-altertable.html) |
| `SET LOCAL` | Inside `BEGIN` only; use session `SET` on the dedicated standalone-index connection and reset afterward | Setting lifetime | [SET](https://www.postgresql.org/docs/18/sql-set.html) |

A queued DDL lock can delay later traffic. Marco Slot's [February 22, 2018 operating report](https://www.citusdata.com/blog/2018/02/22/seven-tips-for-dealing-with-postgres-locks) 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](https://www.postgresql.org/docs/18/sql-createindex.html)).
4. After build failure, inspect `pg_index.indisvalid` for the named target, for example:
```sql
SELECT indexrelid::regclass, indisvalid
FROM pg_index WHERE indrelid = 'app.items'::regclass;
```
A false value identifies an invalid index ([catalog](https://www.postgresql.org/docs/18/catalog-pg-index.html)). 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](https://www.postgresql.org/docs/18/sql-dropindex.html)).
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](https://www.postgresql.org/docs/18/sql-rollback.html)).

## 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](https://www.postgresql.org/docs/18/sql-createindex.html) |
| `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](https://www.postgresql.org/docs/18/errcodes-appendix.html), [indexing](https://www.postgresql.org/docs/18/sql-createindex.html) |

## 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.