Skip to content

Extend NOOP CRCs past the length cache - #237

Open
iskakaushik wants to merge 1 commit into
mainfrom
noop-crc-zero-extension
Open

iskakaushik wants to merge 1 commit into
mainfrom
noop-crc-zero-extension

Conversation

@iskakaushik

@iskakaushik iskakaushik commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Part of #223 (D in its design), first of a stack of four.

A NOOP's body is its main-data marker followed by zeros, so its CRC32C depends
on its length alone. The filter caches it by length for bodies under 8 KiB and
hashes every other body in full, zeros included.

This computes a body the cache lacks by extending the CRC of its marker over the
zeros: one GF(2) operator per power-of-two length, applied as four table
lookups per set bit of the length. Every length then costs the same, which the
next PR needs for a NOOP covering a whole run of dropped records, and the cache
keeps the common lengths at a single load.

A first version replaced the cache with zero extension outright. On the
lifecycle repro (below) that cost the pump about 23 ns per record, roughly
5%, since nearly every dropped record there is a cache hit; so the cache
stays.

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

  • zero_extension_matches_hashing_the_zeros: crc32c_zeros equals CRC32C over
    the actual zeros for every power-of-two length ±1, the SHORT/LONG marker
    switch and 2,000 random lengths up to 1 MiB, from random starting CRCs.
  • noop_of_any_length_parses_with_a_valid_crc: a NOOP of each of those lengths,
    twice so the second pass reads the cache, parses as XLOG_NOOP with xl_prev
    kept and a CRC PostgreSQL accepts.
  • CI green on PG 16 to 19 with WALSHADOW_REQUIRE_TOOLS=1, merged coverage and
    lint; 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, main vs this PR:

main this PR ratio
rec/s 79,964 ± 2,065 80,255 ± 1,766 1.004
daemon cores 0.665 ± 0.017 0.667 ± 0.013 1.004
shadow cores 0.297 ± 0.010 0.295 ± 0.006 0.992
daemon writes, B/B 0.996 0.995 0.999
shadow writes, B/B 4.39 4.39 1.000
sent to shadow, B/B 1.00 1.00 1.000

Lifecycle repro, pump thread per record over three interleaved runs: main
427 ± 16 ns, this PR 438 ± 5 ns. Replacing the cache outright measured
447 ± 8 ns.

🤖 Generated with Claude Code

A NOOP's body is its main-data marker followed by zeros, so its CRC32C
depends on its length alone. The filter caches it by length for bodies
under 8 KiB and hashes every other body in full, zeros included.

Compute a body the cache lacks by extending the CRC of its marker over
the zeros instead: one GF(2) operator per power-of-two length, applied
as four table lookups per set bit of the length. Every length then
costs the same, which a NOOP covering a whole run of dropped records
needs, and the cache keeps the common lengths at a single load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iskakaushik iskakaushik changed the title Compute NOOP CRCs by zero extension Extend NOOP CRCs past the length cache Oct 20, 2026
@iskakaushik
iskakaushik force-pushed the noop-crc-zero-extension branch from 953e03b to f2022ce Compare October 20, 2026 06:53
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