operator handoff 2026-06-11 — postgres workload profile
operator report, measured against the production instance (zlay-db-postgresql-0,
postgres 18.3, 85 days of accumulated stats + live sampling). nothing here is
broken — this is a cost profile. the motivation: stack-for-stack against the
indigo relay, the zlay binary wins big (1.06 vs 2.98 avg cores, 2.4 vs 8.9
GiB), but zlay's postgres runs 2.16 avg cores (p95 4.4) vs indigo's 0.39,
which eats most of the binary's CPU advantage at the whole-stack level. this
doc lays out where those cycles go, with the data mapped to source lines.
what (if anything) to change, and how, is your call — there are operator-side
config levers too, listed at the end, which I'll coordinate separately.
headline numbers
| measure | value | how measured |
|---|---|---|
| transactions | 2,656 tx/s sustained | pg_stat_database diff over 129s |
| lifetime tx | 24.4B over 85d (~3,325/s avg) | pg_stat_database |
| event rate during measurement | ~350 frames/s in | relay_frames_received rate |
| → queries per event | ~7–8 | ratio |
| tuple updates | 342/s | snapshot diff |
| WAL | ~640 KB/s (~55 GB/day) | pg_current_wal_lsn diff |
| foreground WAL fsyncs | 2.52B lifetime (~343/s) | pg_stat_io, client backend |
| account point lookups (now) | 634 idx_scan/s | pg_stat_user_tables diff over 90s |
the per-commit query train (mapped to source)
every #commit event runs, in sequence, each as its own autocommit transaction over the wire:
SELECT uid FROM account WHERE did=$1— only on did_cache miss (event_log.zig:505). the did→uid cache works: only 231M lifetime scans onaccount_did_keyvs 6.03B onaccount_pkey.SELECT host_id FROM account WHERE uid=$1— every event, no cache (uidForDidFromHost → getAccountHostId, event_log.zig:445/559)SELECT status, upstream_status FROM account WHERE uid=$1— every commit, no cache (isAccountActive, event_log.zig:640; called from frame_worker.zig:171 and subscriber.zig:627)SELECT rev, commit_data_cid FROM account_repo WHERE uid=$1— every commit (getAccountState, event_log.zig:529; chain-break detection)INSERT INTO account_repo ... ON CONFLICT (uid) DO UPDATE ... WHERE account_repo.rev < EXCLUDED.rev— every validated commit (updateAccountState, event_log.zig:548). this is the single hottest statement: 16 of 29 active-query samples, 2.11B lifetime executions.
steps 2–4 are uid-keyed point reads of data that either changes rarely (host_id, status — mutations are ~0.8 rows/s on account) or that zlay itself wrote on the previous commit for that uid (rev/data_cid — zlay is the only writer of account_repo). lifetime: 6.03B account_pkey + 4.98B account_repo_pkey index scans.
observed live (30s of pg_stat_activity sampling, 29 active-query hits): 16× the upsert, 4× status, 4× host_id, 3× rev read, 2× did→uid.
where the postgres CPU actually goes
- per-statement transaction overhead. 2,656 tx/s of single-statement
autocommit round trips: parse/plan/execute/commit each time, and — because
synchronous_commit=on— every write transaction does its own foreground WAL flush. pg_stat_io: client backends performed 2.52B WAL fsyncs lifetime (~343/s, ≈ the write-tx rate); the walwriter did only 12M. backends spend their lives committing tiny transactions one at a time. process-levelpsagrees: CPU is smeared evenly across the 20 pool connections (~3% each, mostly idle-between-statements), no single heavy query. - buffer churn on
account. lifetime heap hit ratio onaccountis 24% (53.7B heap blocks read vs 17.3B hit — ~400 TB re-read over 85d). the heap is 877 MB;shared_buffersis 128 MB (stock bitnami).account_repostays ~89% hot because the same pages are hammered continuously. current steady state is gentler (95% overall hit, ~2.5 MB/s reads) — the horror numbers are dominated by the collection-backfill era plus the seq-scan pattern below, but the per-event uid lookups are still ~634 scans/s through a 128 MB sieve.
episodic, not steady-state (but worth knowing)
SELECT COUNT(*) FROM account WHERE host_id=$1(event_log.zig:574, also xrpc.zig:592, and the COALESCE/account_limit variant at event_log.zig:590).accounthas no index on host_id, so each execution is a (parallel) seq scan over 8.7M rows. lifetime: 3.32M seq scans ≈ one per new account ingested; pg_stat_io shows ~430 TB of bulkread traffic attributable to this class. currently 0 per 90s — it fires on new-account admission, so it's invisible in steady state and shows up exactly when a new PDS ramps or a backfill runs (i.e. when you least want it). note the COALESCE form computes the COUNT even when account_limit is set — SQL doesn't short-circuit the aggregate.- collection-index backfill drove most of the lifetime tup_returned (5 trillion) and bulkread volume. done/idle now, but any future re-import repeats the pattern.
what is already good
host.last_seqcursor flushes are debounced through CursorMap (host_ops.zig flush sweep) — 549M updates over 85d ≈ 75/s across ~2,400 active hosts, all HOT updates. the table autovacuums every ~84s, which is noisy but cheap (1.1 MB table).- HOT-update ratios are excellent everywhere (account_repo 99.95%) — no fillfactor/bloat problem. dead tuple counts are healthy. zero rollbacks, zero deadlocks, zero temp spills.
- the did→uid cache demonstrably works (26× fewer did-key scans than uid-key scans).
- DbRequestQueue (2 workers, pool size 20) keeps the pipeline decoupled — this profile is about cost, not stalls.
observations to consider (not prescriptions)
- three of the five per-commit statements read state that zlay either wrote itself (rev/data_cid) or that changes ~5 orders of magnitude slower than it's read (host_id, status). whether that belongs in an app-side cache, a combined statement, or somewhere else entirely is a design call — but each one eliminated removes ~350 tx/s.
- the read-then-upsert pair (steps 4+5) and the guarded upsert overlap in
purpose: the upsert already enforces
rev < EXCLUDED.rev. the separate read exists for chain-break observability. there may be a shape where one round trip serves both. - ~343 foreground WAL fsyncs/s for bookkeeping data raises the question of what durability this data actually needs — all of it is reconstructible from the network (re-crawl) or self-healing (rev guard rejects stale writes). semantics worth a deliberate decision rather than the default.
- the host_id COUNT pattern: an index would make it cheap; a counter would make it free; computing it on the admission path at all is the deeper question. it's also the only query class that punishes new-host ramps.
- if per-statement round trips stay, batching adjacent events' statements (the queue already serializes through 2 workers) changes the tx:event ratio without changing any semantics.
operator-side levers (mine, listed for completeness)
shared_buffers128 MB inside a 1Gi-limit pod,work_mem4 MB,max_wal_size400 MB — all stock bitnami defaults, never tuned. the node has headroom; I can resize the pod + buffers independently of any code change.pg_stat_statementsis not loaded (shared_preload_libraries=pgauditonly), which is why this profile leans on pg_stat_io/activity sampling. I'll add it at the next planned db restart so future profiles have per-query timings.synchronous_commitis a config-side knob too (per-database or per-role), but it changes the same durability semantics as observation 3, so it should be one decision, not two.
context: longer term we may consider running lightrail next to zlay and un-inlining the collection index, but the postgres levers above come first — they're cheaper and the measurements say most of the cost is in the per-event query train, not the index.
— operator