mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-05-18 13:55:18 +02:00
Finalize all cached vec0 stmts on commit (fixes #295)
vec0Sync only finalized stmtLatestChunk and the four stmtRowids* stmts. The IVF/DiskANN/vectors stmts persisted on the vtab until xDisconnect, which blocked sqlite3_close() (non-v2) with SQLITE_BUSY — the original mozStorage case from #295. The same leak also broke VACUUM with "SQL statements in progress" after any DiskANN operation. Switch vec0Sync to call vec0_free_resources, which already finalizes the full cache. Also fix a latent bug: the DiskANN block in vec0_free_resources was nested inside #if SQLITE_VEC_EXPERIMENTAL_IVF_ENABLE, so in the default build (DiskANN on, IVF off) those finalizes were unreachable even from xDisconnect/xDestroy. Split into two independent #if guards. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
8b81f40d1e
commit
fe941716ad
2 changed files with 38 additions and 23 deletions
27
sqlite-vec.c
27
sqlite-vec.c
|
|
@ -3695,13 +3695,15 @@ void vec0_free_resources(vec0_vtab *p) {
|
|||
sqlite3_finalize(p->stmtIvfRowidMapLookup[i]); p->stmtIvfRowidMapLookup[i] = NULL;
|
||||
sqlite3_finalize(p->stmtIvfRowidMapDelete[i]); p->stmtIvfRowidMapDelete[i] = NULL;
|
||||
sqlite3_finalize(p->stmtIvfCentroidsAll[i]); p->stmtIvfCentroidsAll[i] = NULL;
|
||||
}
|
||||
#endif
|
||||
#if SQLITE_VEC_ENABLE_DISKANN
|
||||
for (int i = 0; i < VEC0_MAX_VECTOR_COLUMNS; i++) {
|
||||
sqlite3_finalize(p->stmtDiskannNodeRead[i]); p->stmtDiskannNodeRead[i] = NULL;
|
||||
sqlite3_finalize(p->stmtDiskannNodeWrite[i]); p->stmtDiskannNodeWrite[i] = NULL;
|
||||
sqlite3_finalize(p->stmtDiskannNodeInsert[i]); p->stmtDiskannNodeInsert[i] = NULL;
|
||||
sqlite3_finalize(p->stmtVectorsRead[i]); p->stmtVectorsRead[i] = NULL;
|
||||
sqlite3_finalize(p->stmtVectorsInsert[i]); p->stmtVectorsInsert[i] = NULL;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
@ -10370,28 +10372,7 @@ static int vec0Begin(sqlite3_vtab *pVTab) {
|
|||
return SQLITE_OK;
|
||||
}
|
||||
static int vec0Sync(sqlite3_vtab *pVTab) {
|
||||
UNUSED_PARAMETER(pVTab);
|
||||
vec0_vtab *p = (vec0_vtab *)pVTab;
|
||||
if (p->stmtLatestChunk) {
|
||||
sqlite3_finalize(p->stmtLatestChunk);
|
||||
p->stmtLatestChunk = NULL;
|
||||
}
|
||||
if (p->stmtRowidsInsertRowid) {
|
||||
sqlite3_finalize(p->stmtRowidsInsertRowid);
|
||||
p->stmtRowidsInsertRowid = NULL;
|
||||
}
|
||||
if (p->stmtRowidsInsertId) {
|
||||
sqlite3_finalize(p->stmtRowidsInsertId);
|
||||
p->stmtRowidsInsertId = NULL;
|
||||
}
|
||||
if (p->stmtRowidsUpdatePosition) {
|
||||
sqlite3_finalize(p->stmtRowidsUpdatePosition);
|
||||
p->stmtRowidsUpdatePosition = NULL;
|
||||
}
|
||||
if (p->stmtRowidsGetChunkPosition) {
|
||||
sqlite3_finalize(p->stmtRowidsGetChunkPosition);
|
||||
p->stmtRowidsGetChunkPosition = NULL;
|
||||
}
|
||||
vec0_free_resources((vec0_vtab *)pVTab);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
static int vec0Commit(sqlite3_vtab *pVTab) {
|
||||
|
|
|
|||
34
tests/test-cache-finalize.py
Normal file
34
tests/test-cache-finalize.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
"""Regression tests for #295: vec0 must finalize cached prepared statements
|
||||
on every commit, not just the rowid subset.
|
||||
|
||||
Before the fix, `vec0Sync` only finalized `stmtLatestChunk` and the four
|
||||
`stmtRowids*` stmts; the DiskANN/IVF/vectors-read stmts persisted on the
|
||||
vtab indefinitely. Symptom: VACUUM after any DiskANN operation failed with
|
||||
"SQL statements in progress" because the cached stmts kept the connection
|
||||
busy. (The same leak also caused `sqlite3_close()` non-v2 to return
|
||||
SQLITE_BUSY — the original Firefox case in issue #295.)
|
||||
|
||||
A separate latent bug — the DiskANN finalize block in `vec0_free_resources`
|
||||
was nested inside `#if SQLITE_VEC_EXPERIMENTAL_IVF_ENABLE`, so even
|
||||
xDisconnect/xDestroy didn't finalize DiskANN stmts in the default build.
|
||||
"""
|
||||
from helpers import _f32
|
||||
|
||||
|
||||
def test_vacuum_after_diskann_inserts(db):
|
||||
db.execute(
|
||||
"create virtual table v using vec0("
|
||||
"a float[8] indexed by diskann(neighbor_quantizer=binary))"
|
||||
)
|
||||
for i in range(1, 11):
|
||||
db.execute("insert into v(rowid, a) values (?, ?)",
|
||||
(i, _f32([0.1 * i] * 8)))
|
||||
db.commit()
|
||||
db.execute("VACUUM")
|
||||
|
||||
|
||||
def test_vacuum_after_flat_inserts(db):
|
||||
db.execute("create virtual table v using vec0(a float[2])")
|
||||
db.execute("insert into v(rowid, a) values (1, ?)", (_f32([0.1, 0.2]),))
|
||||
db.commit()
|
||||
db.execute("VACUUM")
|
||||
Loading…
Add table
Add a link
Reference in a new issue