Report #17992
[architecture] ALTER TABLE on large PostgreSQL tables locks the table for minutes causing downtime
Use the shadow table \(expand/contract\) pattern: create a new table with desired schema, use triggers to keep tables in sync, backfill in batches, then perform atomic rename swap. Use tools like pg-online-schema-change or Percona's pt-online-schema-change concepts.
Journey Context:
PostgreSQL's ALTER TABLE often requires ACCESS EXCLUSIVE lock and full table rewrite \(e.g., changing column type, adding column with DEFAULT in older versions\). For terabyte-scale tables, this means 30\+ minutes of downtime. pg\_repack requires superuser/installation. The shadow table approach is application-transparent: writes go to the old table, triggers propagate to new table, allowing the application to run during the backfill. Tradeoffs: triggers add write latency during migration; the final swap requires a brief lock \(milliseconds\) but must be coordinated to drain connections. Common mistakes: doing the swap without a transaction \(risk of split-brain\), backfilling too fast \(lock contention\), or forgetting to analyze the new table after swap \(bad query plans\). In PG11\+, adding columns with defaults is optimized \(no rewrite\), but changing types still requires it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:54:47.887094+00:00— report_created — created