blob: b3523e01affb4d463e1bf150d75d61c81cf005a6 [file] [log] [blame]
dan599e9d22010-07-12 08:39:37 +00001/*
2** 2010 July 12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
drh3a0f13f2010-07-12 16:47:48 +000012**
13** This file contains an implementation of the "dbstat" virtual table.
14**
drhad84bd82019-11-19 14:01:51 +000015** The dbstat virtual table is used to extract low-level storage
drh3a0f13f2010-07-12 16:47:48 +000016** information from an SQLite database in order to implement the
17** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
18** for an example implementation.
drha46a4a62015-09-08 21:12:53 +000019**
20** Additional information is available on the "dbstat.html" page of the
21** official SQLite documentation.
dan599e9d22010-07-12 08:39:37 +000022*/
23
drh8755a5f2015-05-12 19:10:18 +000024#include "sqliteInt.h" /* Requires access to internal data structures */
drh1a4a6802015-05-04 18:31:09 +000025#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
drh59ba6e82015-05-05 10:46:02 +000026 && !defined(SQLITE_OMIT_VIRTUALTABLE)
dan6d3eb822010-07-12 18:12:41 +000027
dan599e9d22010-07-12 08:39:37 +000028/*
29** Page paths:
30**
31** The value of the 'path' column describes the path taken from the
32** root-node of the b-tree structure to each page. The value of the
33** root-node path is '/'.
34**
35** The value of the path for the left-most child page of the root of
drh3a0f13f2010-07-12 16:47:48 +000036** a b-tree is '/000/'. (Btrees store content ordered from left to right
37** so the pages to the left have smaller keys than the pages to the right.)
38** The next to left-most child of the root page is
dan599e9d22010-07-12 08:39:37 +000039** '/001', and so on, each sibling page identified by a 3-digit hex
drh3a0f13f2010-07-12 16:47:48 +000040** value. The children of the 451st left-most sibling have paths such
dan599e9d22010-07-12 08:39:37 +000041** as '/1c2/000/, '/1c2/001/' etc.
42**
43** Overflow pages are specified by appending a '+' character and a
44** six-digit hexadecimal value to the path to the cell they are linked
45** from. For example, the three overflow pages in a chain linked from
46** the left-most cell of the 450th child of the root page are identified
47** by the paths:
48**
49** '/1c2/000+000000' // First page in overflow chain
50** '/1c2/000+000001' // Second page in overflow chain
51** '/1c2/000+000002' // Third page in overflow chain
52**
53** If the paths are sorted using the BINARY collation sequence, then
54** the overflow pages associated with a cell will appear earlier in the
55** sort-order than its child page:
56**
drh3a0f13f2010-07-12 16:47:48 +000057** '/1c2/000/' // Left-most child of 451st child of root
dan599e9d22010-07-12 08:39:37 +000058*/
drhad84bd82019-11-19 14:01:51 +000059#define VTAB_SCHEMA \
60 "CREATE TABLE xx( " \
61 " name TEXT," /* 0 Name of table or index */ \
62 " path TEXT," /* 1 Path to page from root */ \
63 " pageno INTEGER," /* 2 Page number */ \
64 " pagetype TEXT," /* 3 'internal', 'leaf' or 'overflow' */ \
65 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */ \
66 " payload INTEGER," /* 5 Bytes of payload on this page */ \
67 " unused INTEGER," /* 6 Bytes of unused space on this page */ \
68 " mx_payload INTEGER," /* 7 Largest payload size of all cells */ \
69 " pgoffset INTEGER," /* 8 Offset of page in file */ \
70 " pgsize INTEGER," /* 9 Size of the page */ \
71 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */ \
72 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */ \
dan599e9d22010-07-12 08:39:37 +000073 ");"
74
drhad84bd82019-11-19 14:01:51 +000075/* Forward reference to data structured used in this module */
dan599e9d22010-07-12 08:39:37 +000076typedef struct StatTable StatTable;
77typedef struct StatCursor StatCursor;
78typedef struct StatPage StatPage;
79typedef struct StatCell StatCell;
80
drhad84bd82019-11-19 14:01:51 +000081/* Size information for a single cell within a btree page */
dan599e9d22010-07-12 08:39:37 +000082struct StatCell {
83 int nLocal; /* Bytes of local payload */
84 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
85 int nOvfl; /* Entries in aOvfl[] */
86 u32 *aOvfl; /* Array of overflow page numbers */
87 int nLastOvfl; /* Bytes of payload on final overflow page */
88 int iOvfl; /* Iterates through aOvfl[] */
89};
90
drhad84bd82019-11-19 14:01:51 +000091/* Size information for a single btree page */
dan599e9d22010-07-12 08:39:37 +000092struct StatPage {
drhad84bd82019-11-19 14:01:51 +000093 u32 iPgno; /* Page number */
94 DbPage *pPg; /* Page content */
95 int iCell; /* Current cell */
dan599e9d22010-07-12 08:39:37 +000096
97 char *zPath; /* Path to this page */
98
99 /* Variables populated by statDecodePage(): */
100 u8 flags; /* Copy of flags byte */
101 int nCell; /* Number of cells on page */
102 int nUnused; /* Number of unused bytes on page */
103 StatCell *aCell; /* Array of parsed cells */
104 u32 iRightChildPg; /* Right-child page number (or 0) */
drhad84bd82019-11-19 14:01:51 +0000105 int nMxPayload; /* Largest payload of any cell on the page */
dan599e9d22010-07-12 08:39:37 +0000106};
107
drhad84bd82019-11-19 14:01:51 +0000108/* The cursor for scanning the dbstat virtual table */
dan599e9d22010-07-12 08:39:37 +0000109struct StatCursor {
drhad84bd82019-11-19 14:01:51 +0000110 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
dan599e9d22010-07-12 08:39:37 +0000111 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
112 int isEof; /* After pStmt has returned SQLITE_DONE */
drha46a4a62015-09-08 21:12:53 +0000113 int iDb; /* Schema used for this query */
drhad84bd82019-11-19 14:01:51 +0000114 int isAgg; /* Aggregate results for each table */
dan599e9d22010-07-12 08:39:37 +0000115
drhad84bd82019-11-19 14:01:51 +0000116 StatPage aPage[32]; /* Pages in path to current page */
dan599e9d22010-07-12 08:39:37 +0000117 int iPage; /* Current entry in aPage[] */
118
119 /* Values to return. */
120 char *zName; /* Value of 'name' column */
121 char *zPath; /* Value of 'path' column */
122 u32 iPageno; /* Value of 'pageno' column */
123 char *zPagetype; /* Value of 'pagetype' column */
124 int nCell; /* Value of 'ncell' column */
125 int nPayload; /* Value of 'payload' column */
126 int nUnused; /* Value of 'unused' column */
127 int nMxPayload; /* Value of 'mx_payload' column */
drh4c9f1292011-09-28 00:50:14 +0000128 i64 iOffset; /* Value of 'pgOffset' column */
129 int szPage; /* Value of 'pgSize' column */
dan599e9d22010-07-12 08:39:37 +0000130};
131
drhad84bd82019-11-19 14:01:51 +0000132/* An instance of the DBSTAT virtual table */
dan599e9d22010-07-12 08:39:37 +0000133struct StatTable {
drhad84bd82019-11-19 14:01:51 +0000134 sqlite3_vtab base; /* base class. MUST BE FIRST! */
135 sqlite3 *db; /* Database connection that owns this vtab */
drh857df262015-05-07 14:41:56 +0000136 int iDb; /* Index of database to analyze */
dan599e9d22010-07-12 08:39:37 +0000137};
138
139#ifndef get2byte
140# define get2byte(x) ((x)[0]<<8 | (x)[1])
141#endif
142
143/*
drhad84bd82019-11-19 14:01:51 +0000144** Connect to or create a new DBSTAT virtual table.
dan599e9d22010-07-12 08:39:37 +0000145*/
146static int statConnect(
147 sqlite3 *db,
148 void *pAux,
149 int argc, const char *const*argv,
150 sqlite3_vtab **ppVtab,
151 char **pzErr
152){
dan995f8b92015-04-27 19:53:55 +0000153 StatTable *pTab = 0;
154 int rc = SQLITE_OK;
drh857df262015-05-07 14:41:56 +0000155 int iDb;
dan599e9d22010-07-12 08:39:37 +0000156
drh857df262015-05-07 14:41:56 +0000157 if( argc>=4 ){
drh40aced52016-01-22 17:48:09 +0000158 Token nm;
159 sqlite3TokenInit(&nm, (char*)argv[3]);
160 iDb = sqlite3FindDb(db, &nm);
drh857df262015-05-07 14:41:56 +0000161 if( iDb<0 ){
162 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
163 return SQLITE_ERROR;
164 }
165 }else{
166 iDb = 0;
167 }
dan995f8b92015-04-27 19:53:55 +0000168 rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
169 if( rc==SQLITE_OK ){
drhf3cdcdc2015-04-29 16:50:28 +0000170 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
mistachkinfad30392016-02-13 23:43:46 +0000171 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
dan995f8b92015-04-27 19:53:55 +0000172 }
dan599e9d22010-07-12 08:39:37 +0000173
dan995f8b92015-04-27 19:53:55 +0000174 assert( rc==SQLITE_OK || pTab==0 );
175 if( rc==SQLITE_OK ){
176 memset(pTab, 0, sizeof(StatTable));
177 pTab->db = db;
drh857df262015-05-07 14:41:56 +0000178 pTab->iDb = iDb;
dan995f8b92015-04-27 19:53:55 +0000179 }
180
181 *ppVtab = (sqlite3_vtab*)pTab;
182 return rc;
dan599e9d22010-07-12 08:39:37 +0000183}
184
185/*
drhad84bd82019-11-19 14:01:51 +0000186** Disconnect from or destroy the DBSTAT virtual table.
dan599e9d22010-07-12 08:39:37 +0000187*/
188static int statDisconnect(sqlite3_vtab *pVtab){
189 sqlite3_free(pVtab);
190 return SQLITE_OK;
191}
192
193/*
drhad84bd82019-11-19 14:01:51 +0000194** Compute the best query strategy and return the result in idxNum.
drha46a4a62015-09-08 21:12:53 +0000195**
drhad84bd82019-11-19 14:01:51 +0000196** idxNum-Bit Meaning
197** ---------- ----------------------------------------------
198** 0x01 There is a schema=? term in the WHERE clause
199** 0x02 There is a name=? term in the WHERE clause
200** 0x04 There is an aggregate=? term in the WHERE clause
201** 0x08 Output should be ordered by name and path
dan599e9d22010-07-12 08:39:37 +0000202*/
203static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
drha46a4a62015-09-08 21:12:53 +0000204 int i;
drhad84bd82019-11-19 14:01:51 +0000205 int iSchema = -1;
206 int iName = -1;
207 int iAgg = -1;
drha46a4a62015-09-08 21:12:53 +0000208
drha46a4a62015-09-08 21:12:53 +0000209 /* Look for a valid schema=? constraint. If found, change the idxNum to
210 ** 1 and request the value of that constraint be sent to xFilter. And
211 ** lower the cost estimate to encourage the constrained version to be
212 ** used.
213 */
214 for(i=0; i<pIdxInfo->nConstraint; i++){
drh20b3fc42018-11-16 20:18:07 +0000215 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
drhad84bd82019-11-19 14:01:51 +0000216 if( pIdxInfo->aConstraint[i].usable==0 ){
217 /* Force DBSTAT table should always be the right-most table in a join */
218 return SQLITE_CONSTRAINT;
219 }
220 switch( pIdxInfo->aConstraint[i].iColumn ){
221 case 0: { /* name */
222 iName = i;
223 break;
224 }
225 case 10: { /* schema */
226 iSchema = i;
227 break;
228 }
229 case 11: { /* aggregate */
230 iAgg = i;
231 break;
232 }
233 }
drha46a4a62015-09-08 21:12:53 +0000234 }
drhad84bd82019-11-19 14:01:51 +0000235 i = 0;
236 if( iSchema>=0 ){
237 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
238 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
239 pIdxInfo->idxNum |= 0x01;
240 }
241 if( iName>=0 ){
242 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
243 pIdxInfo->aConstraintUsage[iName].omit = 1;
244 pIdxInfo->idxNum |= 0x02;
245 }
246 if( iAgg>=0 ){
247 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
248 pIdxInfo->aConstraintUsage[iAgg].omit = 1;
249 pIdxInfo->idxNum |= 0x04;
250 }
251 pIdxInfo->estimatedCost = 1.0;
dan599e9d22010-07-12 08:39:37 +0000252
253 /* Records are always returned in ascending order of (name, path).
254 ** If this will satisfy the client, set the orderByConsumed flag so that
255 ** SQLite does not do an external sort.
256 */
257 if( ( pIdxInfo->nOrderBy==1
258 && pIdxInfo->aOrderBy[0].iColumn==0
259 && pIdxInfo->aOrderBy[0].desc==0
260 ) ||
261 ( pIdxInfo->nOrderBy==2
262 && pIdxInfo->aOrderBy[0].iColumn==0
263 && pIdxInfo->aOrderBy[0].desc==0
264 && pIdxInfo->aOrderBy[1].iColumn==1
265 && pIdxInfo->aOrderBy[1].desc==0
266 )
267 ){
268 pIdxInfo->orderByConsumed = 1;
drhad84bd82019-11-19 14:01:51 +0000269 pIdxInfo->idxNum |= 0x08;
dan599e9d22010-07-12 08:39:37 +0000270 }
271
dan599e9d22010-07-12 08:39:37 +0000272 return SQLITE_OK;
273}
274
275/*
276** Open a new statvfs cursor.
277*/
278static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
279 StatTable *pTab = (StatTable *)pVTab;
280 StatCursor *pCsr;
dan599e9d22010-07-12 08:39:37 +0000281
drhf3cdcdc2015-04-29 16:50:28 +0000282 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
dan995f8b92015-04-27 19:53:55 +0000283 if( pCsr==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000284 return SQLITE_NOMEM_BKPT;
dan995f8b92015-04-27 19:53:55 +0000285 }else{
286 memset(pCsr, 0, sizeof(StatCursor));
287 pCsr->base.pVtab = pVTab;
drha46a4a62015-09-08 21:12:53 +0000288 pCsr->iDb = pTab->iDb;
dan599e9d22010-07-12 08:39:37 +0000289 }
290
291 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
drha46a4a62015-09-08 21:12:53 +0000292 return SQLITE_OK;
dan599e9d22010-07-12 08:39:37 +0000293}
294
drh5aa20372018-10-29 18:33:42 +0000295static void statClearCells(StatPage *p){
dan599e9d22010-07-12 08:39:37 +0000296 int i;
dan995f8b92015-04-27 19:53:55 +0000297 if( p->aCell ){
298 for(i=0; i<p->nCell; i++){
299 sqlite3_free(p->aCell[i].aOvfl);
300 }
301 sqlite3_free(p->aCell);
dan599e9d22010-07-12 08:39:37 +0000302 }
drh5aa20372018-10-29 18:33:42 +0000303 p->nCell = 0;
304 p->aCell = 0;
305}
306
307static void statClearPage(StatPage *p){
308 statClearCells(p);
dan599e9d22010-07-12 08:39:37 +0000309 sqlite3PagerUnref(p->pPg);
dan599e9d22010-07-12 08:39:37 +0000310 sqlite3_free(p->zPath);
311 memset(p, 0, sizeof(StatPage));
312}
313
314static void statResetCsr(StatCursor *pCsr){
315 int i;
316 sqlite3_reset(pCsr->pStmt);
317 for(i=0; i<ArraySize(pCsr->aPage); i++){
318 statClearPage(&pCsr->aPage[i]);
319 }
320 pCsr->iPage = 0;
321 sqlite3_free(pCsr->zPath);
322 pCsr->zPath = 0;
drha46a4a62015-09-08 21:12:53 +0000323 pCsr->isEof = 0;
dan599e9d22010-07-12 08:39:37 +0000324}
325
326/*
327** Close a statvfs cursor.
328*/
329static int statClose(sqlite3_vtab_cursor *pCursor){
330 StatCursor *pCsr = (StatCursor *)pCursor;
331 statResetCsr(pCsr);
332 sqlite3_finalize(pCsr->pStmt);
333 sqlite3_free(pCsr);
334 return SQLITE_OK;
335}
336
drhad84bd82019-11-19 14:01:51 +0000337/*
338** For a single cell on a btree page, compute the number of bytes of
339** content (payload) stored on that page. That is to say, compute the
340** number of bytes of content not found on overflow pages.
341*/
342static int getLocalPayload(
dan599e9d22010-07-12 08:39:37 +0000343 int nUsable, /* Usable bytes per page */
344 u8 flags, /* Page flags */
drhad84bd82019-11-19 14:01:51 +0000345 int nTotal /* Total record (payload) size */
dan599e9d22010-07-12 08:39:37 +0000346){
347 int nLocal;
348 int nMinLocal;
349 int nMaxLocal;
350
351 if( flags==0x0D ){ /* Table leaf node */
352 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
353 nMaxLocal = nUsable - 35;
354 }else{ /* Index interior and leaf nodes */
355 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
356 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
357 }
358
359 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
360 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
drhad84bd82019-11-19 14:01:51 +0000361 return nLocal;
dan599e9d22010-07-12 08:39:37 +0000362}
363
364static int statDecodePage(Btree *pBt, StatPage *p){
365 int nUnused;
366 int iOff;
367 int nHdr;
368 int isLeaf;
drh4c9f1292011-09-28 00:50:14 +0000369 int szPage;
dan599e9d22010-07-12 08:39:37 +0000370
371 u8 *aData = sqlite3PagerGetData(p->pPg);
372 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
373
374 p->flags = aHdr[0];
drhf8881582018-10-29 16:07:10 +0000375 if( p->flags==0x0A || p->flags==0x0D ){
376 isLeaf = 1;
377 nHdr = 8;
378 }else if( p->flags==0x05 || p->flags==0x02 ){
379 isLeaf = 0;
380 nHdr = 12;
381 }else{
382 goto statPageIsCorrupt;
383 }
384 if( p->iPgno==1 ) nHdr += 100;
dan599e9d22010-07-12 08:39:37 +0000385 p->nCell = get2byte(&aHdr[3]);
386 p->nMxPayload = 0;
drhf8881582018-10-29 16:07:10 +0000387 szPage = sqlite3BtreeGetPageSize(pBt);
dan599e9d22010-07-12 08:39:37 +0000388
389 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
390 nUnused += (int)aHdr[7];
391 iOff = get2byte(&aHdr[1]);
392 while( iOff ){
drhf8881582018-10-29 16:07:10 +0000393 int iNext;
394 if( iOff>=szPage ) goto statPageIsCorrupt;
dan599e9d22010-07-12 08:39:37 +0000395 nUnused += get2byte(&aData[iOff+2]);
drhf8881582018-10-29 16:07:10 +0000396 iNext = get2byte(&aData[iOff]);
397 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
398 iOff = iNext;
dan599e9d22010-07-12 08:39:37 +0000399 }
400 p->nUnused = nUnused;
401 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
402
403 if( p->nCell ){
404 int i; /* Used to iterate through cells */
drhad0961b2015-02-21 00:19:25 +0000405 int nUsable; /* Usable bytes per page */
dan599e9d22010-07-12 08:39:37 +0000406
drhad0961b2015-02-21 00:19:25 +0000407 sqlite3BtreeEnter(pBt);
408 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
409 sqlite3BtreeLeave(pBt);
drhf3cdcdc2015-04-29 16:50:28 +0000410 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
mistachkinfad30392016-02-13 23:43:46 +0000411 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
dan599e9d22010-07-12 08:39:37 +0000412 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
413
414 for(i=0; i<p->nCell; i++){
415 StatCell *pCell = &p->aCell[i];
416
417 iOff = get2byte(&aData[nHdr+i*2]);
drhf8881582018-10-29 16:07:10 +0000418 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
dan599e9d22010-07-12 08:39:37 +0000419 if( !isLeaf ){
420 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
421 iOff += 4;
422 }
423 if( p->flags==0x05 ){
424 /* A table interior node. nPayload==0. */
425 }else{
426 u32 nPayload; /* Bytes of payload total (local+overflow) */
427 int nLocal; /* Bytes of payload stored locally */
428 iOff += getVarint32(&aData[iOff], nPayload);
429 if( p->flags==0x0D ){
430 u64 dummy;
431 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
432 }
drh7da5fcb2012-03-30 14:59:43 +0000433 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
drhad84bd82019-11-19 14:01:51 +0000434 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
drhf8881582018-10-29 16:07:10 +0000435 if( nLocal<0 ) goto statPageIsCorrupt;
dan599e9d22010-07-12 08:39:37 +0000436 pCell->nLocal = nLocal;
drh26d8b0f2012-05-11 15:53:18 +0000437 assert( nPayload>=(u32)nLocal );
dan599e9d22010-07-12 08:39:37 +0000438 assert( nLocal<=(nUsable-35) );
drh7da5fcb2012-03-30 14:59:43 +0000439 if( nPayload>(u32)nLocal ){
dan599e9d22010-07-12 08:39:37 +0000440 int j;
441 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
drhf9dc5f72018-11-13 20:21:52 +0000442 if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
dan599e9d22010-07-12 08:39:37 +0000443 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
444 pCell->nOvfl = nOvfl;
drhf3cdcdc2015-04-29 16:50:28 +0000445 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
mistachkinfad30392016-02-13 23:43:46 +0000446 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
dan599e9d22010-07-12 08:39:37 +0000447 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
448 for(j=1; j<nOvfl; j++){
449 int rc;
450 u32 iPrev = pCell->aOvfl[j-1];
451 DbPage *pPg = 0;
drh9584f582015-11-04 20:22:37 +0000452 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
dan599e9d22010-07-12 08:39:37 +0000453 if( rc!=SQLITE_OK ){
454 assert( pPg==0 );
455 return rc;
456 }
457 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
458 sqlite3PagerUnref(pPg);
459 }
460 }
461 }
462 }
463 }
464
465 return SQLITE_OK;
drhf8881582018-10-29 16:07:10 +0000466
467statPageIsCorrupt:
468 p->flags = 0;
drh5aa20372018-10-29 18:33:42 +0000469 statClearCells(p);
drhf8881582018-10-29 16:07:10 +0000470 return SQLITE_OK;
dan599e9d22010-07-12 08:39:37 +0000471}
472
dan599e9d22010-07-12 08:39:37 +0000473/*
dan2cf7e0a2011-10-05 17:36:27 +0000474** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
475** the current value of pCsr->iPageno.
476*/
477static void statSizeAndOffset(StatCursor *pCsr){
478 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
drh857df262015-05-07 14:41:56 +0000479 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
dan2cf7e0a2011-10-05 17:36:27 +0000480 Pager *pPager = sqlite3BtreePager(pBt);
481 sqlite3_file *fd;
482 sqlite3_int64 x[2];
483
484 /* The default page size and offset */
485 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
dan7c3210e2011-12-21 18:04:41 +0000486 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
dan2cf7e0a2011-10-05 17:36:27 +0000487
488 /* If connected to a ZIPVFS backend, override the page size and
489 ** offset with actual values obtained from ZIPVFS.
490 */
491 fd = sqlite3PagerFile(pPager);
492 x[0] = pCsr->iPageno;
drhafb39a42018-03-29 13:47:01 +0000493 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
dan2cf7e0a2011-10-05 17:36:27 +0000494 pCsr->iOffset = x[0];
drh7da5fcb2012-03-30 14:59:43 +0000495 pCsr->szPage = (int)x[1];
dan2cf7e0a2011-10-05 17:36:27 +0000496 }
497}
498
499/*
dan599e9d22010-07-12 08:39:37 +0000500** Move a statvfs cursor to the next entry in the file.
501*/
502static int statNext(sqlite3_vtab_cursor *pCursor){
503 int rc;
504 int nPayload;
drh5f36a832015-05-07 18:29:04 +0000505 char *z;
dan599e9d22010-07-12 08:39:37 +0000506 StatCursor *pCsr = (StatCursor *)pCursor;
507 StatTable *pTab = (StatTable *)pCursor->pVtab;
drha46a4a62015-09-08 21:12:53 +0000508 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
dan599e9d22010-07-12 08:39:37 +0000509 Pager *pPager = sqlite3BtreePager(pBt);
510
511 sqlite3_free(pCsr->zPath);
512 pCsr->zPath = 0;
513
drha4641712013-11-02 11:34:58 +0000514statNextRestart:
dan599e9d22010-07-12 08:39:37 +0000515 if( pCsr->aPage[0].pPg==0 ){
516 rc = sqlite3_step(pCsr->pStmt);
517 if( rc==SQLITE_ROW ){
dan763afe62010-08-03 06:42:39 +0000518 int nPage;
drh7da5fcb2012-03-30 14:59:43 +0000519 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
dan763afe62010-08-03 06:42:39 +0000520 sqlite3PagerPagecount(pPager, &nPage);
521 if( nPage==0 ){
522 pCsr->isEof = 1;
523 return sqlite3_reset(pCsr->pStmt);
524 }
drh9584f582015-11-04 20:22:37 +0000525 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
dan599e9d22010-07-12 08:39:37 +0000526 pCsr->aPage[0].iPgno = iRoot;
527 pCsr->aPage[0].iCell = 0;
drh5f36a832015-05-07 18:29:04 +0000528 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
dan599e9d22010-07-12 08:39:37 +0000529 pCsr->iPage = 0;
mistachkinfad30392016-02-13 23:43:46 +0000530 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
dan599e9d22010-07-12 08:39:37 +0000531 }else{
532 pCsr->isEof = 1;
533 return sqlite3_reset(pCsr->pStmt);
534 }
535 }else{
536
537 /* Page p itself has already been visited. */
538 StatPage *p = &pCsr->aPage[pCsr->iPage];
539
540 while( p->iCell<p->nCell ){
541 StatCell *pCell = &p->aCell[p->iCell];
542 if( pCell->iOvfl<pCell->nOvfl ){
drhad0961b2015-02-21 00:19:25 +0000543 int nUsable;
544 sqlite3BtreeEnter(pBt);
545 nUsable = sqlite3BtreeGetPageSize(pBt) -
546 sqlite3BtreeGetReserveNoMutex(pBt);
547 sqlite3BtreeLeave(pBt);
dan599e9d22010-07-12 08:39:37 +0000548 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
549 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
550 pCsr->zPagetype = "overflow";
551 pCsr->nCell = 0;
552 pCsr->nMxPayload = 0;
drh5f36a832015-05-07 18:29:04 +0000553 pCsr->zPath = z = sqlite3_mprintf(
dan599e9d22010-07-12 08:39:37 +0000554 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
555 );
556 if( pCell->iOvfl<pCell->nOvfl-1 ){
557 pCsr->nUnused = 0;
558 pCsr->nPayload = nUsable - 4;
559 }else{
560 pCsr->nPayload = pCell->nLastOvfl;
561 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
562 }
563 pCell->iOvfl++;
dan2cf7e0a2011-10-05 17:36:27 +0000564 statSizeAndOffset(pCsr);
mistachkinfad30392016-02-13 23:43:46 +0000565 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
dan599e9d22010-07-12 08:39:37 +0000566 }
567 if( p->iRightChildPg ) break;
568 p->iCell++;
569 }
570
drha4641712013-11-02 11:34:58 +0000571 if( !p->iRightChildPg || p->iCell>p->nCell ){
dan599e9d22010-07-12 08:39:37 +0000572 statClearPage(p);
573 if( pCsr->iPage==0 ) return statNext(pCursor);
574 pCsr->iPage--;
drha4641712013-11-02 11:34:58 +0000575 goto statNextRestart; /* Tail recursion */
dan599e9d22010-07-12 08:39:37 +0000576 }
577 pCsr->iPage++;
drh8eaf5652019-01-09 11:19:41 +0000578 if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
579 statResetCsr(pCsr);
580 return SQLITE_CORRUPT_BKPT;
581 }
dan599e9d22010-07-12 08:39:37 +0000582 assert( p==&pCsr->aPage[pCsr->iPage-1] );
583
584 if( p->iCell==p->nCell ){
585 p[1].iPgno = p->iRightChildPg;
586 }else{
587 p[1].iPgno = p->aCell[p->iCell].iChildPg;
588 }
drh9584f582015-11-04 20:22:37 +0000589 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
dan599e9d22010-07-12 08:39:37 +0000590 p[1].iCell = 0;
drh5f36a832015-05-07 18:29:04 +0000591 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
dan599e9d22010-07-12 08:39:37 +0000592 p->iCell++;
mistachkinfad30392016-02-13 23:43:46 +0000593 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
dan599e9d22010-07-12 08:39:37 +0000594 }
595
596
597 /* Populate the StatCursor fields with the values to be returned
598 ** by the xColumn() and xRowid() methods.
599 */
600 if( rc==SQLITE_OK ){
601 int i;
602 StatPage *p = &pCsr->aPage[pCsr->iPage];
603 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
604 pCsr->iPageno = p->iPgno;
605
dan995f8b92015-04-27 19:53:55 +0000606 rc = statDecodePage(pBt, p);
607 if( rc==SQLITE_OK ){
608 statSizeAndOffset(pCsr);
drh4c9f1292011-09-28 00:50:14 +0000609
dan995f8b92015-04-27 19:53:55 +0000610 switch( p->flags ){
611 case 0x05: /* table internal */
612 case 0x02: /* index internal */
613 pCsr->zPagetype = "internal";
614 break;
615 case 0x0D: /* table leaf */
616 case 0x0A: /* index leaf */
617 pCsr->zPagetype = "leaf";
618 break;
619 default:
620 pCsr->zPagetype = "corrupted";
621 break;
622 }
623 pCsr->nCell = p->nCell;
624 pCsr->nUnused = p->nUnused;
625 pCsr->nMxPayload = p->nMxPayload;
drh5f36a832015-05-07 18:29:04 +0000626 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
mistachkinfad30392016-02-13 23:43:46 +0000627 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
dan995f8b92015-04-27 19:53:55 +0000628 nPayload = 0;
629 for(i=0; i<p->nCell; i++){
630 nPayload += p->aCell[i].nLocal;
631 }
632 pCsr->nPayload = nPayload;
dan599e9d22010-07-12 08:39:37 +0000633 }
dan599e9d22010-07-12 08:39:37 +0000634 }
635
636 return rc;
637}
638
639static int statEof(sqlite3_vtab_cursor *pCursor){
640 StatCursor *pCsr = (StatCursor *)pCursor;
641 return pCsr->isEof;
642}
643
drhad84bd82019-11-19 14:01:51 +0000644/* Initialize a cursor according to the query plan idxNum using the
645** arguments in argv[0]. See statBestIndex() for a description of the
646** meaning of the bits in idxNum.
647*/
dan599e9d22010-07-12 08:39:37 +0000648static int statFilter(
649 sqlite3_vtab_cursor *pCursor,
650 int idxNum, const char *idxStr,
651 int argc, sqlite3_value **argv
652){
dan599e9d22010-07-12 08:39:37 +0000653 StatCursor *pCsr = (StatCursor *)pCursor;
drha46a4a62015-09-08 21:12:53 +0000654 StatTable *pTab = (StatTable*)(pCursor->pVtab);
drhad84bd82019-11-19 14:01:51 +0000655 sqlite3_str *pSql; /* Query of btrees to analyze */
656 char *zSql; /* String value of pSql */
657 int iArg = 0; /* Count of argv[] parameters used so far */
658 int rc = SQLITE_OK; /* Result of this operation */
659 const char *zName = 0; /* Only provide analysis of this table */
dan599e9d22010-07-12 08:39:37 +0000660
drhad84bd82019-11-19 14:01:51 +0000661 statResetCsr(pCsr);
662 sqlite3_finalize(pCsr->pStmt);
663 pCsr->pStmt = 0;
664 if( idxNum & 0x01 ){
665 /* schema=? constraint is present. Get its value */
666 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
drha46a4a62015-09-08 21:12:53 +0000667 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
668 if( pCsr->iDb<0 ){
669 sqlite3_free(pCursor->pVtab->zErrMsg);
670 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
mistachkinfad30392016-02-13 23:43:46 +0000671 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
drha46a4a62015-09-08 21:12:53 +0000672 }
673 }else{
674 pCsr->iDb = pTab->iDb;
675 }
drhad84bd82019-11-19 14:01:51 +0000676 if( idxNum & 0x02 ){
677 /* name=? constraint is present */
678 zName = (const char*)sqlite3_value_text(argv[iArg++]);
679 }
680 if( idxNum & 0x04 ){
681 /* aggregate=? constraint is present */
682 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
683 }else{
684 pCsr->isAgg = 0;
685 }
686 pSql = sqlite3_str_new(pTab->db);
687 sqlite3_str_appendf(pSql,
688 "SELECT * FROM ("
689 "SELECT 'sqlite_master' AS name,1 AS rootpage,'table' AS type"
690 " UNION ALL "
691 "SELECT name,rootpage,type"
692 " FROM \"%w\".sqlite_master WHERE rootpage!=0)",
693 pTab->db->aDb[pCsr->iDb].zDbSName);
694 if( zName ){
695 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
696 }
697 if( idxNum & 0x08 ){
698 sqlite3_str_appendf(pSql, " ORDER BY name");
699 }
700 zSql = sqlite3_str_finish(pSql);
drha46a4a62015-09-08 21:12:53 +0000701 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000702 return SQLITE_NOMEM_BKPT;
drha46a4a62015-09-08 21:12:53 +0000703 }else{
704 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
705 sqlite3_free(zSql);
706 }
707
708 if( rc==SQLITE_OK ){
709 rc = statNext(pCursor);
710 }
711 return rc;
dan599e9d22010-07-12 08:39:37 +0000712}
713
714static int statColumn(
715 sqlite3_vtab_cursor *pCursor,
716 sqlite3_context *ctx,
717 int i
718){
719 StatCursor *pCsr = (StatCursor *)pCursor;
720 switch( i ){
721 case 0: /* name */
drh5f36a832015-05-07 18:29:04 +0000722 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
dan599e9d22010-07-12 08:39:37 +0000723 break;
724 case 1: /* path */
725 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
726 break;
727 case 2: /* pageno */
728 sqlite3_result_int64(ctx, pCsr->iPageno);
729 break;
730 case 3: /* pagetype */
731 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
732 break;
733 case 4: /* ncell */
734 sqlite3_result_int(ctx, pCsr->nCell);
735 break;
736 case 5: /* payload */
737 sqlite3_result_int(ctx, pCsr->nPayload);
738 break;
739 case 6: /* unused */
740 sqlite3_result_int(ctx, pCsr->nUnused);
741 break;
742 case 7: /* mx_payload */
743 sqlite3_result_int(ctx, pCsr->nMxPayload);
744 break;
drh4c9f1292011-09-28 00:50:14 +0000745 case 8: /* pgoffset */
746 sqlite3_result_int64(ctx, pCsr->iOffset);
747 break;
drha46a4a62015-09-08 21:12:53 +0000748 case 9: /* pgsize */
drh4c9f1292011-09-28 00:50:14 +0000749 sqlite3_result_int(ctx, pCsr->szPage);
750 break;
drhad84bd82019-11-19 14:01:51 +0000751 case 10: { /* schema */
drha46a4a62015-09-08 21:12:53 +0000752 sqlite3 *db = sqlite3_context_db_handle(ctx);
753 int iDb = pCsr->iDb;
drh69c33822016-08-18 14:33:11 +0000754 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
drha46a4a62015-09-08 21:12:53 +0000755 break;
756 }
drhad84bd82019-11-19 14:01:51 +0000757 default: { /* aggregate */
758 sqlite3_result_int(ctx, pCsr->isAgg);
759 break;
760 }
dan599e9d22010-07-12 08:39:37 +0000761 }
762 return SQLITE_OK;
763}
764
765static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
766 StatCursor *pCsr = (StatCursor *)pCursor;
767 *pRowid = pCsr->iPageno;
768 return SQLITE_OK;
769}
770
drh1a4a6802015-05-04 18:31:09 +0000771/*
772** Invoke this routine to register the "dbstat" virtual table module
773*/
drh3e0327d2015-05-11 11:59:15 +0000774int sqlite3DbstatRegister(sqlite3 *db){
dan599e9d22010-07-12 08:39:37 +0000775 static sqlite3_module dbstat_module = {
776 0, /* iVersion */
777 statConnect, /* xCreate */
778 statConnect, /* xConnect */
779 statBestIndex, /* xBestIndex */
780 statDisconnect, /* xDisconnect */
781 statDisconnect, /* xDestroy */
782 statOpen, /* xOpen - open a cursor */
783 statClose, /* xClose - close a cursor */
784 statFilter, /* xFilter - configure scan constraints */
785 statNext, /* xNext - advance a cursor */
786 statEof, /* xEof - check for end of scan */
787 statColumn, /* xColumn - read data */
788 statRowid, /* xRowid - read data */
789 0, /* xUpdate */
790 0, /* xBegin */
791 0, /* xSync */
792 0, /* xCommit */
793 0, /* xRollback */
794 0, /* xFindMethod */
795 0, /* xRename */
drh7ccf95d2017-07-12 18:05:54 +0000796 0, /* xSavepoint */
797 0, /* xRelease */
798 0, /* xRollbackTo */
drh84c501b2018-11-05 23:01:45 +0000799 0 /* xShadowName */
dan599e9d22010-07-12 08:39:37 +0000800 };
dand154a432015-04-30 20:26:53 +0000801 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
dan599e9d22010-07-12 08:39:37 +0000802}
drh10819252015-05-12 14:22:05 +0000803#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
drh6582ae52015-05-12 12:24:50 +0000804int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
drh1a4a6802015-05-04 18:31:09 +0000805#endif /* SQLITE_ENABLE_DBSTAT_VTAB */