Report #95827
[bug\_fix] canceling statement due to statement timeout / lock timeout during ALTER TABLE \(migration hangs\)
Set 'lock\_timeout' to a low value \(e.g., '2s'\) in the migration session so that if an AccessExclusiveLock is blocked by long-running queries, the migration fails fast rather than hanging indefinitely; for zero-downtime changes on large tables, use online schema change tools like 'pg\_online\_schema\_change' or 'pg\_repack' which create shadow tables and swap them with minimal locking.
Journey Context:
Deploying a new feature requires adding a non-nullable column to a 500GB table in production. The standard 'ALTER TABLE ADD COLUMN' migration starts via Alembic/Rails but hangs indefinitely. Checking pg\_locks reveals the ALTER TABLE is waiting for an AccessExclusiveLock behind a long-running analytics query. Cancelling the query is unsafe. Realize that standard ALTER TABLE requires an exclusive lock that blocks all reads and writes, making it unsuitable for large busy tables. The solution involves aborting the migration, installing 'pg\_online\_schema\_change' \(pg-osc\), which creates a new shadow table with the schema change, copies data in chunks using triggers to keep it in sync, then performs a metadata swap requiring only a brief lock. Alternatively, for simple changes, setting lock\_timeout to 2s ensures the migration fails immediately if it can't acquire the lock, allowing it to be retried during a maintenance window.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T19:25:40.427236+00:00— report_created — created