Schema changes cause outages in two ways: they take locks that block queries, or they break the application version that is still running during a rolling deploy. The expand and contract pattern, plus knowing which operations lock, avoids both.
Expand and contract
Split every breaking change into backward-compatible steps:
- Expand: add the new structure without removing the old (new column, new table, new index).
- Migrate code: deploy code that writes to both old and new, and reads from new with a fallback.
- Backfill: copy existing data into the new structure in small batches.
- Switch: deploy code that reads and writes only the new structure.
- Contract: remove the old structure in a later release.
Example: renaming a column
Renaming users.name to users.full_name in one step breaks every running instance that still uses name. Instead:
-- release 1: expand
ALTER TABLE users ADD COLUMN full_name text;
-- app writes both name and full_name; reads full_name, falls back to name
-- backfill in batches (repeat until no rows updated)
UPDATE users SET full_name = name
WHERE id IN (SELECT id FROM users WHERE full_name IS NULL LIMIT 5000);
-- release 2: app uses only full_name
-- release 3: contract
ALTER TABLE users DROP COLUMN name;
Operations that lock (PostgreSQL)
| Operation | Safe? | Safer approach |
|---|---|---|
| ADD COLUMN (nullable, no default) | Yes, quick | |
| ADD COLUMN with constant default (PG 11+) | Yes, quick | |
| CREATE INDEX | Blocks writes | CREATE INDEX CONCURRENTLY |
| ADD FOREIGN KEY | Locks both tables while validating | ADD ... NOT VALID, then VALIDATE CONSTRAINT |
| SET NOT NULL | Scans table under lock | Add CHECK (col IS NOT NULL) NOT VALID, validate, then SET NOT NULL |
| ALTER COLUMN TYPE | Usually rewrites table | New column + backfill + switch |
| DROP COLUMN | Quick, but breaks old code | Only after code stops using it |
MySQL with InnoDB supports many online DDL operations, but some still copy the table; tools such as gh-ost or pt-online-schema-change perform large changes without long locks.
Lock timeouts
Even a quick DDL statement waits for an exclusive lock, and while it waits, it blocks every query behind it. Set a short lock timeout so a migration fails fast instead of freezing the table:
SET lock_timeout = '3s';
SET statement_timeout = '15min';
ALTER TABLE orders ADD COLUMN source text;
Retry later if it times out, preferably at a quieter time.
Backfills
Backfill in small batches with pauses, so replication and normal traffic keep up. Make the backfill idempotent and resumable, and run it as a monitored job with a heartbeat.
Monitoring a migration
- Database: lock waits, long-running transactions, replication lag, CPU and IO during backfills. See PostgreSQL monitoring.
- Application: p95 latency and error rate per endpoint, especially those touching the migrated table, around the migration marker.
- Traces: database span durations for the affected queries before and after.
VitalRix APM shows database call volume and latency per statement, so a migration that slows queries is visible immediately, and deploy markers line up the effect with the release.
Worked example
A team added a NOT NULL column with a default to a 60-million-row table on an older PostgreSQL version that rewrote the table. The ALTER held an exclusive lock for 11 minutes and checkout was down. Their new process: lint migrations in CI for unsafe operations, add columns nullable, backfill in batches, add NOT NULL via a validated CHECK constraint, and always set lock_timeout. Later large migrations ran during business hours without user impact.
Checklist
- Expand and contract across releases.
- Check each operation's locking behaviour for your database version.
- lock_timeout on every migration.
- Indexes created concurrently.
- Backfills in batches, monitored.
- Migrations reviewed and tested on production-sized data.
Related: zero-downtime deployments and database indexing for developers.
Common mistakes
- Running a contract step in the same release as the expand step, breaking the version still running.
- Backfilling in one huge UPDATE, holding locks and flooding replication.
- Testing migrations on a tiny development database, where everything is instant.
- No lock_timeout, so a migration waits behind a long query and blocks the table for everyone.
- Forgetting replicas: heavy backfills can push replication lag past what read traffic tolerates.
Can I rename a column without downtime?
Yes, with expand and contract: add the new column, write both, backfill, switch reads, then drop the old column in a later release.
Why does a fast ALTER TABLE still cause an outage?
It waits for an exclusive lock behind long-running queries, and blocks every new query while it waits. Use lock_timeout.
Should migrations run automatically on deploy?
Expand migrations can; contract migrations should run in a later release, once no running code uses the old structure.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.