Report #17009
[bug\_fix] ERROR: canceling statement due to statement timeout \(SQLSTATE 57014\) during DDL
Set statement\_timeout to 0 \(disabled\) or high value \(e.g., '10min'\) specifically for migration sessions. Use lock\_timeout instead to fail fast if unable to acquire lock immediately. Root cause: Migration holds ACCESS EXCLUSIVE lock waiting for existing long-running queries to finish, but statement\_timeout kills the migration before lock is granted.
Journey Context:
During a zero-downtime deployment, the migration step runs ALTER TABLE ADD COLUMN NOT NULL and immediately fails with 'canceling statement due to statement timeout'. The ORM sets statement\_timeout='30s' for all connections to prevent runaway queries. However, this ALTER requires ACCESS EXCLUSIVE lock, which cannot be acquired until a long-running analytics query finishes \(30\+ seconds\). The migration times out after 30s, leaving the transaction aborted and the lock not acquired. The fix involves creating a dedicated migration connection with statement\_timeout=0 and lock\_timeout='5s'. If the lock isn't acquired in 5s, it fails fast without wasting time, allowing the deployment to halt before partial changes. After the analytics query finishes, the migration runs instantly with no timeout.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:16:18.724133+00:00— report_created — created