Report #54920
[architecture] Standard DDL operations \(ADD INDEX, ADD COLUMN NOT NULL\) lock tables for hours on large PostgreSQL tables causing downtime
For PostgreSQL: use CREATE INDEX CONCURRENTLY \(avoids write locks\), add nullable columns first then backfill in batches, add CHECK constraints with NOT VALID then VALIDATE CONCURRENTLY. Never run standard ALTER TABLE on >10M row tables during traffic.
Journey Context:
Developers run ALTER TABLE ADD COLUMN NOT NULL DEFAULT 'x' on a 100M row table and lock the table for hours as it rewrites the entire table to populate the default. Even adding an index blocks writes. PostgreSQL provides CONCURRENTLY options that bypass locks by building the index in the background, but they can't run inside a transaction block and fail if the table is corrupted. The safest pattern for adding NOT NULL constraints is: add column nullable, backfill existing rows in small batches \(avoiding long transactions\), add CHECK constraint NOT VALID \(fast, no scan\), then VALIDATE CONCURRENTLY \(scans without lock\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T22:40:45.125029+00:00— report_created — created