Skip to content

feat(reporting): order the account change feed by (xid, seq) - #3289

Merged
bokelley merged 2 commits into
mainfrom
bokelley/account-feed-xid-order
Oct 15, 2026
Merged

bokelley merged 2 commits into
mainfrom
bokelley/account-feed-xid-order

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Closes #3283

What

The account change feed now orders by (xid, seq) behind xid < pg_snapshot_xmin(pg_current_snapshot()), the way the deployment-wide feed already does. It no longer depends on the account lock to keep seq in commit order, and the #3228 truncation guard is gone. A change with a lower seq and a higher xid that commits late is returned on a later page, after the cursor, instead of being skipped. A writer that takes a different lock key can no longer cause a missed change.

Old-cursor decision: map, don't expire

An account cursor from before this change is { seq } with no xid. Failing it with CURSOR_EXPIRED would force every consumer to resync, and a BigQuery sink would reload its history. A mapping that never skips exists, so changesAfter accepts it:

  • Position = (X, S) with X = LEAST(snapshot xmin, oldest visible xid among the account's changes with seq > S), computed in one statement.
  • Every change after S has an xid at or above both terms: a visible one is at or above the minimum, and one not yet visible belongs to a transaction in flight or started after the snapshot, whose id is at or above xmin. So nothing is skipped.
  • The cost is redelivery: changes with a lower seq and a higher xid, and, if a long transaction was open at that moment, every change committed since it began. Consumers are at-least-once already. The returned cursor is in the new format even on an empty page, so a sink saves it on the next batch.
  • Expiry stays exact: the account horizon keeps pruned_through_seq (highest seq pruned, unchanged semantics) next to the new tuple horizon, and a legacy cursor fails with CURSOR_EXPIRED if any change with a higher seq was pruned. A tuple compare alone would miss a pruned change with a higher seq and a lower xid (covered by a test).
  • Consumer rows saved before the upgrade have NULL cursor_xid and hold by seq until the consumer saves its next cursor. saveFeedConsumerCursor given a legacy cursor maps it the same way.

Other changes

  • pruneChanges selects victims in (xid, seq) order and records the per-account tuple horizon; pruneChanges, retireExpiredPeriods and snapshot compaction share one feedConsumerBehindChange predicate.
  • REPORTING_LEDGER_CHANGES_MIGRATION (idempotent, advisory-locked): adds the horizon xid columns, replaces the consumer table's check constraint, adds (account_id, xid, seq). The old (account_id, seq) index is kept (it serves the legacy mapping). Plan verified: with a realistic table the account page is an ordered index scan with no Sort.
  • Cursor seq and xid are range-checked so out-of-range values fail as INVALID_INPUT rather than a raw database error.
  • Docs: docs/guides/REPORTING-LEDGER.md. Changeset: minor with a migration note (mixed-fleet maintenance, rollback, CREATE INDEX CONCURRENTLY).

Tests

PG suites in test/lib/reporting-*.test.js pass with --test-concurrency=1 (1092 tests). New: lower-seq/higher-xid late commit, held-back change, other-account transaction, legacy mapping (never skips, minimal redelivery, in-flight transaction), legacy expiry on a pruned higher seq, legacy consumer holds, prune ordering and batching, compaction and retirement holds in (xid, seq) order, migration upgrade and 8 concurrent appliers, plan check, BigQuery sink resuming a legacy cursor.

Review gate

  • code-reviewer-deep: no skip bug found in the mapping proof, horizon SQL or NULL handling. Fixed: wording on redelivery bound, forward-only guard for NULL-xid rows, a lock comment. Documented: older hosts' maintenance must not run against new-format consumers (it holds by seq and records only pruned_through_seq), rollback after new-format cursors is unsupported, and large tables should build the index concurrently.
  • nodejs-testing-expert: real gap found and fixed (a limited prune batch could leave a retained change below the account horizon; victims are now taken in (xid, seq) order). Added the missing tests it listed (minimal redelivery, exact mapped values, legacy save over a new-format row, concurrent migration, compaction hold, cursor range). Flake runs: 5/5 alone and alongside other suites; cross-file concurrent runs of the horizon-race tests were already not run concurrently in CI.
  • Codex (review:codex --all): the repo is unreadable in its sandbox, so I fed it the diff on stdin. It raised the same rolling-upgrade pruner issue (addressed by documentation, see above); its "no tests" findings were from a diff that excluded tests. Declined: a conservative compatibility check using pruned_through_seq for new-format cursors, because mixed-fleet maintenance cannot be made safe in general and the false-expiry cost would hit normal operation.

🤖 Generated with Claude Code

bokelley and others added 2 commits October 15, 2026 20:13
The account feed ordered by seq and cut each page before the first in-flight
change, which depends on lock discipline (#3228). Order it by (xid, seq) behind
xid < pg_snapshot_xmin(...), as the deployment-wide feed does, and drop the
truncation guard.

- Account cursors carry xid. A seq-only cursor from before is mapped to a
  position that skips nothing (the smaller of the oldest later change's xid and
  the snapshot's xmin) and upgraded; it fails with CURSOR_EXPIRED if a change
  with a higher seq was pruned.
- The account horizon gains (xid, seq) columns, the consumer table's check
  constraint is replaced, and (account_id, xid, seq) is indexed.
- pruneChanges, retireExpiredPeriods and compaction holds use (xid, seq) for
  account consumers; pre-upgrade rows hold by seq.

Closes #3283

Co-Authored-By: Claude Sonnet <[email protected]>
- pruneChanges takes victims in (xid, seq) order, so a limited batch cannot leave a retained change below the account horizon.
- Cursor seq and xid are range-checked, so out-of-range values fail as INVALID_INPUT.
- A NULL-xid consumer row is replaced only by a cursor whose seq is not behind.
- Document mixed-fleet maintenance, rollback and concurrent index creation.
- Tests: minimal redelivery of a mapped legacy cursor, exact mapped values, prune batching, concurrent migration appliers, compaction hold in (xid, seq) order, legacy cursor save against a new-format row.

Co-Authored-By: Claude Sonnet <[email protected]>
@bokelley
bokelley enabled auto-merge (squash) October 15, 2026 20:55

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ladon verdict: Approve

Approve.

Clean review with no blocking or Medium findings. Verified against the decision table top-down:

  • No critical/high findings (row 1 no match).
  • gated_paths is false (row 2 no match) — note review_decision is REVIEW_REQUIRED, but with gated_paths false the hard gate does not apply.
  • high_risk is false, no deleted/modified high-risk triggers (rows 3–5 no match).
  • No prior decision (row 6 no match).
  • No no-auto-approve team match (row 7 no match).
  • Zero medium findings, so row 8 (≥3 medium) does not fire.
  • Falls through to row 9 → approve.

Checked: account change feed reordered to (xid, seq) matching the deployment feed; legacyAccountCursorXid is snapshot-safe via LEAST(xmin, min-unread-xid) so no post-legacy-cursor change is skipped; assertRetained fail-closes on the original cursor; prune horizon advances monotonically with no fail-open window; cursor validation tightening (fitsUnsigned) is strictly better. Changeset is present and correctly scoped for an @experimental reporting-storage break with a full migration note; migration constraint-swap is guarded, idempotent, and covered by concurrent-applier + re-apply tests.

@bokelley
bokelley merged commit 4a79162 into main Oct 15, 2026
43 checks passed
@bokelley
bokelley deleted the bokelley/account-feed-xid-order branch October 15, 2026 21:08
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.

Order the account change feed by (xid, seq) behind pg_snapshot_xmin instead of depending on lock discipline

1 participant