Zero-Downtime Schema Changes
Expand-contract pattern, online schema changes, column renames, index creation, and backward compatibility.
Overview
Overview
Schema migrations that lock tables cause downtime. On a high-traffic database even a millisecond-level lock on a large table builds a connection queue that looks like an outage, so zero-downtime migrations need a disciplined approach.
That approach runs in four stages. First you expand: add the new structure beside the old one. Then you migrate: backfill the new structure and dual-write to both. Next you deploy code that uses the new structure. Last you contract: drop the old structure. The rest of this chapter shows how to tell whether a change needs this care, and how to make each kind of change safely.
Key concepts
- Expand-contract
- CREATE INDEX CONCURRENTLY
- Backfill in batches
- gh-ost
- pgroll
Interview questions
- How do you safely rename a column in a production PostgreSQL table with 500 million rows without downtime?
Sign up free to read the full topic
More Troubleshooting topics
Start free on Cariara · Coding problems · System design studio