@@ -981,7 +981,7 @@ int vectorIndexSearch(
981
981
int * nWrites ,
982
982
char * * pzErrMsg
983
983
) {
984
- int type , dims , k , rc ;
984
+ int type , dims , k , rc , iDb = -1 ;
985
985
double kDouble ;
986
986
const char * zIdxFullName ;
987
987
char * zIdxDbSNameAlloc = NULL ; // allocated managed schema name string - must be freed if not null
@@ -1059,6 +1059,18 @@ int vectorIndexSearch(
1059
1059
} else {
1060
1060
zIdxDbSName = zIdxDbSNameAlloc ;
1061
1061
zIdxName = zIdxNameAlloc ;
1062
+ iDb = sqlite3FindDbName (db , zIdxDbSName );
1063
+ if ( iDb < 0 ){
1064
+ * pzErrMsg = sqlite3_mprintf ("vector index(search): unknown schema '%s'" , zIdxDbSName );
1065
+ rc = SQLITE_ERROR ;
1066
+ goto out ;
1067
+ }
1068
+ // we need to hold mutex to protect schema against unwanted changes
1069
+ // this code is necessary, otherwise sqlite3SchemaMutexHeld assert will fail
1070
+ if ( iDb != 1 ){
1071
+ // not "main" DB which we already hold mutex for
1072
+ sqlite3BtreeEnter (db -> aDb [iDb ].pBt );
1073
+ }
1062
1074
}
1063
1075
1064
1076
if ( vectorIndexGetParameters (db , zIdxDbSName , zIdxName , & idxParams ) != 0 ){
@@ -1094,6 +1106,9 @@ int vectorIndexSearch(
1094
1106
}
1095
1107
sqlite3DbFree (db , zIdxNameAlloc );
1096
1108
sqlite3DbFree (db , zIdxDbSNameAlloc );
1109
+ if ( iDb >= 0 && iDb != 1 ){
1110
+ sqlite3BtreeLeave (db -> aDb [iDb ].pBt );
1111
+ }
1097
1112
return rc ;
1098
1113
}
1099
1114
0 commit comments