Q: A high-throughput PostgreSQL table ('users', 40 million rows) requires renaming column 'phone' to 'phone_number' and adding a NOT NULL constraint with default values, while maintaining 99.999% API availability. Running a standard 'ALTER TABLE users ADD COLUMN ... NOT NULL DEFAULT ...' or renaming the column locks the table with ACCESS EXCLUSIVE, queuing thousands of web requests until the reverse proxy 504 timeouts. How do you design and execute a multi-phase Expand-Contract migration without downtime or table lock starvation?
Design and execute an Expand-Contract database schema refactoring on a 40-million row table, avoiding ACCESS EXCLUSIVE locks and application downtime.
Want to master this scenario in a live sandbox? KodeKloud's PostgreSQL Database Administration & High Availability Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Situation: Schema Migration Causes Severe Gateway Outages
Task: Execute Phased Refactoring Without Table-Level Lock Contention
Action: Four-Phase Expand-Contract Execution
Result: 100% Zero-Downtime Deployment & Zero Queued Locks
- Destructive DDL like column renames and inline constraints acquire ACCESS EXCLUSIVE locks.
- Use the Expand-Contract pattern: add nullable column, dual-write in application, backfill in batches.
- Add constraints with NOT VALID and validate later to avoid blocking concurrent database writes.