Skip to content

perf(search): find FTS rows by rowid - #13475

Merged
tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/search-fts-rowid
Sep 30, 2026
Merged

tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/search-fts-rowid

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What changes

The search database keeps blocks (id TEXT PRIMARY KEY, title, page) and the FTS5 table blocks_fts(id, title, page), filled by triggers on blocks (src/main/frontend/worker/search.cljs). The delete and update triggers removed a block's old FTS row with DELETE from blocks_fts where id = old.id. FTS5 has no index on a regular column, so that statement reads every row of the FTS table (EXPLAIN QUERY PLAN: SCAN blocks_fts VIRTUAL TABLE INDEX 0:). It runs for every save of a block the index already holds and for every block delete, inside the worker's handling of the edit: in a release build with timing marks, the search index write of a 1-block delete took 43.6 ms (29.3 - 47.4) on a graph with 150,860 index rows and 9.3 ms (6.8 - 15.2) on a new graph, median (range) of 30.

The FTS row of a block now has the rowid of its blocks row, and the triggers find it by rowid (SCAN blocks_fts VIRTUAL TABLE INDEX 0:=):

-- before (update trigger; the delete trigger has the same DELETE)
DELETE from blocks_fts where id = old.id;
INSERT INTO blocks_fts (id, title, page) VALUES (new.id, new.title, new.page);
-- after
DELETE FROM blocks_fts WHERE rowid = old.rowid;
INSERT INTO blocks_fts (rowid, id, title, page) VALUES (new.rowid, new.id, new.title, new.page);

The queries on blocks_fts select id, page, title, rank by title match, title like and page =; none reads the FTS rowid. blocks has no INTEGER PRIMARY KEY, and sqlite's documentation says VACUUM may change the rowids of such a table. Nothing in the worker vacuums the search database: the one VACUUM (:thread-api/gc-graph) runs on the graph database, and backups go through sqlite's backup API, which copies pages.

search-db-version goes from 4 to 5. An index at version 4 is complete, only keyed the old way, so it moves in place instead of being rebuilt from the graph (a rebuild truncates the index first, so search returns partial results until it ends, and it re-embeds the vector index when semantic search is on):

  • :thread-api/search-build-blocks-indice-in-worker, which the renderer calls after the graph is restored, once input has been idle for 5 s (schedule-search-index-build!, src/main/frontend/handler/events.cljs), starts the move in the background and returns 4 at once, so the graph is marked ready as before. Other versions keep the full rebuild.
  • The move creates blocks_fts_next with rowid triggers of its own, next to blocks_fts and its triggers. It copies the blocks rows in batches of 200 (1 INSERT ... SELECT each, skipping rows the new triggers already wrote). After each batch it waits twice as long as the batch took, so it takes at most a third of the worker's time. Then 1 transaction drops the old triggers and table, renames blocks_fts_next to blocks_fts, creates the rowid triggers and sets version 5.
  • Search reads the old table, which its triggers keep complete, until that transaction. A move cut short (graph closed, app quit) leaves version 4 and starts over at the next open. A forced rebuild or a truncate of the index stops it (the build id and the version are checked before each batch), and drop-tables-and-triggers! also drops blocks_fts_next.

Two effects besides the speed:

  • Without ORDER BY, FTS5 returns matches in rowid order (ascending rowids under a LIMIT on the graph below). When more rows match than a query's limit, the rows kept now follow blocks order. Before, an edited block moved to the end, since the update trigger inserted its FTS row with a new rowid.
  • Both FTS tables exist while the index moves: the search database of the graph below went from 247 MB to 419 MB, 173 MB of it free pages that sqlite reuses for later writes (the file shrinks only with VACUUM).

#13438 also bumps search-db-version from 4 to 5. Whichever lands second takes the next number. With #13438 first, its version 5 index would be the one this change moves in place (fts-id-keyed-search-db-version becomes 5), and a version 4 index would get the full rebuild #13438 needs.

Timings on a graph imported from a file graph (150,860 rows in its search index), median (range):

  • The app's 2 statements alone (the upsert of upsert-blocks!, the delete of delete-blocks!) on a copy of its search database, node:sqlite (sqlite 3.53.4), WAL, synchronous FULL as in the app, 15 of each: update of an indexed row 38.9 ms (38.4 - 42.4) with master's triggers, 8.2 ms (7.7 - 15.9) with this change; delete 38.7 ms (38.2 - 39.6) against 8.2 ms (7.5 - 17.4); insert of a new row 8.6 ms (8.3 - 92.1) against 8.0 ms (7.4 - 117.4). With synchronous OFF, which leaves the statements without their disk sync: update 31.5 ms (21.4 - 32.3) against 0.15 ms (0.11 - 0.29), delete 31.4 ms (20.4 - 32.4) against 0.14 ms (0.13 - 1.19).
  • The desktop app, release builds of master 16c4ed1a0 and of this branch (renderer, db worker and Electron main of the same commit), each on a fresh copy of the graph's 3 databases, the branch's copy after its index had moved, 30 cycles each on a new page of 4 blocks, from the key press to the next editor focused (delete) or to the editor closed (save): deleting a block 128.5 ms (93.9 - 241.3) on master, 95.8 ms (81.6 - 160.2) with this change; saving a block the index already holds (the second save of the same block) 122.4 ms (92.9 - 207.7) against 94.4 ms (82.8 - 173.7); the first save of a new block, an insert into the index, 82.6 ms (69.1 - 119.1) against 81.1 ms (66.6 - 117.8); inserting an empty block 99.4 ms (78.2 - 169.3) against 92.8 ms (77.1 - 157.9).
  • The move of that graph's version 4 index in the desktop app, from the worker log: 65.2 s (63.1 - 67.9), 6 opens of fresh copies, each beginning 1.1 s (1.09 - 1.12) after the worker was ready. The worker's start, from its first log line to ready: 5.04 s (4.22 - 5.20) on master, 5.45 s (4.87 - 5.74) with this change, 3 opens each; the move begins after it.
  • Small worker requests while the move runs, a 1-entity query through the plugin API's datascript_query sent from the renderer every 100 ms from the moment the graph is opened, 2 opens per build: during the move 15.0 ms (8.1 - 160.0, p90 40.5) and 14.9 ms (8.0 - 118.8, p90 48.2); on master from 5 to 25 s after its first answer 12.1 ms (8.8 - 79.8, p90 19.0) and 13.0 ms (8.7 - 1461.8, p90 17.9); after the move 12.7 and 11.8 ms. A first version without the wait between batches moved the index in 20.4 s (19.1 - 21.1, 6 opens), but these requests took 75.3 ms (31.2 - 205.7, p90 105.3) and 67.9 ms (30.7 - 355.0, p90 104.0) during it.

The move's SQL also ran on the web worker's sqlite (@sqlite.org/sqlite-wasm, oo1 API, in memory, 500 rows with drifted FTS rowids and writes between batches): 0 rows out of step after it, FTS5's integrity check passes. The web worker itself was not run with this change.

Tests

  • New fts-rows-keep-blocks-rowids-test in frontend.worker.search-test, on a real node:sqlite database through the worker's own sqlite wrapper: after an insert, an update and a delete, every blocks row has the FTS row of its rowid with the same values, and search-blocks returns the updated text, not the old one, and not the deleted block. On master it fails (1 failure: 2 rows out of step, the updated block and the block inserted after it). With this change it passes.
  • New fts-rowid-migration-keeps-search-right-test in frontend.worker.search-test: a version 4 index with a drifted row, the move in batches of 3 with saves, a new block and deletes both before and after the copied rows, search-blocks right in the middle of the move and after it, the triggers and version after it, and the rowid triggers on an update and a delete afterwards.
  • New search-build-blocks-indice-in-worker-moves-version-four-index-to-rowid-keys-test in frontend.worker.db-core-test: the thread API on a version 4 index of 450 rows (3 batches) with 10 drifted rows returns 4 at once, needs no graph connection, and leaves version 5 with every row in step. On master it fails (1 failure: 10 rows out of step).
  • frontend.worker.search-test 67 tests, 235 assertions; frontend.worker.db-core-test 148 tests, 1606 assertions: 0 failures, at TZ=Etc/GMT+12 and TZ=Pacific/Kiritimati.
  • The whole unit suite at TZ=Etc/GMT+12 (2505 tests, 12523 assertions): 0 failures, 0 errors. Its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1472 tests, 9082 assertions): 1 failure, the CPU-time assertion of sync-search-indice-300-new-blocks-performance-when-semantic-search-disabled (1156 ms against its 1000 ms limit; the machine's 5-minute load average read 3.5 just after); sync-search-indice is not changed here, and frontend.worker.search-test alone in that zone right after passed (that test at 250 ms). The first version of this branch, without the wait between batches, passed the same suite in both zones with 0 failures.
  • clj-kondo on the 4 changed files: 0 errors, 0 warnings.

Allow edits from maintainers.


Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

[repo search-db build-id]
(let [started-at (common-util/time-ms)]
(p/let [_ (js/Promise. (fn [resolve] (js/setTimeout resolve 0)))]
(search/start-fts-rowid-migration! search-db)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Cancelled migration leaves duplicate search index

If a search truncate runs before migration's first timer fires, start-fts-rowid-migration! recreates the next index afterward. The version check then stops migration without removing that index. Later block writes update both indexes, wasting disk and worker time until another truncate.

Learn more

The migration schedules its first work with a zero-delay timer. truncate-table! can run in that gap, replacing both FTS tables and setting the search version to zero. After the timer fires, migration unconditionally creates a new blocks_fts_next and three triggers. The following version check stops copying but leaves those triggers attached to blocks, so subsequent edits index two tables until another truncate.

Example: A version-4 graph schedules migration, then :thread-api/search-truncate-tables clears search before the timer runs. Migration recreates blocks_fts_next, sees version 0, and exits; rebuilding the graph then writes each block into both FTS indexes.

Recommended fix: Check the active build ID and version before calling start-fts-rowid-migration!, and ensure cancelled migrations remove their own temporary table and triggers only when a newer build does not own them. Cover truncation before the first timer in a regression test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed this through the compiled worker API. A focused fix and two regression tests are in #13529, based on an unchanged snapshot of this PR because I cannot push to the fork. The fix validates build ID and source version after the first timer and before creating migration structures; it also prevents an obsolete migration from replacing a successor's temporary table. Both new tests fail without the guard and pass with it. This thread remains unresolved until the fix is incorporated here.

Written by Devin

The search index triggers removed a block's old FTS row with
`DELETE FROM blocks_fts WHERE id = old.id`. FTS5 has no index on a
regular column, so that statement reads every FTS row, and it runs on
every save of an indexed block and on every block delete, before the
worker replies.

Give each FTS row the rowid of its blocks row and find it by rowid in
the delete and update triggers. Bump the search index version to 5.

An index at version 4 moves in place instead of a rebuild from the
graph: a new FTS table with rowid triggers of its own is filled from
the blocks table in batches of 200 rows, while the old table and its
triggers keep serving search. After each batch the move pauses twice
as long as the batch took, so it takes at most a third of the worker's
time. 1 transaction then drops the old table and triggers, renames the
new table, creates the rowid triggers and sets version 5. The move
runs from the idle-scheduled index check, after the graph opens.
Other versions still rebuild.
@tiensonqin
tiensonqin force-pushed the perf/search-fts-rowid branch from 29629df to 2ad4244 Compare September 30, 2026 11:00
@tiensonqin
tiensonqin merged commit 792f863 into logseq:master Sep 30, 2026
11 of 14 checks passed
devin-ai-integration Bot pushed a commit that referenced this pull request Sep 30, 2026
…mporary index (#13475 #13529)

Co-authored-by: Tienson Qin <[email protected]>
Signed-off-by: Tienson Qin <[email protected]>
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
@yuxi-liu-wired
yuxi-liu-wired deleted the perf/search-fts-rowid branch September 30, 2026 20:03
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
The search index triggers removed a block's old FTS row with
`DELETE FROM blocks_fts WHERE id = old.id`. FTS5 has no index on a
regular column, so that statement reads every FTS row, and it runs on
every save of an indexed block and on every block delete, before the
worker replies.

Give each FTS row the rowid of its blocks row and find it by rowid in
the delete and update triggers. Bump the search index version to 5.

An index at version 4 moves in place instead of a rebuild from the
graph: a new FTS table with rowid triggers of its own is filled from
the blocks table in batches of 200 rows, while the old table and its
triggers keep serving search. After each batch the move pauses twice
as long as the batch took, so it takes at most a third of the worker's
time. 1 transaction then drops the old table and triggers, renames the
new table, creates the rowid triggers and sets version 5. The move
runs from the idle-scheduled index check, after the graph opens.
Other versions still rebuild.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
…mporary index (#13475 #13529)

Co-authored-by: Tienson Qin <[email protected]>
Signed-off-by: Tienson Qin <[email protected]>
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
The search index triggers removed a block's old FTS row with
`DELETE FROM blocks_fts WHERE id = old.id`. FTS5 has no index on a
regular column, so that statement reads every FTS row, and it runs on
every save of an indexed block and on every block delete, before the
worker replies.

Give each FTS row the rowid of its blocks row and find it by rowid in
the delete and update triggers. Bump the search index version to 5.

An index at version 4 moves in place instead of a rebuild from the
graph: a new FTS table with rowid triggers of its own is filled from
the blocks table in batches of 200 rows, while the old table and its
triggers keep serving search. After each batch the move pauses twice
as long as the batch took, so it takes at most a third of the worker's
time. 1 transaction then drops the old table and triggers, renames the
new table, creates the rowid triggers and sets version 5. The move
runs from the idle-scheduled index check, after the graph opens.
Other versions still rebuild.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
…mporary index (#13475 #13529)

Co-authored-by: Tienson Qin <[email protected]>
Signed-off-by: Tienson Qin <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
The search index triggers removed a block's old FTS row with
`DELETE FROM blocks_fts WHERE id = old.id`. FTS5 has no index on a
regular column, so that statement reads every FTS row, and it runs on
every save of an indexed block and on every block delete, before the
worker replies.

Give each FTS row the rowid of its blocks row and find it by rowid in
the delete and update triggers. Bump the search index version to 5.

An index at version 4 moves in place instead of a rebuild from the
graph: a new FTS table with rowid triggers of its own is filled from
the blocks table in batches of 200 rows, while the old table and its
triggers keep serving search. After each batch the move pauses twice
as long as the batch took, so it takes at most a third of the worker's
time. 1 transaction then drops the old table and triggers, renames the
new table, creates the rowid triggers and sets version 5. The move
runs from the idle-scheduled index check, after the graph opens.
Other versions still rebuild.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
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.

2 participants