Skip to content

Read the high-water mark through the sequence's lock - #285

Merged
RobGrafrath merged 1 commit into
mainfrom
read-the-mark-through-the-sequence-lock
Nov 15, 2026
Merged

RobGrafrath merged 1 commit into
mainfrom
read-the-mark-through-the-sequence-lock

Conversation

@RobGrafrath

@RobGrafrath RobGrafrath commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes and why

WAX-0001 Enriched Event Sourcing defines the high-water mark: the highest global sequence number at or below which every transaction that drew a value has committed or rolled back. Readers of the event stream stop there, and WAX-0013 Event Chain Integrity and Tamper-Evidence ends each checkpoint window there. Neither decision said how to read the number the mark starts from, and the obvious way is wrong.

A plain query of the sequence's last_value column does not take the sequence's lock. In PostgreSQL 17, nextval writes the sequence to the write-ahead log at least once every 33 calls when the sequence has a cache of 1, and while it does, the sequence's row briefly holds a number 32 ahead of the one it hands out. A plain query can see that number. A mark built on it moves past numbers that open transactions are about to commit, and a reader that trusts the mark never reads those events. pg_sequence_last_value() reads the same row under the sequence's lock and cannot see the inflated number.

The Foundation's engine bake-off found this the hard way. Its round 2 diagnostic pass, published at the spikes repository's tag round-2-diag, describes it in the diagnostic pass section of the bake-off README:

  • A standalone test ran 16 clients calling nextval at about 49,000 calls per second in a throwaway container of the bake-off's PostgreSQL 17.11 image. The plain read returned a number not yet handed out about once in 4 to 8 million reads, never more than 32 ahead. The locked read never did.
  • A follow-up repeat of the failing configuration, k5, kept its database after a failure. Three payment events with consecutive numbers never reached the payment read model. The highest missing number is exactly the value the sequence held for a moment when it last logged itself before them, and the three transactions were still open when the reader read past them.
  • The pass's run 2 failed a gate of the same kind in the same configuration. Its database was dropped at the end of the run, so run 2 is attributed to this defect by its configuration and the kind of gate it failed, not by inspection. Over the night that configuration ran 20 times and failed twice.

This pull request adds the missing rule to the design. In WAX-0001's Sequence Numbers section, it says that Wax reads the starting number with pg_sequence_last_value(), which takes the sequence's lock, and it explains why a plain query can cause the skip the mark exists to prevent. In WAX-0013's section on delta checkpoint anchoring, it says that a window ending at the mark is settled only when the mark is read that way, because a window ending at an inflated number could close over numbers not yet handed out. Correcting the bake-off's own readers is separate work in the spikes repository.

Rob approved the wording on 2026-11-15, as a pull request of its own.

Interacting decisions

Edited: WAX-0001 and WAX-0013.

Unchanged, and consistent with the change: WAX-0003 Database Platform, which names transaction identifiers as what a reader finds its high-water mark from, and WAX-0024 High Availability, Disaster Recovery, and Distributed Workloads, which refers to the mark as WAX-0001 defines it. Both pick up the corrected definition by reference. Nothing here contradicts an adopted decision; the change states a requirement the high-water mark always needed.

If this touches decisions/wax/

  • Hash chain integrity: strengthened. A checkpoint window can no longer end past a sequence number still in flight, so an event that commits later cannot land inside a window already anchored without it. The aggregate-level hash chains themselves are unchanged.
  • Event immutability: unchanged. No event is written or rewritten differently.
  • Schema ownership: unchanged. No table, column, or schema moves. pg_sequence_last_value() needs only SELECT or USAGE privilege on the sequence, which a reader already has.
  • Module boundaries: unchanged. The rule binds Wax's own stream readers and checkpoint module; no module gains or loses a responsibility.

Version bumps

Each number below is relative to main at c653153.

  • WAX-0001: 2.3 to 2.4, minor. The Decision section is unchanged; the Sequence Numbers section gains a substantive requirement.
  • WAX-0013: 2.1 to 2.2, minor. The Decision section is unchanged; the Delta-Based External Checkpoint Anchoring section gains a substantive condition.

Coordination with unmerged branches

  • migrations-additive-by-default, the schema migrations branch, is not yet open as a pull request and edits other sections of both files. The two branches overlap only on the version and date lines, so whichever merges second takes the next minor version in each file when it rebases.

Checklist

  • I read the decisions neighboring this change, not just the file I edited.
  • New or retitled decisions have a matching line in INDEX.md, and front matter follows CONTRIBUTING.md. (None is new or retitled.)
  • python3 scripts/validate.py --base main passes locally, or I am relying on CI to tell me. (Run on 2026-11-15 against origin/main c653153. Under WSL, every check passed apart from the version check, which cannot run in a Windows worktree from WSL and exited 128 on git merge-base. Run from Windows, the version check passed, and the only errors were the known INDEX.md path-separator false failures. CI runs the full check.)
  • No pricing, rates, market or customer allocation, consumer data, or client-confidential information.
  • Claims about law or regulation are flagged for verification, not asserted. (None is made.)
  • Commits are signed off (DCO). If I forgot, I will post the retroactive sign-off comment per CONTRIBUTING.md.
  • I license this contribution under CC BY 4.0 (prose) and Apache 2.0 (embedded code samples).

🤖 Generated with Claude Code

WAX-0001 and WAX-0013 rely on the high-water mark, but neither said how
to read the number the mark starts from. A plain query of the
sequence's last_value column can briefly return a number up to 32
higher than any yet handed out while nextval writes the sequence to
the write-ahead log. A mark built on that number moves past events
still in flight, and the reader never sees them. The bake-off's round
2 diagnostic pass traced a read model skip to exactly this.

WAX-0001 now says that Wax reads the number with
pg_sequence_last_value(), which takes the sequence's lock, and why.
WAX-0013 now says that a checkpoint window is settled only when the
mark is read that way. Neither change touches a Decision section, so
WAX-0001 goes from 2.3 to 2.4 and WAX-0013 from 2.1 to 2.2. Rob
approved the wording on 2026-11-15.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Rob Grafrath <rob@openarcollective.org>
@RobGrafrath
RobGrafrath merged commit 868ec5d into main Nov 15, 2026
3 checks passed
@RobGrafrath
RobGrafrath deleted the read-the-mark-through-the-sequence-lock branch November 15, 2026 20:43
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