aboutsummaryrefslogtreecommitdiff
path: root/src/vdbe.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vdbe.c')
-rw-r--r--src/vdbe.c75
1 files changed, 50 insertions, 25 deletions
diff --git a/src/vdbe.c b/src/vdbe.c
index 97f5ae696..128e81bdf 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -43,7 +43,7 @@
** in this file for details. If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
-** $Id: vdbe.c,v 1.827 2009/03/18 10:33:02 danielk1977 Exp $
+** $Id: vdbe.c,v 1.828 2009/03/23 17:11:27 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "vdbeInt.h"
@@ -4371,33 +4371,58 @@ case OP_CreateTable: { /* out2-prerelease */
** then runs the new virtual machine. It is thus a re-entrant opcode.
*/
case OP_ParseSchema: {
- char *zSql;
int iDb = pOp->p1;
- const char *zMaster;
- InitData initData;
-
assert( iDb>=0 && iDb<db->nDb );
- if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
- break;
+
+ /* If pOp->p2 is 0, then this opcode is being executed to read a
+ ** single row, for example the row corresponding to a new index
+ ** created by this VDBE, from the sqlite_master table. It only
+ ** does this if the corresponding in-memory schema is currently
+ ** loaded. Otherwise, the new index definition can be loaded along
+ ** with the rest of the schema when it is required.
+ **
+ ** Although the mutex on the BtShared object that corresponds to
+ ** database iDb (the database containing the sqlite_master table
+ ** read by this instruction) is currently held, it is necessary to
+ ** obtain the mutexes on all attached databases before checking if
+ ** the schema of iDb is loaded. This is because, at the start of
+ ** the sqlite3_exec() call below, SQLite will invoke
+ ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
+ ** iDb mutex may be temporarily released to avoid deadlock. If
+ ** this happens, then some other thread may delete the in-memory
+ ** schema of database iDb before the SQL statement runs. The schema
+ ** will not be reloaded becuase the db->init.busy flag is set. This
+ ** can result in a "no such table: sqlite_master" or "malformed
+ ** database schema" error being returned to the user.
+ */
+ assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
+ sqlite3BtreeEnterAll(db);
+ if( pOp->p2 || DbHasProperty(db, iDb, DB_SchemaLoaded) ){
+ const char *zMaster = SCHEMA_TABLE(iDb);
+ char *zSql;
+ InitData initData;
+ initData.db = db;
+ initData.iDb = pOp->p1;
+ initData.pzErrMsg = &p->zErrMsg;
+ zSql = sqlite3MPrintf(db,
+ "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
+ db->aDb[iDb].zName, zMaster, pOp->p4.z);
+ if( zSql==0 ){
+ rc = SQLITE_NOMEM;
+ }else{
+ (void)sqlite3SafetyOff(db);
+ assert( db->init.busy==0 );
+ db->init.busy = 1;
+ initData.rc = SQLITE_OK;
+ assert( !db->mallocFailed );
+ rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
+ if( rc==SQLITE_OK ) rc = initData.rc;
+ sqlite3DbFree(db, zSql);
+ db->init.busy = 0;
+ (void)sqlite3SafetyOn(db);
+ }
}
- zMaster = SCHEMA_TABLE(iDb);
- initData.db = db;
- initData.iDb = pOp->p1;
- initData.pzErrMsg = &p->zErrMsg;
- zSql = sqlite3MPrintf(db,
- "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
- db->aDb[iDb].zName, zMaster, pOp->p4.z);
- if( zSql==0 ) goto no_mem;
- (void)sqlite3SafetyOff(db);
- assert( db->init.busy==0 );
- db->init.busy = 1;
- initData.rc = SQLITE_OK;
- assert( !db->mallocFailed );
- rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
- if( rc==SQLITE_OK ) rc = initData.rc;
- sqlite3DbFree(db, zSql);
- db->init.busy = 0;
- (void)sqlite3SafetyOn(db);
+ sqlite3BtreeLeaveAll(db);
if( rc==SQLITE_NOMEM ){
goto no_mem;
}