Repository navigation
perf(search): find FTS rows by rowid - #13475
Conversation
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| [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) |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
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.
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.
29629df to
2ad4244
Compare
…mporary index (#13475 #13529) Co-authored-by: Tienson Qin <[email protected]> Signed-off-by: Tienson Qin <[email protected]>
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.
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.
…mporary index (#13475 #13529) Co-authored-by: Tienson Qin <[email protected]> Signed-off-by: Tienson Qin <[email protected]>
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.
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.
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.
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.
…mporary index (#13475 #13529) Co-authored-by: Tienson Qin <[email protected]> Signed-off-by: Tienson Qin <[email protected]>
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.
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.
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.
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.
What changes
The search database keeps
blocks (id TEXT PRIMARY KEY, title, page)and the FTS5 tableblocks_fts(id, title, page), filled by triggers onblocks(src/main/frontend/worker/search.cljs). The delete and update triggers removed a block's old FTS row withDELETE 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
blocksrow, and the triggers find it by rowid (SCAN blocks_fts VIRTUAL TABLE INDEX 0:=):The queries on
blocks_ftsselectid, page, title, rankbytitle match,title likeandpage =; none reads the FTS rowid.blockshas 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-versiongoes 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.blocks_fts_nextwith rowid triggers of its own, next toblocks_ftsand its triggers. It copies theblocksrows in batches of 200 (1INSERT ... SELECTeach, 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, renamesblocks_fts_nexttoblocks_fts, creates the rowid triggers and sets version 5.drop-tables-and-triggers!also dropsblocks_fts_next.Two effects besides the speed:
blocksorder. Before, an edited block moved to the end, since the update trigger inserted its FTS row with a new rowid.#13438 also bumps
search-db-versionfrom 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-versionbecomes 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):
upsert-blocks!, the delete ofdelete-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).16c4ed1a0and 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).datascript_querysent 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
fts-rows-keep-blocks-rowids-testinfrontend.worker.search-test, on a real node:sqlite database through the worker's own sqlite wrapper: after an insert, an update and a delete, everyblocksrow has the FTS row of its rowid with the same values, andsearch-blocksreturns 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.fts-rowid-migration-keeps-search-right-testinfrontend.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-blocksright 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.search-build-blocks-indice-in-worker-moves-version-four-index-to-rowid-keys-testinfrontend.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-test67 tests, 235 assertions;frontend.worker.db-core-test148 tests, 1606 assertions: 0 failures, atTZ=Etc/GMT+12andTZ=Pacific/Kiritimati.TZ=Etc/GMT+12(2505 tests, 12523 assertions): 0 failures, 0 errors. Its time-zone-dependent namespaces atTZ=Pacific/Kiritimati(1472 tests, 9082 assertions): 1 failure, the CPU-time assertion ofsync-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-indiceis not changed here, andfrontend.worker.search-testalone 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.Allow edits from maintainers.