Skip to content

Commit 74eaa05

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 0cf3d41 commit 74eaa05

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/bin/scripts/vacuumdb.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ vacuum_one_database(ConnParams *cparams,
401401
int ntups;
402402
bool failed = false;
403403
bool tables_listed = false;
404-
bool has_where = false;
405404
const char *initcmd;
406405
const char *stage_commands[] = {
407406
"SET default_statistics_target=1; SET vacuum_cost_delay=0;",
@@ -551,11 +550,21 @@ vacuum_one_database(ConnParams *cparams,
551550
" LEFT JOIN pg_catalog.pg_class t"
552551
" ON c.reltoastrelid OPERATOR(pg_catalog.=) t.oid\n");
553552

554-
/* Used to match the tables listed by the user */
553+
/*
554+
* Used to match the tables listed by the user, completing the JOIN
555+
* clause.
556+
*/
555557
if (tables_listed)
556558
appendPQExpBufferStr(&catalog_query, " JOIN listed_tables"
557559
" ON listed_tables.table_oid OPERATOR(pg_catalog.=) c.oid\n");
558560

561+
/*
562+
* Exclude temporary tables, beginning the WHERE clause.
563+
*/
564+
appendPQExpBufferStr(&catalog_query,
565+
" WHERE c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
566+
567+
559568
/*
560569
* If no tables were listed, filter for the relevant relation types. If
561570
* tables were given via --table, don't bother filtering by relation type.
@@ -564,10 +573,9 @@ vacuum_one_database(ConnParams *cparams,
564573
*/
565574
if (!tables_listed)
566575
{
567-
appendPQExpBufferStr(&catalog_query, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array["
576+
appendPQExpBufferStr(&catalog_query, " AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
568577
CppAsString2(RELKIND_RELATION) ", "
569578
CppAsString2(RELKIND_MATVIEW) "])\n");
570-
has_where = true;
571579
}
572580

573581
/*
@@ -580,25 +588,23 @@ vacuum_one_database(ConnParams *cparams,
580588
if (vacopts->min_xid_age != 0)
581589
{
582590
appendPQExpBuffer(&catalog_query,
583-
" %s GREATEST(pg_catalog.age(c.relfrozenxid),"
591+
" AND GREATEST(pg_catalog.age(c.relfrozenxid),"
584592
" pg_catalog.age(t.relfrozenxid)) "
585593
" OPERATOR(pg_catalog.>=) '%d'::pg_catalog.int4\n"
586594
" AND c.relfrozenxid OPERATOR(pg_catalog.!=)"
587595
" '0'::pg_catalog.xid\n",
588-
has_where ? "AND" : "WHERE", vacopts->min_xid_age);
589-
has_where = true;
596+
vacopts->min_xid_age);
590597
}
591598

592599
if (vacopts->min_mxid_age != 0)
593600
{
594601
appendPQExpBuffer(&catalog_query,
595-
" %s GREATEST(pg_catalog.mxid_age(c.relminmxid),"
602+
" AND GREATEST(pg_catalog.mxid_age(c.relminmxid),"
596603
" pg_catalog.mxid_age(t.relminmxid)) OPERATOR(pg_catalog.>=)"
597604
" '%d'::pg_catalog.int4\n"
598605
" AND c.relminmxid OPERATOR(pg_catalog.!=)"
599606
" '0'::pg_catalog.xid\n",
600-
has_where ? "AND" : "WHERE", vacopts->min_mxid_age);
601-
has_where = true;
607+
vacopts->min_mxid_age);
602608
}
603609

604610
/*

0 commit comments

Comments
 (0)