Skip to content

Commit fb56a18

Browse files
vacuumdb: Fix excluding multiple schemas with -N
When specifying multiple schemas to exclude with -N parameters, none of the schemas are actually excluded (a single -N worked as expected). This fixes the catalog query to handle multiple exclusions and adds a test for this case. Backpatch to v16 where this was introduced. Author: Nathan Bossart <nathandbossart@gmail.com> Author: Kuwamura Masaki <kuwamura@db.is.i.nagoya-u.ac.jp> Reported-by: Kuwamura Masaki <kuwamura@db.is.i.nagoya-u.ac.jp> Discussion: https://postgr.es/m/CAMyC8qp9mXPQd5D6s6CJxvmignsbTqGZwDDB6VYJOn1A8WG38w@mail.gmail.com Backpatch-through: 16
1 parent 2e3dc8c commit fb56a18

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
CREATE INDEX i0 ON funcidx ((f1(x)));
113113
CREATE SCHEMA "Foo";
114114
CREATE TABLE "Foo".bar(id int);
115+
CREATE SCHEMA "Bar";
116+
CREATE TABLE "Bar".baz(id int);
115117
|);
116118
$node->command_ok([qw|vacuumdb -Z --table="need""q(uot"(")x") postgres|],
117119
'column list');
@@ -159,10 +161,21 @@
159161
[ 'vacuumdb', '--schema', '"Foo"', 'postgres' ],
160162
qr/VACUUM \(SKIP_DATABASE_STATS\) "Foo".bar/,
161163
'vacuumdb --schema');
164+
$node->issues_sql_like(
165+
[ 'vacuumdb', '--schema', '"Foo"', '--schema', '"Bar"', 'postgres' ],
166+
qr/VACUUM\ \(SKIP_DATABASE_STATS\)\ "Foo".bar
167+
.*VACUUM\ \(SKIP_DATABASE_STATS\)\ "Bar".baz
168+
/sx,
169+
'vacuumdb multiple --schema switches');
162170
$node->issues_sql_like(
163171
[ 'vacuumdb', '--exclude-schema', '"Foo"', 'postgres' ],
164-
qr/(?:(?!VACUUM "Foo".bar).)*/,
172+
qr/^(?!.*VACUUM \(SKIP_DATABASE_STATS\) "Foo".bar).*$/s,
165173
'vacuumdb --exclude-schema');
174+
$node->issues_sql_like(
175+
[ 'vacuumdb', '--exclude-schema', '"Foo"', '--exclude-schema', '"Bar"', 'postgres' ],
176+
qr/^(?!.*VACUUM\ \(SKIP_DATABASE_STATS\)\ "Foo".bar
177+
| VACUUM\ \(SKIP_DATABASE_STATS\)\ "Bar".baz).*$/sx,
178+
'vacuumdb multiple --exclude-schema switches');
166179
$node->command_fails_like(
167180
[ 'vacuumdb', '-N', 'pg_catalog', '-t', 'pg_class', 'postgres', ],
168181
qr/cannot vacuum specific table\(s\) and exclude schema\(s\) at the same time/,

src/bin/scripts/vacuumdb.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -678,18 +678,22 @@ vacuum_one_database(ConnParams *cparams,
678678
/* Used to match the tables or schemas listed by the user */
679679
if (objects_listed)
680680
{
681-
appendPQExpBufferStr(&catalog_query, " JOIN listed_objects"
682-
" ON listed_objects.object_oid ");
683-
684-
if (objfilter & OBJFILTER_SCHEMA_EXCLUDE)
685-
appendPQExpBufferStr(&catalog_query, "OPERATOR(pg_catalog.!=) ");
686-
else
687-
appendPQExpBufferStr(&catalog_query, "OPERATOR(pg_catalog.=) ");
681+
appendPQExpBufferStr(&catalog_query, " LEFT JOIN listed_objects"
682+
" ON listed_objects.object_oid"
683+
" OPERATOR(pg_catalog.=) ");
688684

689685
if (objfilter & OBJFILTER_TABLE)
690686
appendPQExpBufferStr(&catalog_query, "c.oid\n");
691687
else
692688
appendPQExpBufferStr(&catalog_query, "ns.oid\n");
689+
690+
if (objfilter & OBJFILTER_SCHEMA_EXCLUDE)
691+
appendPQExpBuffer(&catalog_query,
692+
" WHERE listed_objects.object_oid IS NULL\n");
693+
else
694+
appendPQExpBuffer(&catalog_query,
695+
" WHERE listed_objects.object_oid IS NOT NULL\n");
696+
has_where = true;
693697
}
694698

695699
/*
@@ -700,9 +704,11 @@ vacuum_one_database(ConnParams *cparams,
700704
*/
701705
if ((objfilter & OBJFILTER_TABLE) == 0)
702706
{
703-
appendPQExpBufferStr(&catalog_query, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array["
704-
CppAsString2(RELKIND_RELATION) ", "
705-
CppAsString2(RELKIND_MATVIEW) "])\n");
707+
appendPQExpBuffer(&catalog_query,
708+
" %s c.relkind OPERATOR(pg_catalog.=) ANY (array["
709+
CppAsString2(RELKIND_RELATION) ", "
710+
CppAsString2(RELKIND_MATVIEW) "])\n",
711+
has_where ? "AND" : "WHERE");
706712
has_where = true;
707713
}
708714

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