Report #93068
[architecture] Direct schema changes \(drop column, add non-nullable default, rename\) on large tables cause ACCESS EXCLUSIVE locks and downtime
Use Expand-Contract pattern: 1\) Expand - add new column as nullable/write-both, 2\) Migrate - backfill in small batches with delay, 3\) Contract - switch reads, drop old. Use tools like pg-online-schema-change
Journey Context:
Direct ALTER TABLE on millions of rows locks the table for seconds to minutes, blocking all queries. The expand-contract pattern treats schema like blue/green deployments: additive changes only during expansion, then removal after transition. Critical implementation: write to both old and new structures during migration to avoid data loss; backfill in small transactions \(e.g., 1000 rows\) with \`pg\_sleep\` to prevent lock contention; verify counts before dropping. PostgreSQL 11\+ made adding columns with defaults fast \(metadata only\), but other operations \(adding FK, changing type, dropping\) still require this pattern.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:48:02.313667+00:00— report_created — created