Lesson 4 of 7

Expand/Contract: The Pattern Behind Every Safe Migration

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source

The one thing to understand first

In a rolling deployment, old and new application code run simultaneously for a window. If a migration changes the schema in a way only the new code understands, the old code breaks — and if you deploy code first, it references columns that do not yet exist. The expand/contract pattern (also called parallel change) makes every step backward-compatible so old and new code coexist safely.

Never let schema and code change in lockstep: add the new shape, run both shapes in parallel until all old code is gone, then remove the old shape — three deploys, not one. Every “safe migration” technique in this pathway is just a special case of this parallel-change discipline.

The three phases

  1. Expand. Add the new schema elements additively — new columns/tables alongside the old ones. Nothing is removed; old code is unaffected.
  2. Migrate. Make both shapes consistent: dual-write from the application (or via triggers), backfill historical data, and switch reads to the new shape once it is complete.
  3. Contract. After all code uses the new shape and you are confident, remove the old elements.

Each phase is deployed and verified independently. A rollback at any point is safe because earlier phases left the old shape intact.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • Worked example: renaming a column
  • Changing a column type the same way
  • Splitting and merging tables
  • Layer 3 — Watch it happen on your own database
  • Layer 4 — The levers this hands you
  • Layer 5 — What an Oracle DBA should expect vs what they get
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Was this helpful?

← Back to 03 — Operations: Zero-Downtime Deployments