Report #90299
[bug\_fix] Migration advisory lock timeout \(Flyway/Liquibase hangs acquiring lock\)
Terminate the PostgreSQL backend process holding the stale advisory lock using SELECT pg\_terminate\_backend\(pid\) FROM pg\_locks WHERE locktype='advisory' AND pid \!= pg\_backend\_pid\(\);, or configure the migration tool and PostgreSQL to use statement\_timeout and lock\_timeout to fail fast instead of hanging indefinitely. Prevent stale locks by ensuring migration jobs gracefully release connections on SIGTERM \(Kubernetes preStop hooks\) before the pod is killed.
Journey Context:
A CI/CD pipeline running on Kubernetes executes database migrations using Flyway during the deploy stage. The pipeline intermittently hangs indefinitely at "Acquiring migration lock...". Investigation shows a previous deployment pod was OOMKilled during the migration step, abruptly terminating the Flyway process without releasing the PostgreSQL advisory lock \(obtained via pg\_try\_advisory\_lock\). The advisory lock remains held by the orphaned backend PID from the crashed pod. Subsequent migration attempts wait indefinitely for the lock. The immediate fix involves connecting to PostgreSQL and running SELECT pg\_terminate\_backend\(pid\) FROM pg\_locks WHERE locktype='advisory'; to kill the stale holder. The permanent fix configures the Flyway datasource with 'lockTimeout' property \(translated to PostgreSQL lock\_timeout\) set to 60 seconds, causing the acquisition attempt to fail with an error after 60s rather than hanging forever. Additionally, a Kubernetes preStop hook is added to gracefully shut down the migration container on SIGTERM, giving Flyway time to release the lock before the pod is killed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T10:09:45.217459+00:00— report_created — created