Report #55925
[bug\_fix] canceling statement due to lock timeout / statement timeout during migration
Set lock\_timeout in the migration script \(e.g., SET lock\_timeout = '5s';\) with aggressive retry logic, or use an online schema change tool like pg-osc or pt-online-schema-change. Root cause is long-running transactions holding AccessShareLock on the table, blocking the ALTER TABLE from acquiring AccessExclusiveLock.
Journey Context:
Developer runs ALTER TABLE users ADD COLUMN phone\_number VARCHAR; on production. Command hangs indefinitely. In another terminal, SELECT \* FROM pg\_locks WHERE locktype = 'relation' AND NOT granted; shows the ALTER TABLE waiting for AccessExclusiveLock. pg\_stat\_activity reveals a long-running BI query started 3 hours ago holding AccessShareLock on users table. Developer realizes ALTER TABLE needs exclusive lock but can't wait forever. Kills the BI query \(risky\), then migration runs instantly. For next deployment, implements migration wrapper that runs SET lock\_timeout = '2s'; ALTER TABLE ...; in a retry loop with exponential backoff, and schedules migrations during low-traffic windows. Considers using pg-online-schema-change to avoid exclusive locks entirely by creating new table and syncing via triggers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:21:43.868436+00:00— report_created — created