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 / diskused.c
| 1 | /* |
| 2 | ** 2026-04-13 |
| 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 extension implements an SQL function: |
| 14 | ** |
| 15 | ** diskused(X) |
| 16 | ** |
| 17 | ** Where X is the schema name (typically 'main'). The output is text |
| 18 | ** that describes how much filesystem space the various tables and indexes |
| 19 | ** of the database consume. |
| 20 | ** |
| 21 | ** This function is a replacement for the (now deprecated) |
| 22 | ** "sqlite3_analyzer" utility program. This function is built |
| 23 | ** into the CLI and is used to implement the ".diskused" command there. |
| 24 | */ |
| 25 | #include "sqlite3ext.h" |
| 26 | SQLITE_EXTENSION_INIT1 |
| 27 | #include <assert.h> |
| 28 | #include <string.h> |
| 29 | #include <ctype.h> |
| 30 | #include <math.h> |
| 31 | |
| 32 | /* |
| 33 | ** State information for the analysis |
| 34 | */ |
| 35 | typedef struct DiskUsed DiskUsed; |
| 36 | struct DiskUsed { |
| 37 | sqlite3 *db; /* Database connection */ |
| 38 | sqlite3_context *context; /* SQL function context */ |
| 39 | sqlite3_str *pOut; /* Write output here */ |
| 40 | char *zSU; /* Name of the temp.space_used table */ |
| 41 | const char *zSchema; /* Schema to be analyzed */ |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | ** Free all resources that the DiskUsed object references and |
| 46 | ** reset the DiskUsed object. |
| 47 | ** |
| 48 | ** Call this routine multiple times on the same DiskUsed object |
| 49 | ** is a harmless no-op, as long as the memory for the object itself |
| 50 | ** has not been freed. |
| 51 | */ |
| 52 | static void diskusedReset(DiskUsed *p){ |
| 53 | if( p->zSU ){ |
| 54 | char *zSql = sqlite3_mprintf("DROP TABLE temp.%s;", p->zSU); |
| 55 | if( zSql ){ |
| 56 | sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 57 | sqlite3_free(zSql); |
| 58 | } |
| 59 | } |
| 60 | sqlite3_str_free(p->pOut); |
| 61 | sqlite3_free(p->zSU); |
| 62 | memset(p, 0, sizeof(*p)); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | ** Report an error using formatted text. If zFormat==NULL then report |
| 67 | ** an OOM error. |
| 68 | */ |
| 69 | static void diskusedError(DiskUsed *p, const char *zFormat, ...){ |
| 70 | char *zErr; |
| 71 | if( zFormat ){ |
| 72 | va_list ap; |
| 73 | va_start(ap, zFormat); |
| 74 | zErr = sqlite3_vmprintf(zFormat, ap); |
| 75 | va_end(ap); |
| 76 | }else{ |
| 77 | zErr = 0; |
| 78 | } |
| 79 | if( zErr==0 ){ |
| 80 | sqlite3_result_error_nomem(p->context); |
| 81 | }else{ |
| 82 | sqlite3_result_error(p->context, zErr, -1); |
| 83 | sqlite3_free(zErr); |
| 84 | } |
| 85 | diskusedReset(p); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | ** Prepare and return an SQL statement. |
| 90 | */ |
| 91 | static sqlite3_stmt *diskusedVPrep(DiskUsed *p, const char *zFmt, va_list ap){ |
| 92 | char *zSql; |
| 93 | int rc; |
| 94 | sqlite3_stmt *pStmt = 0; |
| 95 | zSql = sqlite3_vmprintf(zFmt, ap); |
| 96 | if( zSql==0 ){ diskusedError(p,0); return 0; } |
| 97 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 98 | if( rc ){ |
| 99 | diskusedError(p, "SQL parse error: %s\nOriginal SQL: %s", |
| 100 | sqlite3_errmsg(p->db), zSql); |
| 101 | sqlite3_finalize(pStmt); |
| 102 | diskusedReset(p); |
| 103 | pStmt = 0; |
| 104 | } |
| 105 | sqlite3_free(zSql); |
| 106 | return pStmt; |
| 107 | } |
| 108 | static sqlite3_stmt *diskusedPrepare(DiskUsed *p, const char *zFormat, ...){ |
| 109 | va_list ap; |
| 110 | sqlite3_stmt *pStmt = 0; |
| 111 | va_start(ap, zFormat); |
| 112 | pStmt = diskusedVPrep(p,zFormat,ap); |
| 113 | va_end(ap); |
| 114 | return pStmt; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | ** If rc is something other than SQLITE_DONE or SQLITE_OK, then report |
| 119 | ** an error and return true. |
| 120 | ** |
| 121 | ** If rc is SQLITE_DONE or SQLITE_OK, then return false. |
| 122 | ** |
| 123 | ** The prepared statement is closed in either case. |
| 124 | */ |
| 125 | static int diskusedStmtFinish(DiskUsed *p, int rc, sqlite3_stmt *pStmt){ |
| 126 | if( rc==SQLITE_DONE ){ |
| 127 | rc = SQLITE_OK; |
| 128 | } |
| 129 | if( rc!=SQLITE_OK || (rc = sqlite3_reset(pStmt))!=SQLITE_OK ){ |
| 130 | diskusedError(p, "SQL run-time error: %s\nOriginal SQL: %s", |
| 131 | sqlite3_errmsg(p->db), sqlite3_sql(pStmt)); |
| 132 | diskusedReset(p); |
| 133 | } |
| 134 | sqlite3_finalize(pStmt); |
| 135 | return rc; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | ** Run SQL. Return the number of errors. |
| 140 | */ |
| 141 | static int diskusedSql(DiskUsed *p, const char *zFormat, ...){ |
| 142 | va_list ap; |
| 143 | int rc; |
| 144 | sqlite3_stmt *pStmt = 0; |
| 145 | va_start(ap, zFormat); |
| 146 | pStmt = diskusedVPrep(p,zFormat,ap); |
| 147 | va_end(ap); |
| 148 | if( pStmt==0 ) return 1; |
| 149 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){} |
| 150 | if( rc==SQLITE_DONE ){ |
| 151 | rc = SQLITE_OK; |
| 152 | }else{ |
| 153 | diskusedError(p, "SQL run-time error: %s\nOriginal SQL: %s", |
| 154 | sqlite3_errmsg(p->db), sqlite3_sql(pStmt)); |
| 155 | diskusedReset(p); |
| 156 | } |
| 157 | sqlite3_finalize(pStmt); |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | ** Run an SQL query that returns an integer. Write that integer |
| 163 | ** into *piRes. Return the number of errors. |
| 164 | */ |
| 165 | static int diskusedSqlInt( |
| 166 | DiskUsed *p, |
| 167 | sqlite3_int64 *piRes, |
| 168 | const char *zFormat, ... |
| 169 | ){ |
| 170 | va_list ap; |
| 171 | int rc; |
| 172 | sqlite3_stmt *pStmt = 0; |
| 173 | va_start(ap, zFormat); |
| 174 | pStmt = diskusedVPrep(p,zFormat,ap); |
| 175 | va_end(ap); |
| 176 | if( pStmt==0 ) return 1; |
| 177 | rc = sqlite3_step(pStmt); |
| 178 | if( rc==SQLITE_ROW ){ |
| 179 | *piRes = sqlite3_column_int64(pStmt, 0); |
| 180 | rc = SQLITE_OK; |
| 181 | }else if( rc==SQLITE_DONE ){ |
| 182 | rc = SQLITE_OK; |
| 183 | }else{ |
| 184 | if( p->db ){ |
| 185 | /* p->db is NULL if there was some prior error */ |
| 186 | diskusedError(p, "SQL run-time error: %s\nOriginal SQL: %s", |
| 187 | sqlite3_errmsg(p->db), sqlite3_sql(pStmt)); |
| 188 | } |
| 189 | diskusedReset(p); |
| 190 | } |
| 191 | sqlite3_finalize(pStmt); |
| 192 | return rc; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | ** Add to the output a title line that contains the text determined |
| 197 | ** by the format string. If the output is initially empty, begin |
| 198 | ** the title line with "/" so that it forms the beginning of a C-style |
| 199 | ** comment. Otherwise begin with a new-line. Always finish with a |
| 200 | ** newline. |
| 201 | */ |
| 202 | static void diskusedTitle(DiskUsed *p, const char *zFormat, ...){ |
| 203 | char *zFirst; |
| 204 | char *zTitle; |
| 205 | size_t nTitle; |
| 206 | va_list ap; |
| 207 | va_start(ap, zFormat); |
| 208 | zTitle = sqlite3_vmprintf(zFormat, ap); |
| 209 | va_end(ap); |
| 210 | if( zTitle==0 ){ |
| 211 | diskusedError(p, 0); |
| 212 | return; |
| 213 | } |
| 214 | zFirst = sqlite3_str_length(p->pOut)==0 ? "/" : "\n*"; |
| 215 | nTitle = strlen(zTitle); |
| 216 | if( nTitle>=75 ){ |
| 217 | sqlite3_str_appendf(p->pOut, "%s** %z\n\n", zFirst, zTitle); |
| 218 | }else{ |
| 219 | int nExtra = 74 - (int)nTitle; |
| 220 | sqlite3_str_appendf(p->pOut, "%s** %z %.*c\n\n", zFirst, zTitle, |
| 221 | nExtra, '*'); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | ** Add an output line that begins with the zDesc text extended out to |
| 227 | ** 50 columns with "." characters, and followed by whatever text is |
| 228 | ** described by zFormat. |
| 229 | */ |
| 230 | static void diskusedLine( |
| 231 | DiskUsed *p, /* DiskUsed context */ |
| 232 | const char *zDesc, /* Description */ |
| 233 | const char *zFormat, /* Argument to the description */ |
| 234 | ... |
| 235 | ){ |
| 236 | char *zTxt; |
| 237 | size_t nDesc; |
| 238 | va_list ap; |
| 239 | va_start(ap, zFormat); |
| 240 | zTxt = sqlite3_vmprintf(zFormat, ap); |
| 241 | va_end(ap); |
| 242 | if( zTxt==0 ){ |
| 243 | diskusedError(p, 0); |
| 244 | return; |
| 245 | } |
| 246 | nDesc = zDesc ? strlen(zDesc) : 0; |
| 247 | if( nDesc>=50 ){ |
| 248 | sqlite3_str_appendf(p->pOut, "%s %z", zDesc, zTxt); |
| 249 | }else{ |
| 250 | int nExtra = 50 - (int)nDesc; |
| 251 | sqlite3_str_appendf(p->pOut, "%s%.*c %z", zDesc, nExtra, '.', zTxt); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | ** Write a percentage into the output. The number written should show |
| 257 | ** two or three significant digits, with the decimal point being the fourth |
| 258 | ** character. |
| 259 | */ |
| 260 | static void diskusedPercent( |
| 261 | DiskUsed *p, /* Context of the disk-usage analysis */ |
| 262 | sqlite3_int64 num, /* Numerator of the fraction */ |
| 263 | sqlite3_int64 denom /* Denominator of the fraction. Might be zero! */ |
| 264 | ){ |
| 265 | char zNum[100]; |
| 266 | char *zDP; |
| 267 | int nLeadingDigit; |
| 268 | int sz; |
| 269 | double r; |
| 270 | if( num==0 ){ |
| 271 | r = 0.0; |
| 272 | }else if( denom==0 ){ |
| 273 | sqlite3_str_appendchar(p->pOut, 1, '\n'); |
| 274 | return; |
| 275 | }else{ |
| 276 | r = num*100.0/(double)denom; |
| 277 | } |
| 278 | if( r>0.0 && r<0.0001 ){ |
| 279 | /* Avoid exponential notation for very small percentages */ |
| 280 | sqlite3_snprintf(sizeof(zNum)-5, zNum, "<0.0001"); |
| 281 | }else{ |
| 282 | sqlite3_snprintf(sizeof(zNum)-5, zNum, r>=10.0 ? "%.3g" :"%.2g", r); |
| 283 | } |
| 284 | sz = (int)strlen(zNum); |
| 285 | zDP = strchr(zNum, '.'); |
| 286 | if( zDP==0 ){ |
| 287 | memcpy(zNum+sz,".0",3); |
| 288 | nLeadingDigit = sz; |
| 289 | sz += 2; |
| 290 | }else{ |
| 291 | nLeadingDigit = (int)(zDP - zNum); |
| 292 | } |
| 293 | if( nLeadingDigit<3 ){ |
| 294 | sqlite3_str_appendchar(p->pOut, 3-nLeadingDigit, ' '); |
| 295 | } |
| 296 | sqlite3_str_append(p->pOut, zNum, sz); |
| 297 | sqlite3_str_append(p->pOut, "%\n", 2); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | ** Create a subreport on a subset of tables and/or indexes. |
| 302 | ** |
| 303 | ** The title if the subreport is given by zTitle. zWhere is |
| 304 | ** a boolean expression that can go in the WHERE clause to select |
| 305 | ** the relevant rows of the s.zSU table. |
| 306 | */ |
| 307 | static int diskusedSubreport( |
| 308 | DiskUsed *p, /* DiskUsed context */ |
| 309 | char *zTitle, /* Title for this subreport */ |
| 310 | char *zWhere, /* WHERE clause for this subreport */ |
| 311 | sqlite3_int64 pgsz, /* Database page size */ |
| 312 | sqlite3_int64 nPage /* Number of pages in entire database */ |
| 313 | ){ |
| 314 | sqlite3_stmt *pStmt; /* Statement to query p->zSU */ |
| 315 | sqlite3_int64 nentry; /* Number of btree entires */ |
| 316 | sqlite3_int64 payload; /* Payload in bytes */ |
| 317 | sqlite3_int64 ovfl_payload; /* overflow payload in bytes */ |
| 318 | sqlite3_int64 mx_payload; /* largest individual payload */ |
| 319 | sqlite3_int64 ovfl_cnt; /* Number entries using overflow */ |
| 320 | sqlite3_int64 leaf_pages; /* Leaf pages */ |
| 321 | sqlite3_int64 int_pages; /* internal pages */ |
| 322 | sqlite3_int64 ovfl_pages; /* overflow pages */ |
| 323 | sqlite3_int64 leaf_unused; /* unused bytes on leaf pages */ |
| 324 | sqlite3_int64 int_unused; /* unused bytes on internal pages */ |
| 325 | sqlite3_int64 ovfl_unused; /* unused bytes on overflow pages */ |
| 326 | sqlite3_int64 int_cell; /* B-tree entries on internal pages */ |
| 327 | sqlite3_int64 depth; /* btree depth */ |
| 328 | sqlite3_int64 cnt; /* Number of s.zSU entries that match */ |
| 329 | sqlite3_int64 storage; /* Total bytes */ |
| 330 | sqlite3_int64 total_pages; /* Total page count */ |
| 331 | sqlite3_int64 total_unused; /* Total unused bytes */ |
| 332 | sqlite3_int64 total_meta; /* Total metadata */ |
| 333 | int rc; |
| 334 | |
| 335 | if( zTitle==0 || zWhere==0 ){ |
| 336 | diskusedError(p, 0); |
| 337 | return SQLITE_NOMEM; |
| 338 | } |
| 339 | pStmt = diskusedPrepare(p, |
| 340 | "SELECT\n" |
| 341 | " sum(if(is_without_rowid OR is_index,nentry,leaf_entries)),\n" /* 0 */ |
| 342 | " sum(payload),\n" /* 1 */ |
| 343 | " sum(ovfl_payload),\n" /* 2 */ |
| 344 | " max(mx_payload),\n" /* 3 */ |
| 345 | " sum(ovfl_cnt),\n" /* 4 */ |
| 346 | " sum(leaf_pages),\n" /* 5 */ |
| 347 | " sum(int_pages),\n" /* 6 */ |
| 348 | " sum(ovfl_pages),\n" /* 7 */ |
| 349 | " sum(leaf_unused),\n" /* 8 */ |
| 350 | " sum(int_unused),\n" /* 9 */ |
| 351 | " sum(ovfl_unused),\n" /* 10 */ |
| 352 | " max(depth),\n" /* 11 */ |
| 353 | " count(*),\n" /* 12 */ |
| 354 | " sum(int_entries)\n" /* 13 */ |
| 355 | " FROM temp.%s WHERE %s", |
| 356 | p->zSU, zWhere); |
| 357 | if( pStmt==0 ) return 1; |
| 358 | rc = sqlite3_step(pStmt); |
| 359 | if( rc==SQLITE_ROW ){ |
| 360 | diskusedTitle(p, "%s", zTitle); |
| 361 | |
| 362 | nentry = sqlite3_column_int64(pStmt, 0); |
| 363 | payload = sqlite3_column_int64(pStmt, 1); |
| 364 | ovfl_payload = sqlite3_column_int64(pStmt, 2); |
| 365 | mx_payload = sqlite3_column_int64(pStmt, 3); |
| 366 | ovfl_cnt = sqlite3_column_int64(pStmt, 4); |
| 367 | leaf_pages = sqlite3_column_int64(pStmt, 5); |
| 368 | int_pages = sqlite3_column_int64(pStmt, 6); |
| 369 | ovfl_pages = sqlite3_column_int64(pStmt, 7); |
| 370 | leaf_unused = sqlite3_column_int64(pStmt, 8); |
| 371 | int_unused = sqlite3_column_int64(pStmt, 9); |
| 372 | ovfl_unused = sqlite3_column_int64(pStmt, 10); |
| 373 | depth = sqlite3_column_int64(pStmt, 11); |
| 374 | cnt = sqlite3_column_int64(pStmt, 12); |
| 375 | int_cell = sqlite3_column_int64(pStmt, 13); |
| 376 | rc = SQLITE_DONE; |
| 377 | |
| 378 | total_pages = leaf_pages + int_pages + ovfl_pages; |
| 379 | diskusedLine(p, "Percentage of total database", "%.3g%%\n", |
| 380 | (total_pages*100.0)/(double)nPage); |
| 381 | diskusedLine(p, "Number of entries", "%lld\n", nentry); |
| 382 | storage = total_pages*pgsz; |
| 383 | diskusedLine(p, "Bytes of storage consumed", "%lld\n", storage); |
| 384 | diskusedLine(p, "Bytes of payload", "%-11lld ", payload); |
| 385 | diskusedPercent(p, payload, storage); |
| 386 | if( ovfl_cnt>0 ){ |
| 387 | diskusedLine(p, "Bytes of payload in overflow","%-11lld ",ovfl_payload); |
| 388 | diskusedPercent(p, ovfl_payload, payload); |
| 389 | } |
| 390 | total_unused = leaf_unused + int_unused + ovfl_unused; |
| 391 | total_meta = storage - payload - total_unused; |
| 392 | diskusedLine(p, "Bytes of metadata","%-11lld ", total_meta); |
| 393 | diskusedPercent(p, total_meta, storage); |
| 394 | if( cnt==1 ){ |
| 395 | diskusedLine(p, "B-tree depth", "%lld\n", depth); |
| 396 | if( int_cell>1 ){ |
| 397 | diskusedLine(p, "Average fanout", "%.1f\n", |
| 398 | (double)(int_cell+int_pages)/(double)int_pages); |
| 399 | } |
| 400 | } |
| 401 | if( nentry>0 ){ |
| 402 | diskusedLine(p, "Average payload per entry", "%.1f\n", |
| 403 | (double)payload/(double)nentry); |
| 404 | diskusedLine(p, "Average unused bytes per entry", "%.1f\n", |
| 405 | (double)total_unused/(double)nentry); |
| 406 | diskusedLine(p, "Average metadata per entry", "%.1f\n", |
| 407 | (double)total_meta/(double)nentry); |
| 408 | } |
| 409 | diskusedLine(p, "Maximum single-entry payload", "%lld\n", mx_payload); |
| 410 | if( nentry>0 ){ |
| 411 | diskusedLine(p, "Entries that use overflow", "%-11lld ", ovfl_cnt); |
| 412 | diskusedPercent(p, ovfl_cnt, nentry); |
| 413 | } |
| 414 | if( int_pages>0 ){ |
| 415 | diskusedLine(p, "Index pages used", "%lld\n", int_pages); |
| 416 | } |
| 417 | diskusedLine(p, "Primary pages used", "%lld\n", leaf_pages); |
| 418 | if( ovfl_cnt ){ |
| 419 | diskusedLine(p, "Overflow pages used", "%lld\n", ovfl_pages); |
| 420 | } |
| 421 | diskusedLine(p, "Total pages used", "%lld\n", total_pages); |
| 422 | if( int_pages>0 ){ |
| 423 | diskusedLine(p, "Unused bytes on index pages", "%lld\n", int_unused); |
| 424 | } |
| 425 | diskusedLine(p, "Unused bytes on primary pages", "%lld\n", leaf_unused); |
| 426 | if( ovfl_cnt ){ |
| 427 | diskusedLine(p, "Unused bytes on overflow pages", "%lld\n", ovfl_unused); |
| 428 | } |
| 429 | diskusedLine(p, "Unused bytes on all pages", "%-11lld ", total_unused); |
| 430 | diskusedPercent(p, total_unused, storage); |
| 431 | } |
| 432 | return diskusedStmtFinish(p, rc, pStmt); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | ** SQL Function: diskused(SCHEMA) |
| 437 | ** |
| 438 | ** Analyze the database schema named in the argument. Return text |
| 439 | ** containing the space utilization stats. |
| 440 | */ |
| 441 | static void diskusedFunc( |
| 442 | sqlite3_context *context, |
| 443 | int argc, |
| 444 | sqlite3_value **argv |
| 445 | ){ |
| 446 | int rc; |
| 447 | sqlite3_stmt *pStmt; |
| 448 | int n; |
| 449 | char *z; |
| 450 | sqlite3_int64 ii; |
| 451 | sqlite3_int64 pgsz; |
| 452 | sqlite3_int64 nPage; |
| 453 | sqlite3_int64 nPageInUse; |
| 454 | sqlite3_int64 nFreeList; |
| 455 | sqlite3_int64 nIndex; |
| 456 | sqlite3_int64 nWORowid; |
| 457 | DiskUsed s; |
| 458 | sqlite3_uint64 r[2]; |
| 459 | |
| 460 | (void)argc; |
| 461 | memset(&s, 0, sizeof(s)); |
| 462 | s.db = sqlite3_context_db_handle(context); |
| 463 | s.context = context; |
| 464 | s.pOut = sqlite3_str_new(0); |
| 465 | if( sqlite3_str_errcode(s.pOut) ){ |
| 466 | diskusedError(&s, 0); |
| 467 | return; |
| 468 | } |
| 469 | s.zSchema = (const char*)sqlite3_value_text(argv[0]); |
| 470 | if( s.zSchema==0 ){ |
| 471 | s.zSchema = "main"; |
| 472 | }else if( sqlite3_strlike("temp",s.zSchema,0)==0 ){ |
| 473 | diskusedReset(&s); |
| 474 | sqlite3_result_text(context, "cannot analyze \"temp\"",-1,SQLITE_STATIC); |
| 475 | return; |
| 476 | } |
| 477 | ii = 0; |
| 478 | rc = diskusedSqlInt(&s,&ii,"SELECT 1 FROM pragma_database_list" |
| 479 | " WHERE name=%Q COLLATE nocase",s.zSchema); |
| 480 | if( rc || ii==0 ){ |
| 481 | diskusedReset(&s); |
| 482 | sqlite3_result_text(context,"no such database",-1,SQLITE_STATIC); |
| 483 | return; |
| 484 | } |
| 485 | sqlite3_randomness(sizeof(r), &r); |
| 486 | s.zSU = sqlite3_mprintf("diskused%016llx%016llx", r[0], r[1]); |
| 487 | if( s.zSU==0 ){ diskusedError(&s, 0); return; } |
| 488 | |
| 489 | /* The s.zSU table contains the data used for the analysis. |
| 490 | ** The table name contains 128-bits of randomness to avoid |
| 491 | ** collisions with preexisting tables in temp. |
| 492 | */ |
| 493 | rc = diskusedSql(&s, |
| 494 | "CREATE TABLE temp.%s(\n" |
| 495 | " name text, -- A table or index\n" |
| 496 | " tblname text, -- Table that owns name\n" |
| 497 | " is_index boolean, -- TRUE if it is an index\n" |
| 498 | " is_without_rowid boolean, -- TRUE if WITHOUT ROWID table\n" |
| 499 | " nentry int, -- Number of entries in the BTree\n" |
| 500 | " leaf_entries int, -- Number of leaf entries\n" |
| 501 | " depth int, -- Depth of the b-tree\n" |
| 502 | " payload int, -- Total data stored in this table/index\n" |
| 503 | " ovfl_payload int, -- Total data stored on overflow pages\n" |
| 504 | " ovfl_cnt int, -- Number of entries that use overflow\n" |
| 505 | " mx_payload int, -- Maximum payload size\n" |
| 506 | " int_pages int, -- Interior pages used\n" |
| 507 | " leaf_pages int, -- Leaf pages used\n" |
| 508 | " ovfl_pages int, -- Overflow pages used\n" |
| 509 | " int_unused int, -- Unused bytes on interior pages\n" |
| 510 | " leaf_unused int, -- Unused bytes on primary pages\n" |
| 511 | " ovfl_unused int, -- Unused bytes on overflow pages\n" |
| 512 | " int_entries int -- Btree cells on internal pages\n" |
| 513 | ");", |
| 514 | s.zSU |
| 515 | ); |
| 516 | if( rc ) return; |
| 517 | |
| 518 | /* Populate the s.zSU table |
| 519 | */ |
| 520 | rc = diskusedSql(&s, |
| 521 | "WITH\n" |
| 522 | " allidx(idxname) AS (\n" |
| 523 | " SELECT name FROM \"%w\".sqlite_schema WHERE type='index'\n" |
| 524 | " ),\n" |
| 525 | " allobj(allname,tblname,isidx,isworowid) AS (\n" |
| 526 | " SELECT 'sqlite_schema',\n" |
| 527 | " 'sqlite_schema',\n" |
| 528 | " 0,\n" |
| 529 | " 0\n" |
| 530 | " UNION ALL\n" |
| 531 | " SELECT name,\n" |
| 532 | " tbl_name,\n" |
| 533 | " type='index',\n" |
| 534 | " EXISTS(SELECT 1\n" |
| 535 | " FROM pragma_index_list(sqlite_schema.name,%Q)\n" |
| 536 | " WHERE pragma_index_list.origin='pk'\n" |
| 537 | " AND pragma_index_list.name NOT IN allidx)\n" |
| 538 | " FROM \"%w\".sqlite_schema\n" |
| 539 | " )\n" |
| 540 | "INSERT INTO temp.%s\n" |
| 541 | " SELECT\n" |
| 542 | " allname,\n" |
| 543 | " tblname,\n" |
| 544 | " isidx,\n" |
| 545 | " isworowid,\n" |
| 546 | " sum(ncell),\n" |
| 547 | " sum((pagetype='leaf')*ncell),\n" |
| 548 | " max((length(if(path GLOB '*+*','',path))+3)/4),\n" |
| 549 | " sum(payload),\n" |
| 550 | " sum((pagetype='overflow')*payload),\n" |
| 551 | " sum(path GLOB '*+000000'),\n" |
| 552 | " max(mx_payload),\n" |
| 553 | " sum(pagetype='internal'),\n" |
| 554 | " sum(pagetype='leaf'),\n" |
| 555 | " sum(pagetype='overflow'),\n" |
| 556 | " sum((pagetype='internal')*unused),\n" |
| 557 | " sum((pagetype='leaf')*unused),\n" |
| 558 | " sum((pagetype='overflow')*unused),\n" |
| 559 | " sum(if(pagetype='internal',ncell))\n" |
| 560 | " FROM allobj CROSS JOIN dbstat(%Q) \n" |
| 561 | " WHERE dbstat.name=allobj.allname\n" |
| 562 | " GROUP BY allname;\n", |
| 563 | s.zSchema, /* %w.sqlite_schema -- in allidx */ |
| 564 | s.zSchema, /* pragma_index_list(...,%Q) */ |
| 565 | s.zSchema, /* %w.sqlite_schema */ |
| 566 | s.zSU, /* INTO temp.%s */ |
| 567 | s.zSchema /* JOIN dbstat(%Q) */ |
| 568 | ); |
| 569 | if( rc ) return; |
| 570 | |
| 571 | nPage = 0; |
| 572 | rc = diskusedSqlInt(&s, &nPage, "PRAGMA \"%w\".page_count", s.zSchema); |
| 573 | if( rc ) return; |
| 574 | if( nPage<=0 ){ |
| 575 | /* Very brief reply for an empty database */ |
| 576 | diskusedReset(&s); |
| 577 | sqlite3_result_text(context, "empty database", -1, SQLITE_STATIC); |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | /* Begin generating the report */ |
| 582 | diskusedTitle(&s, "Database storage utilization report"); |
| 583 | pgsz = 0; |
| 584 | rc = diskusedSqlInt(&s, &pgsz, "PRAGMA \"%w\".page_size", s.zSchema); |
| 585 | if( rc ) return; |
| 586 | diskusedLine(&s, "Page size in bytes","%lld\n",pgsz); |
| 587 | diskusedLine(&s, "Pages in the database", "%lld\n", nPage); |
| 588 | |
| 589 | nPageInUse = 0; |
| 590 | rc = diskusedSqlInt(&s, &nPageInUse, |
| 591 | "SELECT sum(leaf_pages+int_pages+ovfl_pages) FROM temp.%s", s.zSU); |
| 592 | if( rc ) return; |
| 593 | diskusedLine(&s, "Pages that store data", "%-11lld ", nPageInUse); |
| 594 | diskusedPercent(&s, nPageInUse, nPage); |
| 595 | |
| 596 | nFreeList = 0; |
| 597 | rc = diskusedSqlInt(&s, &nFreeList, "PRAGMA \"%w\".freelist_count",s.zSchema); |
| 598 | if( rc ) return; |
| 599 | diskusedLine(&s, "Pages on the freelist", "%-11lld ", nFreeList); |
| 600 | diskusedPercent(&s, nFreeList, nPage); |
| 601 | |
| 602 | ii = 0; |
| 603 | rc = diskusedSqlInt(&s, &ii, "PRAGMA \"%w\".auto_vacuum", s.zSchema); |
| 604 | if( rc ) return; |
| 605 | if( ii==0 || nPage<=1 ){ |
| 606 | ii = 0; |
| 607 | }else{ |
| 608 | double rPtrsPerPage = pgsz/5; |
| 609 | double rAvPage = (nPage-1.0)/(rPtrsPerPage+1.0); |
| 610 | ii = (sqlite3_int64)ceil(rAvPage); |
| 611 | } |
| 612 | diskusedLine(&s, "Pages of auto-vacuum overhead", "%-11lld ", ii); |
| 613 | diskusedPercent(&s, ii, nPage); |
| 614 | |
| 615 | ii = 0; |
| 616 | rc = diskusedSqlInt(&s, &ii, |
| 617 | "SELECT count(*)+1 FROM \"%w\".sqlite_schema WHERE type='table'", |
| 618 | s.zSchema); |
| 619 | if( rc ) return; |
| 620 | diskusedLine(&s, "Number of tables", "%lld\n", ii); |
| 621 | nWORowid = 0; |
| 622 | rc = diskusedSqlInt(&s, &nWORowid, |
| 623 | "SELECT count(*) FROM \"%w\".pragma_table_list WHERE wr", |
| 624 | s.zSchema); |
| 625 | if( rc ) return; |
| 626 | if( nWORowid>0 ){ |
| 627 | diskusedLine(&s, "Number of WITHOUT ROWID tables", "%lld\n", nWORowid); |
| 628 | diskusedLine(&s, "Number of rowid tables", "%lld\n", ii - nWORowid); |
| 629 | } |
| 630 | nIndex = 0; |
| 631 | rc = diskusedSqlInt(&s, &nIndex, |
| 632 | "SELECT count(*) FROM \"%w\".sqlite_schema WHERE type='index'", |
| 633 | s.zSchema); |
| 634 | if( rc ) return; |
| 635 | diskusedLine(&s, "Number of indexes", "%lld\n", nIndex); |
| 636 | ii = 0; |
| 637 | rc = diskusedSqlInt(&s, &ii, |
| 638 | "SELECT count(*) FROM \"%w\".sqlite_schema" |
| 639 | " WHERE name GLOB 'sqlite_autoindex_*' AND type='index'", |
| 640 | s.zSchema); |
| 641 | if( rc ) return; |
| 642 | diskusedLine(&s, "Number of defined indexes", "%lld\n", nIndex - ii); |
| 643 | diskusedLine(&s, "Number of implied indexes", "%lld\n", ii); |
| 644 | diskusedLine(&s, "Size of the database in bytes", "%lld\n", pgsz*nPage); |
| 645 | ii = 0; |
| 646 | rc = diskusedSqlInt(&s, &ii, |
| 647 | "SELECT sum(payload) FROM temp.%s" |
| 648 | " WHERE NOT is_index AND name NOT LIKE 'sqlite_schema'", |
| 649 | s.zSU); |
| 650 | if( rc ) return; |
| 651 | diskusedLine(&s, "Bytes of payload", "%-11lld ", ii); |
| 652 | diskusedPercent(&s, ii, pgsz*nPage); |
| 653 | |
| 654 | diskusedTitle(&s, "Page counts for all tables with their indexes"); |
| 655 | pStmt = diskusedPrepare(&s, |
| 656 | "SELECT upper(tblname),\n" |
| 657 | " sum(int_pages+leaf_pages+ovfl_pages)\n" |
| 658 | " FROM temp.%s\n" |
| 659 | " WHERE tblname IS NOT NULL\n" |
| 660 | " GROUP BY 1\n" |
| 661 | " ORDER BY 2 DESC, 1;", |
| 662 | s.zSU); |
| 663 | if( pStmt==0 ) return; |
| 664 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ |
| 665 | sqlite3_int64 nn = sqlite3_column_int64(pStmt,1); |
| 666 | diskusedLine(&s, (const char*)sqlite3_column_text(pStmt,0), "%-11lld ", nn); |
| 667 | diskusedPercent(&s, nn, nPage); |
| 668 | } |
| 669 | if( diskusedStmtFinish(&s, rc, pStmt) ) return; |
| 670 | |
| 671 | diskusedTitle(&s, "Page counts for all tables and indexes separately"); |
| 672 | pStmt = diskusedPrepare(&s, |
| 673 | "SELECT upper(name),\n" |
| 674 | " sum(int_pages+leaf_pages+ovfl_pages)\n" |
| 675 | " FROM temp.%s\n" |
| 676 | " WHERE name IS NOT NULL\n" |
| 677 | " GROUP BY 1\n" |
| 678 | " ORDER BY 2 DESC, 1;", |
| 679 | s.zSU); |
| 680 | if( pStmt==0 ) return; |
| 681 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ |
| 682 | sqlite3_int64 nn = sqlite3_column_int64(pStmt,1); |
| 683 | diskusedLine(&s, (const char*)sqlite3_column_text(pStmt,0), "%-11lld ", nn); |
| 684 | diskusedPercent(&s, nn, nPage); |
| 685 | } |
| 686 | if( diskusedStmtFinish(&s, rc, pStmt) ) return; |
| 687 | |
| 688 | rc = diskusedSubreport(&s, "All tables and indexes", "1", pgsz, nPage); |
| 689 | if( rc ) return; |
| 690 | rc = diskusedSubreport(&s, "All tables", "NOT is_index", pgsz, nPage); |
| 691 | if( rc ) return; |
| 692 | if( nWORowid>0 ){ |
| 693 | rc = diskusedSubreport(&s, "All WITHOUT ROWID tables", "is_without_rowid", |
| 694 | pgsz, nPage); |
| 695 | if( rc ) return; |
| 696 | rc = diskusedSubreport(&s, "All rowid tables", |
| 697 | "NOT is_without_rowid AND NOT is_index", |
| 698 | pgsz, nPage); |
| 699 | if( rc ) return; |
| 700 | } |
| 701 | rc = diskusedSubreport(&s, "All indexes", "is_index", pgsz, nPage); |
| 702 | if( rc ) return; |
| 703 | |
| 704 | pStmt = diskusedPrepare(&s, |
| 705 | "SELECT upper(tblname), tblname, sum(is_index) FROM temp.%s" |
| 706 | " GROUP BY 1 ORDER BY 1", |
| 707 | s.zSU); |
| 708 | if( pStmt==0 ) return; |
| 709 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ |
| 710 | const char *zUpper = (const char*)sqlite3_column_text(pStmt, 0); |
| 711 | const char *zName = (const char*)sqlite3_column_text(pStmt, 1); |
| 712 | int nSubIndex = sqlite3_column_int(pStmt, 2); |
| 713 | if( nSubIndex==0 ){ |
| 714 | char *zTitle = sqlite3_mprintf("Table %s", zUpper); |
| 715 | char *zWhere = sqlite3_mprintf("name=%Q", zName); |
| 716 | rc = diskusedSubreport(&s, zTitle, zWhere, pgsz, nPage); |
| 717 | sqlite3_free(zTitle); |
| 718 | sqlite3_free(zWhere); |
| 719 | if( rc ) break; |
| 720 | }else{ |
| 721 | sqlite3_stmt *pS2; |
| 722 | char *zTitle = sqlite3_mprintf("Table %s and all its indexes", zUpper); |
| 723 | char *zWhere = sqlite3_mprintf("tblname=%Q", zName); |
| 724 | rc = diskusedSubreport(&s, zTitle, zWhere, pgsz, nPage); |
| 725 | sqlite3_free(zTitle); |
| 726 | sqlite3_free(zWhere); |
| 727 | if( rc ) break; |
| 728 | zTitle = sqlite3_mprintf("Table %s w/o any indexes", zUpper); |
| 729 | zWhere = sqlite3_mprintf("name=%Q", zName); |
| 730 | rc = diskusedSubreport(&s, zTitle, zWhere, pgsz, nPage); |
| 731 | sqlite3_free(zTitle); |
| 732 | sqlite3_free(zWhere); |
| 733 | if( rc ) break; |
| 734 | if( nSubIndex>1 ){ |
| 735 | zTitle = sqlite3_mprintf("All indexes of table %s", zUpper); |
| 736 | zWhere = sqlite3_mprintf("tblname=%Q AND is_index", zName); |
| 737 | rc = diskusedSubreport(&s, zTitle, zWhere, pgsz, nPage); |
| 738 | sqlite3_free(zTitle); |
| 739 | sqlite3_free(zWhere); |
| 740 | if( rc ) break; |
| 741 | } |
| 742 | pS2 = diskusedPrepare(&s, |
| 743 | "SELECT name, upper(name) FROM temp.%s" |
| 744 | " WHERE is_index AND tblname=%Q", |
| 745 | s.zSU, zName); |
| 746 | if( pS2==0 ){ |
| 747 | rc = SQLITE_NOMEM; |
| 748 | break; |
| 749 | } |
| 750 | while( (rc = sqlite3_step(pS2))==SQLITE_ROW ){ |
| 751 | const char *zU = (const char*)sqlite3_column_text(pS2, 1); |
| 752 | const char *zN = (const char*)sqlite3_column_text(pS2, 0); |
| 753 | zTitle = sqlite3_mprintf("Index %s", zU); |
| 754 | zWhere = sqlite3_mprintf("name=%Q", zN); |
| 755 | rc = diskusedSubreport(&s, zTitle, zWhere, pgsz, nPage); |
| 756 | sqlite3_free(zTitle); |
| 757 | sqlite3_free(zWhere); |
| 758 | if( rc ) break; |
| 759 | } |
| 760 | rc = diskusedStmtFinish(&s, rc, pS2); |
| 761 | if( rc ) break; |
| 762 | } |
| 763 | } |
| 764 | if( diskusedStmtFinish(&s, rc, pStmt) ) return; |
| 765 | |
| 766 | /* Append SQL statements that will recreate the raw data used for |
| 767 | ** the analysis. |
| 768 | */ |
| 769 | diskusedTitle(&s, "Raw data used to generate this report"); |
| 770 | sqlite3_str_appendf(s.pOut, |
| 771 | "The following SQL will create a table named \"space_used\" which\n" |
| 772 | "contains most of the information used to generate the report above.\n" |
| 773 | ); |
| 774 | z = sqlite3_str_value(s.pOut); |
| 775 | while( z && (z = strstr(z,"*/"))!=0 ){ z[0] = '+'; } |
| 776 | sqlite3_str_appendf(s.pOut, |
| 777 | "*/\n" |
| 778 | "BEGIN;\n" |
| 779 | "CREATE TABLE space_used(\n" |
| 780 | " name text, -- A table or index\n" /* 0 */ |
| 781 | " tblname text, -- Table that owns name\n" /* 1 */ |
| 782 | " is_index boolean, -- TRUE if it is an index\n" /* 2 */ |
| 783 | " is_without_rowid boolean, -- TRUE if WITHOUT ROWID table\n" /* 3 */ |
| 784 | " nentry int, -- Number of entries in the BTree\n" /* 4 */ |
| 785 | " leaf_entries int, -- Number of leaf entries\n" /* 5 */ |
| 786 | " depth int, -- Depth of the b-tree\n" /* 6 */ |
| 787 | " payload int, -- Total data in this table/index\n" /* 7 */ |
| 788 | " ovfl_payload int, -- Total data on overflow pages\n" /* 8 */ |
| 789 | " ovfl_cnt int, -- Entries that use overflow\n" /* 9 */ |
| 790 | " mx_payload int, -- Maximum payload size\n" /* 10 */ |
| 791 | " int_pages int, -- Interior pages used\n" /* 11 */ |
| 792 | " leaf_pages int, -- Leaf pages used\n" /* 12 */ |
| 793 | " ovfl_pages int, -- Overflow pages used\n" /* 13 */ |
| 794 | " int_unused int, -- Unused bytes on interior pages\n" /* 14 */ |
| 795 | " leaf_unused int, -- Unused bytes on primary pages\n" /* 15 */ |
| 796 | " ovfl_unused int, -- Unused bytes on overflow pages\n" /* 16 */ |
| 797 | " int_entries int -- B-tree entries on internal pages\n"/* 17 */ |
| 798 | ");\n" |
| 799 | "INSERT INTO space_used VALUES\n" |
| 800 | ); |
| 801 | pStmt = diskusedPrepare(&s, |
| 802 | "SELECT quote(name), quote(tblname),\n" /* 0..1 */ |
| 803 | " is_index, is_without_rowid, nentry, leaf_entries,\n" /* 2..5 */ |
| 804 | " depth, payload, ovfl_payload, ovfl_cnt, mx_payload,\n" /* 6..10 */ |
| 805 | " int_pages, leaf_pages, ovfl_pages, int_unused,\n" /* 11..14 */ |
| 806 | " leaf_unused, ovfl_unused, int_entries\n" /* 15..17 */ |
| 807 | " FROM temp.%s;", |
| 808 | s.zSU); |
| 809 | if( pStmt==0 ) return; |
| 810 | n = 0; |
| 811 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ |
| 812 | if( n++ ) sqlite3_str_appendf(s.pOut,",\n"); |
| 813 | sqlite3_str_appendf(s.pOut, |
| 814 | " (%s,%s,%lld,%lld,%lld,%lld,%lld,%lld,%lld," |
| 815 | "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld)", |
| 816 | sqlite3_column_text(pStmt, 0), |
| 817 | sqlite3_column_text(pStmt, 1), |
| 818 | sqlite3_column_int64(pStmt, 2), |
| 819 | sqlite3_column_int64(pStmt, 3), |
| 820 | sqlite3_column_int64(pStmt, 4), |
| 821 | sqlite3_column_int64(pStmt, 5), |
| 822 | sqlite3_column_int64(pStmt, 6), |
| 823 | sqlite3_column_int64(pStmt, 7), |
| 824 | sqlite3_column_int64(pStmt, 8), |
| 825 | sqlite3_column_int64(pStmt, 9), |
| 826 | sqlite3_column_int64(pStmt, 10), |
| 827 | sqlite3_column_int64(pStmt, 11), |
| 828 | sqlite3_column_int64(pStmt, 12), |
| 829 | sqlite3_column_int64(pStmt, 13), |
| 830 | sqlite3_column_int64(pStmt, 14), |
| 831 | sqlite3_column_int64(pStmt, 15), |
| 832 | sqlite3_column_int64(pStmt, 16), |
| 833 | sqlite3_column_int64(pStmt, 17)); |
| 834 | } |
| 835 | if( rc!=SQLITE_DONE ){ |
| 836 | diskusedError(&s, "SQL run-time error: %s\nSQL: %s", |
| 837 | sqlite3_errmsg(s.db), sqlite3_sql(pStmt)); |
| 838 | sqlite3_finalize(pStmt); |
| 839 | return; |
| 840 | } |
| 841 | sqlite3_str_appendf(s.pOut,";\nCOMMIT;"); |
| 842 | sqlite3_finalize(pStmt); |
| 843 | |
| 844 | if( sqlite3_str_length(s.pOut) ){ |
| 845 | sqlite3_result_text(context, sqlite3_str_finish(s.pOut), -1, |
| 846 | sqlite3_free); |
| 847 | s.pOut = 0; |
| 848 | } |
| 849 | diskusedReset(&s); |
| 850 | } |
| 851 | |
| 852 | |
| 853 | #ifdef _WIN32 |
| 854 | __declspec(dllexport) |
| 855 | #endif |
| 856 | int sqlite3_diskused_init( |
| 857 | sqlite3 *db, |
| 858 | char **pzErrMsg, |
| 859 | const sqlite3_api_routines *pApi |
| 860 | ){ |
| 861 | int rc = SQLITE_OK; |
| 862 | SQLITE_EXTENSION_INIT2(pApi); |
| 863 | (void)pzErrMsg; /* Unused parameter */ |
| 864 | rc = sqlite3_create_function(db, "diskused", 1, |
| 865 | SQLITE_UTF8|SQLITE_DIRECTONLY, |
| 866 | 0, diskusedFunc, 0, 0); |
| 867 | return rc; |
| 868 | } |