fix(postgres): index-backed RRF hybrid search (#10186)#46
Merged
Conversation
The hybrid search query wrapped the vector distance operator in a scalar similarity expression and sorted on the alias (ORDER BY similarity DESC). That blinds the planner: pgvector's HNSW/DiskANN index can only serve a bare "ORDER BY embedding <=> $vec" path, so the wrapped form degraded to a full sequential scan over every row. On multi-million-row collections this exceeds the statement timeout and returns an empty result set (mudler/LocalAI#10186). Rewrite the query using the Reciprocal Rank Fusion pattern recommended by pgvector and Timescale (pg_textsearch + pgvectorscale): each arm retrieves its top-N candidates via a bare operator (index-served) and assigns a rank; the arms are combined with a FULL OUTER JOIN and scored by weighted RRF (weight/(60+rank)), then joined back to the table by primary key to fetch payloads. RRF fuses by rank rather than raw score, so it no longer mixes BM25's unbounded scores with cosine similarity's [0,1] range. The bm25 and vector weights are preserved as per-arm RRF multipliers. Add a planning regression test that asserts, via EXPLAIN, that the vector ordering is served by the index ("Order By: (embedding <=>") and that the documents table is never sequentially scanned. Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
15256b2 to
a6485bf
Compare
mudler
added a commit
to mudler/LocalAGI
that referenced
this pull request
Jun 5, 2026
Pulls in mudler/LocalRecall#46, which rewrites the PostgreSQL hybrid search using the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes the full sequential scan that blew past the statement timeout on large collections (mudler/LocalAI#10186). Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
mudler
added a commit
to mudler/LocalAI
that referenced
this pull request
Jun 5, 2026
Bumps the agent stack to pull in the PostgreSQL hybrid-search fix: - mudler/localrecall -> v0.6.3-...-a6485bf68230 (mudler/LocalRecall#46) - mudler/LocalAGI -> ...-0ec165c86c05 (re-pins localrecall) localrecall's hybrid search previously sorted on a wrapped scalar similarity expression, which blinded the planner into a full sequential scan over every row and exceeded the statement timeout on large collections, returning an empty result set. It now uses the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes #10186 Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
docker-compose starts LocalAI with the embedding model on its command line, but the model is downloaded and loaded lazily on first request. wait-localai only checked the HTTP health endpoints, which return OK as soon as the server is up - so the test suites raced the model download and intermittently failed in BeforeEach/Store with `model "granite-embedding-107m-multilingual" not found` (404), across both the postgres and chromem engines. Warm the model as part of wait-localai: after the server is healthy, POST to /v1/embeddings and retry until it answers (up to 600s, since the first call triggers the download). This makes the model a hard readiness gate for every suite that goes through start-test-services, removing the race at its source rather than retrying each individual embedding call. Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
mudler
added a commit
to mudler/LocalAGI
that referenced
this pull request
Jun 6, 2026
Pulls in mudler/LocalRecall#46 (merged), which rewrites the PostgreSQL hybrid search using the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes the full sequential scan that blew past the statement timeout on large collections (mudler/LocalAI#10186). Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
mudler
added a commit
to mudler/LocalAI
that referenced
this pull request
Jun 6, 2026
Bumps the agent stack to pull in the PostgreSQL hybrid-search fix: - mudler/localrecall -> v0.6.3-...-9a3b3321a9cd (mudler/LocalRecall#46, merged) - mudler/LocalAGI -> ...-4b2bc28cc506 (re-pins localrecall) localrecall's hybrid search previously sorted on a wrapped scalar similarity expression, which blinded the planner into a full sequential scan over every row and exceeded the statement timeout on large collections, returning an empty result set. It now uses the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes #10186 Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
mudler
added a commit
to mudler/LocalAGI
that referenced
this pull request
Jun 6, 2026
chore: bump localrecall to index-backed RRF hybrid search Pulls in mudler/LocalRecall#46 (merged), which rewrites the PostgreSQL hybrid search using the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes the full sequential scan that blew past the statement timeout on large collections (mudler/LocalAI#10186). Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
mudler
added a commit
to mudler/LocalAI
that referenced
this pull request
Jun 6, 2026
Bumps the agent stack to pull in the PostgreSQL hybrid-search fix: - mudler/localrecall -> v0.6.3-...-9a3b3321a9cd (mudler/LocalRecall#46, merged) - mudler/LocalAGI -> ...-14aed1ae4336 (mudler/LocalAGI#477, merged) localrecall's hybrid search previously sorted on a wrapped scalar similarity expression, which blinded the planner into a full sequential scan over every row and exceeded the statement timeout on large collections, returning an empty result set. It now uses the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes #10186 Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
mudler
added a commit
to mudler/LocalAI
that referenced
this pull request
Jun 6, 2026
#10186) (#10192) chore: bump LocalAGI and localrecall (index-backed RRF hybrid search) Bumps the agent stack to pull in the PostgreSQL hybrid-search fix: - mudler/localrecall -> v0.6.3-...-9a3b3321a9cd (mudler/LocalRecall#46, merged) - mudler/LocalAGI -> ...-14aed1ae4336 (mudler/LocalAGI#477, merged) localrecall's hybrid search previously sorted on a wrapped scalar similarity expression, which blinded the planner into a full sequential scan over every row and exceeded the statement timeout on large collections, returning an empty result set. It now uses the canonical Reciprocal Rank Fusion pattern (index-backed candidate retrieval + FULL OUTER JOIN + weighted RRF). Fixes #10186 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
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.
Problem
The hybrid search query (
PostgresDB.Search) wrapped the vector distance operator in a scalar similarity expression and sorted on the alias:pgvector's HNSW/DiskANN index can only serve a bare
ORDER BY embedding <=> $vecpath. Sorting on the wrapped scalar blinds the planner into a full sequential scan over every row, then an in-memory top-N sort. On multi-million-row collections this exceeds the statement timeout and returns{"count":0,"results":[]}(see mudler/LocalAI#10186).Fix
Adopt the canonical Reciprocal Rank Fusion hybrid-search pattern recommended by both pgvector and Timescale (the
pg_textsearch+pgvectorscalestack this engine runs on):ORDER BY full_text <@> to_bm25query(...)/ORDER BY embedding <=> $vec, both index-served) and assigns aROW_NUMBER()rank.FULL OUTER JOINand scored by weighted RRF:bm25Weight/(60+rank) + vectorWeight/(60+rank).RRF fuses by rank, not raw score, so it no longer mixes BM25's unbounded scores with cosine similarity's
[0,1]range. The existingbm25Weight/vectorWeightknobs are preserved as per-arm RRF multipliers (equal by default). Candidate pool per arm =GREATEST(limit*10, 100).Verification (
EXPLAIN, DiskANN + pg_textsearch stack)Before (wrapped scalar):
Seq Scan on documents_x (cost=0.00..5095.00 rows=2000)+ in-memorySort.After (RRF):
No
Seq Scan; cost 5128 -> 648. Query executes correctly (the BM25-matching doc ranks #1).Tests
Adds
rag/engine/postgres_planning_test.go, a planning regression test that builds the real schema viasetupDatabase()(no embedding model needed), seeds rows, and asserts viaEXPLAINthat the vector ordering is index-served and the table is never sequentially scanned. Fusion-agnostic, so it guards the index path regardless of scoring. Runs in the existingmake test-unitharness.Fixes mudler/LocalAI#10186