Repository navigation
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 intoNov 13, 2026
Conversation
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
neoromantic
deleted the
bb/keep-the-search-index-merged-149-142-and-retune-thr_tsamh5mrtd
branch
November 13, 2026 09:57
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
src/db/search-merge.ts): writes never merge (automerge0, set once when a cache is opened). The daemon tick andtg db migraterun FTS5mergesteps in short write transactions (16 pages a step, no step started after 50 ms):mergemerges every segment into one.deletemergestays off. On a level whose entries are all deleted, one step writes nothing and reads the whole level (1.3 s at 1M).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.bench:storagegains: 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.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)messages search, common word /--offset 200messages search, common word /--offset 200Limits:
detail=columnwould 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).Tests
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.tsadjusted to the new options and runner.bun run check, unit tests (search, merge, re-index, migrations, daemon, schema, sql, status: 248 pass), e2edb-migrate,messages,messages-search-query,search-names(72 pass).🤖 Generated with Claude Code