Skip to content

feat(storage): incremental HNSW updates on embedding writes - #53

Merged
Patel230 merged 1 commit into
mainfrom
feat/storage-hnsw-incremental
Aug 15, 2026
Merged

feat(storage): incremental HNSW updates on embedding writes#53
Patel230 merged 1 commit into
mainfrom
feat/storage-hnsw-incremental

Conversation

@Patel230

Copy link
Copy Markdown
Contributor

Phase 2 follow-up to the Phase 1 HNSW work (#50).

Problem: every embedding write required a full BuildHNSWIndex rebuild, so callers either searched a stale index or paid the O(n) rebuild cost.

Solution:

  • HNSWIndex.Upsert(ctx, store, model, nodeID, vec) — add or replace one vector: detach old links, re-insert, and upsert only the affected rows in embeddings_hnsw
  • HNSWIndex.Remove(ctx, store, model, nodeID) — detach with neighbor link pruning, entry-point repair (when the top-level node is removed), and row deletion
  • Store.SaveEmbedding applies Upsert to the built index for the embedding's model (best-effort; the embeddings table stays source of truth and a later build reconciles)
  • Store.DeleteEmbedding removes the graph row before the embedding row (FK order — this also fixes a latent constraint failure on the delete path)
  • Transactional embedding writes (WithTx) mark touched models dirty and drop the cache after commit, so the next SearchHNSW rebuilds from the table (mid-transaction index mutation would be unsafe across busy-retry re-runs)

Tests: incremental add/replace, delete detach + row cleanup, transactional invalidation, and incremental-vs-full-build top-5 parity. Full go test ./... green.

- HNSWIndex.Upsert/Remove: single-vector add/replace and detach with
  neighbor link pruning, entry-point repair, and per-node row upsert
  into embeddings_hnsw (no full rebuild)
- Store.SaveEmbedding applies Upsert to the built index for the model
  (best-effort; next Build reconciles); Store.DeleteEmbedding removes
  the graph row before the embedding row (FK order)
- Transactional embedding writes mark models dirty and drop the cache
  after commit, so the next SearchHNSW rebuilds from the embeddings
  table (the index cannot be safely mutated mid-transaction)
- Tests: incremental upsert/replace, delete detach, tx invalidation,
  incremental-vs-full-build parity
@Patel230
Patel230 merged commit da4568d into main Aug 15, 2026
12 checks passed
@Patel230
Patel230 deleted the feat/storage-hnsw-incremental branch August 15, 2026 10:52
Patel230 added a commit that referenced this pull request Aug 15, 2026
docs: mark Phase 2 items 1-3 shipped (PRs #53-55)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant