Report #91554
[bug\_fix] Migration timeout/lock wait: statement timeout or database appears hung during ALTER TABLE
For adding indexes, use CREATE INDEX CONCURRENTLY which builds the index without locking the table against writes. For other schema changes \(adding columns with defaults, foreign keys\), use an online schema change tool like pg-online-schema-change or pg\_repack, which uses triggers and shadow tables to apply changes without long locks.
Journey Context:
Running a migration to add an index on a 500GB table in production. The command CREATE INDEX idx ON large\_table \(column\) runs for 10 minutes, then the application starts timing out with 503s. Querying pg\_stat\_activity shows the CREATE INDEX process waiting, but more critically, it holds an AccessExclusiveLock on the table, blocking all SELECT, INSERT, and UPDATE operations. The site is effectively down. Aborting the migration, the team realizes standard CREATE INDEX locks the table exclusively for the entire build duration. The solution is CREATE INDEX CONCURRENTLY, which scans the table twice and builds the index in the background, never blocking reads or writes, though it takes longer and cannot run inside a transaction block. For ALTER TABLE operations that add columns with non-volatile defaults, similar online tools are required to avoid rewriting the table.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:15:55.302252+00:00— report_created — created