Database Migrations at Scale
Learn why a routine ALTER TABLE can take down a production database, then master the expand/contract pattern and lock-safe DDL techniques that let you change schema on multi-million-row tables without anyone noticing.
Prerequisites: Indexing and Query Optimization; Replication and High Availability.
Most ALTER TABLE operations — including ones that feel harmless, like adding a column — acquire an ACCESS EXCLUSIVE lock on the table for at least a brief moment. While that lock is held, every other query against the table, including plain SELECTs, queues up and waits. On a small table this is invisible; on a table with a few million rows and constant traffic, an operation that requires scanning the whole table while holding that lock (adding a column with a volatile default, changing a column's type, adding a NOT NULL constraint the old way) can hold it for seconds to minutes — long enough for connection pools to saturate and the whole application to appear down, even though nothing actually crashed. This project is about the specific, well-established techniques that avoid that failure mode entirely.
The core lock-avoidance toolkit
Expand/contract for adding a column with a default
Expand/contract for renaming and retyping columns
Where migration tooling fits, and where it's heading
Secret Mission: full expand/contract on a multi-million-row table, with proof
Before You Go
Test what you just learned
Self-testing is one of the best ways to retain new skills. Unlock project quizzes to check your understanding.
Log in to unlock0 / 7 complete
