Skip to content

fix(search): refresh sender and chat names in the index - #141

Merged
neoromantic merged 7 commits into
mainfrom
fix/search-reindex-names
Nov 13, 2026
Merged

neoromantic merged 7 commits into
mainfrom
fix/search-reindex-names

Conversation

@neoromantic

@neoromantic neoromantic commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Fixes #93

Problem

The search index takes a message's sender and chat names from users_cache / chats_cache when 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 by sender: / chat:.

Change

  • New src/db/search-reindex.ts: table search_reindex_queue and triggers users_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.
  • runPendingMigrations drains the queue after the background migrations, in the same short batches (one write transaction each, cursor committed with the rows; 500 messages, 2,500 for tg db migrate next to the daemon, none started after 50 ms). Both the daemon tick and tg db migrate make progress; the daemon now checks every account each tick (a few reads when idle).
  • One-off background migration message_search_names re-indexes every message of existing caches; it shows in tg status / tg db migrate like the others. A full index rebuild empties the queue.
  • Short batches: message_search gets the FTS5 option deletemerge 0 (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 of INSERT … SELECT (FTS5 flushes at every statement savepoint, so that wrote a segment per message).
  • Disabled search (older SQLite without contentless_delete): the queue and the one-off re-index wait instead of failing with no such table: search_reindex_queue; a newer SQLite rebuilds the index anyway.
  • A channel or group that posts as itself (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.
  • Docs: search.md, database-schema.md, daemon.md, cli-commands.md, storage.md, schema annotations, CHANGELOG.

Measured (made-up data, this machine)

  • 100,000 messages of one sender, every word in each, then the sender cached: the queue drains in 1.0 s with batches of at most 41 ms (before: 9.1 s, batches up to 0.55 s).
  • One-off re-index (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).
  • Index right after the one-off re-index: 19.7 MB instead of 11.8 MB at 100k, 106 MB instead of 104 MB at 1M, until its segments merge.
  • History writes queued 0 peers. The triggers add about 9 µs per unchanged users_cache upsert.

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 and deletemerge on 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

neoromantic and others added 6 commits November 13, 2026 06:13
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]>
@neoromantic
neoromantic merged commit e1ebad9 into main Nov 13, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/search-reindex-names branch November 13, 2026 03:23
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: re-index older messages after a person or chat is renamed

1 participant