Repository navigation
perf(core): count admitted vector chunks from the manifest alone - #1793
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
phernandez
added this pull request to stack #1797
October 20, 2026 15:56
The filtered-search admission count joined search_vector_embeddings to apply the full manifest filter. Postgres planned that as a hash join over both whole tables, which the LIMIT cannot stop: on a 480k-chunk project every filtered vector or hybrid search read 2.5 GB (8.2 s cold, 4.2 s warm) and flushed the cache for the queries after it. The count only picks exact ranking or the HNSW scan, so it now reads just the chunk columns the admission names: an index-only scan of the location index that stops at the cap (1.4 s cold, 16 ms warm, 5.3k pages). Refs #1791 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv Signed-off-by: phernandez <paul@basicmachines.co>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv Signed-off-by: phernandez <paul@basicmachines.co>
phernandez
force-pushed
the
search-read-cost
branch
from
October 20, 2026 16:06
b7a01f8 to
4f8b0b1
Compare
phernandez
added a commit
that referenced
this pull request
Oct 20, 2026
Part 2 of #1791, stacked on #1793: FTS predicate placement. ## Problem `compile_fts_filter` joins `search_index` to a UNION of GIN candidates: parent matches plus chunk matches. The planner estimates one row from that UNION, so it pushes the document-vector condition down onto the scan of every row in scope and joins the candidates afterwards. That condition runs one `EXISTS` probe into `search_index_fts_chunks` per query operand, for every scoped row, before the join. ## Change The candidate join and the cheap shared conditions (scope, type, candidate keys) now sit in an `OFFSET 0` subquery aliased `search_index`. Postgres cannot push conditions through that fence, so the text conditions (word, script) run above it, on candidates only. Every other reference to `search_index` is unchanged. The metadata join and the title and permalink conditions still apply outside. Queries with no candidate join (no text, or stopword-only text) are unchanged. ## Measured on the production tenant from #1791 4 projects, entity filter, "meeting budget review", search page limit 130, `EXPLAIN (ANALYZE, BUFFERS)`, read-only: | | buffer touches | chunk probes (operand subplan loops) | |---|---|---| | before | 1,137k | 19,221 | | after | 516k | 7,992 | Timings varied with the compute's cache state: the same plan ran from 1.8 s warm to 28.5 s cold. Buffers are the comparable number. About 259k of the remaining touches are the chunk-candidate sequential scan, which detoasts every chunk's `tsvector`. That is the next PR in the stack. It needs a way to make the planner take the GIN index, because it does not cost TOAST. ## Tests - `test_postgres_text_conditions_run_only_on_candidates` runs `EXPLAIN (FORMAT JSON)` on the compiled search SQL against Postgres. It asserts that the document-vector condition (`to_tsvector`) appears only on a `Subquery Scan`, never on a scan of `search_index`. Postgres always pushes a condition on a plain table into that table's scan, so the plan shape is deterministic. - **Load-bearing:** without the fence, the test fails with `{'Index Scan'} == {'Subquery Scan'}`. With the fence, it passes. - 434 repository search tests pass on Postgres. Lint is clean; typecheck shows only the pre-existing `pymilvus` errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv Signed-off-by: phernandez <paul@basicmachines.co> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Part 1 of #1791: the admission count, defect 1 of 3. The FTS predicate placement and
search.countfollow in stacked PRs.Problem
Every filtered vector or hybrid search first counts its admitted chunks, up to
EXACT_ADMISSION_CHUNK_LIMIT + 1, to choose between exact ranking and the HNSW scan. The default entity filter makes nearly every MCP search filtered. The count joinedsearch_vector_embeddingsto apply the full manifest filter. Postgres planned it as a parallel hash join over both whole tables, and theLIMITcannot stop a hash build.On a production tenant with 480k chunks, the old count took 8.2 s cold and 4.2 s warm and read 318k pages (2.5 GB). That also flushed the cache for the FTS and HNSW queries that run next.
Change
admission_count_sqlcounts fromsearch_vector_chunksalone, applying only the project scope and the admission predicate. Both reference only chunk columns:source_type,project_id,source_row_id. The query becomes an index-only scan ofidx_search_vector_chunks_locationthat stops at the cap.Pending or stale chunks are now counted too. The count only picks a plan, so that can only tip a search near the cap toward the HNSW scan, which is correct for any count.
Measured on the same production tenant
EXPLAIN (ANALYZE, BUFFERS), read-only, 4 projects,source_type = 'entity':The remaining pages are heap visibility checks (7.5k heap fetches) on rows that autovacuum has not yet marked all-visible.
Tests
test_postgres_admission_count_reads_only_the_chunk_manifestrunsEXPLAIN (FORMAT JSON)on the count SQL against Postgres. It asserts the plan reads onlysearch_vector_chunksand that the count equals the admitted chunks.admission_count_sql, the test fails. Restored, it passes.test_postgres_search_repository.py). Lint is clean. Typecheck shows only the pre-existingpymilvusimport errors.🤖 Generated with Claude Code
https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv