GuideAdvanced1 min read
Zero-Downtime Database Migrations with Expand–Migrate–Contract
Ship schema changes safely across rolling deployments, batch backfills, concurrent indexes and delayed destructive cleanup.
- Author
- Pradeep Gajendra
- Published
- Updated
Scope of this article
- Versions
- PostgreSQL 16 and framework-independent migration practices
Zero-downtime database migrations are a compatibility problem. During a rolling deploy, old and new application versions may run against the same schema. A migration is safe only if both versions can tolerate the transition and rollback path.
Use expand, migrate, contract#
Example: splitting a display name#
-- Expand without deleting the existing column.
alter table profiles add column given_name text;
alter table profiles add column family_name text;
-- New code may write both representations during transition.Deploy code that continues reading the old field while writing both old and new forms. Backfill with a deterministic migration in bounded batches. Instrument divergence until the new fields are trustworthy, then deploy readers of the new shape. Remove the legacy column only after every old instance has left service.
Indexes and locks are part of the release#
Large tables make apparently trivial DDL risky. PostgreSQL supports CREATE INDEX CONCURRENTLY so writers can continue while an index is built, subject to additional overhead and restrictions [1]. Measure lock duration and use a lock_timeout in migration sessions where appropriate.
Backfill without monopolizing the database#
Process bounded primary-key ranges with a checkpoint.
Use small transactions, control write rate and monitor replication lag.
Make the operation restartable and idempotent; an interrupted batch should be safe to rerun.
Do not assume all rows share valid legacy data; log exceptions before enforcing NOT NULL constraints.
Validate new indexes/constraints before dropping the old path.
Rollback must be possible after the schema changes#
Test the previous application release against the expanded schema. If reverting code would require restoring a dropped column, the deploy is not yet backward compatible. Separate destructive cleanup into a later deployment with a reversible operational plan.
Sources
- [1]PostgreSQL 16 — CREATE INDEX CONCURRENTLY
PostgreSQL Global Development Group · publication date unknown · PostgreSQL 16 · retrieved 2026-11-02
Spotted an error? Use “Suggest a correction” in the discussion, or read our corrections policy.
Discussion
Sign in to join the discussion. Reading is always open.
Loading discussion…