Files
buzz/docs
Luke Tornquist 0a362eef15 perf(db,relay): bound thread participant scans and coalesce live summaries (#8635)
## Summary

A thread summary's `participants` (up to 10 recent authors, used for the
facepile) was computed by aggregating **every** reply in the thread. The
live `kind:39005` emit runs that aggregate on the writer after every
reply insert and every threaded delete. Each summary therefore cost
O(thread), recomputed after every reply, so one very large thread could
saturate the writer.

### Scale and impact

A single long-running thread can grow to hundreds of thousands of
replies and take several new replies per second. Each of those replies
then pays a whole-thread aggregate on the writer, so writer CPU follows
the reply rate of the largest active threads, and the read-side
participant queries (channel window, thread root summaries) slow down
the same way.

Synthetic reproduction (PostgreSQL 17, the repo's `schema/schema.sql`,
default `shared_buffers`): one channel with 200k background messages and
three threads of 450k, 6k and 300 replies from a few authors with
~800-byte bodies. `EXPLAIN (ANALYZE, BUFFERS)` of the participant query,
warm cache (runs 2–3 of 3):

| Thread | Before (whole-thread aggregate) | After (newest 1000 replies)
|
|---|---|---|
| 450k replies | ~7.6 s, ~117k buffers read, ~16k temp blocks spilled |
~6.8 ms, ~4k buffers |
| 6k replies | ~25 ms, ~24k buffers | ~7 ms, ~4k buffers |
| 300 replies | ~2.6 ms, ~1.2k buffers | ~2.6 ms, ~1.2k buffers |

Cost now stops growing at 1000 replies, and small threads cost the same
as before. With coalescing, a busy thread runs this query at most about
once per second per pod instead of once per reply.

### Change

1. **Bound the participant scan to the newest 1000 replies.** All three
participant readers now share one SQL fragment
(`recent_thread_participants_sql!`):
   - `get_thread_summary` (the live 39005 emit)
   - the channel-window batch
   - `resolve_thread_root_summaries`

The fragment walks `idx_thread_metadata_window` newest-first with `LIMIT
1000`, then joins `events` only for those rows, to get `pubkey` and drop
deleted replies. Deleted replies keep their `thread_metadata` row, so
they count toward the 1000 but contribute no author. It aggregates by
author and returns the 10 most recently active. Cost no longer grows
with thread size. Desktop and mobile show the 3 most recent participants
and the forum view shows up to 10, so an author who hasn't posted in the
last 1000 replies is not visible in any client today. `NIP-CW`,
`docs/bridge-channel-window.md` and the `ThreadSummary::participants`
doc now state the bound, deleted replies included.
2. **Coalesce live summary emits per thread root.**
`emit_live_thread_summary` goes through a new `CoalescedRefresh` keyed
by `(community, root)`:
   - A quiet thread emits immediately, as before.
- On a busy thread, emits are spaced at least 1s apart per pod. A
trailing emit covers the rest of the burst and reads state committed
after its last mutation.
- Because emits are at least 1s apart, consecutive overlays from one pod
always get strictly increasing `created_at`, so the client's newest-wins
replacement is never tied.
- A panicking or cancelled refresh releases its key, including a task
dropped before its first poll: the release guard is built before the
spawn. Each run is bounded by a 30s timeout so a hung refresh can't
wedge it.

### What got simpler

- Three hand-written copies of the participant aggregate are now one
fragment.
- `get_thread_summary` is one statement instead of two round trips.
- The channel-window batch dropped its `ROW_NUMBER()` window function
and the `GROUP BY root, pubkey` over every reply of every root on the
page.

### Related issue

None found.

### Testing

- `participant_summaries_read_only_the_newest_replies` (buzz-db,
Postgres) seeds a thread with 1001 replies:
  - an early author, pushed just outside the window
  - a filler author
  - a deleted reply
  - a latest author

All three readers must return `[latest, filler]`. Mutant check: removing
the `LIMIT` (whole-thread aggregate) turns it red.
- `coalesced_refresh` unit tests (paused tokio clock):
- A 100-request burst runs once immediately and once trailing, then
frees the key.
  - Separate keys run independently.
  - A panicking refresh doesn't wedge its key.
- A hung refresh times out and releases its key. Mutant check: removing
the timeout turns it red.
- A request that arrives while a run is in flight gets one trailing run,
and that run reads the state written mid-run. Mutant check: dropping the
rerun flag turns it red.
- A task dropped before its first poll (runtime shut down) still
releases its key. Mutant check: building the guard inside the task turns
it red.

These run in `just test-unit` (added to the buzz-relay `--lib` filter
and `scripts/run-tests.sh`).
- Blox gate: `cargo fmt --check`, clippy `-D warnings` (buzz-db,
buzz-relay), and the full buzz-db and buzz-relay Postgres nextest lanes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 15:59:19 -04:00
..
…