Blog

Zero-Downtime Database Migrations: Expand Before You Contract

old and new code overlap
A database bridge widened under live traffic before the old lane is removed

During a rolling deployment, old and new application versions run at the same time. A migration safe for one version can break the other before rollout reaches 20 percent.

Expand

Add the new nullable column, table, or compatible index first. Deploy code that can read old state and write enough information for both representations.

Avoid a table rewrite or long blocking lock without measuring on production-size data. PostgreSQL concurrent index creation, for example, changes locking trade-offs but also has operational rules and failure states to monitor.

Backfill In Batches

Update a bounded number of rows, commit, pause if necessary, and expose progress. One giant transaction creates locks, WAL volume, replica lag, and rollback pain.

Make the backfill restartable. Store progress or select remaining rows by a stable predicate.

Switch Reads, Then Contract

After data is complete and new code is fully deployed, switch reads. Observe. Remove old writes later. Drop old columns or constraints in a separate release after the compatibility window closes.

Rollback is easiest during expansion because both shapes still exist. Contracting early converts rollback into archaeology.

Migration files are production code. Review lock behavior, duration, disk requirements, replica impact, and failure recovery, not only final schema.