Do not filter by relkind in vacuumdb's catalog query if --table is used
authorMichael Paquier <michael@paquier.xyz>
Wed, 30 Jan 2019 00:44:08 +0000 (09:44 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 30 Jan 2019 00:44:08 +0000 (09:44 +0900)
If a user specifies a relation name which cannot be processed, then the
backend can warn directly about what is wrong with it.  This fixes an
oversight from e0c2933.

Author: Nathan Bossart
Discussion: https://postgr.es/m/32049A78-C429-4742-AEC1-941C9ABDE7B8@amazon.com

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

index ff0d97971bded338d4d4ed62f29ffc3a87219b8d..5e87af2d519af62c91ec40ea124663751f8b95e1 100644 (file)
@@ -3,7 +3,7 @@ use warnings;
 
 use PostgresNode;
 use TestLib;
-use Test::More tests => 35;
+use Test::More tests => 38;
 
 program_help_ok('vacuumdb');
 program_version_ok('vacuumdb');
@@ -64,6 +64,7 @@ $node->safe_psql(
    'postgres', q|
   CREATE TABLE "need""q(uot" (")x" text);
   CREATE TABLE vactable (a int, b int);
+  CREATE VIEW vacview AS SELECT 1 as a;
 
   CREATE FUNCTION f0(int) RETURNS int LANGUAGE SQL AS 'SELECT $1 * $1';
   CREATE FUNCTION f1(int) RETURNS int LANGUAGE SQL AS 'SELECT f0($1)';
@@ -88,3 +89,9 @@ $node->issues_sql_like(
    [ 'vacuumdb', '--analyze-only', '--table', 'vactable(b)', 'postgres' ],
    qr/statement: ANALYZE public.vactable\(b\);/,
    'vacuumdb --analyze-only with partial column list');
+$node->command_checks_all(
+   [ 'vacuumdb', '--analyze', '--table', 'vacview', 'postgres' ],
+   0,
+   [qr/^.*vacuuming database "postgres"/],
+   [qr/^WARNING.*cannot vacuum non-tables or special system tables/s],
+   'vacuumdb with view');
index 05321edb8d4fb07e993d019594692e6aca976849..40ba8283a287f440835b06866df459ff21938f74 100644 (file)
@@ -484,9 +484,16 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
        appendPQExpBuffer(&catalog_query, " JOIN listed_tables"
                          " ON listed_tables.table_oid OPERATOR(pg_catalog.=) c.oid\n");
 
-   appendPQExpBuffer(&catalog_query, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array["
-                     CppAsString2(RELKIND_RELATION) ", "
-                     CppAsString2(RELKIND_MATVIEW) "])\n");
+   /*
+    * If no tables were listed, filter for the relevant relation types.  If
+    * tables were given via --table, don't bother filtering by relation type.
+    * Instead, let the server decide whether a given relation can be
+    * processed in which case the user will know about it.
+    */
+   if (!tables_listed)
+       appendPQExpBuffer(&catalog_query, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array["
+                         CppAsString2(RELKIND_RELATION) ", "
+                         CppAsString2(RELKIND_MATVIEW) "])\n");
 
    /*
     * Execute the catalog query.  We use the default search_path for this