sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / btreeinfo.c
| 1 | /* |
| 2 | ** 2017-10-24 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This file contains an implementation of the "sqlite_btreeinfo" virtual table. |
| 14 | ** |
| 15 | ** The sqlite_btreeinfo virtual table is a read-only eponymous-only virtual |
| 16 | ** table that shows information about all btrees in an SQLite database file. |
| 17 | ** The schema is like this: |
| 18 | ** |
| 19 | ** CREATE TABLE sqlite_btreeinfo( |
| 20 | ** type TEXT, -- "table" or "index" |
| 21 | ** name TEXT, -- Name of table or index for this btree. |
| 22 | ** tbl_name TEXT, -- Associated table |
| 23 | ** rootpage INT, -- The root page of the btree |
| 24 | ** sql TEXT, -- SQL for this btree - from sqlite_schema |
| 25 | ** hasRowid BOOLEAN, -- True if the btree has a rowid |
| 26 | ** nEntry INT, -- Estimated number of entries |
| 27 | ** nPage INT, -- Estimated number of pages |
| 28 | ** depth INT, -- Depth of the btree |
| 29 | ** szPage INT, -- Size of each page in bytes |
| 30 | ** zSchema TEXT HIDDEN -- The schema to which this btree belongs |
| 31 | ** ); |
| 32 | ** |
| 33 | ** The first 5 fields are taken directly from the sqlite_schema table. |
| 34 | ** Considering only the first 5 fields, the only difference between |
| 35 | ** this virtual table and the sqlite_schema table is that this virtual |
| 36 | ** table omits all entries that have a 0 or NULL rowid - in other words |
| 37 | ** it omits triggers and views. |
| 38 | ** |
| 39 | ** The value added by this table comes in the next 5 fields. |
| 40 | ** |
| 41 | ** Note that nEntry and nPage are *estimated*. They are computed doing |
| 42 | ** a single search from the root to a leaf, counting the number of cells |
| 43 | ** at each level, and assuming that unvisited pages have a similar number |
| 44 | ** of cells. |
| 45 | ** |
| 46 | ** The sqlite_dbpage virtual table must be available for this virtual table |
| 47 | ** to operate. |
| 48 | ** |
| 49 | ** USAGE EXAMPLES: |
| 50 | ** |
| 51 | ** Show the table btrees in a schema order with the tables with the most |
| 52 | ** rows occurring first: |
| 53 | ** |
| 54 | ** SELECT name, nEntry |
| 55 | ** FROM sqlite_btreeinfo |
| 56 | ** WHERE type='table' |
| 57 | ** ORDER BY nEntry DESC, name; |
| 58 | ** |
| 59 | ** Show the names of all WITHOUT ROWID tables: |
| 60 | ** |
| 61 | ** SELECT name FROM sqlite_btreeinfo |
| 62 | ** WHERE type='table' AND NOT hasRowid; |
| 63 | ** |
| 64 | ** UNUSED, UNTESTED, UNSUPPORTED |
| 65 | ** |
| 66 | ** This extension exists for information and demonstration purposes |
| 67 | ** only. The developers are not aware of any real-world use of this |
| 68 | ** extension. The extension has no formal test cases. The developers |
| 69 | ** do not actively support it. |
| 70 | */ |
| 71 | #if !defined(SQLITEINT_H) |
| 72 | #include "sqlite3ext.h" |
| 73 | #endif |
| 74 | SQLITE_EXTENSION_INIT1 |
| 75 | #include <string.h> |
| 76 | #include <assert.h> |
| 77 | |
| 78 | /* Columns available in this virtual table */ |
| 79 | #define BINFO_COLUMN_TYPE 0 |
| 80 | #define BINFO_COLUMN_NAME 1 |
| 81 | #define BINFO_COLUMN_TBL_NAME 2 |
| 82 | #define BINFO_COLUMN_ROOTPAGE 3 |
| 83 | #define BINFO_COLUMN_SQL 4 |
| 84 | #define BINFO_COLUMN_HASROWID 5 |
| 85 | #define BINFO_COLUMN_NENTRY 6 |
| 86 | #define BINFO_COLUMN_NPAGE 7 |
| 87 | #define BINFO_COLUMN_DEPTH 8 |
| 88 | #define BINFO_COLUMN_SZPAGE 9 |
| 89 | #define BINFO_COLUMN_SCHEMA 10 |
| 90 | |
| 91 | /* Forward declarations */ |
| 92 | typedef struct BinfoTable BinfoTable; |
| 93 | typedef struct BinfoCursor BinfoCursor; |
| 94 | |
| 95 | /* A cursor for the sqlite_btreeinfo table */ |
| 96 | struct BinfoCursor { |
| 97 | sqlite3_vtab_cursor base; /* Base class. Must be first */ |
| 98 | sqlite3_stmt *pStmt; /* Query against sqlite_schema */ |
| 99 | int rc; /* Result of previous sqlite_step() call */ |
| 100 | int hasRowid; /* hasRowid value. Negative if unknown. */ |
| 101 | sqlite3_int64 nEntry; /* nEntry value */ |
| 102 | int nPage; /* nPage value */ |
| 103 | int depth; /* depth value */ |
| 104 | int szPage; /* size of a btree page. 0 if unknown */ |
| 105 | char *zSchema; /* Schema being interrogated */ |
| 106 | }; |
| 107 | |
| 108 | /* The sqlite_btreeinfo table */ |
| 109 | struct BinfoTable { |
| 110 | sqlite3_vtab base; /* Base class. Must be first */ |
| 111 | sqlite3 *db; /* The databse connection */ |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | ** Connect to the sqlite_btreeinfo virtual table. |
| 116 | */ |
| 117 | static int binfoConnect( |
| 118 | sqlite3 *db, |
| 119 | void *pAux, |
| 120 | int argc, const char *const*argv, |
| 121 | sqlite3_vtab **ppVtab, |
| 122 | char **pzErr |
| 123 | ){ |
| 124 | BinfoTable *pTab = 0; |
| 125 | int rc = SQLITE_OK; |
| 126 | rc = sqlite3_declare_vtab(db, |
| 127 | "CREATE TABLE x(\n" |
| 128 | " type TEXT,\n" |
| 129 | " name TEXT,\n" |
| 130 | " tbl_name TEXT,\n" |
| 131 | " rootpage INT,\n" |
| 132 | " sql TEXT,\n" |
| 133 | " hasRowid BOOLEAN,\n" |
| 134 | " nEntry INT,\n" |
| 135 | " nPage INT,\n" |
| 136 | " depth INT,\n" |
| 137 | " szPage INT,\n" |
| 138 | " zSchema TEXT HIDDEN\n" |
| 139 | ")"); |
| 140 | if( rc==SQLITE_OK ){ |
| 141 | pTab = (BinfoTable *)sqlite3_malloc64(sizeof(BinfoTable)); |
| 142 | if( pTab==0 ) rc = SQLITE_NOMEM; |
| 143 | } |
| 144 | assert( rc==SQLITE_OK || pTab==0 ); |
| 145 | if( pTab ){ |
| 146 | pTab->db = db; |
| 147 | } |
| 148 | *ppVtab = (sqlite3_vtab*)pTab; |
| 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | ** Disconnect from or destroy a btreeinfo virtual table. |
| 154 | */ |
| 155 | static int binfoDisconnect(sqlite3_vtab *pVtab){ |
| 156 | sqlite3_free(pVtab); |
| 157 | return SQLITE_OK; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | ** idxNum: |
| 162 | ** |
| 163 | ** 0 Use "main" for the schema |
| 164 | ** 1 Schema identified by parameter ?1 |
| 165 | */ |
| 166 | static int binfoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 167 | int i; |
| 168 | pIdxInfo->estimatedCost = 10000.0; /* Cost estimate */ |
| 169 | pIdxInfo->estimatedRows = 100; |
| 170 | for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 171 | struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; |
| 172 | if( p->usable |
| 173 | && p->iColumn==BINFO_COLUMN_SCHEMA |
| 174 | && p->op==SQLITE_INDEX_CONSTRAINT_EQ |
| 175 | ){ |
| 176 | pIdxInfo->estimatedCost = 1000.0; |
| 177 | pIdxInfo->idxNum = 1; |
| 178 | pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 179 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | return SQLITE_OK; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | ** Open a new btreeinfo cursor. |
| 188 | */ |
| 189 | static int binfoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 190 | BinfoCursor *pCsr; |
| 191 | |
| 192 | pCsr = (BinfoCursor *)sqlite3_malloc64(sizeof(BinfoCursor)); |
| 193 | if( pCsr==0 ){ |
| 194 | return SQLITE_NOMEM; |
| 195 | }else{ |
| 196 | memset(pCsr, 0, sizeof(BinfoCursor)); |
| 197 | pCsr->base.pVtab = pVTab; |
| 198 | } |
| 199 | |
| 200 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 201 | return SQLITE_OK; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | ** Close a btreeinfo cursor. |
| 206 | */ |
| 207 | static int binfoClose(sqlite3_vtab_cursor *pCursor){ |
| 208 | BinfoCursor *pCsr = (BinfoCursor *)pCursor; |
| 209 | sqlite3_finalize(pCsr->pStmt); |
| 210 | sqlite3_free(pCsr->zSchema); |
| 211 | sqlite3_free(pCsr); |
| 212 | return SQLITE_OK; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | ** Move a btreeinfo cursor to the next entry in the file. |
| 217 | */ |
| 218 | static int binfoNext(sqlite3_vtab_cursor *pCursor){ |
| 219 | BinfoCursor *pCsr = (BinfoCursor *)pCursor; |
| 220 | pCsr->rc = sqlite3_step(pCsr->pStmt); |
| 221 | pCsr->hasRowid = -1; |
| 222 | return pCsr->rc==SQLITE_ERROR ? SQLITE_ERROR : SQLITE_OK; |
| 223 | } |
| 224 | |
| 225 | /* We have reached EOF if previous sqlite3_step() returned |
| 226 | ** anything other than SQLITE_ROW; |
| 227 | */ |
| 228 | static int binfoEof(sqlite3_vtab_cursor *pCursor){ |
| 229 | BinfoCursor *pCsr = (BinfoCursor *)pCursor; |
| 230 | return pCsr->rc!=SQLITE_ROW; |
| 231 | } |
| 232 | |
| 233 | /* Position a cursor back to the beginning. |
| 234 | */ |
| 235 | static int binfoFilter( |
| 236 | sqlite3_vtab_cursor *pCursor, |
| 237 | int idxNum, const char *idxStr, |
| 238 | int argc, sqlite3_value **argv |
| 239 | ){ |
| 240 | BinfoCursor *pCsr = (BinfoCursor *)pCursor; |
| 241 | BinfoTable *pTab = (BinfoTable *)pCursor->pVtab; |
| 242 | char *zSql; |
| 243 | int rc; |
| 244 | |
| 245 | sqlite3_free(pCsr->zSchema); |
| 246 | if( idxNum==1 && sqlite3_value_type(argv[0])!=SQLITE_NULL ){ |
| 247 | pCsr->zSchema = sqlite3_mprintf("%s", sqlite3_value_text(argv[0])); |
| 248 | }else{ |
| 249 | pCsr->zSchema = sqlite3_mprintf("main"); |
| 250 | } |
| 251 | zSql = sqlite3_mprintf( |
| 252 | "SELECT 0, 'table','sqlite_schema','sqlite_schema',1,NULL " |
| 253 | "UNION ALL " |
| 254 | "SELECT rowid, type, name, tbl_name, rootpage, sql" |
| 255 | " FROM \"%w\".sqlite_schema WHERE rootpage>=1", |
| 256 | pCsr->zSchema); |
| 257 | sqlite3_finalize(pCsr->pStmt); |
| 258 | pCsr->pStmt = 0; |
| 259 | pCsr->hasRowid = -1; |
| 260 | rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); |
| 261 | sqlite3_free(zSql); |
| 262 | if( rc==SQLITE_OK ){ |
| 263 | rc = binfoNext(pCursor); |
| 264 | } |
| 265 | return rc; |
| 266 | } |
| 267 | |
| 268 | /* Decode big-endian integers */ |
| 269 | static unsigned int get_uint16(unsigned char *a){ |
| 270 | return (a[0]<<8)|a[1]; |
| 271 | } |
| 272 | static unsigned int get_uint32(unsigned char *a){ |
| 273 | return (a[0]<<24)|(a[1]<<16)|(a[2]<<8)|a[3]; |
| 274 | } |
| 275 | |
| 276 | /* Examine the b-tree rooted at pgno and estimate its size. |
| 277 | ** Return non-zero if anything goes wrong. |
| 278 | */ |
| 279 | static int binfoCompute(sqlite3 *db, int pgno, BinfoCursor *pCsr){ |
| 280 | sqlite3_int64 nEntry = 1; |
| 281 | int nPage = 1; |
| 282 | unsigned char *aData; |
| 283 | sqlite3_stmt *pStmt = 0; |
| 284 | int rc = SQLITE_OK; |
| 285 | int pgsz = 0; |
| 286 | int nCell; |
| 287 | int iCell; |
| 288 | |
| 289 | rc = sqlite3_prepare_v2(db, |
| 290 | "SELECT data FROM sqlite_dbpage('main') WHERE pgno=?1", -1, |
| 291 | &pStmt, 0); |
| 292 | if( rc ) return rc; |
| 293 | pCsr->depth = 1; |
| 294 | while(1){ |
| 295 | sqlite3_bind_int(pStmt, 1, pgno); |
| 296 | if( pCsr->depth>25 ){ |
| 297 | sqlite3_set_errmsg(db, SQLITE_CORRUPT, "btree nested too deep"); |
| 298 | rc = SQLITE_ERROR; |
| 299 | break; |
| 300 | } |
| 301 | rc = sqlite3_step(pStmt); |
| 302 | if( rc!=SQLITE_ROW ){ |
| 303 | rc = SQLITE_ERROR; |
| 304 | break; |
| 305 | } |
| 306 | pCsr->szPage = pgsz = sqlite3_column_bytes(pStmt, 0); |
| 307 | aData = (unsigned char*)sqlite3_column_blob(pStmt, 0); |
| 308 | if( aData==0 ){ |
| 309 | rc = SQLITE_NOMEM; |
| 310 | break; |
| 311 | } |
| 312 | if( pgno==1 ){ |
| 313 | aData += 100; |
| 314 | pgsz -= 100; |
| 315 | } |
| 316 | pCsr->hasRowid = aData[0]!=2 && aData[0]!=10; |
| 317 | nCell = get_uint16(aData+3); |
| 318 | nEntry *= (nCell+1); |
| 319 | if( aData[0]==10 || aData[0]==13 ) break; |
| 320 | nPage *= (nCell+1); |
| 321 | if( 14+2*(nCell/2)>=pgsz ){ |
| 322 | rc = SQLITE_CORRUPT; |
| 323 | break; |
| 324 | } |
| 325 | if( nCell<=1 ){ |
| 326 | pgno = get_uint32(aData+8); |
| 327 | }else{ |
| 328 | iCell = get_uint16(aData+12+2*(nCell/2)); |
| 329 | if( pgno==1 ) iCell -= 100; |
| 330 | if( iCell<=12 || iCell>=pgsz-4 ){ |
| 331 | rc = SQLITE_CORRUPT; |
| 332 | break; |
| 333 | } |
| 334 | pgno = get_uint32(aData+iCell); |
| 335 | } |
| 336 | pCsr->depth++; |
| 337 | sqlite3_reset(pStmt); |
| 338 | } |
| 339 | sqlite3_finalize(pStmt); |
| 340 | pCsr->nPage = nPage; |
| 341 | pCsr->nEntry = nEntry; |
| 342 | if( rc==SQLITE_ROW ) rc = SQLITE_OK; |
| 343 | return rc; |
| 344 | } |
| 345 | |
| 346 | /* Return a column for the sqlite_btreeinfo table */ |
| 347 | static int binfoColumn( |
| 348 | sqlite3_vtab_cursor *pCursor, |
| 349 | sqlite3_context *ctx, |
| 350 | int i |
| 351 | ){ |
| 352 | BinfoCursor *pCsr = (BinfoCursor *)pCursor; |
| 353 | if( i>=BINFO_COLUMN_HASROWID && i<=BINFO_COLUMN_SZPAGE && pCsr->hasRowid<0 ){ |
| 354 | int pgno = sqlite3_column_int(pCsr->pStmt, BINFO_COLUMN_ROOTPAGE+1); |
| 355 | sqlite3 *db = sqlite3_context_db_handle(ctx); |
| 356 | int rc = binfoCompute(db, pgno, pCsr); |
| 357 | if( rc ){ |
| 358 | pCursor->pVtab->zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); |
| 359 | return SQLITE_ERROR; |
| 360 | } |
| 361 | } |
| 362 | switch( i ){ |
| 363 | case BINFO_COLUMN_NAME: |
| 364 | case BINFO_COLUMN_TYPE: |
| 365 | case BINFO_COLUMN_TBL_NAME: |
| 366 | case BINFO_COLUMN_ROOTPAGE: |
| 367 | case BINFO_COLUMN_SQL: { |
| 368 | sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1)); |
| 369 | break; |
| 370 | } |
| 371 | case BINFO_COLUMN_HASROWID: { |
| 372 | sqlite3_result_int(ctx, pCsr->hasRowid); |
| 373 | break; |
| 374 | } |
| 375 | case BINFO_COLUMN_NENTRY: { |
| 376 | sqlite3_result_int64(ctx, pCsr->nEntry); |
| 377 | break; |
| 378 | } |
| 379 | case BINFO_COLUMN_NPAGE: { |
| 380 | sqlite3_result_int(ctx, pCsr->nPage); |
| 381 | break; |
| 382 | } |
| 383 | case BINFO_COLUMN_DEPTH: { |
| 384 | sqlite3_result_int(ctx, pCsr->depth); |
| 385 | break; |
| 386 | } |
| 387 | case BINFO_COLUMN_SCHEMA: { |
| 388 | sqlite3_result_text(ctx, pCsr->zSchema, -1, SQLITE_STATIC); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | return SQLITE_OK; |
| 393 | } |
| 394 | |
| 395 | /* Return the ROWID for the sqlite_btreeinfo table */ |
| 396 | static int binfoRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
| 397 | BinfoCursor *pCsr = (BinfoCursor *)pCursor; |
| 398 | *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); |
| 399 | return SQLITE_OK; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | ** Invoke this routine to register the "sqlite_btreeinfo" virtual table module |
| 404 | */ |
| 405 | int sqlite3BinfoRegister(sqlite3 *db){ |
| 406 | static sqlite3_module binfo_module = { |
| 407 | 0, /* iVersion */ |
| 408 | 0, /* xCreate */ |
| 409 | binfoConnect, /* xConnect */ |
| 410 | binfoBestIndex, /* xBestIndex */ |
| 411 | binfoDisconnect, /* xDisconnect */ |
| 412 | 0, /* xDestroy */ |
| 413 | binfoOpen, /* xOpen - open a cursor */ |
| 414 | binfoClose, /* xClose - close a cursor */ |
| 415 | binfoFilter, /* xFilter - configure scan constraints */ |
| 416 | binfoNext, /* xNext - advance a cursor */ |
| 417 | binfoEof, /* xEof - check for end of scan */ |
| 418 | binfoColumn, /* xColumn - read data */ |
| 419 | binfoRowid, /* xRowid - read data */ |
| 420 | 0, /* xUpdate */ |
| 421 | 0, /* xBegin */ |
| 422 | 0, /* xSync */ |
| 423 | 0, /* xCommit */ |
| 424 | 0, /* xRollback */ |
| 425 | 0, /* xFindMethod */ |
| 426 | 0, /* xRename */ |
| 427 | 0, /* xSavepoint */ |
| 428 | 0, /* xRelease */ |
| 429 | 0, /* xRollbackTo */ |
| 430 | 0, /* xShadowName */ |
| 431 | 0 /* xIntegrity */ |
| 432 | }; |
| 433 | return sqlite3_create_module(db, "sqlite_btreeinfo", &binfo_module, 0); |
| 434 | } |
| 435 | |
| 436 | #ifdef _WIN32 |
| 437 | __declspec(dllexport) |
| 438 | #endif |
| 439 | int sqlite3_btreeinfo_init( |
| 440 | sqlite3 *db, |
| 441 | char **pzErrMsg, |
| 442 | const sqlite3_api_routines *pApi |
| 443 | ){ |
| 444 | SQLITE_EXTENSION_INIT2(pApi); |
| 445 | return sqlite3BinfoRegister(db); |
| 446 | } |