Skip to content

Add ceramic reproducer for TOAST-bound drain - #229

Draft
iskakaushik wants to merge 4 commits into
mainfrom
ceramic-reproducer
Draft

iskakaushik wants to merge 4 commits into
mainfrom
ceramic-reproducer

Conversation

@iskakaushik

@iskakaushik iskakaushik commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Adds bench/ceramic/, a fixed-backlog drain benchmark shaped after a production workload that walshadow falls behind on.

What it reproduces

In production the serial queue worker stays pinned and the pump is blocked on it 60-80% of the time, yet the process uses only 1.0-1.6 of 8 cores. The worker is latency-bound. Most transactions lock a row with FOR UPDATE, then make a non-HOT status update that leaves an out-of-line jsonb value unchanged. walshadow fetches that value from the shadow, one row at a time on the worker. Each fetch is a bridge fetch_toast plus a replay_lsn round trip, and the worker spends about half its wall time on them. The rest of its time goes to records it discards: row locks, index inserts and TOAST index inserts. The existing local repro has almost no TOAST and records about a sixth the size, so it overstates gains from per-record CPU work.

Throughput follows rec/s = 1 / (c_rec + f * c_fetch), where f is TOAST fetches per WAL record.

How it works

prepare.sh builds a golden snapshot once (~1.5 min):

  • PostgreSQL 18 source with checksums, lz4 toast and lz4 wal_compression.
  • 24 tables. The hot one has 38 columns, six btree indexes (two partial on status), REPLICA IDENTITY DEFAULT and nested jsonb two and one TOAST chunks wide.
  • Shadow bootstrapped in [toast] mode = "shadow", with production's pool sizes (decoders 16, inserters 16, resolvers 8, bridge workers 8).
  • Daemon stopped, with a fixed ~4 GiB WAL backlog generated behind it.

run.sh restores the snapshot (WAL hard-linked, shadow and state copied to a real disk), starts the daemon pinned and resumes it from its persisted floor. It then drains the identical backlog and prints one summary line. ab.sh interleaves two trees and prints mean ± stdev and the ratio.

Validation

All runs pin the daemon to 8 cores, with the source and ClickHouse on their own cores. PostgreSQL 18.6, x86. Shadow, out, spill and source WAL share one EBS volume, as in production. Mean ± stdev of 3 interleaved runs.

tree rec/s f (fetch/rec) c_fetch c_rec TOAST s/s pump wait cores
v0.1.4 (7909215) 217k ± 2.8k 0.052 45 µs 2.3 µs 0.50 0.62 1.16
main (830e418) 213k ± 6.7k 0.052 47 µs 2.3 µs 0.52 0.59 1.15
#222 (d54463c) 232k ± 4.7k 0.052 54 µs 1.5 µs 0.65 0.71 0.91
#226 (034fe7b) 232k ± 4.8k 0.052 52 µs 1.6 µs 0.62 0.71 0.91
production, v0.1.4 0.041-0.058 ~115 µs ~4.5 µs 0.46-0.57 0.6-0.8 1.0-1.6

c_rec is 1/rec_s - f * c_fetch. For #222 it can be checked on its own: the walshadow-queue thread is on CPU 36% of the time at 1.55 µs per record, which matches the derived 1.5 µs. The worker's wall time is its CPU time plus fetch waits, and no thread is saturated. Each fetch is one batch, one fetch_toast and one replay_lsn.

At 4 cores, #222 gets 1.10x over v0.1.4 (234k against 214k). Aiming all transitions at recently inserted rows (UPD_RECENT_FRAC=1) brings fetches into shared_buffers: c_fetch drops to 28 µs and #222 gets 1.14x. Aiming them all at old rows (UPD_RECENT_FRAC=0) brings #222 to about 1.0x. The 1.8x seen on insert-only runs does not carry over to this shape, because the fetches dominate.

WAL record mix, harness against production (% of records): Btree INSERT_LEAF 39.2 / 40.7, Heap LOCK 15.5 / 16.1, Heap INSERT 11.1 / 10.3, COMMIT 6.6 / 8.1, Heap DELETE 5.2 / 6.2, Heap UPDATE 5.1 / 6.6, Btree INSERT_POST 4.0 / 4.3. FPI is 9.8% of records and 64% of bytes here, against 11-17% and 65% in production. Average record size is 761 B against 860 B.

A run takes about 35 s end to end: 3-4 s restore and sync, 25-28 s drain, 1-2 s ack tail. Noise is 1-3% stdev over mean.

Known gaps

  • c_fetch is 45-54 µs against ~115 µs in production, and fetch_toast 33-41 µs against 70-145 µs. The toast relation fits in page cache here, and the CPU is x86 against arm64 in production. So the TOAST share is understated, and c_rec differs by CPU.
  • Heap2 PRUNE_ON_ACCESS is 5.4% of records against 1.5%, because the hot table is small and pages are revisited. COMMIT share is 6.6% against 8.1%.
  • The source streams directly. Production was fed from the WAL archive, which was not the limit.

🤖 Generated with Claude Code

On a production workload the serial queue worker stays pinned while the
process uses about 1.3 of 8 cores. Each status transition is a non-HOT
update that leaves an out-of-line jsonb value unchanged. walshadow then
fetches that value from the shadow, one row at a time on the worker, and
each fetch is a bridge fetch_toast plus a replay_lsn round trip. The
existing local repro has almost no TOAST and much smaller records, so it
overstates gains from per-record CPU work.

bench/ceramic reproduces that shape. The hot table has six btree indexes
(two partial on status), lz4 nested jsonb two and one chunks wide, row
locks before each transition, upserts and checksums. The shadow runs in
[toast] mode shadow on PostgreSQL 18. prepare.sh builds a golden snapshot
once: a bootstrapped shadow and walshadow state with the daemon stopped
and a fixed WAL backlog behind it. run.sh restores the snapshot, resumes
the daemon pinned from its persisted floor, drains the identical backlog
and prints one line with throughput, the TOAST fetch rate and latency,
pump wait and per-thread CPU. A run takes about 35 s, and run-to-run noise
is 1-3%. ab.sh interleaves two trees and reports mean, stdev and ratio.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The benchmark used a fresh end LSN on each run and stopped when PostgreSQL saw replay catch up. Replay the same backlog through either feed and measure until ClickHouse acknowledges a fixed end LSN. Verify that archive runs read archived segments.

Tune WAL volume and TOAST fetch frequency independently. Report WAL, row, and TOAST batch rates, and alternate run order when comparing builds or read-ahead settings.
@serprex

serprex commented Nov 20, 2026

Copy link
Copy Markdown
Member

this benchmark tends to update same row multiple time, which makes TOAST cache appealing, but imo that's non representative when heavy workloads will usually be table-wide updates

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.

2 participants