Report #52482
[bug\_fix] ERROR: canceling statement due to lock timeout \(SQLSTATE 57014\) during ALTER TABLE
Use online schema change tools such as pg\_repack or pg-online-schema-change \(pg-osc\) that create a shadow table, copy data in batches, synchronize changes via triggers, and perform a brief atomic swap, avoiding long-held AccessExclusiveLock. Alternatively, set a short lock\_timeout and retry, but this rarely succeeds on busy tables. Root cause: ALTER TABLE requires an AccessExclusiveLock which conflicts with every other lock; long-running queries or uncommitted transactions hold weaker locks, causing the ALTER to wait and eventually timeout or block production traffic indefinitely.
Journey Context:
You run a migration to add a NOT NULL column with a default on a 500GB table in production. You execute ALTER TABLE bookings ADD COLUMN promo\_code VARCHAR\(255\) DEFAULT NULL; but it hangs. You check pg\_locks and see the ALTER is waiting for AccessExclusiveLock behind a long-running BI report query that started 2 hours ago. After 30 seconds your migration tool \(Flyway\) times out with 'canceling statement due to lock timeout'. You try again at 3 AM but another microservice holds a transaction open due to a missing commit in a background job. You realize you cannot get a window long enough to hold an exclusive lock on a busy table. You research and find pg\_repack, which creates a new table, copies data in batches, installs triggers to sync changes, then swaps the tables using brief locks. You test pg\_repack --no-order --table=bookings -d production\_db. It runs for 2 hours but the table remains accessible. At the end it swaps the files in milliseconds. The migration succeeds with zero downtime. You adopt pg-osc for all future large-table alters.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:35:12.316312+00:00— report_created — created