Report #53555
[bug\_fix] ERROR: canceling statement due to lock timeout during ALTER TABLE or CREATE INDEX
DDL statements \(ALTER TABLE, CREATE INDEX\) require an ACCESS EXCLUSIVE lock. If long-running queries or idle transactions hold locks, the DDL waits, potentially exceeding statement\_timeout or lock\_timeout, causing the migration to fail. Fix: Before migration, identify and terminate blocking queries \(SELECT pid, usename, state, query FROM pg\_stat\_activity WHERE datname = 'db' AND state \!= 'idle'; then SELECT pg\_terminate\_backend\(pid\) for blockers\). Alternatively, set a high lock\_timeout for the migration session only, and implement a retry loop with exponential backoff. For zero-downtime, use pg-online-schema-change or CREATE INDEX CONCURRENTLY \(which requires two scans and doesn't block reads/writes, but takes longer and can't run inside a transaction\).
Journey Context:
We were deploying a new feature that required adding a non-nullable column to a 500GB table in production. We ran the migration using Alembic \(Python\). The command ALTER TABLE large\_table ADD COLUMN new\_col VARCHAR NOT NULL DEFAULT 'x' hung for 60 seconds then failed with ERROR: canceling statement due to statement timeout. We checked pg\_stat\_activity: our ALTER was in 'active' state waiting for AccessExclusiveLock, while three connections \(from reporting tools\) were in 'idle in transaction' state holding AccessShareLock on the same table. Those idle transactions were started hours ago by analysts who left their psql sessions open. The migration couldn't get the lock in time. We terminated those idle backends using pg\_terminate\_backend. We then re-ran the migration, but this time we wrapped it in a script that first checks for blockers. For future safety, we switched to using CREATE INDEX CONCURRENTLY for indexes, and for adding columns we started using the pt-online-schema-change approach \(renaming tables\) for true zero-downtime, or we ensure migrations run during maintenance windows with all idle transactions cleared.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T20:23:28.458427+00:00— report_created — created