Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
main
Commit ID
de64386657a07eb3bcb2b411cf7989023b1e16c0
Other Environment Information
- Hardware parameters: not required; the defect is deterministic from the in-process merge path.
- OS type: any supported platform.
- Others: fix(plan): preserve pagination across LIMIT optimizations #25587 (
df6a34119e) updated the existing merge path but did not complete its intended bound.
Actual Behavior
HNSW searches over multiple index files still retain every per-file candidate in the shared merge heap. The recent mitigation caps only the heap's initial allocation, not its length:
boundedHnswSearchLimitsrequestsmin(LIMIT, fileCardinality)candidates from every index file.- Every returned candidate is unconditionally appended via
SearchResultSafeHeap.Push. SearchResultSafeHeapis an ordinary unbounded min-heap. Itssizeconstructor argument is capacity only.- Only after all workers finish does the code pop and return the global
LIMITrows.
Therefore the live candidate set is sum(min(LIMIT, cardinality_i)), which is index_file_count × LIMIT for sufficiently populated files, while the user-visible result is at most LIMIT. This is an avoidable memory and CPU amplification on fragmented HNSW indexes. The comment above boundedHnswSearchLimits says it prevents this multiplication, but the current Push path does not enforce that bound.
Relevant code on main:
pkg/vectorindex/hnsw/search.go:boundedHnswSearchLimitscomputes per-file K and a preallocation capacity.pkg/vectorindex/hnsw/search.go: workers unconditionally push every candidate intoSearchResultSafeHeap.pkg/vectorindex/index.go:SearchResultSafeHeap.Pushhas no maximum-size or eviction logic.
This is related to the earlier HNSW benchmark OOM reports (#24041, #23904), but is a precise current root cause in the merge path and a follow-up to #25587 rather than a duplicate of their environment-level symptoms.
Expected Behavior
Each index file may search its local top K, because that is necessary for exact global top-K merging. The merge structure itself must retain only the best global K candidates. Peak merge memory should be O(min(LIMIT, total_cardinality)), independent of index-file count.
Steps to Reproduce
- Build or mock an
HnswSearchwith at least two non-empty index files. - Choose
LIMIT = Kwhere each file contains at least K vectors. - Run
HnswSearch.Search. - Observe from the code path that each file returns K candidates and all
N × Kcandidates are pushed before the final result is truncated to K.
A regression test should use multiple fake/index files with a small K and assert both:
- returned rows are the exact globally nearest K in ascending distance order;
- the merge heap never contains more than K entries.
Additional information
First-principles fix direction:
- keep the per-file search limit, because reducing it can lose a valid global top-K result;
- replace the shared unbounded min-heap with a concurrency-safe bounded max-heap of global K entries;
- on insertion after capacity, evict only the current worst distance when the new candidate is better;
- drain the final max-heap in reverse order (or sort K entries) so the public result remains nearest-first;
- retain overflow/cardinality tests, and add a multi-file peak-size regression.
Do not address this by imposing an arbitrary semantic limit below the SQL candidate budget.
Activity
fix(vectorindex): bound HNSW multi-index merge heap to LIMIT (#25637) (…
fix(vectorindex): bound HNSW multi-index merge heap to LIMIT (matrixo…
fix(vectorindex): bound HNSW multi-index merge heap to LIMIT (matrixo…
fix(vectorindex): bound HNSW multi-index merge heap to LIMIT (matrixo…