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