sqlite-vec/tests/test-cache-finalize.py
Alex Garcia fe941716ad 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>
2026-05-17 23:49:05 -07:00

34 lines
1.3 KiB
Python

"""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")