Skip to content

Merge runs of dropped records into one NOOP - #238

Open
iskakaushik wants to merge 1 commit into
noop-crc-zero-extensionfrom
merge-dropped-runs
Open

iskakaushik wants to merge 1 commit into
noop-crc-zero-extensionfrom
merge-dropped-runs

Conversation

@iskakaushik

@iskakaushik iskakaushik commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

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_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.

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_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 moves by the CRC of the change
alone (patch_prev).

Determinism

The shadow may hold bytes a restarted daemon rebuilds, so the layout has to be
a function of the source WAL:

  • A restart resumes at a segment boundary. A record that ends in a later
    segment is never merged, so every segment starts from the source's xl_prev.
  • A run open at a fork stays open, as a restart reading the descendant's copy
    of the fork segment sees it.
  • Bytes another layout wrote would still differ: an older release's one NOOP
    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-dir and the shadow's pg_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 every
record 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 made
the shadow read zeros and drop its walreceiver. The full suite caught that one.

The stack

  1. Extend NOOP CRCs past the length cache #237 Extend NOOP CRCs past the length cache (D)
  2. Merge runs of dropped records into one NOOP #238 Merge runs of dropped records into one NOOP (C)
  3. Store filtered segments with zero runs elided #239 Store filtered segments with zero runs elided (A2)
  4. Stream compact WAL to a shadow that expands it #240 Stream compact WAL to a shadow that expands it (A)

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-dir change comes before the wire
change: 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:

  • The walshadow host writes each source WAL byte about three times to one
    EBS volume: the daemon's --out-dir segments, the shadow's pg_wal, and
    the shadow's zero-fill of each new segment.
  • After two to two and a half hours of a backlog the instance runs out of
    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.
  • 85% of the records, and about 93% of the bytes in --out-dir and on the
    wire, 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 WAL
    with real page headers, contrecords, xl_prev chain and CRCs; records up to
    70 KB, so some span pages and segments; random push chunking): every record's
    CRC and xl_prev hold, page headers and kept records are the source's but
    for xl_prev, each NOOP covers exactly a run of dropped records, every sealed
    record 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 a
    copy 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 pass pg_waldump from
    their first record to their last, with fewer NOOPs than dropped records.
  • patched_prev_keeps_the_crc_valid.
  • Standby replay: the whole e2e suite replays merged NOOPs on real shadows.
    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/ceramic from the ceramic-reproducer branch: PG 18,
[toast] mode = "shadow", a fixed 4.0 GiB backlog of 5.64M records), daemon
pinned to 8 CPUs, ab.sh <parent> <this> 4 interleaved, mean ± stdev, on a
64-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>/io
write_bytes less cancelled_write_bytes over the drain, per source WAL byte
drained: the daemon, and the shadow's postmaster with its children. "Sent" is
the walsender socket's bytes_sent per backlog byte. On this VM ceramic is
bound 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:

#237 this PR ratio
rec/s 80,340 ± 1,494 82,133 ± 2,236 1.022
daemon cores 0.670 ± 0.014 0.677 ± 0.015 1.011
shadow cores 0.297 ± 0.005 0.300 ± 0.008 1.008
daemon writes, B/B 0.995 0.995 1.000
shadow writes, B/B 4.39 4.38 0.997
sent to shadow, B/B 1.00 1.00 1.000

Records the shadow replays over the backlog, from pg_waldump --stats on the
--out-dir of one run per side kept whole (0/BB000130 to 1/B9000000; this PR
at a revision with the same filtered bytes):

main this PR ratio
records 5,637,176 2,889,496 0.513
of them NOOPs 3,719,409 971,729 0.261

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

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>
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