Overview
Changing a schema on a live database can block every query behind a lock, rewrite a huge table, or break the running application version. A safe migration is small, reversible where possible, lock-aware, and ordered with the application deploy.
Core concepts
Expand and contract (parallel change) splits a breaking change into compatible steps:
- Expand: add the new column/table/index, nullable or with a safe default. Old code still works.
- Migrate: deploy code that writes both shapes; backfill old rows in small batches.
- Switch: deploy code that reads the new shape.
- Contract: drop the old column/table in a later release, once nothing uses it.
Where the danger is:
- Lock level and duration. Many
ALTER TABLEforms take an exclusive lock. If a long query holds a conflicting lock, yourALTERwaits, and every later query queues behind it. Set a lock timeout and retry (SET lock_timeout = '3s'in PostgreSQL,SET LOCK_TIMEOUT 3000in SQL Server). - Table rewrites. Some changes rewrite the whole table. PostgreSQL 11+ adds a column with a constant default without a rewrite; changing a column type usually rewrites. SQL Server and Oracle also treat adding a defaulted
NOT NULLcolumn as a metadata change in current versions, but check your version and edition. - Index builds. PostgreSQL:
CREATE INDEX CONCURRENTLY(cannot run in a transaction; a failure leaves an invalid index to drop). SQL Server:WITH (ONLINE = ON)where the edition supports it. Oracle:CREATE INDEX ... ONLINE. - Constraints. Add a foreign key or check as
NOT VALID(PostgreSQL) orWITH NOCHECK(SQL Server) first, then validate separately to shorten the lock.
Worked example
Make orders.status NOT NULL safely on PostgreSQL:
SET lock_timeout = '3s';
ALTER TABLE orders ADD CONSTRAINT status_nn CHECK (status IS NOT NULL) NOT VALID; -- quick
-- backfill NULLs in batches of ~10k rows, committing between batches
ALTER TABLE orders VALIDATE CONSTRAINT status_nn; -- scans without blocking writes
-- PostgreSQL 12+ then uses the validated check to set NOT NULL without a full scan
ALTER TABLE orders ALTER COLUMN status SET NOT NULL;
ALTER TABLE orders DROP CONSTRAINT status_nn;Pitfalls and production notes
- Never run a large backfill as one statement: it holds locks and bloats the log. Batch by key range and throttle.
- Deploy order matters: add-column first, then code; code that stops using a column first, then drop it.
- Keep migration scripts in version control, run them with the same tool in every environment, and rehearse on a production-size copy.
- Have a rollback or roll-forward plan for each step; dropping data is the irreversible one.
- Check replication lag during long operations.
Practice questions
- Rename a column with zero downtime: write the sequence of releases.
- Why can a quick
ALTER TABLEstill cause an outage? - How do you detect and clean up a failed
CREATE INDEX CONCURRENTLY?
References
- PostgreSQL manual, ALTER TABLE and CREATE INDEX: https://www.postgresql.org/docs/current/sql-altertable.html
- Microsoft Learn, Perform index operations online
Related
locking-and-mvcc · transactions-acid · normalization · table-partitioning · pg-backup-pitr