Skip to content

perf(core): count admitted vector chunks from the manifest alone - #1793

Merged
phernandez merged 2 commits into
mainfrom
search-read-cost
Oct 20, 2026
Merged

phernandez merged 2 commits into
mainfrom
search-read-cost

Conversation

@phernandez

Copy link
Copy Markdown
Member

Part 1 of #1791: the admission count, defect 1 of 3. The FTS predicate placement and search.count follow 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 joined search_vector_embeddings to apply the full manifest filter. Postgres planned it as a parallel hash join over both whole tables, and the LIMIT cannot 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_sql counts from search_vector_chunks alone, 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 of idx_search_vector_chunks_location that 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':

cold warm pages
before (joined) 8,207 ms 4,226 ms 318k
after (manifest only) 1,426 ms 16 ms 5.3k

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_manifest runs EXPLAIN (FORMAT JSON) on the count SQL against Postgres. It asserts the plan reads only search_vector_chunks and that the count equals the admitted chunks.
  • Load-bearing: with the embeddings join put back into admission_count_sql, the test fails. Restored, it passes.
  • The existing exact versus filtered-HNSW tests still pass (10 selected in test_postgres_search_repository.py). Lint is clean. Typecheck shows only the pre-existing pymilvus import errors.

🤖 Generated with Claude Code

https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv

@phernandez phernandez added the cost-review-2026-10 October 2026 COGS review: cost leaks, heavy tenants, and production error triage label Oct 20, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T16:10:02.249303Z 4f8b0b1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

phernandez and others added 2 commits October 20, 2026 10:58
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
phernandez merged commit c17fd2b into main Oct 20, 2026
32 checks passed
@phernandez
phernandez deleted the search-read-cost branch October 20, 2026 16:37
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cost-review-2026-10 October 2026 COGS review: cost leaks, heavy tenants, and production error triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant