From 14d518ae31e0110a4325f922882955ead554ca64 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sun, 6 Mar 2022 08:18:20 -0600
Subject: [PATCH 2/3] f1

---
 src/bin/scripts/vacuumdb.c | 48 ++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 25 deletions(-)

diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index 3dca22e1c88..f76d8472c1f 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -485,7 +485,6 @@ vacuum_one_database(ConnParams *cparams,
 	int			ntups;
 	bool		failed = false;
 	bool		tables_listed = false;
-	bool		schemas_listed = false;
 	bool		has_where = false;
 	const char *initcmd;
 	const char *stage_commands[] = {
@@ -655,42 +654,41 @@ vacuum_one_database(ConnParams *cparams,
 	if (tables_listed)
 		appendPQExpBufferStr(&catalog_query, " JOIN listed_tables"
 							 " ON listed_tables.table_oid OPERATOR(pg_catalog.=) c.oid\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)
+	else
 	{
+		/*
+		 * 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.
+		 */
 		appendPQExpBufferStr(&catalog_query, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array["
 							 CppAsString2(RELKIND_RELATION) ", "
 							 CppAsString2(RELKIND_MATVIEW) "])\n");
 		has_where = true;
 
-		for (cell = schemas ? schemas->head : NULL; cell; cell = cell->next)
+		if (schemas != NULL && schemas->head != NULL)
 		{
-			if (!schemas_listed) {
+			appendPQExpBufferStr(&catalog_query,
+								 " AND c.relnamespace");
+			if (schema_exclusion)
 				appendPQExpBufferStr(&catalog_query,
-									 " AND pg_catalog.quote_ident(ns.nspname)");
-				if (schema_exclusion)
-					appendPQExpBufferStr(&catalog_query, " NOT IN (");
-				else
-					appendPQExpBufferStr(&catalog_query, " IN (");
-
-				schemas_listed = true;
-			}
+									" OPERATOR(pg_catalog.!=) ALL (ARRAY[");
 			else
-				appendPQExpBufferStr(&catalog_query, ", ");
+				appendPQExpBufferStr(&catalog_query,
+									" OPERATOR(pg_catalog.=) ANY (ARRAY[");
 
-			appendStringLiteralConn(&catalog_query, cell->val, conn);
-			appendPQExpBufferStr(&catalog_query, "::pg_catalog.regnamespace::pg_catalog.name");
+			for (cell = schemas->head; cell != NULL; cell = cell->next)
+			{
+				appendStringLiteralConn(&catalog_query, cell->val, conn);
+
+				if (cell->next != NULL)
+					appendPQExpBufferStr(&catalog_query, ", ");
+			}
 
+			/* Finish formatting schema filter */
+			appendPQExpBufferStr(&catalog_query, "]::pg_catalog.regnamespace[])\n");
 		}
-		/* Finish formatting schema filter */
-		if (schemas_listed)
-			appendPQExpBufferStr(&catalog_query, ")\n");
 	}
 
 	/*
-- 
2.17.1

