Skip to content

fix(core): avoid correlated sqlite-vec orphan cleanup - #1612

Merged
phernandez merged 2 commits into
mainfrom
fix/1581-sqlite-vec-orphans
Sep 29, 2026
Merged

phernandez merged 2 commits into
mainfrom
fix/1581-sqlite-vec-orphans

Conversation

@phernandez

Copy link
Copy Markdown
Member

Supersedes #1584 by @mikemikimike, landed through an in-repo branch so CI runs (fork PRs get no CI). Their commit is cherry-picked with authorship and sign-off intact; it will be rebase-merged to keep it.

Fixes #1581

Why

SQLiteVecIndex.delete_orphans deleted stale vectors with a subquery that read search_vector_embeddings.source_hash from the outer DELETE. That makes it a correlated subquery, so SQLite re-ran it once per vec0 row. The work is quadratic in vault size and holds the write lock long enough that concurrent writers fail with database is locked.

What changed

  • @mikemikimike's fix: the subquery joins search_vector_embeddings by rowid and compares source_hash there. It deletes the same rows. Their test extends the reconciliation case (stale hash, other model, other index, other project, true orphan).
  • Follow-up commit: that test passes on the old SQL too, since both versions delete the same rows. A new test captures the executed DELETE and asserts its EXPLAIN QUERY PLAN has no CORRELATED step. It fails on main and passes with the fix. Also a comment on the join and a CHANGELOG entry.

Verified against current main (partitioned vec0)

The #1558 work added a project_id partition key to the vec0 table. The old SQL was unchanged on main, and the fix still applies. Benchmark on the partitioned schema, 5,000 rows, 64 dimensions:

plan time
main CORRELATED LIST SUBQUERY aborted at 30s
this PR LIST SUBQUERY, chunks by idx_search_vector_chunks_project_entity, vectors by rowid point lookup 0.026s, correct 43 rows deleted
  • just fast-check passes
  • just fast-test passes on the sqlite-vec, semantic, and vector-sync repository tests

🤖 Generated with Claude Code

https://claude.ai/code/session_01BF4TkgAJpVrgwzrhemkdig

mikemikimike and others added 2 commits September 28, 2026 21:28
The contributor's reconciliation test checks which rows the cleanup keeps,
and it passes on the old correlated DELETE too, since both statements
delete the same rows. Add a regression test that captures the executed
DELETE and asserts its EXPLAIN QUERY PLAN has no correlated subquery. It
fails on main and passes with the join.

Also add a comment on why the subquery joins by rowid, and a CHANGELOG
entry crediting @mikemikimike for #1581.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BF4TkgAJpVrgwzrhemkdig
Signed-off-by: phernandez <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T02:35:13.898908Z a2a9119 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@phernandez
phernandez merged commit 20b1523 into main Sep 29, 2026
34 checks passed
@phernandez
phernandez deleted the fix/1581-sqlite-vec-orphans branch September 29, 2026 03:13
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.

[BUG] sqlite-vec delete_orphans uses a correlated DELETE that stalls cleanup and blocks writers

2 participants