Why Naive Schema Changes Trigger Production Outages
Adding a column with a non-constant rewrite or creating a standard index on a busy table acquires locks that stall incoming reads and writes. During rolling container deploys, old and new application code run side by side for several minutes.
The expand-and-contract pattern decouples schema evolution into backward-compatible steps so old replicas never crash on missing columns.
The Three-Step Expand & Contract Sequence
Always split destructive renames or type changes across separate deployments:
- 1. Expand: Add nullable columns or create indexes with CONCURRENTLY
- 2. Dual-Write & Backfill: Update code to write both fields and backfill batches safely
- 3. Contract: Remove legacy reads and drop deprecated columns once traffic shifts
-- Safe index creation without blocking active reads or writes
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_invoices_tenant_created
ON invoices (tenant_id, created_at DESC);