Skip to content

Commit

Permalink
properly lock schema in case when we work with attached DB
Browse files Browse the repository at this point in the history
  • Loading branch information
sivukhin committed Oct 29, 2024
1 parent badbbe9 commit 26ba2d9
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion libsql-sqlite3/src/vectorIndex.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ int vectorIndexSearch(
int *nWrites,
char **pzErrMsg
) {
int type, dims, k, rc;
int type, dims, k, rc, iDb = -1;
double kDouble;
const char *zIdxFullName;
char *zIdxDbSNameAlloc = NULL; // allocated managed schema name string - must be freed if not null
Expand Down Expand Up @@ -1059,6 +1059,18 @@ int vectorIndexSearch(
} else{
zIdxDbSName = zIdxDbSNameAlloc;
zIdxName = zIdxNameAlloc;
iDb = sqlite3FindDbName(db, zIdxDbSName);
if( iDb < 0 ){
*pzErrMsg = sqlite3_mprintf("vector index(search): unknown schema '%s'", zIdxDbSName);
rc = SQLITE_ERROR;
goto out;
}
// we need to hold mutex to protect schema against unwanted changes
// this code is necessary, otherwise sqlite3SchemaMutexHeld assert will fail
if( iDb !=1 ){
// not "main" DB which we already hold mutex for
sqlite3BtreeEnter(db->aDb[iDb].pBt);
}
}

if( vectorIndexGetParameters(db, zIdxDbSName, zIdxName, &idxParams) != 0 ){
Expand Down Expand Up @@ -1094,6 +1106,9 @@ int vectorIndexSearch(
}
sqlite3DbFree(db, zIdxNameAlloc);
sqlite3DbFree(db, zIdxDbSNameAlloc);
if( iDb >= 0 && iDb != 1 ){
sqlite3BtreeLeave(db->aDb[iDb].pBt);
}
return rc;
}

Expand Down

0 comments on commit 26ba2d9

Please sign in to comment.