Report #29644
[bug\_fix] ERROR: canceling statement due to lock timeout
Before running DDL \(e.g., ALTER TABLE\), execute 'SET LOCAL lock\_timeout = '10s';' so the statement fails fast if it cannot acquire the lock, rather than hanging indefinitely or until statement\_timeout. For zero-downtime changes on large tables, use 'pg\_repack', 'pg-online-schema-change', or 'ALTER TABLE ... ADD COLUMN ... DEFAULT ... NOT NULL' with PostgreSQL 11\+ optimizations. Root cause: Long-running SELECTs or uncommitted transactions on the table hold ACCESS SHARE or ROW SHARE locks, blocking the ALTER TABLE's requirement for an ACCESS EXCLUSIVE lock.
Journey Context:
A DevOps engineer runs 'ALTER TABLE events ADD COLUMN metadata JSONB;' on a production PostgreSQL 14 database with 500GB of data. The command hangs for 20 minutes. In another terminal, pg\_stat\_activity shows the ALTER is waiting for AccessExclusiveLock behind a long-running BI report query. The engineer panics and cancels the query, but the application briefly errored out. The solution is to run 'SET lock\_timeout = '5s';' before the ALTER; if it fails, the team knows to retry during the maintenance window or use pg\_repack, which creates a shadow table and uses triggers to sync changes, applying the schema change without long locks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:08:54.348802+00:00— report_created — created