perf(search): stored tsvector column + saner short-term prefixing for ⌘K search - #597
Open
16francej wants to merge 2 commits into
Open
perf(search): stored tsvector column + saner short-term prefixing for ⌘K search#59716francej wants to merge 2 commits into
16francej wants to merge 2 commits into
Conversation
added 2 commits
August 19, 2026 03:08
The chat-search GIN index was built over an expression that calls a plpgsql JSON parse per row, so every lossy bitmap recheck re-parsed entry payloads — benchmarked at ~96% of query time on a 1M-row corpus (102.6s -> 4.0s for a two-term query). Store the tsvector in a generated column, index that, and query it directly. Also stop prefix-expanding 1-2 character terms: a short prefix expands to a huge GIN key set (14.7s inside the index scan alone for 'de:*'). Short terms now match whole words; the in-memory mirror follows suit. The old expression index is dropped after the replacement index builds. Note: adding the stored generated column rewrites session_entries once at boot.
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.
What
Speeds up the ⌘K chat search dramatically by fixing where the query actually spends its time.
search_tsvgenerated column, GIN-indexed. The previous index was an expression index over a plpgsql function that JSON-parses the payload. Prefix tsqueries force a recheck of every lossy bitmap candidate, and each recheck re-ran that parse. On a 1M-row / 878MB benchmark corpus this was ~96% of query time: 102.6s → 4.0s for a representative two-term query, identical result sets. The recheck now reads a stored column instead.de:*) expands to an enormous GIN key set — 14.7s inside the index scan alone on the same corpus. Terms under 3 chars now match exactly; the in-memory store's mirror (matchesSearchTerms) follows the same rule so both stores agree.entry_search_textis markedPARALLEL SAFE(it's pure), unblocking parallel plans.Migration notes
GENERATED ALWAYS … STORED, so theALTER TABLErewritessession_entriesonce at boot. The replacement index buildsCONCURRENTLY(same machinery as before), then the old expression index is dropped.searchEntriesoutput text still usesentry_search_text(payload)for the ≤40 returned rows — cheap, and avoids storing the text twice.Benchmarks (1M entries, worst-case dense vocabulary)
deploy:* & error:*de:*Tests
test/entry-search.test.ts— new cases for short-term exact matching in bothtsPrefixQueryandmatchesSearchTerms.test/session-search.test.tsandtest/postgres-store.test.ts(against a real Postgres) pass, exercisingsearchEntriesthrough the new column and index.Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.