Repository navigation
Merge runs of dropped records into one NOOP - #238
Open
iskakaushik wants to merge 1 commit into
Open
iskakaushik wants to merge 1 commit into
iskakaushik wants to merge 1 commit into
Conversation
This was referenced Oct 20, 2026
The filter rewrites every record the shadow does not need into an XLOG_NOOP of the same length, so the shadow reads and replays one record per dropped record: about 85% of the records on a write-heavy source. Rewrite each run of dropped records that start on one WAL page into a single NOOP over the same bytes. LSNs, page headers and the xl_prev chain stay valid: a record joins only while it starts on the run's page, and the run closes once a member ends past it, so only the last member crosses a page header and the merged record ends where it did. The record after a merged run points xl_prev at the run's start, its CRC moved by the CRC of the change alone. The bytes have to be a function of the source WAL, since a restarted daemon resumes at a segment boundary and rebuilds bytes the shadow may already hold. A record that ends in a later segment is never merged, so every segment starts from the source's xl_prev, and a run open at a fork stays open, as a restart reading the descendant's copy sees it. Bytes another layout wrote would still differ: an older release's one NOOP per record, or bootstrap's landed WAL, cut at the backup end, which on a standby source can be any record's end. A resumed stream reads the copies in --out-dir and the shadow's pg_wal, as it already does for a segment's continuation, and ends a run at each record end they hold, through the first segment it finds no copy of. A run's header is final only once the run ends, so its bytes and its records wait for the next record; on a source that goes quiet mid transaction, at most until the next RUNNING_XACTS. A run that opens a page holds back the page's header too: at a page start, PostgreSQL's reader waits for the header alone and then reads the first record's length (xlogreader.c, XLogDecodeNextRecord). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
iskakaushik
force-pushed
the
merge-dropped-runs
branch
from
October 20, 2026 06:53
5aacb7b to
3df5828
Compare
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.
Part of #223 (C in its design), second of a stack of four, on top of #237.
The filter rewrites every record the shadow does not need into an
XLOG_NOOPof the same length, so the shadow reads and replays one record per dropped
record: about 85% of the records on a write-heavy source.
This rewrites each run of dropped records that start on one WAL page into a
single NOOP over the same bytes. LSNs, page headers and the
xl_prevchainstay valid: a record joins only while it starts on the run's page, and the run
closes once a member ends past it, so only the last member crosses a page
header and the merged record ends where it did. The record after a merged run
points
xl_prevat the run's start; its CRC moves by the CRC of the changealone (
patch_prev).Determinism
The shadow may hold bytes a restarted daemon rebuilds, so the layout has to be
a function of the source WAL:
segment is never merged, so every segment starts from the source's
xl_prev.of the fork segment sees it.
per record (every upgrade), and bootstrap's landed WAL, which is cut at the
backup end; on a standby source that is
minRecoveryPoint, any record's end.A resumed stream reads the copies in
--out-dirand the shadow'spg_wal,as it already does for a segment's continuation, and ends a run at every
record end they hold, for every segment named there when it starts.
Holding a run
A run's header is final only once the run ends, so its bytes and its records
wait for the next record. On a source that goes quiet mid transaction that is
at most until the next
RUNNING_XACTS. Commits are kept records, so everyrecord of a transaction dispatches by its commit, and the catalog gate and
TOAST fetches, which wait on commits, never wait on an open run. A run that opens
a page also holds back that page's header: at a page start PostgreSQL's reader
waits for the header alone and then reads the first record's length
(
xlogreader.c,XLogDecodeNextRecord), so a chunk ending at the header madethe shadow read zeros and drop its walreceiver. The full suite caught that one.
The stack
Each is one idea and under 1,000 changed lines. Merging comes before either
compact form because it turns the padding into a few long zero runs per page,
which is what makes both small. The
--out-dirchange comes before the wirechange: the measured bottleneck is disk writes, while the wire is loopback and
costs about 1.5% of a core.
Why
The issue's first premise was CPU: sending to the shadow turned out to cost
about 1.5% of a core. A production deployment shows the real cost is disk
bytes:
EBS volume: the daemon's
--out-dirsegments, the shadow'spg_wal, andthe shadow's zero-fill of each new segment.
EBS burst credits and is capped at 312.5 MB/s, about 100 MB/s of source
WAL. The shadow's TOAST reads queue behind those writes: fetch latency
rises from about 120 µs to 250-800 µs.
--out-dirand on thewire, are NOOP padding.
So the win is fewer bytes written to walshadow's own disk and sent on the
wire, plus less replay work on the shadow.
Tests
merged_noops_replay_and_rebuild_identically_from_any_segment(synthetic WALwith real page headers, contrecords,
xl_prevchain and CRCs; records up to70 KB, so some span pages and segments; random push chunking): every record's
CRC and
xl_prevhold, page headers and kept records are the source's butfor
xl_prev, each NOOP covers exactly a run of dropped records, every sealedrecord dispatches in order, the wire carries the archive's bytes in chunks
that never end at a page header, and a stream resumed at every segment
boundary rebuilds identical bytes.
resume_keeps_the_layout_already_retained: an older release's layout, and acopy filtered from WAL cut mid-run as bootstrap lands it, are rebuilt byte
for byte and merging resumes after them.
filter_round_trip: the classify and filter fixtures passpg_waldumpfromtheir first record to their last, with fewer NOOPs than dropped records.
patched_prev_keeps_the_crc_valid.CI green on PG 16 to 19 with
WALSHADOW_REQUIRE_TOOLS=1, merged coverage and lint; the full suite locally on PG 17, fmt, clippy-D warnings.Benchmark
Setup. ceramic (
bench/ceramicfrom the ceramic-reproducer branch: PG 18,[toast] mode = "shadow", a fixed 4.0 GiB backlog of 5.64M records), daemonpinned to 8 CPUs,
ab.sh <parent> <this> 4interleaved, mean ± stdev, on a64-vCPU dev VM with an EBS root volume. The VM first assigned picked up an
unrelated disk-heavy benchmark partway through, so every number here comes
from a second, idle VM of the same shape. Disk bytes are
/proc/<pid>/iowrite_byteslesscancelled_write_bytesover the drain, per source WAL bytedrained: the daemon, and the shadow's postmaster with its children. "Sent" is
the walsender socket's
bytes_sentper backlog byte. On this VM ceramic isbound by TOAST fetch latency (about 200 µs a fetch; the pump waits on a full
queue 83% of the time), not by CPU, so rec/s barely moves unthrottled.
The lifecycle repro is the issue's workload (PG 17, one wide table with five
btree indexes, an insert and two non-HOT updates per transaction, 10 s of
pgbench, about 2.8 GiB of WAL at 113 B/rec, no TOAST). It is pump-bound at
about 1.8M rec/s, so the pump thread's CPU per record is the number to watch;
its drain time steps in about 1 s increments with the source's status
cadence, so its MiB/s does not resolve a few percent.
ceramic, #237 vs this PR:
Records the shadow replays over the backlog, from
pg_waldump --statson the--out-dirof one run per side kept whole (0/BB000130 to 1/B9000000; this PRat a revision with the same filtered bytes):
Shadow CPU barely moves: next to ceramic's TOAST replay and bridge reads, a
NOOP costs little to replay.
Lifecycle repro, pump thread per record over three interleaved runs: main
427 ± 16 ns, #237 438 ± 5 ns, this PR 453 ± 11 ns. A first version awaited an
async call per record and per run and cost about 36 ns over main; closing a
run synchronously and boxing the rare read of a retained copy brought that to
about 26 ns.
🤖 Generated with Claude Code