vacuumdb: Make vacuumdb --analyze-only process partitioned tables.
authorFujii Masao <[email protected]>
Wed, 20 Aug 2025 04:16:06 +0000 (13:16 +0900)
committerFujii Masao <[email protected]>
Wed, 20 Aug 2025 04:16:06 +0000 (13:16 +0900)
vacuumdb should follow the behavior of the underlying VACUUM and ANALYZE
commands. When --analyze-only is used, it ought to analyze regular tables,
materialized views, and partitioned tables, just as ANALYZE (with no explicit
target tables) does. Otherwise, it should only process regular tables and
materialized views, since VACUUM skips partitioned tables when no targets
are given.

Previously, vacuumdb --analyze-only skipped partitioned tables. This was
inconsistent, and also inconvenient after pg_upgrade, where --analyze-only
is typically used to gather missing statistics.

This commit fixes the behavior so that vacuumdb --analyze-only also processes
partitioned tables. As a result, both vacuumdb --analyze-only and
ANALYZE (with no explicit targets) now analyze regular tables,
partitioned tables, and materialized views, but not foreign tables.

Because this is a nontrivial behavior change, it is applied only to master.

Reported-by: Zechman, Derek S <[email protected]>
Author: Laurenz Albe <[email protected]>
Co-authored-by: Mircea Cadariu <[email protected]>
Reviewed-by: Fujii Masao <[email protected]>
Discussion: https://postgr.es/m/CO1PR04MB8281387B9AD9DE30976966BBC045A%40CO1PR04MB8281.namprd04.prod.outlook.com

doc/src/sgml/ref/vacuumdb.sgml
src/bin/scripts/t/100_vacuumdb.pl
src/bin/scripts/vacuumdb.c

index c7d9dca17b867c9474fbdb0cfb0e8ab44683b27d..53147480515e6385156a809d8aba63ddbfa3aacb 100644 (file)
@@ -397,6 +397,15 @@ PostgreSQL documentation
         Multiple tables can be vacuumed by writing multiple
         <option>-t</option> switches.
        </para>
+       <para>
+        If no tables are specified with the <option>--table</option> option,
+        <application>vacuumdb</application> will clean all regular tables
+        and materialized views in the connected database.
+        If <option>--analyze-only</option> or
+        <option>--analyze-in-stages</option> is also specified,
+        it will analyze all regular tables, partitioned tables,
+        and materialized views (but not foreign tables).
+       </para>
        <tip>
         <para>
          If you specify columns, you probably have to escape the parentheses
index ff56a13b46bbbc88b4a15992801c2f1fb5cfc92d..240f0fdd3e5cb7d5c9c184bba5f935c1ece1994b 100644 (file)
@@ -340,4 +340,15 @@ $node->issues_sql_unlike(
    qr/statement:\ ANALYZE/sx,
    '--missing-stats-only with no missing partition stats');
 
+$node->safe_psql('postgres',
+   "CREATE TABLE parent_table (a INT) PARTITION BY LIST (a);\n"
+     . "CREATE TABLE child_table PARTITION OF parent_table FOR VALUES IN (1);\n"
+     . "INSERT INTO parent_table VALUES (1);\n");
+$node->issues_sql_like(
+   [
+       'vacuumdb', '--analyze-only', 'postgres'
+   ],
+   qr/statement: ANALYZE public.parent_table/s,
+   '--analyze-only updates statistics for partitioned tables');
+
 done_testing();
index 79b1096eb08c4a0a23dbb9a95ba9ee3c5f37fc9b..22093e50aa5edc6d15159d131bb283f5a11ff743 100644 (file)
@@ -911,10 +911,26 @@ retrieve_objects(PGconn *conn, vacuumingOptions *vacopts,
     */
    if ((objfilter & OBJFILTER_TABLE) == 0)
    {
-       appendPQExpBufferStr(&catalog_query,
-                            " AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
-                            CppAsString2(RELKIND_RELATION) ", "
-                            CppAsString2(RELKIND_MATVIEW) "])\n");
+       /*
+        * vacuumdb should generally follow the behavior of the underlying
+        * VACUUM and ANALYZE commands. If analyze_only is true, process
+        * regular tables, materialized views, and partitioned tables, just
+        * like ANALYZE (with no specific target tables) does. Otherwise,
+        * process only regular tables and materialized views, since VACUUM
+        * skips partitioned tables when no target tables are specified.
+        */
+       if (vacopts->analyze_only)
+           appendPQExpBufferStr(&catalog_query,
+                                " AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
+                                CppAsString2(RELKIND_RELATION) ", "
+                                CppAsString2(RELKIND_MATVIEW) ", "
+                                CppAsString2(RELKIND_PARTITIONED_TABLE) "])\n");
+       else
+           appendPQExpBufferStr(&catalog_query,
+                                " AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
+                                CppAsString2(RELKIND_RELATION) ", "
+                                CppAsString2(RELKIND_MATVIEW) "])\n");
+
    }
 
    /*