Status: Accepted (2026-11-28). Amends the “Tail counters” part of 0703. Wave-3 review item (PLAN follow-up “review the compactor’s write to realtime-owned columns”).
pages.tail_ops/tail_bytes decide when a page is compacted. Realtime adds +ops/+bytes per committed batch, but lazily: deltas are buffered for up to about 1 s (more while its flush retries) and lost if the process crashes. The compactor subtracted what it folded, greatest(0, tail - folded), and ADR 0703 claimed the two writers commute. The review found two flaws:
tail - folded goes below 0 and is clamped. The late flush then adds them back, so the counter overcounts by folded - tail, permanently. That happens on every busy page; the property test reproduces it.pages only where snapshot_seq = <the job's base seq>. If another job moved it, the transaction rolls back (‘raced’), the snapshot row is not inserted, and the job’s blob files are removed. Folded ranges are therefore disjoint and consecutive, so every batch is subtracted exactly once. A same-seq duplicate is still a no-op (‘duplicate’).QUIET_S (60 s ≫ realtime’s 1 s flush), no delta can still be in flight. The save then sets both counters to the exact tail, 0. clearTail (a job that found nothing to fold) zeroes only under the same condition. This repairs deltas lost in a realtime crash without racing live flushes.it.accepted and it.ops.length. it.ops is exactly the page_ops.ops bytes it journals, and the compactor counts the decoded op list’s length and row.ops.length, so the two quantities are the same.Each writer issues a single UPDATE pages SET c = c + d WHERE id = ... (realtime: d = +ops of a batch; compactor: d = -ops folded). Postgres takes the row lock and, under READ COMMITTED, re-evaluates the expression on the latest committed version, so no update is lost. Integer addition is commutative and associative, so after any interleaving c = Σ(realtime deltas delivered) − Σ(folded ops). The CAS makes the folded sets a partition of the batches at or below snapshot_seq. With all deltas delivered, c = Σ ops of batches above snapshot_seq: the exact tail. The only non-additive writes (zeroing) happen when the tail is provably empty and no delta can be pending, so they agree with that sum. A lost delta (crash) leaves a constant offset until the next quiet reconcile.
page_ops at every save: races realtime’s in-flight deltas the same way the clamp does, and needs an op count per row (decoding).snapshot_seq (head - snapshot_seq): counts batches, not ops and bytes, and needs a realtime read of compactor state (rejected in 0703 for the same reason).apps/docworker/test/tail-counters.test.ts). The race and the quiet reconcile are tested too.tail_ops > 0) and the partial index ignore such pages, which have nothing to fold anyway.