From 139a9cb7acd969848ee55fc4c08619cff4376c66 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sun, 6 Mar 2022 09:02:13 -0600
Subject: [PATCH 3/3] f2

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

diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index f76d8472c1f..9ed2b95d7a5 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -46,7 +46,7 @@ typedef struct vacuumingOptions
 	bool		process_toast;
 } vacuumingOptions;
 
-static bool schema_exclusion = false;
+enum trivalue schema_is_exclude = TRI_DEFAULT;
 
 static void vacuum_one_database(ConnParams *cparams,
 								vacuumingOptions *vacopts,
@@ -130,7 +130,6 @@ main(int argc, char *argv[])
 	int			concurrentCons = 1;
 	int			tbl_count = 0;
 	SimpleStringList schemas = {NULL, NULL};
-	SimpleStringList schemas_excluded = {NULL, NULL};
 
 	/* initialize options */
 	memset(&vacopts, 0, sizeof(vacopts));
@@ -209,11 +208,24 @@ main(int argc, char *argv[])
 					exit(1);
 				break;
 			case 'n':                       /* include schema(s) */
+				if (schema_is_exclude == TRI_YES)
+				{
+					pg_log_error("cannot vacuum all tables in schema(s) and exclude specific schema(s) at the same time");
+					exit(1);
+				}
+
 				simple_string_list_append(&schemas, optarg);
+				schema_is_exclude = TRI_NO;
 				break;
 			case 'N':			/* exclude schema(s) */
-				simple_string_list_append(&schemas_excluded, optarg);
-				schema_exclusion = true;
+				if (schema_is_exclude == TRI_NO)
+				{
+					pg_log_error("cannot vacuum all tables in schema(s) and exclude specific schema(s) at the same time");
+					exit(1);
+				}
+
+				simple_string_list_append(&schemas, optarg);
+				schema_is_exclude = TRI_YES;
 				break;
 			case 2:
 				maintenance_db = pg_strdup(optarg);
@@ -358,22 +370,12 @@ main(int argc, char *argv[])
 	 * When filtereing on schema name, filter by table is not allowed.
 	 * The schema name can already be set to a fqdn table name.
 	 */
-	if (tbl_count && (schemas.head != NULL || schemas_excluded.head != NULL))
+	if (tbl_count && (schemas.head != NULL))
 	{
 		pg_log_error("cannot vacuum all tables in schema(s) and specific table(s) at the same time");
 		exit(1);
 	}
 
-	/*
-	 * Options -n | --schema and -N | --exclude-schema
-	 * can not be used together
-	 */
-	if (schemas.head != NULL && schemas_excluded.head != NULL)
-	{
-		pg_log_error("cannot vacuum all tables in schema(s) and and exclude specific schema(s) at the same time");
-		exit(1);
-	}
-
 	/* Avoid opening extra connections. */
 	if (tbl_count && (concurrentCons > tbl_count))
 		concurrentCons = tbl_count;
@@ -388,13 +390,10 @@ main(int argc, char *argv[])
 
 		if (schemas.head != NULL)
 		{
-			pg_log_error("cannot vacuum specific schema(s) in all databases");
-			exit(1);
-		}
-
-		if (schemas_excluded.head != NULL)
-		{
-			pg_log_error("cannot exclude from vacuum specific schema(s) in all databases");
+			if (schema_is_exclude == TRI_YES)
+				pg_log_error("cannot exclude from vacuum specific schema(s) in all databases");
+			else if (schema_is_exclude == TRI_NO)
+				pg_log_error("cannot vacuum specific schema(s) in all databases");
 			exit(1);
 		}
 
@@ -433,7 +432,7 @@ main(int argc, char *argv[])
 			{
 				vacuum_one_database(&cparams, &vacopts,
 									stage,
-									(schema_exclusion) ? &schemas_excluded : &schemas,
+									&schemas,
 									&tables,
 									concurrentCons,
 									progname, echo, quiet);
@@ -442,7 +441,7 @@ main(int argc, char *argv[])
 		else
 			vacuum_one_database(&cparams, &vacopts,
 								ANALYZE_NO_STAGE,
-								(schema_exclusion) ? &schemas_excluded : &schemas,
+								&schemas,
 								&tables,
 								concurrentCons,
 								progname, echo, quiet);
@@ -671,10 +670,10 @@ vacuum_one_database(ConnParams *cparams,
 		{
 			appendPQExpBufferStr(&catalog_query,
 								 " AND c.relnamespace");
-			if (schema_exclusion)
+			if (schema_is_exclude == TRI_YES)
 				appendPQExpBufferStr(&catalog_query,
 									" OPERATOR(pg_catalog.!=) ALL (ARRAY[");
-			else
+			else if (schema_is_exclude == TRI_NO)
 				appendPQExpBufferStr(&catalog_query,
 									" OPERATOR(pg_catalog.=) ANY (ARRAY[");
 
-- 
2.17.1

