Difficulty: Intermediate
How do you safely apply schema changes to a production database without downtime?
Treat schema changes as versioned, reviewed migration files run by a tool like Flyway, Liquibase, Prisma Migrate, Alembic or Rails migrations, and make every change backward compatible with the code currently running. The standard technique is expand and contract: add new structures first, deploy code that uses both, backfill data, switch reads, and only then remove the old structures in a later release.
In development, ALTER TABLE finishes instantly. In production the table might hold 200 million rows and receive thousands of writes per second. Two things bite:
ACCESS EXCLUSIVE lock in PostgreSQL. Even a fast statement can queue behind a long-running query, and every new query then queues behind it, causing an outage.Renaming users.phone to users.mobile_number safely looks like this:
mobile_number. Old code ignores it.phone.mobile_number.phone and drop it.Each step is independently deployable and reversible.
| Change | Risky way | Safer way (PostgreSQL) |
|---|---|---|
| Add index | CREATE INDEX blocks writes | CREATE INDEX CONCURRENTLY (cannot run inside a transaction) |
| Add NOT NULL | SET NOT NULL scans under exclusive lock | Add CHECK (col IS NOT NULL) NOT VALID, VALIDATE, then SET NOT NULL (PG 12+ skips the scan) |
| Add foreign key | Validates every row under lock | Add with NOT VALID, then VALIDATE CONSTRAINT separately |
| Add column with default | Rewrote the table before PG 11 | Constant defaults are metadata-only since PG 11 |
| Change column type | Usually rewrites the table | New column plus backfill plus switch |
In MySQL 8.0, many column additions use ALGORITHM=INSTANT, and tools like gh-ost or pt-online-schema-change copy the table in the background for heavier changes.
lock_timeout (for example '5s') in migrations so a blocked DDL fails fast instead of freezing traffic, then retry.DROP COLUMN.UPDATE in one transaction, which bloats the table and lags replicas.-- Fail fast instead of blocking traffic behind a lock queue
SET lock_timeout = '5s';
-- 1. Index without blocking writes (run outside a transaction)
CREATE INDEX CONCURRENTLY IF NOT EXISTS orders_user_id_idx
ON orders (user_id);
-- 2. Make a column NOT NULL without a long exclusive lock
ALTER TABLE orders
ADD CONSTRAINT orders_user_id_not_null CHECK (user_id IS NOT NULL) NOT VALID;
ALTER TABLE orders VALIDATE CONSTRAINT orders_user_id_not_null; -- weaker lock
ALTER TABLE orders ALTER COLUMN user_id SET NOT NULL; -- uses the CHECK, no scan (PG 12+)
ALTER TABLE orders DROP CONSTRAINT orders_user_id_not_null;
NOT VALID plus VALIDATE splits the expensive row check from the brief exclusive lock, and CONCURRENTLY keeps writes flowing during the index build.
-- Run repeatedly from a job until it updates 0 rows
UPDATE users
SET mobile_number = phone
WHERE id IN (
SELECT id FROM users
WHERE mobile_number IS NULL AND phone IS NOT NULL
LIMIT 5000
);
Small batches keep each transaction short, limit row locks and give replicas time to catch up.
Schema Migration, Expand and Contract, Zero-Downtime Deployment, Lock Timeout, Backward Compatibility