How-To Guide

How to Run Zero-Downtime Database Migrations

Change a schema while old and new code run at once by expanding first, migrating data, and contracting last.

On this page

  1. Introduction
  2. Why a Single Migration Breaks a Rolling Deploy
  3. Expand: Add the New Without Removing the Old
  4. Migrate: Backfill and Switch Readers
  5. Contract: Remove the Old
  6. Operations That Lock the Table
  7. Verifying Each Step
  8. Conclusion

Introduction

A rolling deploy runs old and new versions of the application at the same time, so a schema change must be compatible with both versions or the deploy breaks whichever runs against the other version. A migration that renames a column breaks the old code that still reads the old name; one that adds a NOT NULL column without a default breaks the old code that inserts without it. Zero-downtime migrations are therefore not about running the migration quickly but about sequencing it so every intermediate state is compatible with both versions. This article covers the expand-migrate-contract pattern, the operations that lock a table, and how to verify each step before the next.

Why a Single Migration Breaks a Rolling Deploy

A migration is written against one version of the code, but during a rolling deploy both versions run. If the migration removes a column the old code reads, the old instances fail at runtime; if it adds a column the new code writes but the old code’s insert does not include, the insert fails on a not-null constraint. The migration is correct for the new code and wrong for the old one.

The fix is to change the schema in steps, each compatible with both versions, and to change the code in steps that match. No single step is a breaking change; the sequence transforms the schema safely. This is the expand-migrate-contract pattern, and it applies to renames, type changes, splits and merges.

Expand: Add the New Without Removing the Old

The first step adds the new column or table without touching the old. The new column is nullable or has a default, so inserts from the old code still succeed, and the old code ignores it. Deploy the schema change first, then deploy code that writes both the old and the new column, so data flows into both.

-- Expand: add the new column, nullable so old inserts still succeed.
ALTER TABLE orders ADD COLUMN currency text;

-- Backfill in batches without locking the table (see the backfill pattern).
-- Then deploy code that writes BOTH amount_cents (old) and currency (new).

-- Later, once all instances write the new column, add the constraint safely:
-- use a NOT VALID constraint so adding it does not scan the whole table.
ALTER TABLE orders ADD CONSTRAINT orders_currency_not_null
  CHECK (currency IS NOT NULL) NOT VALID;
-- Validate it without holding a strong lock for long:
ALTER TABLE orders VALIDATE CONSTRAINT orders_currency_not_null;

Migrate: Backfill and Switch Readers

Once the new column is written by all producers, backfill the historical rows in small batches, then deploy code that reads the new column and falls back to the old during the overlap. After that, no code depends on the old column, which is the precondition for contracting.

Order matters: write the new column first, then read it. If you switch readers before writers, the new column is null for new rows and readers get nothing. If you backfill before writers populate the new column, new rows written after the backfill are null and the backfill is invalidated. Each step has a precondition that the previous step established.

Contract: Remove the Old

The final step removes the old column or table, but only after verifying that no code reads it. Removing it earlier breaks an un-upgraded instance, and with a rolling deploy there is always a window where an old instance runs. Instrument the old column or check the logs to confirm zero reads across a full deploy cycle before dropping it.

A destructive migration should be reversible by the previous release, which means keeping it a separate release from the code that stopped using the column. If you must roll back, the previous version of the code still reads the old column, so dropping it in the same release that stops reading it makes a rollback impossible. Separate the releases so the rollback is safe.

Operations That Lock the Table

Some schema operations take a lock that blocks reads or writes, and on a large table that lock lasts long enough to cause an outage. Adding a column with a volatile default used to rewrite the table; modern databases make a constant default cheap. Adding an index without CONCURRENTLY blocks writes; use the concurrent form. Changing a column type rewrites the table and blocks.

Adding a constraint with validation scans the whole table and holds a lock; add it NOT VALID and validate separately, which takes a weaker lock for the scan. Each of these has a non-blocking alternative, and knowing which operations lock is what lets you write a migration that does not stall the application.

-- Non-blocking index creation: does not hold a write lock for the build.
CREATE INDEX CONCURRENTLY idx_orders_currency ON orders (currency);
-- Note: CONCURRENTLY cannot run inside a transaction block.

-- Drop an index without blocking writes.
DROP INDEX CONCURRENTLY idx_orders_old_col;

-- Add a foreign key without scanning under a strong lock.
ALTER TABLE orders ADD CONSTRAINT orders_user_fk
  FOREIGN KEY (user_id) REFERENCES users (id) NOT VALID;
ALTER TABLE orders VALIDATE CONSTRAINT orders_user_fk;

Verifying Each Step

Because each step is compatible, you can verify it in production before the next: deploy the schema, confirm both versions still work, deploy the writing code, confirm both write, backfill, confirm the new column has no nulls in new rows, switch the readers, confirm no reads of the old column, then contract. Each verification is a gate, and skipping one is how a migration causes an outage.

For a large table, monitor the migration’s effect: the lock wait time, the replication lag caused by the write volume, and the number of rows still needing a backfill. A short lock can still block if a long transaction is holding a conflicting lock, so run migrations when long transactions are absent and set a lock timeout so the migration fails fast rather than building a queue.

Conclusion

Zero-downtime migrations are about sequencing, not speed. Expand with a nullable new column, deploy writers that populate it, backfill in batches, switch readers with a fallback, then contract the old column in a separate release that can be rolled back. Use the non-blocking form of every locking operation, add constraints NOT VALID and validate separately, and verify each step before the next. The schema changes while old and new code coexist, and no single step is a breaking change.

Sources: PostgreSQL: ALTER TABLE, PostgreSQL: CREATE INDEX CONCURRENTLY, Expand/contract pattern.

FAQ

Why can a schema change be compatible with new code but break old code?
Because a rolling deploy runs both versions at once. A migration is written for one version; removing a column the old code reads, or adding a not-null column the old insert omits, breaks the old instances during the deploy window. Change the schema in steps compatible with both.
How do I drop a column safely?
Only after confirming no code reads it across a full deploy cycle, and in a separate release from the code that stopped reading it, so a rollback to the previous version still works. Dropping it in the same release that stops reading it makes rollback impossible.

Related Examples