Skip to content

Commit 6429e5b

Browse files
MasaoFujiimcadariu
andcommitted
vacuumdb: Make vacuumdb --analyze-only process partitioned tables.
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 <Derek.S.Zechman@snapon.com> Author: Laurenz Albe <laurenz.albe@cybertec.at> Co-authored-by: Mircea Cadariu <cadariu.mircea@gmail.com> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/CO1PR04MB8281387B9AD9DE30976966BBC045A%40CO1PR04MB8281.namprd04.prod.outlook.com
1 parent 3eec0e6 commit 6429e5b

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

doc/src/sgml/ref/vacuumdb.sgml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ PostgreSQL documentation
397397
Multiple tables can be vacuumed by writing multiple
398398
<option>-t</option> switches.
399399
</para>
400+
<para>
401+
If no tables are specified with the <option>--table</option> option,
402+
<application>vacuumdb</application> will clean all regular tables
403+
and materialized views in the connected database.
404+
If <option>--analyze-only</option> or
405+
<option>--analyze-in-stages</option> is also specified,
406+
it will analyze all regular tables, partitioned tables,
407+
and materialized views (but not foreign tables).
408+
</para>
400409
<tip>
401410
<para>
402411
If you specify columns, you probably have to escape the parentheses

src/bin/scripts/t/100_vacuumdb.pl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,15 @@
340340
qr/statement:\ ANALYZE/sx,
341341
'--missing-stats-only with no missing partition stats');
342342

343+
$node->safe_psql('postgres',
344+
"CREATE TABLE parent_table (a INT) PARTITION BY LIST (a);\n"
345+
. "CREATE TABLE child_table PARTITION OF parent_table FOR VALUES IN (1);\n"
346+
. "INSERT INTO parent_table VALUES (1);\n");
347+
$node->issues_sql_like(
348+
[
349+
'vacuumdb', '--analyze-only', 'postgres'
350+
],
351+
qr/statement: ANALYZE public.parent_table/s,
352+
'--analyze-only updates statistics for partitioned tables');
353+
343354
done_testing();

src/bin/scripts/vacuumdb.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,26 @@ retrieve_objects(PGconn *conn, vacuumingOptions *vacopts,
911911
*/
912912
if ((objfilter & OBJFILTER_TABLE) == 0)
913913
{
914-
appendPQExpBufferStr(&catalog_query,
915-
" AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
916-
CppAsString2(RELKIND_RELATION) ", "
917-
CppAsString2(RELKIND_MATVIEW) "])\n");
914+
/*
915+
* vacuumdb should generally follow the behavior of the underlying
916+
* VACUUM and ANALYZE commands. If analyze_only is true, process
917+
* regular tables, materialized views, and partitioned tables, just
918+
* like ANALYZE (with no specific target tables) does. Otherwise,
919+
* process only regular tables and materialized views, since VACUUM
920+
* skips partitioned tables when no target tables are specified.
921+
*/
922+
if (vacopts->analyze_only)
923+
appendPQExpBufferStr(&catalog_query,
924+
" AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
925+
CppAsString2(RELKIND_RELATION) ", "
926+
CppAsString2(RELKIND_MATVIEW) ", "
927+
CppAsString2(RELKIND_PARTITIONED_TABLE) "])\n");
928+
else
929+
appendPQExpBufferStr(&catalog_query,
930+
" AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
931+
CppAsString2(RELKIND_RELATION) ", "
932+
CppAsString2(RELKIND_MATVIEW) "])\n");
933+
918934
}
919935

920936
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy