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