Skip to content

fix(hnsw): keep the graph reachable after DELETE then INSERT (#30660); upgrade usearch to 2.26.5 - #30823

Merged
XuPeng-SH merged 10 commits into
matrixorigin:mainfrom
cpegeric:bug_30660
Oct 26, 2026
Merged

XuPeng-SH merged 10 commits into
matrixorigin:mainfrom
cpegeric:bug_30660

Conversation

@cpegeric

@cpegeric cpegeric commented Oct 22, 2026 •

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:

Fixes #30660

What this PR does / why we need it:

After an HNSW index is built, a DELETE followed by an INSERT can make other live rows unreachable through the index. In the issue's repro (1000 rows on a line, delete id 100, insert a far row) the index returns 901 rows, with ids 1–99 missing. This persists after the new model is published and after a restart; only FORCE_SYNC (a full rebuild) restores it.

Root cause (usearch, not ISCP). The CDC path is correct: HnswSync.sequentialUpdate applies Remove(100) then Add(2001). The same two calls on bare usearch, with no SQL or ISCP involved, give the same 901 result. In usearch, remove() only tombstones the node and queues its slot. The next add() reuses that slot, and index_gt::update() rewrites the node in place at the new vector's position, clearing its neighbour lists without repairing anything (upstream leaves a TODO there). Whatever was reachable only through the removed node is cut off. Points on a line make the removed node the only bridge, so the loss is a whole range; on realistic data the same mechanism causes partial recall loss. Full trace: #30660 (comment)

Fix. The vendored usearch is upgraded from usearch-2.25.3.tar.gz to usearch-2.26.5.tar.gz, built with git archive (same layout as before) from cpegeric/usearch matrixone_20261022. That branch is upstream v2.26.5 plus the existing concurrent_add (#735) fix plus fix_delete_20261022, which cherry-picks cleanly onto upstream for its own upstream PR. Upstream v2.26.5 still has this bug: the new line-reachability test fails on unmodified v2.26.5.

Upgrading to 2.26.5 also brings upstream #769, which rehashes tombstones in the key-lookup table. In v2.25.3, remove() could spin at 100% CPU under heavy delete/insert churn.

Upstream #777 changes Reserve(n): it now allocates exactly n slots, where it used to round up to the key-lookup table's power-of-two size (about +37%, e.g. Reserve(1000) gave 1365). MO always reserves its real capacity: MaxCapacity on build, which the build path never exceeds, and IndexCapacity on a sync load, which defaults to 1,000,000. Updates reuse the slot their own Remove freed. So no production path depended on that extra room. Only TestModel, which loaded with IndexCapacity 0, relied on it, and it now sets a real capacity.

  • Repair: before a removed node's slot is reused, update() relinks the node's old neighbours on every level. Each neighbour drops its link to the reused slot and re-picks its neighbours, using the existing heuristic, from its own list plus the removed node's other neighbours. Nodes are locked one at a time, as in form_reverse_links_. The snapshot uses a reused per-thread buffer, and out-of-memory is reported as an error.
  • Concurrency: index_dense_gt::add_() serializes adds that reuse a slot, because the in-place rewrite is not safe against concurrent rewrites of neighbouring nodes. Fresh adds and searches stay concurrent. MO's CDC update path is already sequential.

No memory growth: removed slots are still reused. Apart from the usearch upgrade, the only MO change is the TestModel capacity in its test setup.

Tests

Validated on 2.26.5:

  • Unit tests: all 43 pkg/vectorindex/... packages pass.
  • BVT: vector_hnsw_delete_insert passes 44/44. pessimistic_transaction/vector has 0 failures, vector passes 3301/3301, and branch_hnsw_async_refresh, fulltext2_hnsw_clone, index_cache_size_bounds, clone and branch_multi_index_restore_init have 0 failures.

The results below were first measured on the 2.25.3-based tarball:

  • Go: new TestRemoveAddKeepsGraphReachable (pkg/vectorindex/hnsw) goes through HnswModel: remove 100, remove 500, and save→load then remove, each followed by a far add, asserting every live key is returned. All three fail on the previous tarball (901 / 501 rows) and pass with this one. All pkg/vectorindex/hnsw/... and pkg/vectorindex/usearchex packages pass.
  • usearch C++ (cpp/test.cpp in the fork): the remove + far add on a line test for slot32_t and uint40_t fails without the fix. Remove/add churn rounds, single- and multi-threaded, assert every live key finds itself in an exhaustive search. On the merged branch, these and the Data lost after restart server #735 concurrent-add reachability test pass 10/10 runs with internal assertions enabled.
  • BVT: new pessimistic_transaction/vector/vector_hnsw_delete_insert. It mirrors the issue (single-threaded build + FORCE_SYNC, DELETE id 100 + far INSERT, then DELETE id 500 + another far INSERT) and waits for the published model's checksum to change before querying. With this tarball: 44/44, and the index returns all 1000 rows, every id below the deleted one, and the true nearest row. On the previous tarball the same case returns 901 / 502 rows, none of the ids below the deleted one, and id 101 as the nearest row to [1,1,2]. The pessimistic_transaction/vector (703 pass, 0 failed) and vector suites stay green. The one suite-only failure, in IVF vector_filtered_search_knobs, passes 51/51 when run alone and doesn't involve HNSW.
  • Live SQL, using the issue's exact repro after waiting for the async model to be published: the index returns 1000/1000 for query vectors [1,1,2], [777.2,1,2] and [1000,1,2], and 1000/1000 in the delete-id-500 variant (was 501).

🤖 Generated with Claude Code

…rigin#30660)

A CDC DELETE followed by INSERT calls usearch Remove then Add. Add reuses the
removed node's slot and rewrites the node in place at the new vector's position,
dropping its neighbor links without repairing them. Everything reachable only
through the removed node became unreachable: on 1000 points on a line, deleting
id 100 and inserting a far row left the index returning 901 rows (ids 1..99
missing) even after the new model was published and after a restart.

Vendor the usearch fix (cpegeric/usearch matrixone_20261022 = concurrent_add +
fix_delete_20261022): before reusing a removed slot, relink the removed node's
old neighbors to each other on every level, and serialize slot-reusing adds.

Add TestRemoveAddKeepsGraphReachable through HnswModel (remove 100, remove 500,
and save/load then remove); it fails on the previous tarball and passes now.

Co-Authored-By: Claude Opus 5.5 <[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 →

Mirrors the issue: 1000 rows on a line, single-threaded build + FORCE_SYNC, then
DELETE id 100 + INSERT a far row (and later id 500 + another far row). Waits for
the published model checksum to change before querying, then asserts the index
returns all 1000 rows, every id below the deleted one, and the true nearest row.

On the previous usearch tarball the same case returns 901 / 502 rows, no ids below
the deleted one, and id 101 as the nearest row to [1,1,2].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mergify

mergify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

🛑 Queue command has been cancelled

@mergify

mergify Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

queue

🛑 The queue request has been cancelled: new commits were pushed

Details

@cpegeric pushed to this pull request after queue was requested. Review the new commits and request it again.

@mergify

mergify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-26 14:12 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-26 16:52 UTC · at b3b1e735af9c7dbcbe327b78a05e11dd57838ebf

This pull request spent 2 hours 40 minutes 22 seconds in the queue, with no time running CI.

Reason

Pull request #30823 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.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

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

  • Requeue this pull request

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: HNSW delete-then-insert persists short KNN results after async model update

5 participants