SQLite

Changes On Branch stmtstatus-memused
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch stmtstatus-memused Excluding Merge-Ins

This is equivalent to a diff from cb4c5c66 to b57d5104

2017-05-31
17:30
Add the SQLITE_STMTSTATUS_MEMUSED opcode to sqlite3_stmt_status() for finding the heap memory usage by a single prepared statement. (check-in: c26cf978 user: drh tags: trunk)
16:21
Add the SQLITE_STMTSTATUS_MEMUSED option for sqlite3_stmt_status() that reports the amount of heap memory used for a single prepared statement. (Closed-Leaf check-in: b57d5104 user: drh tags: stmtstatus-memused)
16:09
Add the experimental "stmts" virtual table for introspection of prepared statements. (check-in: cb4c5c66 user: drh tags: trunk)
13:45
Avoid allocating excess memory to the KeyInfo objects. (check-in: df785943 user: drh tags: trunk)

Changes to ext/misc/stmts.c.

94
95
96
97
98
99
100

101
102
103
104
105
106
107
108
109
110
111
#define STMTS_COLUMN_NCOL  2    /* Number of result columns */
#define STMTS_COLUMN_RO    3    /* True if read-only */
#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 */



  rc = sqlite3_declare_vtab(db,
     "CREATE TABLE x(ptr,sql,ncol,ro,busy,nscan,nsort,naidx,nstep)");
  if( rc==SQLITE_OK ){
    pNew = sqlite3_malloc( sizeof(*pNew) );
    *ppVtab = (sqlite3_vtab*)pNew;
    if( pNew==0 ) return SQLITE_NOMEM;
    memset(pNew, 0, sizeof(*pNew));
    pNew->db = db;
  }







>



|







94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#define STMTS_COLUMN_NCOL  2    /* Number of result columns */
#define STMTS_COLUMN_RO    3    /* True if read-only */
#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,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));
    pNew->db = db;
  }
182
183
184
185
186
187
188
189

190
191
192
193
194
195
196
    case STMTS_COLUMN_BUSY: {
      sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
      break;
    }
    case STMTS_COLUMN_NSCAN:
    case STMTS_COLUMN_NSORT:
    case STMTS_COLUMN_NAIDX:
    case STMTS_COLUMN_NSTEP: {

      sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
                      i-STMTS_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
      break;
    }
  }
  return SQLITE_OK;
}







|
>







183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    case STMTS_COLUMN_BUSY: {
      sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
      break;
    }
    case STMTS_COLUMN_NSCAN:
    case STMTS_COLUMN_NSORT:
    case STMTS_COLUMN_NAIDX:
    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;
    }
  }
  return SQLITE_OK;
}

Changes to src/sqlite.h.in.

7129
7130
7131
7132
7133
7134
7135




7136
7137
7138
7139
7140
7141
7142

7143
7144
7145
7146
7147
7148
7149
** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
** <dd>^This is the number of virtual machine operations executed
** 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.




** </dd>
** </dl>
*/
#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
#define SQLITE_STMTSTATUS_SORT              2
#define SQLITE_STMTSTATUS_AUTOINDEX         3
#define SQLITE_STMTSTATUS_VM_STEP           4


/*
** CAPI3REF: Custom Page Cache Object
**
** The sqlite3_pcache type is opaque.  It is implemented by
** the pluggable module.  The SQLite core has no knowledge of
** its size or internal structure and never deals with the







>
>
>
>







>







7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
** <dd>^This is the number of virtual machine operations executed
** 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]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
** <dd>^This is the approximate number of bytes of heap memory
** used to store the prepared statement.
** </dd>
** </dl>
*/
#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
** the pluggable module.  The SQLite core has no knowledge of
** its size or internal structure and never deals with the

Changes to src/vdbeapi.c.

1609
1610
1611
1612
1613
1614
1615










1616
1617

1618
1619
1620
1621
1622
1623
1624
  u32 v;
#ifdef SQLITE_ENABLE_API_ARMOR
  if( !pStmt ){
    (void)SQLITE_MISUSE_BKPT;
    return 0;
  }
#endif










  v = pVdbe->aCounter[op];
  if( resetFlag ) pVdbe->aCounter[op] = 0;

  return (int)v;
}

/*
** Return the SQL associated with a prepared statement
*/
const char *sqlite3_sql(sqlite3_stmt *pStmt){







>
>
>
>
>
>
>
>
>
>
|
|
>







1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
  u32 v;
#ifdef SQLITE_ENABLE_API_ARMOR
  if( !pStmt ){
    (void)SQLITE_MISUSE_BKPT;
    return 0;
  }
#endif
  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
*/
const char *sqlite3_sql(sqlite3_stmt *pStmt){