Skip to content

postgres: re-check the schema stamp under the bootstrap lock (40P01 on concurrent first open) - #462

Merged
sathish-mg merged 2 commits into
mainfrom
fix/pg-bootstrap-deadlock
Nov 24, 2026
Merged

sathish-mg merged 2 commits into
mainfrom
fix/pg-bootstrap-deadlock

Conversation

@sathish-mg

Copy link
Copy Markdown
Contributor

Fixes the flaky concurrent_first_open_bootstraps_once (40P01 deadlock detected, seen in CI on #452).

Cause: the bootstrap serialises openers with pg_advisory_xact_lock but read the stamp only before the lock. An opener that queued behind the winner re-ran the whole DDL + seed. Its counters upserts run in the order seq, op, hlc. The winner's first writes (reserve_write) lock the same rows in key order: hlc, op, seq. That is a lock cycle, and Postgres broke it with 40P01 on an open or on a write made right after one.

Fix:

  1. Re-read the stamp inside the lock (double-checked locking); a schema that is already current is left alone.
  2. For a real migration, take counters FOR UPDATE in key order before the seed, so it joins reserve_write's single lock order.

PG_SEED is untouched, so there is no PG_SCHEMA_VERSION bump and no forced re-bootstrap on existing deployments.

Evidence. The original test passed 150 of 150 locally on the old code, because the race is timing-dependent. So I wrote a harsher test, first_openers_that_write_at_once_never_deadlock: 40 fresh schemas, with 6 openers each writing straight after opening. On the old code it failed 3 of 3 runs, in round 0, with deadlocks on both open and write. On the fix it passes 5 of 5.

Gates run locally (Postgres 16 + pgvector): the full areev-conformance suite on both layouts (single and paired schema), 371 areev-store tests with the postgres feature, and clippy -D warnings.

Docs: crates/areev-store/CLAUDE.md (bootstrap section), CHANGELOG [Unreleased].

…unters in key order (40P01 on concurrent first open)

An opener that queued on the advisory lock behind the winner re-ran the whole
DDL + seed. Its counters upserts (seq, op, hlc) raced the winner's first
writes, which lock the same rows in key order (hlc, op, seq); Postgres broke
the cycle with 40P01, on an open or on a write. Read the stamp again inside
the lock (double-checked locking) and, for a real migration, take counters
FOR UPDATE in key order before the seed. PG_SEED is untouched, so no
PG_SCHEMA_VERSION bump.

Adds first_openers_that_write_at_once_never_deadlock: 40 fresh schemas x 6
openers writing at once. Fails on every run without the fix (0/3), passes
5/5 with it.
@sathish-mg
sathish-mg requested a review from a team as a code owner November 24, 2026 05:58
@sathish-mg
sathish-mg merged commit 1911fb4 into main Nov 24, 2026
20 checks passed
@sathish-mg
sathish-mg deleted the fix/pg-bootstrap-deadlock branch November 24, 2026 07:06
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.

1 participant