You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Search index merges can hold the write lock for hundreds of ms on a big cache #142
FTS5 merges segments of the search index inside the write that triggers the merge, and a merge step stops only between tokens: the doclist of a common word is copied whole. So on a big cache an ordinary write sometimes holds the write lock for much longer than usual.
Measured with bun run bench:storage on 1M made-up messages (#141): building the cache, 4 of 10,050 history pages took over 100 ms (longest 253 ms); right after the one-off name re-index (#93), 16 of the next 100 pages took 104–212 ms (in another run up to 730 ms), and re-index batches took 100–180 ms, once 1.04 s. Other writers (the daemon, tg db migrate, CLI commands that cache) wait that long.
Ideas to measure:
Turn off automatic merges (automerge, crisismerge) and merge in small explicit steps ('merge', N) from the same background runners as the migrations (Background, resumable migrations for big caches #89), when nothing else writes.
detail=column makes doclists smaller, but changes phrase search ("…"), so it needs a product decision.
Done when the longest write on the 1M benchmark (history pages, live messages, re-index batches) stays under ~100 ms, search results don't change, and docs/storage.md has the numbers. Related: #93, #118.
Related finding from #118 (PR #144): the newest-first search asks the index whether each recent message matches, and each such check costs about 12 µs on a made-up cache written like history sync (per chat, pages of 100). After the FTS5 optimize command the same check costs about 4.8 µs (2.5× cheaper), which would let the search use that walk for less frequent words too (its threshold is tuned to the current cost). Keeping the index merged in small background steps, as this issue proposes for the write spikes, would likely help both.
Reopened: #150 moved merges out of the writes and keeps the index merged in background steps, but one FTS5 merge step of a word with a long entry list can't be cut short: up to about 200 ms at 1M messages and 0.7 s at 10M, holding the write lock. Shrinking it needs detail=column, which drops phrase and NEAR queries. Separately, a few history pages (5 of 10,486 at 1M) take over 100 ms on main as well, with merges off; cause not found yet.
Decided: no detail=column. Phrase and NEAR queries stay, and a merge step of up to about 0.2 s at 1M messages is acceptable. What remains open here: the rare history pages that take over 100 ms with merges off (5 of 10,486 at 1M), cause not found yet.
Activity
neoromantic commentedon Nov 13, 2026
Related finding from #118 (PR #144): the newest-first search asks the index whether each recent message matches, and each such check costs about 12 µs on a made-up cache written like history sync (per chat, pages of 100). After the FTS5
optimizecommand the same check costs about 4.8 µs (2.5× cheaper), which would let the search use that walk for less frequent words too (its threshold is tuned to the current cost). Keeping the index merged in small background steps, as this issue proposes for the write spikes, would likely help both.docs(storage): numbers of the merged search index and the new walk
docs(storage): numbers of the merged search index and the new walk
neoromantic commentedon Nov 13, 2026
Reopened: #150 moved merges out of the writes and keeps the index merged in background steps, but one FTS5 merge step of a word with a long entry list can't be cut short: up to about 200 ms at 1M messages and 0.7 s at 10M, holding the write lock. Shrinking it needs
detail=column, which drops phrase and NEAR queries. Separately, a few history pages (5 of 10,486 at 1M) take over 100 ms on main as well, with merges off; cause not found yet.neoromantic commentedon Nov 13, 2026
Decided: no
detail=column. Phrase and NEAR queries stay, and a merge step of up to about 0.2 s at 1M messages is acceptable. What remains open here: the rare history pages that take over 100 ms with merges off (5 of 10,486 at 1M), cause not found yet.