Repository navigation
fix: honor migration lock wait budgets without blocking concurrent indexes - #1706
IANTHEREAL wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The advisory-lock suite’s unsupported CREATE DATABASE setup causes all four tests to fail in Multigres CI before exercising lock behavior.
1 open issue
What changed in this PR
Updates database migration locking so waiting migrations honor the connection’s statement timeout rather than a separate three-second deadline.
Changes:
- Uses blocking advisory-lock acquisition when waiting is enabled, while preserving fail-fast behavior otherwise.
- Releases the advisory lock only after successful acquisition.
- Adds integration coverage for extended waits, contention, timeout cancellation, rollback, and migration-ledger state.
| File | Description |
|---|---|
src/test/migrations-advisory-lock.test.ts |
Adds four integration tests for migration lock behavior and failure handling. |
src/internal/database/migrations/migrate.ts |
Replaces lock polling with database-controlled waiting and guards lock release. |
Review effort: Balanced
|
|
||
| // Real PostgreSQL connections exercise migration serialization, cancellation, | ||
| // failure rollback and lock ownership. Run with the normal integration config. | ||
| describe('migration advisory lock lifecycle', () => { |
|
Independent review of Fixed in The review found no additional production blocker in the migration callers, statement timeout, error propagation, rollback or dedicated-connection cleanup. Next: fresh independent review of the updated head and the upstream CI checks. |
|
Final independent review passes at The complete PostgreSQL application differential invalidated the earlier blocking advisory-lock implementation: a waiting SELECT retained a snapshot, and the lock holder's CREATE INDEX CONCURRENTLY waited for that virtual transaction, producing 40P01. A focused real-connection test reproduced the cycle. This was a current blocker, not a transient failure to hide with retries. The final owner fix keeps nonblocking lock probes, sleeps outside SQL, and derives a monotonic contention deadline from the connection's canonical statement_timeout setting. Explicit zero, fail-fast mode and acquired-lock cleanup retain their intended semantics. The default production migration connection supplies a 12-hour budget. Exhaustion is the existing application LockTimeout; actual query failures still propagate. Final-head maintained evidence: 6/6 real-connection cases pass on PostgreSQL 17.7 and db9, including concurrent-index progress, and a fresh reviewer independently passes the same 6/6 PostgreSQL fixture. Migration/vector-store/file-backend unit suites pass 188/188 with no skips when run as the non-root node user. The initial root run skipped two filesystem-permission cases; both were actually executed in the complete non-root rerun. TypeScript build and Biome pass. Boundary notes: request cancellation propagation predates this change and is not claimed here. Multigres cannot create this fixture's disposable databases and remains explicitly excluded; no Multigres coverage is claimed. Upstream CI/Acceptance are waiting for maintainer approval on this fork PR; local validation does not substitute for those checks. The final v1.96.2 production-image-based runtime also passes the complete isolated Storage H-3 matrix against db9 floor 52 and PostgreSQL 17.7: ten fresh/retry/concurrent migration runs per engine (all 34 manager and 81 tenant ledger entries match by ID/name/hash), 27 direct API checks, eight multitenant checks including two-replica on-request migration, six notification/update/rollback checks, eleven TUS checks, and 27 checks through each PgBouncer mode. Fault-injected migration rollback/retry and all four black-box lock checks pass. Each engine completed 552 requests over five minutes with zero failures; Storage container restarts and db9 restart/reconnect passed. This is Storage-only acceptance, not the combined 90-minute Supabase production release gate. |

Concurrent fresh migrations can legitimately hold the migration advisory lock longer than the runner's fixed three-second polling budget. A second replica then returns
LockTimeouteven though the first is making progress. This is reproducible with the official Storage v1.96.2 image, and on PostgreSQL 17.7 by holding the lock for 4.5 seconds.Keep nonblocking
pg_try_advisory_lockattempts and wait outside SQL statements. Read the connection'sstatement_timeoutfrompg_settingsin canonical milliseconds as the total contention budget (the normal migration connection configures 12 hours; zero explicitly disables the deadline). Preserve fail-fast mode, report an exhausted contention budget as the existingLockTimeout, and unlock only after successful acquisition.A blocking
SELECT pg_advisory_lockis unsafe here: its active snapshot can deadlock the holder'sCREATE INDEX CONCURRENTLYvalidation. A real two-replica on-request test caught40P01, and the new connection-level regression reproduces that cycle. PostgreSQL describes the older-snapshot wait in its concurrent-index documentation.Six real-connection cases cover waiting beyond three seconds, fail-fast mode, the configured deadline, concurrent-index progress, disabled deadlines, and failure rollback/lock release. All six pass against PostgreSQL 17.7 and db9 at the final head. The complete v1.96.2 Storage H-3 matrix, both PgBouncer modes, TUS, migration rollback/retry and restart checks pass on both engines. The migration and file-backend unit suites pass 188/188 with no skips under a non-root user. The fixture uses the existing Multigres exclusion convention because that engine cannot create disposable databases; it does not claim Multigres coverage.