Introduction
A product manager asks for a small change: rename a column, split a field into two, and make an optional value required. The developer writes a migration, it passes in staging, and in production it locks a 40-million-row table for six minutes while every API request that touches it queues up. Support tickets arrive before the migration finishes.
The migration was not wrong. It was written as if the application and the database change at the same instant, which never happens in a system with rolling deploys, background workers, and multiple app versions running at once.
In this article you will learn the expand-contract pattern, which ensures Postgres operations are safe online, how to backfill large tables without hurting production, and how to sequence deploys so old and new code can run side by side.
Why Migrations Cause Downtime
Most migration outages come from locks, not from the work itself. Many ALTER TABLE forms take an ACCESS EXCLUSIVE lock, which blocks reads and writes.…