Repository navigation
fix(search): refresh sender and chat names in the index - #141
Merged
Merged
Conversation
The search index took a message's sender and chat names when the message was written, so a rename, or a sender or chat cached after its messages, left older messages unfindable by name. Triggers on users_cache and chats_cache now queue a re-index of the peer's messages when a name the index reads changes or the row is inserted; upserts that change no name queue nothing. The daemon and tg db migrate drain the queue in short batches after the background migrations, and a one-off migration (message_search_names) re-indexes every message of existing caches. Fixes #93 Co-Authored-By: Claude Opus 5.5 <[email protected]>
bench:storage times the one-off re-index of names in process; 14,324 messages/s on 1M made-up messages, 70 s with tg db migrate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
An older SQLite without contentless_delete disables the search index and drops its triggers. The background runner still drained the name queue and ran message_search_names: on a cache from before the queue it failed with "no such table: search_reindex_queue" on every pass of the daemon and of tg db migrate. Both now wait until a newer SQLite rebuilds the index, which empties the queue. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A channel or group posting as itself has its marked ID as from_id, and the sender column read names only from users_cache, so sender: found it only by number. The sender now takes a negative from_id's title and username from chats_cache; a chat insert or rename also queues the messages it posted as itself, and older caches get message triggers that read those names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A batch of the name re-index held the write lock for up to 0.55 s on 100,000 messages of one sender, though it stops after 50 ms: FTS5 merged a segment of the index once 10% of its rows were deleted, inside the insert that flushed, and an INSERT ... SELECT per message flushed a segment per message. message_search now has deletemerge 0, so deleted rows go when their segments merge, and the re-index inserts plain values. That scenario's longest batch is 41 ms and the queue drains in 1.0 s instead of 9.1 s; the one-off re-index of the 1M bench cache takes 27 s instead of 70 s. bench:storage reports the longest batch and the index size after it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
SQLite before 3.43 has no contentless-delete tables and no deletemerge option, so setting it on the regular index there would fail every open. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test measures a writer next to the raw_json migration; the other migrations, now with the name re-index (#93), added a second of paced batches and pushed it past its 5 s timeout on CI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 #93
Problem
The search index takes a message's
senderandchatnames fromusers_cache/chats_cachewhen the message is written. A rename, or a sender or chat cached after its messages (most history synced before 0.9.0, #126), left older messages unfindable bysender:/chat:.Change
src/db/search-reindex.ts: tablesearch_reindex_queueand triggersusers_cache_search_ai/_au,chats_cache_search_ai/_au. A peer with cached messages is queued when its row is inserted or a name the index reads changes (username, first/last/display name; title/username). Upserts that change no name, and peers without messages, queue nothing. Only messages up to the current max rowid are queued: later ones are indexed with the new names.runPendingMigrationsdrains the queue after the background migrations, in the same short batches (one write transaction each, cursor committed with the rows; 500 messages, 2,500 fortg db migratenext to the daemon, none started after 50 ms). Both the daemon tick andtg db migratemake progress; the daemon now checks every account each tick (a few reads when idle).message_search_namesre-indexes every message of existing caches; it shows intg status/tg db migratelike the others. A full index rebuild empties the queue.message_searchgets the FTS5 optiondeletemerge0 (FTS5 otherwise merged a segment inside an insert once 10% of its rows were deleted, up to 0.55 s per batch), and the re-index inserts plain values instead ofINSERT … SELECT(FTS5 flushes at every statement savepoint, so that wrote a segment per message).contentless_delete): the queue and the one-off re-index wait instead of failing withno such table: search_reindex_queue; a newer SQLite rebuilds the index anyway.from_id= its marked ID) is indexed under its title and username; a chat insert or rename also queues the messages it posted as itself. Older caches get message triggers that read those names.search.md,database-schema.md,daemon.md,cli-commands.md,storage.md, schema annotations, CHANGELOG.Measured (made-up data, this machine)
bun run bench:storage): 53,719 messages/s at 100k, 37,741/s at 1M (27 s alone; before 16,085 and 14,324/s). Longest batch 139 ms at 100k and 1.04 s in one 1M run; another 1M run had 14 of 2,070 batches over 100 ms (max 179 ms; before 195, max 574 ms). These are FTS5 merges of big segments, a whole common word per step; ordinary history pages hit them too (building the 1M cache in pages of 100: 4 of 10,050 pages over 100 ms, max 253 ms).users_cacheupsert.Tests
src/__tests__/search-reindex.test.ts: what gets queued, no-op upserts, rename re-index, a channel as sender, resume after a stop, time limit, rollback, rebuild empties the queue, one-off migration, disabled search (with and without the queue table), trigger upgrade anddeletemergeon an older cache.src/__e2e__/search-names.e2e.test.ts: rename a user and a chat,tg db migrate, find older messages by the new names.🤖 Generated with Claude Code