Repository navigation
ops(migrate): bound migration advisory-lock acquisition with statement_timeout - #3509
Merged
Merged
Conversation
…t_timeout If a prior runMigrations() session crashed while holding pg_advisory_lock, the next deploy waits until Postgres' TCP keepalive reaps the dead session (default ≈2 hours). With Fly's 15-minute release_command_timeout, the deploy "hangs" until manual intervention. acquireMigrationLock now sets statement_timeout to 5 minutes while waiting for the lock, then clears it once acquired so the migration itself isn't capped. On timeout (pg code 57014), we throw a diagnostic error naming the lock key and the SQL to find/terminate the wedged session. Behavior is unchanged in the happy path: legitimate concurrent callers (release_command + a dev's docker-compose) still serialize on the blocking pg_advisory_lock, just with a bound on stale-session waits. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced Oct 29, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Defensive follow-up to PR #3491. Today, if a previous `runMigrations()` session crashes while holding `pg_advisory_lock` (release_command OOM, machine kill mid-migration, etc.), the next deploy waits until Postgres' TCP keepalive reaps the dead session — by default ≈2 hours. Fly's `release_command_timeout` is 15 minutes, so the deploy fails with no diagnostic about why it's waiting.
This PR caps the wait at 5 minutes via `SET statement_timeout = '5min'` on the lock-acquisition query, then clears the timeout immediately after the lock is held so the migration itself isn't capped. On timeout (pg code `57014`), we throw a clear error that names the lock key and the SQL to find and terminate the wedged session:
```
SELECT pid, state, query_start, query FROM pg_stat_activity
WHERE pid IN (SELECT pid FROM pg_locks WHERE locktype='advisory' AND objid=1835102578);
```
Happy path is unchanged: legitimate concurrent callers (release_command + a dev's docker-compose containers) still serialize on the blocking `pg_advisory_lock`. We just bound the stale-session wait.
Test plan
🤖 Generated with Claude Code