Index: ext/misc/stmts.c ================================================================== --- ext/misc/stmts.c +++ ext/misc/stmts.c @@ -96,14 +96,15 @@ #define STMTS_COLUMN_BUSY 4 /* True if currently busy */ #define STMTS_COLUMN_NSCAN 5 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */ #define STMTS_COLUMN_NSORT 6 /* SQLITE_STMTSTATUS_SORT */ #define STMTS_COLUMN_NAIDX 7 /* SQLITE_STMTSTATUS_AUTOINDEX */ #define STMTS_COLUMN_NSTEP 8 /* SQLITE_STMTSTATUS_VM_STEP */ +#define STMTS_COLUMN_MEM 9 /* SQLITE_STMTSTATUS_MEMUSED */ rc = sqlite3_declare_vtab(db, - "CREATE TABLE x(ptr,sql,ncol,ro,busy,nscan,nsort,naidx,nstep)"); + "CREATE TABLE x(ptr,sql,ncol,ro,busy,nscan,nsort,naidx,nstep,mem)"); if( rc==SQLITE_OK ){ pNew = sqlite3_malloc( sizeof(*pNew) ); *ppVtab = (sqlite3_vtab*)pNew; if( pNew==0 ) return SQLITE_NOMEM; memset(pNew, 0, sizeof(*pNew)); @@ -184,11 +185,12 @@ break; } case STMTS_COLUMN_NSCAN: case STMTS_COLUMN_NSORT: case STMTS_COLUMN_NAIDX: - case STMTS_COLUMN_NSTEP: { + case STMTS_COLUMN_NSTEP: + case STMTS_COLUMN_MEM: { sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt, i-STMTS_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0)); break; } } Index: src/sqlite.h.in ================================================================== --- src/sqlite.h.in +++ src/sqlite.h.in @@ -7131,17 +7131,22 @@ ** by the prepared statement if that number is less than or equal ** to 2147483647. The number of virtual machine operations can be ** used as a proxy for the total work done by the prepared statement. ** If the number of virtual machine operations exceeds 2147483647 ** then the value returned by this statement status code is undefined. +** +** [[SQLITE_STMTSTATUS_MEMUSED]]
SQLITE_STMTSTATUS_MEMUSED
+**
^This is the approximate number of bytes of heap memory +** used to store the prepared statement. **
** */ #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 #define SQLITE_STMTSTATUS_SORT 2 #define SQLITE_STMTSTATUS_AUTOINDEX 3 #define SQLITE_STMTSTATUS_VM_STEP 4 +#define SQLITE_STMTSTATUS_MEMUSED 5 /* ** CAPI3REF: Custom Page Cache Object ** ** The sqlite3_pcache type is opaque. It is implemented by Index: src/vdbeapi.c ================================================================== --- src/vdbeapi.c +++ src/vdbeapi.c @@ -1611,12 +1611,23 @@ if( !pStmt ){ (void)SQLITE_MISUSE_BKPT; return 0; } #endif - v = pVdbe->aCounter[op]; - if( resetFlag ) pVdbe->aCounter[op] = 0; + if( op==SQLITE_STMTSTATUS_MEMUSED ){ + sqlite3 *db = pVdbe->db; + sqlite3_mutex_enter(db->mutex); + v = 0; + db->pnBytesFreed = (int*)&v; + sqlite3VdbeClearObject(db, pVdbe); + sqlite3DbFree(db, pVdbe); + db->pnBytesFreed = 0; + sqlite3_mutex_leave(db->mutex); + }else{ + v = pVdbe->aCounter[op]; + if( resetFlag ) pVdbe->aCounter[op] = 0; + } return (int)v; } /* ** Return the SQL associated with a prepared statement