Skip to content

fix: honor migration lock wait budgets without blocking concurrent indexes - #1706

Open
IANTHEREAL wants to merge 3 commits into
supabase:masterfrom
IANTHEREAL:chordra/storage-migration-lock-wait
Open

IANTHEREAL wants to merge 3 commits into
supabase:masterfrom
IANTHEREAL:chordra/storage-migration-lock-wait

Conversation

@IANTHEREAL

@IANTHEREAL IANTHEREAL commented Nov 16, 2026 •

Copy link
Copy Markdown

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 LockTimeout even 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_lock attempts and wait outside SQL statements. Read the connection's statement_timeout from pg_settings in 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 existing LockTimeout, and unlock only after successful acquisition.

A blocking SELECT pg_advisory_lock is unsafe here: its active snapshot can deadlock the holder's CREATE INDEX CONCURRENTLY validation. A real two-replica on-request test caught 40P01, 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.

@IANTHEREAL
IANTHEREAL requested a review from a team as a code owner November 16, 2026 20:47
Copilot AI balanced review requested due to automatic review settings November 16, 2026 20:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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', () => {
@IANTHEREAL

Copy link
Copy Markdown
Author

Independent review of 70a30b24cd5f283dce1385804c45705f20dfcec4 found one blocking integration-fixture issue: Multigres runs the integration inventory but cannot create disposable databases. This is corroborated by the existing lifecycle migration fixtures, including the explicit explanation in lifecycle-migration-upgrade.test.ts.

Fixed in 887fa0354a8d02902fa4458029feea1fddaefb2e by applying the same engine guard. The PostgreSQL test bodies and production lock implementation are unchanged. The four real-connection cases and 33 existing unit cases passed before this fixture-only change; Biome passes, and a targeted Multigres collection run now correctly excludes the unsupported disposable-database fixture. This does not claim Multigres lock coverage.

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.

@IANTHEREAL

Copy link
Copy Markdown
Author

Final independent review passes at 5d6e4bad38bd2db7024902e91b49223a20010464.

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.

@IANTHEREAL IANTHEREAL changed the title fix: honor migration lock waiting until the database deadline fix: honor migration lock wait budgets without blocking concurrent indexes Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants