Report #13796
[bug\_fix] canceling statement due to lock timeout during CREATE INDEX or ALTER TABLE
Root cause is that DDL operations \(like CREATE INDEX or ADD COLUMN\) require an ACCESS EXCLUSIVE lock on the table. If long-running queries or idle transactions hold a lock, the DDL waits. If lock\_timeout or statement\_timeout is set, it cancels. The fix for Postgres is to use CREATE INDEX CONCURRENTLY \(which takes a SHARE UPDATE EXCLUSIVE lock, allowing reads/writes, though it takes longer and cannot run inside a transaction block\). For other DDL, use lock\_timeout low to fail fast, kill blocking queries, then retry. Also, run migrations during low-traffic windows.
Journey Context:
You run a migration to add an index: CREATE INDEX idx\_user\_email ON users\(email\);. On production, the command hangs for 10 minutes then fails with 'canceling statement due to statement timeout'. You check pg\_stat\_activity and see a 2-hour-old idle transaction from a BI tool holding AccessShareLock on the users table. Your migration is blocked behind it. You realize CREATE INDEX needs an exclusive lock. You terminate the BI connection, but this is risky. You research and find CREATE INDEX CONCURRENTLY. You test it: it takes 5 minutes but doesn't block writes. You modify your migration framework to use concurrently=True for index creation. You also set lock\_timeout = '5s' in migration scripts to fail fast if blocked. Future migrations deploy smoothly without downtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:47:12.195918+00:00— report_created — created