Menu

1 / 2

Zero-Downtime Database Migrations: The Expand-and-Contract Pattern (With PostgreSQL Examples)

DEV Community: postgres·Prism Infoways Private Limited·25 days ago
#HIY5TDDK
#devtopostgresql#devto#dev#column#name
Long article (1207 words) — no AI summary yet.
Reading 0:00
15s threshold

Most deploys that take a site down for a few minutes aren't code problems. They're schema changes: a column renamed while the old version of the app is still running, or an ALTER TABLE that locks a busy table for longer than anyone expected.

The fix is a pattern called expand and contract (also known as parallel change). Instead of changing the schema in one step, you split the change into small steps. Each step works with both the old and the new version of your code. Here's how it works, with PostgreSQL examples.

Why one-step migrations break

During a rolling deploy, old and new app instances run side by side for a few minutes. Say you ship this as a single migration:

ALTER TABLE users RENAME COLUMN name TO full_name ;

Enter fullscreen mode

Exit fullscreen mode

The new code works, but every old instance still querying name starts throwing errors until the deploy finishes.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.