Skip to content

Commit 1ab67c9

Browse files
committed
vacuumdb: Skip temporary tables in query to build list of relations
Running vacuumdb with a non-superuser while another user has created a temporary table would lead to a mid-flight permission failure, interrupting the operation. vacuum_rel() skips temporary relations of other backends, and it makes no sense for vacuumdb to know about these relations, so let's switch it to ignore temporary relations entirely. Adding a qual in the query based on relpersistence simplifies the generation of its WHERE clause in vacuum_one_database(), per se the removal of "has_where". Author: VaibhaveS, Michael Paquier Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/CAM_eQjwfAR=y3G1fGyS1U9FTmc+FyJm9amNfY2QCZBnDDbNPZg@mail.gmail.com Backpatch-through: 12
1 parent 7fdeaf5 commit 1ab67c9

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/bin/scripts/vacuumdb.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ vacuum_one_database(ConnParams *cparams,
491491
int ntups;
492492
bool failed = false;
493493
bool objects_listed = false;
494-
bool has_where = false;
495494
const char *initcmd;
496495
const char *stage_commands[] = {
497496
"SET default_statistics_target=1; SET vacuum_cost_delay=0;",
@@ -665,7 +664,10 @@ vacuum_one_database(ConnParams *cparams,
665664
" LEFT JOIN pg_catalog.pg_class t"
666665
" ON c.reltoastrelid OPERATOR(pg_catalog.=) t.oid\n");
667666

668-
/* Used to match the tables or schemas listed by the user */
667+
/*
668+
* Used to match the tables or schemas listed by the user, completing the
669+
* JOIN clause.
670+
*/
669671
if (objects_listed)
670672
{
671673
appendPQExpBufferStr(&catalog_query, " LEFT JOIN listed_objects"
@@ -676,14 +678,26 @@ vacuum_one_database(ConnParams *cparams,
676678
appendPQExpBufferStr(&catalog_query, "c.oid\n");
677679
else
678680
appendPQExpBufferStr(&catalog_query, "ns.oid\n");
681+
}
679682

683+
/*
684+
* Exclude temporary tables, beginning the WHERE clause.
685+
*/
686+
appendPQExpBufferStr(&catalog_query,
687+
" WHERE c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
688+
689+
/*
690+
* Used to match the tables or schemas listed by the user, for the WHERE
691+
* clause.
692+
*/
693+
if (objects_listed)
694+
{
680695
if (objfilter & OBJFILTER_SCHEMA_EXCLUDE)
681696
appendPQExpBuffer(&catalog_query,
682-
" WHERE listed_objects.object_oid IS NULL\n");
697+
" AND listed_objects.object_oid IS NULL\n");
683698
else
684699
appendPQExpBuffer(&catalog_query,
685-
" WHERE listed_objects.object_oid IS NOT NULL\n");
686-
has_where = true;
700+
" AND listed_objects.object_oid IS NOT NULL\n");
687701
}
688702

689703
/*
@@ -695,11 +709,9 @@ vacuum_one_database(ConnParams *cparams,
695709
if ((objfilter & OBJFILTER_TABLE) == 0)
696710
{
697711
appendPQExpBuffer(&catalog_query,
698-
" %s c.relkind OPERATOR(pg_catalog.=) ANY (array["
712+
" AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
699713
CppAsString2(RELKIND_RELATION) ", "
700-
CppAsString2(RELKIND_MATVIEW) "])\n",
701-
has_where ? "AND" : "WHERE");
702-
has_where = true;
714+
CppAsString2(RELKIND_MATVIEW) "])\n");
703715
}
704716

705717
/*
@@ -712,25 +724,23 @@ vacuum_one_database(ConnParams *cparams,
712724
if (vacopts->min_xid_age != 0)
713725
{
714726
appendPQExpBuffer(&catalog_query,
715-
" %s GREATEST(pg_catalog.age(c.relfrozenxid),"
727+
" AND GREATEST(pg_catalog.age(c.relfrozenxid),"
716728
" pg_catalog.age(t.relfrozenxid)) "
717729
" OPERATOR(pg_catalog.>=) '%d'::pg_catalog.int4\n"
718730
" AND c.relfrozenxid OPERATOR(pg_catalog.!=)"
719731
" '0'::pg_catalog.xid\n",
720-
has_where ? "AND" : "WHERE", vacopts->min_xid_age);
721-
has_where = true;
732+
vacopts->min_xid_age);
722733
}
723734

724735
if (vacopts->min_mxid_age != 0)
725736
{
726737
appendPQExpBuffer(&catalog_query,
727-
" %s GREATEST(pg_catalog.mxid_age(c.relminmxid),"
738+
" AND GREATEST(pg_catalog.mxid_age(c.relminmxid),"
728739
" pg_catalog.mxid_age(t.relminmxid)) OPERATOR(pg_catalog.>=)"
729740
" '%d'::pg_catalog.int4\n"
730741
" AND c.relminmxid OPERATOR(pg_catalog.!=)"
731742
" '0'::pg_catalog.xid\n",
732-
has_where ? "AND" : "WHERE", vacopts->min_mxid_age);
733-
has_where = true;
743+
vacopts->min_mxid_age);
734744
}
735745

736746
/*

0 commit comments

Comments
 (0)