xref: /sqlite-3.40.0/src/dbstat.c (revision c28cc32d)
1 /*
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 ******************************************************************************
12 **
13 ** This file contains an implementation of the "dbstat" virtual table.
14 **
15 ** The dbstat virtual table is used to extract low-level storage
16 ** 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.
19 **
20 ** Additional information is available on the "dbstat.html" page of the
21 ** official SQLite documentation.
22 */
23 
24 #include "sqliteInt.h"   /* Requires access to internal data structures */
25 #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
26     && !defined(SQLITE_OMIT_VIRTUALTABLE)
27 
28 /*
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
36 **   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
39 **   '/001', and so on, each sibling page identified by a 3-digit hex
40 **   value. The children of the 451st left-most sibling have paths such
41 **   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 **
57 **      '/1c2/000/'               // Left-most child of 451st child of root
58 */
59 static const char zDbstatSchema[] =
60   "CREATE TABLE x("
61   " name       TEXT,"          /*  0 Name of table or index */
62   " path       TEXT,"          /*  1 Path to page from root (NULL for agg) */
63   " pageno     INTEGER,"       /*  2 Page number (page count for aggregates) */
64   " pagetype   TEXT,"          /*  3 'internal', 'leaf', 'overflow', or NULL */
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 (NULL for agg) */
70   " pgsize     INTEGER,"       /*  9 Size of the page (sum for aggregate) */
71   " schema     TEXT HIDDEN,"   /* 10 Database schema being analyzed */
72   " aggregate  BOOLEAN HIDDEN" /* 11 aggregate info for each table */
73   ")"
74 ;
75 
76 /* Forward reference to data structured used in this module */
77 typedef struct StatTable StatTable;
78 typedef struct StatCursor StatCursor;
79 typedef struct StatPage StatPage;
80 typedef struct StatCell StatCell;
81 
82 /* Size information for a single cell within a btree page */
83 struct StatCell {
84   int nLocal;                     /* Bytes of local payload */
85   u32 iChildPg;                   /* Child node (or 0 if this is a leaf) */
86   int nOvfl;                      /* Entries in aOvfl[] */
87   u32 *aOvfl;                     /* Array of overflow page numbers */
88   int nLastOvfl;                  /* Bytes of payload on final overflow page */
89   int iOvfl;                      /* Iterates through aOvfl[] */
90 };
91 
92 /* Size information for a single btree page */
93 struct StatPage {
94   u32 iPgno;                      /* Page number */
95   DbPage *pPg;                    /* Page content */
96   int iCell;                      /* Current cell */
97 
98   char *zPath;                    /* Path to this page */
99 
100   /* Variables populated by statDecodePage(): */
101   u8 flags;                       /* Copy of flags byte */
102   int nCell;                      /* Number of cells on page */
103   int nUnused;                    /* Number of unused bytes on page */
104   StatCell *aCell;                /* Array of parsed cells */
105   u32 iRightChildPg;              /* Right-child page number (or 0) */
106   int nMxPayload;                 /* Largest payload of any cell on the page */
107 };
108 
109 /* The cursor for scanning the dbstat virtual table */
110 struct StatCursor {
111   sqlite3_vtab_cursor base;       /* base class.  MUST BE FIRST! */
112   sqlite3_stmt *pStmt;            /* Iterates through set of root pages */
113   u8 isEof;                       /* After pStmt has returned SQLITE_DONE */
114   u8 isAgg;                       /* Aggregate results for each table */
115   int iDb;                        /* Schema used for this query */
116 
117   StatPage aPage[32];             /* Pages in path to current page */
118   int iPage;                      /* Current entry in aPage[] */
119 
120   /* Values to return. */
121   u32 iPageno;                    /* Value of 'pageno' column */
122   char *zName;                    /* Value of 'name' column */
123   char *zPath;                    /* Value of 'path' column */
124   char *zPagetype;                /* Value of 'pagetype' column */
125   int nPage;                      /* Number of pages in current btree */
126   int nCell;                      /* Value of 'ncell' column */
127   int nMxPayload;                 /* Value of 'mx_payload' column */
128   i64 nUnused;                    /* Value of 'unused' column */
129   i64 nPayload;                   /* Value of 'payload' column */
130   i64 iOffset;                    /* Value of 'pgOffset' column */
131   i64 szPage;                     /* Value of 'pgSize' column */
132 };
133 
134 /* An instance of the DBSTAT virtual table */
135 struct StatTable {
136   sqlite3_vtab base;              /* base class.  MUST BE FIRST! */
137   sqlite3 *db;                    /* Database connection that owns this vtab */
138   int iDb;                        /* Index of database to analyze */
139 };
140 
141 #ifndef get2byte
142 # define get2byte(x)   ((x)[0]<<8 | (x)[1])
143 #endif
144 
145 /*
146 ** Connect to or create a new DBSTAT virtual table.
147 */
148 static int statConnect(
149   sqlite3 *db,
150   void *pAux,
151   int argc, const char *const*argv,
152   sqlite3_vtab **ppVtab,
153   char **pzErr
154 ){
155   StatTable *pTab = 0;
156   int rc = SQLITE_OK;
157   int iDb;
158 
159   if( argc>=4 ){
160     Token nm;
161     sqlite3TokenInit(&nm, (char*)argv[3]);
162     iDb = sqlite3FindDb(db, &nm);
163     if( iDb<0 ){
164       *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
165       return SQLITE_ERROR;
166     }
167   }else{
168     iDb = 0;
169   }
170   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
171   rc = sqlite3_declare_vtab(db, zDbstatSchema);
172   if( rc==SQLITE_OK ){
173     pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
174     if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
175   }
176 
177   assert( rc==SQLITE_OK || pTab==0 );
178   if( rc==SQLITE_OK ){
179     memset(pTab, 0, sizeof(StatTable));
180     pTab->db = db;
181     pTab->iDb = iDb;
182   }
183 
184   *ppVtab = (sqlite3_vtab*)pTab;
185   return rc;
186 }
187 
188 /*
189 ** Disconnect from or destroy the DBSTAT virtual table.
190 */
191 static int statDisconnect(sqlite3_vtab *pVtab){
192   sqlite3_free(pVtab);
193   return SQLITE_OK;
194 }
195 
196 /*
197 ** Compute the best query strategy and return the result in idxNum.
198 **
199 **   idxNum-Bit        Meaning
200 **   ----------        ----------------------------------------------
201 **      0x01           There is a schema=? term in the WHERE clause
202 **      0x02           There is a name=? term in the WHERE clause
203 **      0x04           There is an aggregate=? term in the WHERE clause
204 **      0x08           Output should be ordered by name and path
205 */
206 static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
207   int i;
208   int iSchema = -1;
209   int iName = -1;
210   int iAgg = -1;
211 
212   /* Look for a valid schema=? constraint.  If found, change the idxNum to
213   ** 1 and request the value of that constraint be sent to xFilter.  And
214   ** lower the cost estimate to encourage the constrained version to be
215   ** used.
216   */
217   for(i=0; i<pIdxInfo->nConstraint; i++){
218     if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
219     if( pIdxInfo->aConstraint[i].usable==0 ){
220       /* Force DBSTAT table should always be the right-most table in a join */
221       return SQLITE_CONSTRAINT;
222     }
223     switch( pIdxInfo->aConstraint[i].iColumn ){
224       case 0: {    /* name */
225         iName = i;
226         break;
227       }
228       case 10: {   /* schema */
229         iSchema = i;
230         break;
231       }
232       case 11: {   /* aggregate */
233         iAgg = i;
234         break;
235       }
236     }
237   }
238   i = 0;
239   if( iSchema>=0 ){
240     pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
241     pIdxInfo->aConstraintUsage[iSchema].omit = 1;
242     pIdxInfo->idxNum |= 0x01;
243   }
244   if( iName>=0 ){
245     pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
246     pIdxInfo->idxNum |= 0x02;
247   }
248   if( iAgg>=0 ){
249     pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
250     pIdxInfo->idxNum |= 0x04;
251   }
252   pIdxInfo->estimatedCost = 1.0;
253 
254   /* Records are always returned in ascending order of (name, path).
255   ** If this will satisfy the client, set the orderByConsumed flag so that
256   ** SQLite does not do an external sort.
257   */
258   if( ( pIdxInfo->nOrderBy==1
259      && pIdxInfo->aOrderBy[0].iColumn==0
260      && pIdxInfo->aOrderBy[0].desc==0
261      ) ||
262       ( pIdxInfo->nOrderBy==2
263      && pIdxInfo->aOrderBy[0].iColumn==0
264      && pIdxInfo->aOrderBy[0].desc==0
265      && pIdxInfo->aOrderBy[1].iColumn==1
266      && pIdxInfo->aOrderBy[1].desc==0
267      )
268   ){
269     pIdxInfo->orderByConsumed = 1;
270     pIdxInfo->idxNum |= 0x08;
271   }
272 
273   return SQLITE_OK;
274 }
275 
276 /*
277 ** Open a new DBSTAT cursor.
278 */
279 static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
280   StatTable *pTab = (StatTable *)pVTab;
281   StatCursor *pCsr;
282 
283   pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
284   if( pCsr==0 ){
285     return SQLITE_NOMEM_BKPT;
286   }else{
287     memset(pCsr, 0, sizeof(StatCursor));
288     pCsr->base.pVtab = pVTab;
289     pCsr->iDb = pTab->iDb;
290   }
291 
292   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
293   return SQLITE_OK;
294 }
295 
296 static void statClearCells(StatPage *p){
297   int i;
298   if( p->aCell ){
299     for(i=0; i<p->nCell; i++){
300       sqlite3_free(p->aCell[i].aOvfl);
301     }
302     sqlite3_free(p->aCell);
303   }
304   p->nCell = 0;
305   p->aCell = 0;
306 }
307 
308 static void statClearPage(StatPage *p){
309   statClearCells(p);
310   sqlite3PagerUnref(p->pPg);
311   sqlite3_free(p->zPath);
312   memset(p, 0, sizeof(StatPage));
313 }
314 
315 static void statResetCsr(StatCursor *pCsr){
316   int i;
317   /* In some circumstances, specifically if an OOM has occurred, the call
318   ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
319   ** important that statClearPage() is called to free any page refs before
320   ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
321   for(i=0; i<ArraySize(pCsr->aPage); i++){
322     statClearPage(&pCsr->aPage[i]);
323   }
324   sqlite3_reset(pCsr->pStmt);
325   pCsr->iPage = 0;
326   sqlite3_free(pCsr->zPath);
327   pCsr->zPath = 0;
328   pCsr->isEof = 0;
329 }
330 
331 /* Resize the space-used counters inside of the cursor */
332 static void statResetCounts(StatCursor *pCsr){
333   pCsr->nCell = 0;
334   pCsr->nMxPayload = 0;
335   pCsr->nUnused = 0;
336   pCsr->nPayload = 0;
337   pCsr->szPage = 0;
338   pCsr->nPage = 0;
339 }
340 
341 /*
342 ** Close a DBSTAT cursor.
343 */
344 static int statClose(sqlite3_vtab_cursor *pCursor){
345   StatCursor *pCsr = (StatCursor *)pCursor;
346   statResetCsr(pCsr);
347   sqlite3_finalize(pCsr->pStmt);
348   sqlite3_free(pCsr);
349   return SQLITE_OK;
350 }
351 
352 /*
353 ** For a single cell on a btree page, compute the number of bytes of
354 ** content (payload) stored on that page.  That is to say, compute the
355 ** number of bytes of content not found on overflow pages.
356 */
357 static int getLocalPayload(
358   int nUsable,                    /* Usable bytes per page */
359   u8 flags,                       /* Page flags */
360   int nTotal                      /* Total record (payload) size */
361 ){
362   int nLocal;
363   int nMinLocal;
364   int nMaxLocal;
365 
366   if( flags==0x0D ){              /* Table leaf node */
367     nMinLocal = (nUsable - 12) * 32 / 255 - 23;
368     nMaxLocal = nUsable - 35;
369   }else{                          /* Index interior and leaf nodes */
370     nMinLocal = (nUsable - 12) * 32 / 255 - 23;
371     nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
372   }
373 
374   nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
375   if( nLocal>nMaxLocal ) nLocal = nMinLocal;
376   return nLocal;
377 }
378 
379 /* Populate the StatPage object with information about the all
380 ** cells found on the page currently under analysis.
381 */
382 static int statDecodePage(Btree *pBt, StatPage *p){
383   int nUnused;
384   int iOff;
385   int nHdr;
386   int isLeaf;
387   int szPage;
388 
389   u8 *aData = sqlite3PagerGetData(p->pPg);
390   u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
391 
392   p->flags = aHdr[0];
393   if( p->flags==0x0A || p->flags==0x0D ){
394     isLeaf = 1;
395     nHdr = 8;
396   }else if( p->flags==0x05 || p->flags==0x02 ){
397     isLeaf = 0;
398     nHdr = 12;
399   }else{
400     goto statPageIsCorrupt;
401   }
402   if( p->iPgno==1 ) nHdr += 100;
403   p->nCell = get2byte(&aHdr[3]);
404   p->nMxPayload = 0;
405   szPage = sqlite3BtreeGetPageSize(pBt);
406 
407   nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
408   nUnused += (int)aHdr[7];
409   iOff = get2byte(&aHdr[1]);
410   while( iOff ){
411     int iNext;
412     if( iOff>=szPage ) goto statPageIsCorrupt;
413     nUnused += get2byte(&aData[iOff+2]);
414     iNext = get2byte(&aData[iOff]);
415     if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
416     iOff = iNext;
417   }
418   p->nUnused = nUnused;
419   p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
420 
421   if( p->nCell ){
422     int i;                        /* Used to iterate through cells */
423     int nUsable;                  /* Usable bytes per page */
424 
425     sqlite3BtreeEnter(pBt);
426     nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
427     sqlite3BtreeLeave(pBt);
428     p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
429     if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
430     memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
431 
432     for(i=0; i<p->nCell; i++){
433       StatCell *pCell = &p->aCell[i];
434 
435       iOff = get2byte(&aData[nHdr+i*2]);
436       if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
437       if( !isLeaf ){
438         pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
439         iOff += 4;
440       }
441       if( p->flags==0x05 ){
442         /* A table interior node. nPayload==0. */
443       }else{
444         u32 nPayload;             /* Bytes of payload total (local+overflow) */
445         int nLocal;               /* Bytes of payload stored locally */
446         iOff += getVarint32(&aData[iOff], nPayload);
447         if( p->flags==0x0D ){
448           u64 dummy;
449           iOff += sqlite3GetVarint(&aData[iOff], &dummy);
450         }
451         if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
452         nLocal = getLocalPayload(nUsable, p->flags, nPayload);
453         if( nLocal<0 ) goto statPageIsCorrupt;
454         pCell->nLocal = nLocal;
455         assert( nPayload>=(u32)nLocal );
456         assert( nLocal<=(nUsable-35) );
457         if( nPayload>(u32)nLocal ){
458           int j;
459           int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
460           if( iOff+nLocal>nUsable || nPayload>0x7fffffff ){
461             goto statPageIsCorrupt;
462           }
463           pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
464           pCell->nOvfl = nOvfl;
465           pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
466           if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
467           pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
468           for(j=1; j<nOvfl; j++){
469             int rc;
470             u32 iPrev = pCell->aOvfl[j-1];
471             DbPage *pPg = 0;
472             rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
473             if( rc!=SQLITE_OK ){
474               assert( pPg==0 );
475               return rc;
476             }
477             pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
478             sqlite3PagerUnref(pPg);
479           }
480         }
481       }
482     }
483   }
484 
485   return SQLITE_OK;
486 
487 statPageIsCorrupt:
488   p->flags = 0;
489   statClearCells(p);
490   return SQLITE_OK;
491 }
492 
493 /*
494 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
495 ** the current value of pCsr->iPageno.
496 */
497 static void statSizeAndOffset(StatCursor *pCsr){
498   StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
499   Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
500   Pager *pPager = sqlite3BtreePager(pBt);
501   sqlite3_file *fd;
502   sqlite3_int64 x[2];
503 
504   /* If connected to a ZIPVFS backend, find the page size and
505   ** offset from ZIPVFS.
506   */
507   fd = sqlite3PagerFile(pPager);
508   x[0] = pCsr->iPageno;
509   if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
510     pCsr->iOffset = x[0];
511     pCsr->szPage += x[1];
512   }else{
513     /* Not ZIPVFS: The default page size and offset */
514     pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
515     pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
516   }
517 }
518 
519 /*
520 ** Move a DBSTAT cursor to the next entry.  Normally, the next
521 ** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
522 ** the next entry is the next btree.
523 */
524 static int statNext(sqlite3_vtab_cursor *pCursor){
525   int rc;
526   int nPayload;
527   char *z;
528   StatCursor *pCsr = (StatCursor *)pCursor;
529   StatTable *pTab = (StatTable *)pCursor->pVtab;
530   Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
531   Pager *pPager = sqlite3BtreePager(pBt);
532 
533   sqlite3_free(pCsr->zPath);
534   pCsr->zPath = 0;
535 
536 statNextRestart:
537   if( pCsr->aPage[0].pPg==0 ){
538     /* Start measuring space on the next btree */
539     statResetCounts(pCsr);
540     rc = sqlite3_step(pCsr->pStmt);
541     if( rc==SQLITE_ROW ){
542       int nPage;
543       u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
544       sqlite3PagerPagecount(pPager, &nPage);
545       if( nPage==0 ){
546         pCsr->isEof = 1;
547         return sqlite3_reset(pCsr->pStmt);
548       }
549       rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
550       pCsr->aPage[0].iPgno = iRoot;
551       pCsr->aPage[0].iCell = 0;
552       if( !pCsr->isAgg ){
553         pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
554         if( z==0 ) rc = SQLITE_NOMEM_BKPT;
555       }
556       pCsr->iPage = 0;
557       pCsr->nPage = 1;
558     }else{
559       pCsr->isEof = 1;
560       return sqlite3_reset(pCsr->pStmt);
561     }
562   }else{
563     /* Continue analyzing the btree previously started */
564     StatPage *p = &pCsr->aPage[pCsr->iPage];
565     if( !pCsr->isAgg ) statResetCounts(pCsr);
566     while( p->iCell<p->nCell ){
567       StatCell *pCell = &p->aCell[p->iCell];
568       while( pCell->iOvfl<pCell->nOvfl ){
569         int nUsable, iOvfl;
570         sqlite3BtreeEnter(pBt);
571         nUsable = sqlite3BtreeGetPageSize(pBt) -
572                         sqlite3BtreeGetReserveNoMutex(pBt);
573         sqlite3BtreeLeave(pBt);
574         pCsr->nPage++;
575         statSizeAndOffset(pCsr);
576         if( pCell->iOvfl<pCell->nOvfl-1 ){
577           pCsr->nPayload += nUsable - 4;
578         }else{
579           pCsr->nPayload += pCell->nLastOvfl;
580           pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
581         }
582         iOvfl = pCell->iOvfl;
583         pCell->iOvfl++;
584         if( !pCsr->isAgg ){
585           pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
586           pCsr->iPageno = pCell->aOvfl[iOvfl];
587           pCsr->zPagetype = "overflow";
588           pCsr->zPath = z = sqlite3_mprintf(
589               "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
590           );
591           return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
592         }
593       }
594       if( p->iRightChildPg ) break;
595       p->iCell++;
596     }
597 
598     if( !p->iRightChildPg || p->iCell>p->nCell ){
599       statClearPage(p);
600       if( pCsr->iPage>0 ){
601         pCsr->iPage--;
602       }else if( pCsr->isAgg ){
603         /* label-statNext-done:  When computing aggregate space usage over
604         ** an entire btree, this is the exit point from this function */
605         return SQLITE_OK;
606       }
607       goto statNextRestart; /* Tail recursion */
608     }
609     pCsr->iPage++;
610     if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
611       statResetCsr(pCsr);
612       return SQLITE_CORRUPT_BKPT;
613     }
614     assert( p==&pCsr->aPage[pCsr->iPage-1] );
615 
616     if( p->iCell==p->nCell ){
617       p[1].iPgno = p->iRightChildPg;
618     }else{
619       p[1].iPgno = p->aCell[p->iCell].iChildPg;
620     }
621     rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
622     pCsr->nPage++;
623     p[1].iCell = 0;
624     if( !pCsr->isAgg ){
625       p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
626       if( z==0 ) rc = SQLITE_NOMEM_BKPT;
627     }
628     p->iCell++;
629   }
630 
631 
632   /* Populate the StatCursor fields with the values to be returned
633   ** by the xColumn() and xRowid() methods.
634   */
635   if( rc==SQLITE_OK ){
636     int i;
637     StatPage *p = &pCsr->aPage[pCsr->iPage];
638     pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
639     pCsr->iPageno = p->iPgno;
640 
641     rc = statDecodePage(pBt, p);
642     if( rc==SQLITE_OK ){
643       statSizeAndOffset(pCsr);
644 
645       switch( p->flags ){
646         case 0x05:             /* table internal */
647         case 0x02:             /* index internal */
648           pCsr->zPagetype = "internal";
649           break;
650         case 0x0D:             /* table leaf */
651         case 0x0A:             /* index leaf */
652           pCsr->zPagetype = "leaf";
653           break;
654         default:
655           pCsr->zPagetype = "corrupted";
656           break;
657       }
658       pCsr->nCell += p->nCell;
659       pCsr->nUnused += p->nUnused;
660       if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
661       if( !pCsr->isAgg ){
662         pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
663         if( z==0 ) rc = SQLITE_NOMEM_BKPT;
664       }
665       nPayload = 0;
666       for(i=0; i<p->nCell; i++){
667         nPayload += p->aCell[i].nLocal;
668       }
669       pCsr->nPayload += nPayload;
670 
671       /* If computing aggregate space usage by btree, continue with the
672       ** next page.  The loop will exit via the return at label-statNext-done
673       */
674       if( pCsr->isAgg ) goto statNextRestart;
675     }
676   }
677 
678   return rc;
679 }
680 
681 static int statEof(sqlite3_vtab_cursor *pCursor){
682   StatCursor *pCsr = (StatCursor *)pCursor;
683   return pCsr->isEof;
684 }
685 
686 /* Initialize a cursor according to the query plan idxNum using the
687 ** arguments in argv[0].  See statBestIndex() for a description of the
688 ** meaning of the bits in idxNum.
689 */
690 static int statFilter(
691   sqlite3_vtab_cursor *pCursor,
692   int idxNum, const char *idxStr,
693   int argc, sqlite3_value **argv
694 ){
695   StatCursor *pCsr = (StatCursor *)pCursor;
696   StatTable *pTab = (StatTable*)(pCursor->pVtab);
697   sqlite3_str *pSql;      /* Query of btrees to analyze */
698   char *zSql;             /* String value of pSql */
699   int iArg = 0;           /* Count of argv[] parameters used so far */
700   int rc = SQLITE_OK;     /* Result of this operation */
701   const char *zName = 0;  /* Only provide analysis of this table */
702 
703   statResetCsr(pCsr);
704   sqlite3_finalize(pCsr->pStmt);
705   pCsr->pStmt = 0;
706   if( idxNum & 0x01 ){
707     /* schema=? constraint is present.  Get its value */
708     const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
709     pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
710     if( pCsr->iDb<0 ){
711       pCsr->iDb = 0;
712       pCsr->isEof = 1;
713       return SQLITE_OK;
714     }
715   }else{
716     pCsr->iDb = pTab->iDb;
717   }
718   if( idxNum & 0x02 ){
719     /* name=? constraint is present */
720     zName = (const char*)sqlite3_value_text(argv[iArg++]);
721   }
722   if( idxNum & 0x04 ){
723     /* aggregate=? constraint is present */
724     pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
725   }else{
726     pCsr->isAgg = 0;
727   }
728   pSql = sqlite3_str_new(pTab->db);
729   sqlite3_str_appendf(pSql,
730       "SELECT * FROM ("
731         "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
732         " UNION ALL "
733         "SELECT name,rootpage,type"
734         " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
735       pTab->db->aDb[pCsr->iDb].zDbSName);
736   if( zName ){
737     sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
738   }
739   if( idxNum & 0x08 ){
740     sqlite3_str_appendf(pSql, " ORDER BY name");
741   }
742   zSql = sqlite3_str_finish(pSql);
743   if( zSql==0 ){
744     return SQLITE_NOMEM_BKPT;
745   }else{
746     rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
747     sqlite3_free(zSql);
748   }
749 
750   if( rc==SQLITE_OK ){
751     rc = statNext(pCursor);
752   }
753   return rc;
754 }
755 
756 static int statColumn(
757   sqlite3_vtab_cursor *pCursor,
758   sqlite3_context *ctx,
759   int i
760 ){
761   StatCursor *pCsr = (StatCursor *)pCursor;
762   switch( i ){
763     case 0:            /* name */
764       sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
765       break;
766     case 1:            /* path */
767       if( !pCsr->isAgg ){
768         sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
769       }
770       break;
771     case 2:            /* pageno */
772       if( pCsr->isAgg ){
773         sqlite3_result_int64(ctx, pCsr->nPage);
774       }else{
775         sqlite3_result_int64(ctx, pCsr->iPageno);
776       }
777       break;
778     case 3:            /* pagetype */
779       if( !pCsr->isAgg ){
780         sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
781       }
782       break;
783     case 4:            /* ncell */
784       sqlite3_result_int(ctx, pCsr->nCell);
785       break;
786     case 5:            /* payload */
787       sqlite3_result_int(ctx, pCsr->nPayload);
788       break;
789     case 6:            /* unused */
790       sqlite3_result_int(ctx, pCsr->nUnused);
791       break;
792     case 7:            /* mx_payload */
793       sqlite3_result_int(ctx, pCsr->nMxPayload);
794       break;
795     case 8:            /* pgoffset */
796       if( !pCsr->isAgg ){
797         sqlite3_result_int64(ctx, pCsr->iOffset);
798       }
799       break;
800     case 9:            /* pgsize */
801       sqlite3_result_int(ctx, pCsr->szPage);
802       break;
803     case 10: {         /* schema */
804       sqlite3 *db = sqlite3_context_db_handle(ctx);
805       int iDb = pCsr->iDb;
806       sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
807       break;
808     }
809     default: {         /* aggregate */
810       sqlite3_result_int(ctx, pCsr->isAgg);
811       break;
812     }
813   }
814   return SQLITE_OK;
815 }
816 
817 static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
818   StatCursor *pCsr = (StatCursor *)pCursor;
819   *pRowid = pCsr->iPageno;
820   return SQLITE_OK;
821 }
822 
823 /*
824 ** Invoke this routine to register the "dbstat" virtual table module
825 */
826 int sqlite3DbstatRegister(sqlite3 *db){
827   static sqlite3_module dbstat_module = {
828     0,                            /* iVersion */
829     statConnect,                  /* xCreate */
830     statConnect,                  /* xConnect */
831     statBestIndex,                /* xBestIndex */
832     statDisconnect,               /* xDisconnect */
833     statDisconnect,               /* xDestroy */
834     statOpen,                     /* xOpen - open a cursor */
835     statClose,                    /* xClose - close a cursor */
836     statFilter,                   /* xFilter - configure scan constraints */
837     statNext,                     /* xNext - advance a cursor */
838     statEof,                      /* xEof - check for end of scan */
839     statColumn,                   /* xColumn - read data */
840     statRowid,                    /* xRowid - read data */
841     0,                            /* xUpdate */
842     0,                            /* xBegin */
843     0,                            /* xSync */
844     0,                            /* xCommit */
845     0,                            /* xRollback */
846     0,                            /* xFindMethod */
847     0,                            /* xRename */
848     0,                            /* xSavepoint */
849     0,                            /* xRelease */
850     0,                            /* xRollbackTo */
851     0                             /* xShadowName */
852   };
853   return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
854 }
855 #elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
856 int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
857 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
858