Skip to content

[Bug]: HNSW multi-index merge still materializes shard_count × LIMIT candidates #25637

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

de64386657a07eb3bcb2b411cf7989023b1e16c0

Other Environment Information

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:

  1. boundedHnswSearchLimits requests min(LIMIT, fileCardinality) candidates from every index file.
  2. Every returned candidate is unconditionally appended via SearchResultSafeHeap.Push.
  3. SearchResultSafeHeap is an ordinary unbounded min-heap. Its size constructor argument is capacity only.
  4. Only after all workers finish does the code pop and return the global LIMIT rows.

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: boundedHnswSearchLimits computes per-file K and a preallocation capacity.
  • pkg/vectorindex/hnsw/search.go: workers unconditionally push every candidate into SearchResultSafeHeap.
  • pkg/vectorindex/index.go: SearchResultSafeHeap.Push has 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

  1. Build or mock an HnswSearch with at least two non-empty index files.
  2. Choose LIMIT = K where each file contains at least K vectors.
  3. Run HnswSearch.Search.
  4. Observe from the code path that each file returns K candidates and all N × K candidates 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugseverity/s0

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions