]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add the .dbstat command to the CLI.
authordrh <>
Wed, 15 Apr 2026 11:41:50 +0000 (11:41 +0000)
committerdrh <>
Wed, 15 Apr 2026 11:41:50 +0000 (11:41 +0000)
FossilOrigin-Name: fdba76df2b3a5b4d56ba79f80fd8b16d5faebca1fb07a266262be2ea635e6f94

ext/misc/analyze.c
manifest
manifest.uuid
src/shell.c.in

index c017fb898ecef1e3287f3edbf59bf9a6aee98de2..472e727105f86ce939915d2e8737bb8e2425cc8f 100644 (file)
@@ -172,8 +172,11 @@ static int analysisSqlInt(
   }else if( rc==SQLITE_DONE ){
     rc = SQLITE_OK;
   }else{
-    analysisError(p, "SQL run-time error: %s\nOriginal SQL: %s",
-                  sqlite3_errmsg(p->db), sqlite3_sql(pStmt));
+    if( p->db ){
+      /* p->db is NULL if there was some prior error */
+      analysisError(p, "SQL run-time error: %s\nOriginal SQL: %s",
+                    sqlite3_errmsg(p->db), sqlite3_sql(pStmt));
+    }
     analysisReset(p);
   }
   sqlite3_finalize(pStmt);
@@ -365,7 +368,7 @@ static int analysisSubreport(
       analysisLine(p, "B-tree depth", "%lld\n", depth);
       if( int_cell>1 ){
         analysisLine(p, "Average fanout", "%.1f\n",
-                     (double)(int_cell+1)/(double)int_pages);
+                     (double)(int_cell+int_pages)/(double)int_pages);
       }
     }
     if( nentry>0 ){
@@ -436,6 +439,7 @@ static void analyzeFunc(
     s.zSchema = "main";
   }else if( sqlite3_strlike("temp",s.zSchema,0)==0 ){
     /* Attempt to analyze "temp" returns NULL */
+    analysisReset(&s);
     return;
   }
   i64 = 0;
@@ -443,6 +447,7 @@ static void analyzeFunc(
                              " WHERE name=%Q COLLATE nocase",s.zSchema);
   if( rc || i64==0 ){
     /* Return NULL the named schema does not exist */
+    analysisReset(&s);
     return;
   }
   sqlite3_randomness(sizeof(r), &r);
@@ -665,8 +670,8 @@ static void analyzeFunc(
   while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){
     const char *zUpper = (const char*)sqlite3_column_text(pStmt, 0);
     const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
-    int nIndex = sqlite3_column_int(pStmt, 2);
-    if( nIndex==0 ){
+    int nSubIndex = sqlite3_column_int(pStmt, 2);
+    if( nSubIndex==0 ){
       char *zTitle = sqlite3_mprintf("Table %s", zUpper);
       char *zWhere = sqlite3_mprintf("name=%Q", zName);
       rc = analysisSubreport(&s, zTitle, zWhere, pgsz, nPage);
@@ -687,7 +692,7 @@ static void analyzeFunc(
       sqlite3_free(zTitle);
       sqlite3_free(zWhere);
       if( rc ) break;
-      if( nIndex>1 ){
+      if( nSubIndex>1 ){
         zTitle = sqlite3_mprintf("All indexes of table %s", zUpper);
         zWhere = sqlite3_mprintf("tblname=%Q AND is_index", zName);
         rc = analysisSubreport(&s, zTitle, zWhere, pgsz, nPage);
index 9f332a5c7476fdfb6febe5333feea33135491a33..af7d2ac8df62d24cf5945f35d68501ac3313e7de 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Compute\sthe\saverage\sfanout\sfor\sb-trees\sof\sdepth\s2\sor\smore.
-D 2026-04-15T10:49:14.796
+C Add\sthe\s.dbstat\scommand\sto\sthe\sCLI.
+D 2026-04-15T11:41:50.450
 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -358,7 +358,7 @@ F ext/jni/src/tests/000-001-ignored.test e17e874c6ab3c437f1293d88093cf06286083b6
 F ext/jni/src/tests/900-001-fts.test bf0ce17a8d082773450e91f2388f5bbb2dfa316d0b676c313c637a91198090f0
 F ext/misc/README.md 6243cdc4d7eb791c41ef0716f3980b8b5f6aa8c61ff76a3958cbf0031c6ebfa7
 F ext/misc/amatch.c 8d237cc014b3736922c26a76a451050d244aa4980c47c531f368f817b1e77b49
-F ext/misc/analyze.c b4796025ba05b31d55150b735aaffcf1f1138ed112243b3d18884a31d049a7d1
+F ext/misc/analyze.c c329e7fdd23caebbe5362b25a416a7fda7e1524ddba03be12f1d2e4a903caa02
 F ext/misc/anycollseq.c 5ffdfde9829eeac52219136ad6aa7cd9a4edb3b15f4f2532de52f4a22525eddb
 F ext/misc/appendvfs.c 9642c7a194a2a25dca7ad3e36af24a0a46d7702168c4ad7e59c9f9b0e16a3824
 F ext/misc/base64.c 1445761667c16356e827fc6418294c869468be934429aaa8315035e76dd58acf
@@ -736,7 +736,7 @@ F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
 F src/resolve.c 928ff887f2a7c64275182060d94d06fdddbe32226c569781cf7e7edc6f58d7fd
 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
 F src/select.c ffe199f025a0dd74670d2a77232bdea364a4d7b36f32c64a6572d39ba6a11576
-F src/shell.c.in ed8e5819501faf4c390bb3db13931d3d75b8bff3deb31c952bbba14f4dbe4b16
+F src/shell.c.in e7be8a3828a04287f837b4d5c1204e0dae738b5d387abfb82a6e94c88c4b2981
 F src/sqlite.h.in 39d2e09114d2bdb7afd998f4a469c8f8cd065f8093835a7d0422f260fc78fb4f
 F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479
 F src/sqlite3ext.h 9788c301f95370fa30e808861f1d2e6f022a816ddbe2a4f67486784c1b31db2e
@@ -2199,8 +2199,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee
 F tool/warnings.sh a554d13f6e5cf3760f041b87939e3d616ec6961859c3245e8ef701d1eafc2ca2
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
 F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c
-P 953969c88149ea2f589942b98a0a96d907956319e5aa09e2d5a3ae76d3e7d70a
-R 5d654c3affeea1d5147e00173ff491e0
+P 2f8598bb09f32100f22dd1d9465f3fa0f7f8d81f5d67c34de9919708345f090b
+R 471b533e81c36baf308cf822e0d8f494
 U drh
-Z a1e7f4cc02c6ed8230a8c2c2df43d5f3
+Z 707bbe98ff3922436fb3c9c945220a8b
 # Remove this line to create a well-formed Fossil manifest.
index 990a560966cd421cad2304ede159084f30dc030b..70b21474cbb75d2362c563624558567b42b8f21e 100644 (file)
@@ -1 +1 @@
-2f8598bb09f32100f22dd1d9465f3fa0f7f8d81f5d67c34de9919708345f090b
+fdba76df2b3a5b4d56ba79f80fd8b16d5faebca1fb07a266262be2ea635e6f94
index 76d22b5662e11c06a86c196812dae4d8395b21a0..25ff57a85600422df8fed8a2836148513c5a048f 100644 (file)
@@ -299,6 +299,7 @@ INCLUDE ../ext/intck/sqlite3intck.h
 INCLUDE ../ext/intck/sqlite3intck.c
 INCLUDE ../ext/misc/stmtrand.c
 INCLUDE ../ext/misc/vfstrace.c
+INCLUDE ../ext/misc/analyze.c
 
 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
 #define SQLITE_SHELL_HAVE_RECOVER 1
@@ -3955,6 +3956,7 @@ static const char *(azHelp[]) = {
 #if SQLITE_SHELL_HAVE_RECOVER
   ".dbinfo ?DB?             Show status information about the database",
 #endif
+  ".dbstat ?SCHEMA?         Report database space and size stats",
   ".dbtotxt                 Hex dump of the database file",
   ".dump ?OBJECTS?          Render database content as SQL",
   "   Options:",
@@ -4778,6 +4780,7 @@ static void open_db(ShellState *p, int openFlags){
     sqlite3_regexp_init(p->db, 0, 0);
     sqlite3_ieee_init(p->db, 0, 0);
     sqlite3_series_init(p->db, 0, 0);
+    sqlite3_analyze_init(p->db, 0, 0);
 #ifndef SQLITE_SHELL_FIDDLE
     sqlite3_fileio_init(p->db, 0, 0);
     sqlite3_completion_init(p->db, 0, 0);
@@ -9571,12 +9574,42 @@ static int do_meta_command(const char *zLine, ShellState *p){
   if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
     rc = shell_dbinfo_command(p, nArg, azArg);
   }else
+#endif /* SQLITE_SHELL_HAVE_RECOVER */
 
-  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
+  if( c=='d' && n==6 && cli_strncmp(azArg[0], "dbstat", n)==0 ){
+    const char *zSchema = 0;
+    int ii;
+    char *zSql;
+    for(ii=1; ii<nArg; ii++){
+      const char *z = azArg[ii];
+      if( z[0]=='-' ){
+        dotCmdError(p, ii, "unknown option", 0);
+        rc = 1;
+        goto meta_command_exit;
+      }
+      if( zSchema ){
+        dotCmdError(p, ii, "unknown argument", 0);
+        rc = 1;
+        goto meta_command_exit;
+      }
+      zSchema = z;
+    }
+    zSql = sqlite3_mprintf("SELECT analyze(%Q)", zSchema);
+    shell_check_oom(zSql);
+    modePush(p);
+    modeChange(p, MODE_BATCH);
+    p->mode.spec.nLineLimit = 0;
+    p->mode.spec.nCharLimit = 0;
+    p->mode.spec.nTitleLimit = 0;
+    shell_exec(p, zSql, 0);
+    modePop(p);
+    sqlite3_free(zSql);
+  }else
+
+  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
     open_db(p, 0);
-    rc = recoverDatabaseCmd(p, nArg, azArg);
+    rc = shell_dbtotxt_command(p, nArg, azArg);
   }else
-#endif /* SQLITE_SHELL_HAVE_RECOVER */
 
   if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
     char *zLike = 0;
@@ -9708,11 +9741,6 @@ static int do_meta_command(const char *zLine, ShellState *p){
     }
   }else
 
-  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
-    open_db(p, 0);
-    rc = shell_dbtotxt_command(p, nArg, azArg);
-  }else
-
   if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
     if( nArg==2 ){
       if( p->mode.autoEQPtrace ){
@@ -10765,6 +10793,13 @@ static int do_meta_command(const char *zLine, ShellState *p){
   }else
 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
 
+#if SQLITE_SHELL_HAVE_RECOVER
+  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
+    open_db(p, 0);
+    rc = recoverDatabaseCmd(p, nArg, azArg);
+  }else
+#endif /* SQLITE_SHELL_HAVE_RECOVER */
+
 #ifndef SQLITE_SHELL_FIDDLE
   if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
     const char *zSrcFile;