Skip to content
envexa

Cloud & DevOps

Zero-downtime database migrations: the expand and contract pattern

Change your PostgreSQL schema without downtime: expand and contract, safe index creation, lock timeouts, batched backfills and a pre-deploy checklist.

By Envexa Engineering6 min read

During a deployment, old and new versions of your application run side by side, and both must work with the database in whatever state the migration has left it. A migration that renames a column or adds a blocking lock turns a routine release into an outage. The discipline that avoids this is to make every schema change backwards compatible and to split risky changes into steps.

Expand, migrate, contract

Almost any breaking change can be expressed in three releases. Take renaming users.fullname to users.display_name:

  1. Expand. Add display_name as a new nullable column. Deploy code that writes to both columns and still reads the old one.
  2. Migrate. Backfill existing rows in small batches. Switch reads to the new column. Verify nothing reads the old one.
  3. Contract. Stop writing the old column, deploy, then drop it in a later migration once no running version needs it.

Each step is safe to roll back on its own. The same pattern covers splitting tables, changing types and moving data.

Know which operations take heavy locks

  • Adding a column is quick. Since PostgreSQL 11, adding a column with a constant default does not rewrite the table either.
  • Adding `NOT NULL` or a foreign key validates existing rows while holding a lock. Add a NOT VALID constraint first, then run VALIDATE CONSTRAINT, which takes a lighter lock.
  • Creating an index blocks writes. Use CREATE INDEX CONCURRENTLY (it cannot run inside a transaction block).
  • Changing a column type often rewrites the whole table. Prefer adding a new column and migrating.

Always set timeouts

SET lock_timeout = '3s';        -- give up instead of queueing behind a long query
SET statement_timeout = '60s';
ALTER TABLE orders ADD COLUMN shipped_at timestamptz;

A DDL statement waiting for a lock also blocks every query queued behind it. A short lock_timeout makes the migration fail fast and retry, rather than freezing the application. Run migrations at quieter times and retry on timeout.

Backfill in batches

Updating millions of rows in one statement holds locks, bloats the table and floods replicas. Update in batches of a few thousand by primary key, commit between batches, sleep briefly if replication lag grows, and make the job resumable and idempotent.

Pre-deploy checklist

  • Does the old application version still work with the new schema? Does the new version work with the old schema?
  • Which locks does each statement take, and for how long on production-sized data?
  • Is there a lock_timeout, and is the migration safe to re-run?
  • Has it been tried on a copy of production data volume, not only on an empty test database?
  • What is the rollback, and does it lose data?

Run migrations as an explicit pipeline step, separate from application start-up, so a slow migration does not coincide with many replicas booting at once. Done this way, schema changes become routine instead of the riskiest part of a release.

MigrationsPostgreSQLDeploymentsReliability

Building something like this?

We are happy to review your architecture or help you build it.