Skip to content

fix(vectorindex): bound HNSW multi-index merge heap to LIMIT (#25637) - #25716

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
cpegeric:issue_25637
Jul 14, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
cpegeric:issue_25637

Conversation

@cpegeric

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #25637

What this PR does / why we need it:

The HNSW multi-index search pushed every per-file candidate (sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an unbounded min-heap and truncated to LIMIT only at the end, so peak merge memory scaled with the number of index files. On fragmented indexes this is an OOM vector (follow-up to #25587; relates to #24041, #23904).

Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it retains only the limit smallest-distance results, evicting the current worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap (worst at root) is required so the worst retained item can be evicted cheaply; the search drains it back-to-front to restore ascending (nearest-first) order. boundedHnswSearchLimits no longer needs to return the sum-based heap capacity; the heap bound is simply the result limit. The retained set is the exact global top-K regardless of push order (order-independent invariant), so parallel workers can keep pushing concurrently.

Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits updated for the 2-value signature.

…rigin#25637)

The HNSW multi-index search pushed every per-file candidate
(sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an
unbounded min-heap and truncated to LIMIT only at the end, so peak merge
memory scaled with the number of index files. On fragmented indexes this is
an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904).

Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it
retains only the `limit` smallest-distance results, evicting the current
worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory
is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap
(worst at root) is required so the worst retained item can be evicted cheaply;
the search drains it back-to-front to restore ascending (nearest-first) order.
boundedHnswSearchLimits no longer needs to return the sum-based heap capacity;
the heap bound is simply the result limit. The retained set is the exact global
top-K regardless of push order (order-independent invariant), so parallel
workers can keep pushing concurrently.

Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a
bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits
updated for the 2-value signature.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify mergify Bot added the kind/bug Something isn't working label Jul 14, 2026
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Jul 14, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the bounded HNSW merge implementation across correctness, performance, concurrency, and unhappy paths. The max-heap invariant retains the exact global smallest K independently of push order; reverse draining preserves nearest-first output; zero/large limits and preallocation are bounded; and the existing worker cancellation/resource lifecycle remains intact. CI is green, and local race/property tests, vectorindex build, and vet all passed. No merge-blocking issues found.

@mergify mergify Bot added the queued label Jul 14, 2026
@mergify

mergify Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-14 12:18 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue — 2026-07-14 12:19 UTC · at 489c490e8dcf1d2e1b03d86235ecca377dfd02f9

This pull request spent 1 minute 6 seconds in the queue, with no time running CI.

Reason

Pull request #25716 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit cd362cf into matrixorigin:main Jul 14, 2026
18 of 20 checks passed
@mergify mergify Bot removed the queued label Jul 14, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#25637) (matrixorigin#25716)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#25637

## What this PR does / why we need it:

The HNSW multi-index search pushed every per-file candidate (sum of
min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an
unbounded min-heap and truncated to LIMIT only at the end, so peak merge
memory scaled with the number of index files. On fragmented indexes this
is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904).

Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it
retains only the `limit` smallest-distance results, evicting the current
worst (heap root) in O(log k) once full; rejects are O(1). Peak merge
memory is now O(min(LIMIT, total_cardinality)), independent of file
count. A max-heap (worst at root) is required so the worst retained item
can be evicted cheaply; the search drains it back-to-front to restore
ascending (nearest-first) order. boundedHnswSearchLimits no longer needs
to return the sum-based heap capacity; the heap bound is simply the
result limit. The retained set is the exact global top-K regardless of
push order (order-independent invariant), so parallel workers can keep
pushing concurrently.

Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently
into a bound-10 heap -> retains exactly the 10 smallest);
TestBoundedHnswSearchLimits updated for the 2-value signature.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#25637) (matrixorigin#25716)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#25637

## What this PR does / why we need it:

The HNSW multi-index search pushed every per-file candidate (sum of
min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an
unbounded min-heap and truncated to LIMIT only at the end, so peak merge
memory scaled with the number of index files. On fragmented indexes this
is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904).

Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it
retains only the `limit` smallest-distance results, evicting the current
worst (heap root) in O(log k) once full; rejects are O(1). Peak merge
memory is now O(min(LIMIT, total_cardinality)), independent of file
count. A max-heap (worst at root) is required so the worst retained item
can be evicted cheaply; the search drains it back-to-front to restore
ascending (nearest-first) order. boundedHnswSearchLimits no longer needs
to return the sum-based heap capacity; the heap bound is simply the
result limit. The retained set is the exact global top-K regardless of
push order (order-independent invariant), so parallel workers can keep
pushing concurrently.

Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently
into a bound-10 heap -> retains exactly the 10 smallest);
TestBoundedHnswSearchLimits updated for the 2-value signature.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#25637) (matrixorigin#25716)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#25637

## What this PR does / why we need it:

The HNSW multi-index search pushed every per-file candidate (sum of
min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an
unbounded min-heap and truncated to LIMIT only at the end, so peak merge
memory scaled with the number of index files. On fragmented indexes this
is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904).

Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it
retains only the `limit` smallest-distance results, evicting the current
worst (heap root) in O(log k) once full; rejects are O(1). Peak merge
memory is now O(min(LIMIT, total_cardinality)), independent of file
count. A max-heap (worst at root) is required so the worst retained item
can be evicted cheaply; the search drains it back-to-front to restore
ascending (nearest-first) order. boundedHnswSearchLimits no longer needs
to return the sum-based heap capacity; the heap bound is simply the
result limit. The retained set is the exact global top-K regardless of
push order (order-independent invariant), so parallel workers can keep
pushing concurrently.

Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently
into a bound-10 heap -> retains exactly the 10 smallest);
TestBoundedHnswSearchLimits updated for the 2-value signature.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants