/ Check-in [eed701ef]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:In the "pgidx" command of the showdb utility, try to identify orphaned pages and show when pages have been zeroed out.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: eed701ef919c70d891727250be6a1b626aeb894c562d221e319ae6d74fc71d3d
User & Date: drh 2019-04-17 12:29:45
Context
2019-04-17
13:23
In the "pgidx" report from "showdb", also show the number of rows on each database btree page. check-in: 2bda9dc4 user: drh tags: trunk
12:29
In the "pgidx" command of the showdb utility, try to identify orphaned pages and show when pages have been zeroed out. check-in: eed701ef user: drh tags: trunk
12:07
Small performance improvement on the variable-length integer decoder: sqlite3GetVarint(). check-in: 5df2bf62 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to tool/showdb.c.

   831    831         a = fileRead((ovfl-1)*(sqlite3_int64)g.pagesize, 4);
   832    832         ovfl = decodeInt32(a);
   833    833         sqlite3_free(a);
   834    834       }
   835    835     }
   836    836   }
   837    837   
          838  +/*
          839  +** True if the memory is all zeros
          840  +*/
          841  +static int allZero(unsigned char *a, int n){
          842  +  while( n && (a++)[0]==0 ){ n--; }
          843  +  return n==0;
          844  +}
          845  +
   838    846   
   839    847   /*
   840         -** Describe the usages of a b-tree page
          848  +** Describe the usages of a b-tree page.
          849  +**
          850  +** If parent==0, then this is the root of a btree.  If parent<0 then
          851  +** this is an orphan page.
   841    852   */
   842    853   static void page_usage_btree(
   843    854     int pgno,             /* Page to describe */
   844    855     int parent,           /* Parent of this page.  0 for root pages */
   845    856     int idx,              /* Which child of the parent */
   846    857     const char *zName     /* Name of the table */
   847    858   ){
................................................................................
   850    861     int nCell;
   851    862     int i;
   852    863     int hdr = pgno==1 ? 100 : 0;
   853    864   
   854    865     if( pgno<=0 || pgno>g.mxPage ) return;
   855    866     a = fileRead((pgno-1)*g.pagesize, g.pagesize);
   856    867     switch( a[hdr] ){
          868  +    case 0: {
          869  +      if( allZero(a, g.pagesize) ){
          870  +        zType = "zeroed page";
          871  +      }else if( parent<0 ){
          872  +        return;
          873  +      }else{
          874  +        zType = "corrupt node";
          875  +      }
          876  +      break;
          877  +    }
   857    878       case 2:  zType = "interior node of index";  break;
   858    879       case 5:  zType = "interior node of table";  break;
   859    880       case 10: zType = "leaf of index";           break;
   860    881       case 13: zType = "leaf of table";           break;
          882  +    default: {
          883  +      if( parent<0 ) return;
          884  +      zType = "corrupt node";
          885  +    }
   861    886     }
   862         -  if( parent ){
          887  +  if( parent>0 ){
   863    888       page_usage_msg(pgno, "%s [%s], child %d of page %d",
   864    889                      zType, zName, idx, parent);
   865         -  }else{
          890  +  }else if( parent==0 ){
   866    891       page_usage_msg(pgno, "root %s [%s]", zType, zName);
          892  +  }else{
          893  +    page_usage_msg(pgno, "orphaned %s", zType);
   867    894     }
   868    895     nCell = a[hdr+3]*256 + a[hdr+4];
   869    896     if( a[hdr]==2 || a[hdr]==5 ){
   870    897       int cellstart = hdr+12;
   871    898       unsigned int child;
   872    899       for(i=0; i<nCell; i++){
   873    900         int ofst;
................................................................................
   984   1011       rc = sqlite3_finalize(pStmt);
   985   1012       if( rc==SQLITE_OK ) break;
   986   1013     }
   987   1014     sqlite3_close(db);
   988   1015   
   989   1016     /* Print the report and free memory used */
   990   1017     for(i=1; i<=g.mxPage; i++){
         1018  +    if( zPageUse[i]==0 ) page_usage_btree(i, -1, 0, 0);
   991   1019       printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???");
   992   1020       sqlite3_free(zPageUse[i]);
   993   1021     }
   994   1022     sqlite3_free(zPageUse);
   995   1023     zPageUse = 0;
   996   1024   }
   997   1025