Schema Evolution & Zero-Downtime Column Migrations
Evolve schemas with expand/contract migrations that avoid long locks.
In plain words
Change the database in small safe steps: add the new thing, move everything over to it, and remove the old thing last.
Think of it like…
Replacing a bridge while traffic keeps flowing: build the new lanes next to the old ones, move traffic across lane by lane, and only demolish the old bridge when nobody is on it.
Where you’ve seen it
Renaming orders.amount to total_amount: (1) add total_amount; (2) deploy code that writes both columns; (3) backfill total_amount in batches; (4) deploy code that reads total_amount; (5) stop writing amount; (6) drop amount. At no point does a running app version find a column missing.
Words you’ll hear
- Expand and contract
- Add the new structure, migrate to it, then remove the old one.
- Backward-compatible migration
- A schema change the currently running code still works with.
- Backfill
- Filling a new column for existing rows, done in batches.
- lock_timeout
- Makes a migration give up quickly if it can't get its lock.
Visual diagram
Theory & example queries
Changes that are safe, and ones that aren't
Additive changes are usually safe: new tables, new nullable columns, columns with a constant default (instant in PostgreSQL 11+), and new indexes built with CREATE INDEX CONCURRENTLY. Old code simply ignores what it doesn't know about.
Breaking changes need expand-and-contract: renaming or dropping columns that running code reads, changing a column's type, splitting a table, or adding NOT NULL to a column that old code leaves empty. Also watch for locking: altering a column type rewrites the table under an exclusive lock, adding a foreign key or CHECK validates every row, and any ALTER queued behind a long-running query blocks all traffic behind it.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SET lock_timeout = '5s';ALTER TABLE ordersADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id) NOT VALID;ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_fk;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Representative PostgreSQL responses
Note: Success responses omit client timing; lock duration and whether validation finds invalid rows are workload-dependent.
Key takeaway: Additive changes are safe; anything removing or reshaping needs steps.
Running a multi-step migration
Order matters: the database must support both the current and the next code version before you deploy. Expand the schema, deploy code that writes to both old and new structures (or uses a database trigger to sync them), backfill existing rows in batches with short transactions, verify the data matches, switch reads to the new structure, stop writing the old one, and drop it in a later release.
Keep each migration small and reversible, set lock_timeout and retry rather than letting a migration queue behind a long query, and monitor locks while it runs. Feature flags help switch reads gradually. Tools such as gh-ost and pt-online-schema-change (MySQL) and pgroll or pg_repack (PostgreSQL) automate parts of the process for large tables.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
UPDATE ordersSET total_amount = amountWHERE id IN ( SELECT id FROM orders WHERE total_amount IS NULL LIMIT 10000 );PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative backfill progress4 sample rows
| batch | affected rows |
|---|---|
| 1 | 10000 |
| 2 | 10000 |
| 3 | 1840 |
| 4 | 0 |
Note: Assumes 21,840 rows initially need backfilling; stop when a batch affects 0 rows.
repeated until it updates 0 rows, with a short pause between batches.
Key takeaway: Expand, dual-write, backfill, switch reads, contract, one deploy at a time.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Expand the schema
Add the new column, table or index in a way the currently deployed code doesn't notice.
ALTER TABLE ordersADD COLUMN total_amount numeric(12, 2)When to use it & trade-offs
What it is
Zero-downtime schema changes alter a live database without stopping the application. The core idea is expand and contract: first make an additive change that both the old and new code can live with (add a column, a table, an index), deploy code that uses it, backfill data in batches, and only when nothing uses the old structure, remove it. Each step is small, reversible and avoids long locks.
Why it matters: Applications deploy many times a day, while old and new versions of the code run side by side during a rollout. A migration that renames a column or rewrites a big table in one step either breaks the running version or locks the table for minutes. Senior backend and DevOps interviews ask how you'd change a schema safely.
When to reach for it
- Renaming or retyping columns on production tables
- Adding constraints and indexes to big tables
- Any migration during continuous deployment
When to avoid or be careful
- Renaming or dropping columns that running code still uses
- ALTERs without lock_timeout on busy tables
- Backfilling millions of rows in a single UPDATE
Ways to change a live schema
Strengths: Simple and immediate.
Watch out: Breaking or rewriting changes cause errors or long locks.
Strengths: No downtime; each step reversible.
Watch out: Several deploys and a backfill to coordinate.
Strengths: Copy and swap tables in the background.
Watch out: Extra tooling, disk space and operational risk.
Interview signals & numbers to know
When this topic comes up
- “How do you rename a column without downtime?”
- “A migration locked the table during deploy”
- Continuous deployment with database changes
- Rolling deploys
- Backfills
Benchmarks & rules of thumb
- 6
- steps for a safe rename
- 10,000
- a typical backfill batch size
- CONCURRENTLY
- index builds without blocking writes
Common mistakes & follow-ups
Common interview mistakes
- Renaming a column in one migration, so pods still running the old code start failing immediately.
- Adding a foreign key to a huge table without NOT VALID and blocking writes while it validates.
- Dropping the old column in the same release that switched reads, leaving no rollback path.
Likely interviewer follow-ups
- Walk me through renaming a heavily used column with zero downtime.
- Why must migrations be compatible with both the old and new code?
- How would you add an index to a 500-million-row table safely?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Schema Evolution & Zero-Downtime Column Migrations with hands-on SQL practice and interview Q&A.