Repository navigation
postgres: re-check the schema stamp under the bootstrap lock (40P01 on concurrent first open) - #462
Merged
Merged
Conversation
…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.
…lock # Conflicts: # CHANGELOG.md
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.
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_lockbut read the stamp only before the lock. An opener that queued behind the winner re-ran the whole DDL + seed. Itscountersupserts 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 with40P01on an open or on a write made right after one.Fix:
countersFOR UPDATEin key order before the seed, so it joinsreserve_write's single lock order.PG_SEEDis untouched, so there is noPG_SCHEMA_VERSIONbump 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-conformancesuite on both layouts (single and paired schema), 371areev-storetests with the postgres feature, and clippy-D warnings.Docs:
crates/areev-store/CLAUDE.md(bootstrap section), CHANGELOG[Unreleased].