Skip to content

Keep the search index merged in the background, walk the newest messages from a few percent of matches (#149, #142) - #150

Merged
neoromantic merged 4 commits into
mainfrom
bb/keep-the-search-index-merged-149-142-and-retune-thr_tsamh5mrtd
Nov 13, 2026
Merged

neoromantic merged 4 commits into
mainfrom
bb/keep-the-search-index-merged-149-142-and-retune-thr_tsamh5mrtd

Conversation

@neoromantic

@neoromantic neoromantic commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Fixes #149
Part of #142 (merge steps of a common word still take up to 146 ms at 1M and 0.7 s at 10M; see Limits)

Problem

Change

  • Background merges (src/db/search-merge.ts): writes never merge (automerge 0, set once when a cache is opened). The daemon tick and tg db migrate run FTS5 merge steps in short write transactions (16 pages a step, no step started after 50 ms):
    • after writes, a level merges at two segments;
    • between re-index batches, only at four, which keeps every level under FTS5's 16-segment crisis merge without rewriting the index again and again;
    • once 10% of the entries are deleted (counted from FTS5's structure record), a negative merge merges every segment into one.
    • FTS5's own deletemerge stays off. On a level whose entries are all deleted, one step writes nothing and reads the whole level (1.3 s at 1M).
    • When nothing was written since the last merge, the check is a single read of the structure row.
  • Walk retune (src/db/messages-search.ts): the walk reads the newest messages in chunks and asks the index once per run of rowids at most 32 apart (rowid BETWEEN), not once per message.
    • The threshold goes from √(900·P·N) to √(20·P·N), about 3% of 1M messages.
    • The walk gives up and sorts once it has spent 0.25 µs per counted match, a quarter of the least the sort can take.
    • Results and their order stay the same.
  • bench:storage gains: merges after history and live writes (timed separately), the longest writes, the re-index with merges, a re-index the way 0.11.1 ran it plus the clean-up after it, the index's size, segment count and cost of one question, a 7% word, and a worst case.
  • Docs: search.md (Keeping the index merged, Query Pattern), storage.md (new section 8, all tables re-run at 100k/1M/10M), database-schema.md, daemon.md, cli-commands.md, schema annotations, CHANGELOG.

Numbers (bun run bench:storage, seed 1, this machine)

0.11.1 now
1M: longest batch of the one-off re-index 997 ms 32 ms (merge batches between: 90 ms)
1M: cleaning up the index 0.11.1 left (202 MB) never 2.6 s, 33 batches → 104 MB, 8.8 µs a question
10M: the same clean-up (2.0 GB) never 61 s, 774 batches, longest 597 ms → 1.06 GB, 14 µs
1M: messages search, common word / --offset 200 11 / 308 ms 2.2 / 5.1 ms
10M: messages search, common word / --offset 200 21 / 64 ms (0.11.0) 6.8 / 13 ms
1M, same file: a word in 7% / in 4.4% 85 / 54 ms 6.7 / 9.2 ms
1M, same file, worst case (no match among the newest messages, then sort) 64 / 41 ms 72 / 49 ms (1.12× / 1.20×)
1M, the index 0.11.1 left: a word in 30% / in 4.4% 488 / 74 ms 363 / 82 ms
10M, same file: a word in 4.4% / in 7% 586 / 29 ms 6.4 / 8.6 ms
10M, same file, worst case 678 / 442 ms 695 / 448 ms (1.02× / 1.01×)
1M: live messages / history pages, messages/s 4,676 / 7,062 6,849 / 7,879 (+19.6 s of merges)

Limits:

  • A merge step still copies a word's whole entry list, so the longest background merge batches are 146 ms at 1M and 0.7 s at 10M. Only detail=column would shrink them, and it changes phrase search, so that is a product decision (Search index merges can hold the write lock for hundreds of ms on a big cache #142).
  • A few history pages take more than 100 ms (5 of 10,486 at 1M, 31 of 105,673 at 10M). They also happen with 0.11.1 and with WAL checkpoints off, so they aren't merges; I didn't find the cause.

Tests

  • New src/__tests__/search-merge.test.ts: writes pile segments up until a merge; the old entries of a re-index are dropped; under 10% deleted they stay; the merge is due again only after a write; a batch resumes after its time limit; bulk mode merges levels of four only; options on a new cache; the runner merges at the end.
  • messages-search-newest.test.ts: the 200-query equivalence test (search, count, walk) now also runs on an index that keeps every old entry. A new case covers a clock that runs out the walk's budget.
  • search-reindex.test.ts, background-migrations.test.ts adjusted to the new options and runner.
  • bun run check, unit tests (search, merge, re-index, migrations, daemon, schema, sql, status: 248 pass), e2e db-migrate, messages, messages-search-query, search-names (72 pass).

🤖 Generated with Claude Code

neoromantic and others added 4 commits November 13, 2026 12:24
FTS5 merged its segments inside the writes that added them, a whole word's
entry list per step, so a write could hold the lock for up to a second on a
big cache (#142). And a level of three segments waited for a fourth: after
the one-off re-index of names a cache could keep the old entry of every
message, twice the index, and asking it about one message took ms (#149).

Writes no longer merge (automerge 0). The background runners (daemon tick,
tg db migrate) merge in short batches of FTS5 'merge' steps: a level of two
after writes, a level of four between the batches of a re-index, and every
segment into one once a tenth of the entries are deleted. FTS5's own
deletemerge stays off: on a level with only deleted entries a step writes
nothing and reads the whole level at once. Checking costs one read while
nothing was written since the last merge.

bench:storage times history and live writes with the merges after them,
the re-index with merges between batches, and an index as 0.11.1 left it
(202 MB, 3.8 ms a question at 1M) and its clean-up (2.6 s, 104 MB, 8.8 us).

Fixes #149
Fixes #142

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The newest-first walk of #118 asked the index about one message at a time,
a fresh FTS5 cursor each, so it paid only for a word in about 21% of 1M
messages; on a real cache no word reached that. Now it reads the newest
messages in chunks and asks the index once per run of nearby rowids
(rowid BETWEEN), which history pages and live messages give a chat's
newest messages. The threshold drops from sqrt(900*P*N) to sqrt(20*P*N),
about 3% of 1M messages and 1% of 10M. The walk stops and sorts once it
has taken 0.25 us per counted match, a quarter of the least the sort
takes, so a word whose matches are all old, or an index that still holds
deleted entries, costs about 1.2 times the sort at most.

On one 1M made-up cache, 0.11.1 against now: a word in 7% 85 -> 6.7 ms,
in 4.4% 54 -> 9.2 ms, --offset 200 of a common word 278 -> 6.4 ms; no
match among the newest 64 -> 72 ms. Results stay the same: the random
equivalence test also runs on an index that keeps every old entry, and
on a clock that runs the budget out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Re-ran bench:storage at 100,000, 1M and 10M. The at-a-glance table, the
byte counts of the index, the re-index, rebuild and VACUUM numbers, and all
query times now come from these runs. A new section 8 covers merging the
search index: the index after writes and after a re-index, the state that
0.11.1 left, its clean-up, the longest writes and merge batches, and what
the merges cost. Section 7 gets the walk since #149, with 0.11.1 compared
on the same 1M and 10M cache files. #142 and #149 move to done.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 5bdc4b5 into main Nov 13, 2026
5 checks passed
@neoromantic
neoromantic deleted the bb/keep-the-search-index-merged-149-142-and-retune-thr_tsamh5mrtd branch November 13, 2026 09:57
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.

Search index keeps the old entries of every re-indexed message

1 participant