Files
buzz/crates/buzz-db
Wes 7cf19b713c perf(db): bound deep keyset history pages by the cursor timestamp (#9549)
## Problem

`query_events` and `get_channel_window` page history with a composite
`(until, before_id)` cursor under `ORDER BY created_at DESC, id ASC`.
The cursor predicate was only:

```sql
created_at < $u OR (created_at = $u AND id > $bid)
```

PostgreSQL cannot use that OR as the start of an index range, so a page
deep in a channel walks the `(community_id, channel_id, created_at DESC,
id)` index from the newest row and filters every newer row before it
reaches the page. The cost of a page grows with its depth.

## Fix

Add the redundant bound `created_at <= $u` next to the OR in both
queries (`store/event.rs` and `store/thread.rs`). It does not change
which rows match; it gives the planner an `Index Cond` range start at
the cursor. This is the same bound the thread window query already
carries (`store/thread_window.rs`).

`api/bridge.rs` has the pattern only in a test fake that emulates
`query_events_on`; the bridge itself routes cursors into these two store
functions, so it needs no change.

## EXPLAIN before / after

`EXPLAIN (ANALYZE, BUFFERS)` of the `query_events` page shape, `LIMIT
51`, kind 9, by cursor depth.

Copy of a real channel (2,277 messages), custom plan, shared buffers
hit:

| Depth | Before | After |
| ---: | ---: | ---: |
| 50 | 96 | 96 |
| 300 | 311 | 84 |
| 1,000 | 969 | 80 |
| 1,500 | 1,443 | 92 |
| 2,100 | 2,006 | 89 |

At depth 2,100: execution 0.709 ms to 0.058 ms, rows removed by filter
2,101 to 42. With a generic plan the after numbers are also flat (80 to
96 buffers at every depth).

Synthetic table (one channel with 20,000 kind-9 rows plus 20 other
channels of 2,000 each):

| Query | Depth | Before | After |
| --- | ---: | ---: | ---: |
| `query_events`, custom plan | 10,000 | 701 | 11 |
| `query_events`, generic plan | 50 | 874 (7.5 ms) | 11 (0.11 ms) |
| `query_events`, generic plan | 10,000 | 444 | 11 |
| `get_channel_window`, custom plan | 10,000 | 1,006 (2.35 ms) | 312
(0.49 ms) |
| `get_channel_window`, custom plan | 19,940 | 1,690 (4.35 ms) | 314
(0.47 ms) |

For `get_channel_window` at depth 19,940 the events index scan went from
1,385 buffers and 19,941 rows removed by filter to 9 buffers and 4 rows;
the rest of its plan is the thread metadata join, which is unchanged. On
the small 2,277-row channel the planner picks a sequential or bitmap
scan for the window query, so its plan is unchanged there.

## Tests

New Postgres test
`store::event::postgres_tests::deep_keyset_pages_read_from_the_cursor`:

- Inserts 1,000 kind-9 rows into one channel, two per second, so the
cursor lands on same-second ties.
- Turns off sequential and bitmap scans for the session, so the small
fixture uses the ordered index walk a large channel uses, then reads one
50-row page at depth 900 through both `query_events_on` and
`get_channel_window_on`. It asserts each page equals the expected rows
and that each read touched at most 150 event rows (summed `seq_tup_read
+ idx_tup_fetch` over the `events` partitions).
- Pages the whole channel through both paths and asserts every row comes
back once, in order.

Checked by reverting each half of the fix:

- `event.rs` bound removed: `events page at depth 900 read 950 rows for
50`
- `thread.rs` bound removed: `window page at depth 900 read 1000 rows
for 50`
- With the fix: passes (5 of 5 under `--stress-count 5`), reading 52 and
53 rows.

Runs:

- `scripts/postgres-test-run.sh -p buzz-db --lib --tests`: 763 run, 756
passed. The 7 failures were the new test before its final fixture change
(it now passes) and 6 that fail the same way on unmodified `main` in my
environment because they spawn a host `psql` binary that isn't installed
there:
`migration_schema_media_prebuild_waits_for_old_transactions_during_creation_and_cleanup`,
`script_stops_at_the_first_auth_refusal`,
`script_renames_each_planned_channel_once_whatever_its_name`,
`desired_event_follow_up_contract_app_only`,
`desired_event_follow_up_contract_dual`,
`migration_schema_event_follow_up_contract_app_only`. CI's Postgres lane
is the authoritative run for those.
- On the final head: the new test plus every `channel_window` Postgres
test, 9 of 9 passed.
- `cargo nextest run -p buzz-db --lib` (no infrastructure): 216 passed.
`--test observability_source`: 28 passed. Doc tests: 5 passed.
- `scripts/test-postgres-test-discovery.sh`: passed.
- Pre-push `rust-tests` lane: passed.

## How to test

1. Run `scripts/postgres-test-run.sh -p buzz-db --lib -E
'test(deep_keyset_pages_read_from_the_cursor)'`.
2. Optionally, in a database with a channel of a few thousand messages,
open the channel in a client and scroll back through history. Older
pages should load as quickly as the first ones, and no message should be
skipped or repeated across page boundaries.

## Related

No duplicate PR or issue found. Related: #1787 (keyset index for thread
replies) and #6004 (CLI wiring for the composite cursors).

Signed-off-by: Wes <wesb@block.xyz>
Co-authored-by: Groot <f4c08302591916fb4b80926bf7e18d22eef8aa0c877e4e841acc46405a45700b@buzz.block.builderlab.xyz>
2026-11-18 13:02:31 -05:00
..