From a416a9594fd7433e5abbd4f68de97fed0579ac78 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <nathan@postgresql.org>
Date: Wed, 3 Dec 2025 16:12:37 -0600
Subject: [PATCH v7 1/3] vacuumdb: Move some options to vacuumingOptions
 struct.

TODO

Reviewed-by: Corey Huinker <corey.huinker@gmail.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/CADkLM%3DckHkX7Of5SrK7g0LokPUwJ%3Dkk8JU1GXGF5pZ1eBVr0%3DQ%40mail.gmail.com
---
 src/bin/scripts/vacuumdb.c  | 12 ++------
 src/bin/scripts/vacuuming.c | 61 ++++++++++++++++++-------------------
 src/bin/scripts/vacuuming.h |  4 ++-
 3 files changed, 36 insertions(+), 41 deletions(-)

diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index e117dac2242..6783c843637 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -68,8 +68,6 @@ main(int argc, char *argv[])
 	const char *dbname = NULL;
 	const char *maintenance_db = NULL;
 	ConnParams	cparams;
-	bool		echo = false;
-	bool		quiet = false;
 	vacuumingOptions vacopts;
 	SimpleStringList objects = {NULL, NULL};
 	int			concurrentCons = 1;
@@ -78,11 +76,7 @@ main(int argc, char *argv[])
 
 	/* initialize options */
 	memset(&vacopts, 0, sizeof(vacopts));
-	vacopts.objfilter = 0;		/* no filter */
 	vacopts.parallel_workers = -1;
-	vacopts.buffer_usage_limit = NULL;
-	vacopts.no_index_cleanup = false;
-	vacopts.force_index_cleanup = false;
 	vacopts.do_truncate = true;
 	vacopts.process_main = true;
 	vacopts.process_toast = true;
@@ -110,7 +104,7 @@ main(int argc, char *argv[])
 				dbname = pg_strdup(optarg);
 				break;
 			case 'e':
-				echo = true;
+				vacopts.echo = true;
 				break;
 			case 'f':
 				vacopts.full = true;
@@ -143,7 +137,7 @@ main(int argc, char *argv[])
 					exit(1);
 				break;
 			case 'q':
-				quiet = true;
+				vacopts.quiet = true;
 				break;
 			case 't':
 				vacopts.objfilter |= OBJFILTER_TABLE;
@@ -312,7 +306,7 @@ main(int argc, char *argv[])
 	ret = vacuuming_main(&cparams, dbname, maintenance_db, &vacopts,
 						 &objects, tbl_count,
 						 concurrentCons,
-						 progname, echo, quiet);
+						 progname);
 	exit(ret);
 }
 
diff --git a/src/bin/scripts/vacuuming.c b/src/bin/scripts/vacuuming.c
index f836f21fb03..74a1f20e0f3 100644
--- a/src/bin/scripts/vacuuming.c
+++ b/src/bin/scripts/vacuuming.c
@@ -30,21 +30,20 @@ static int	vacuum_one_database(ConnParams *cparams,
 								SimpleStringList *objects,
 								SimpleStringList **found_objs,
 								int concurrentCons,
-								const char *progname, bool echo, bool quiet);
+								const char *progname);
 static int	vacuum_all_databases(ConnParams *cparams,
 								 vacuumingOptions *vacopts,
 								 SimpleStringList *objects,
 								 int concurrentCons,
-								 const char *progname, bool echo, bool quiet);
+								 const char *progname);
 static SimpleStringList *retrieve_objects(PGconn *conn,
 										  vacuumingOptions *vacopts,
-										  SimpleStringList *objects,
-										  bool echo);
+										  SimpleStringList *objects);
 static void free_retrieved_objects(SimpleStringList *list);
 static void prepare_vacuum_command(PGconn *conn, PQExpBuffer sql,
 								   vacuumingOptions *vacopts, const char *table);
-static void run_vacuum_command(PGconn *conn, const char *sql, bool echo,
-							   const char *table);
+static void run_vacuum_command(PGconn *conn, vacuumingOptions *vacopts,
+							   const char *sql, const char *table);
 
 /*
  * Executes vacuum/analyze as indicated.  Returns 0 if the plan is carried
@@ -56,7 +55,7 @@ vacuuming_main(ConnParams *cparams, const char *dbname,
 			   const char *maintenance_db, vacuumingOptions *vacopts,
 			   SimpleStringList *objects,
 			   unsigned int tbl_count, int concurrentCons,
-			   const char *progname, bool echo, bool quiet)
+			   const char *progname)
 {
 	setup_cancel_handler(NULL);
 
@@ -71,7 +70,7 @@ vacuuming_main(ConnParams *cparams, const char *dbname,
 		return vacuum_all_databases(cparams, vacopts,
 									objects,
 									concurrentCons,
-									progname, echo, quiet);
+									progname);
 	}
 	else
 	{
@@ -100,7 +99,7 @@ vacuuming_main(ConnParams *cparams, const char *dbname,
 										  objects,
 										  vacopts->missing_stats_only ? &found_objs : NULL,
 										  concurrentCons,
-										  progname, echo, quiet);
+										  progname);
 				if (ret != 0)
 				{
 					free_retrieved_objects(found_objs);
@@ -116,7 +115,7 @@ vacuuming_main(ConnParams *cparams, const char *dbname,
 									   ANALYZE_NO_STAGE,
 									   objects, NULL,
 									   concurrentCons,
-									   progname, echo, quiet);
+									   progname);
 	}
 }
 
@@ -167,7 +166,7 @@ vacuum_one_database(ConnParams *cparams,
 					SimpleStringList *objects,
 					SimpleStringList **found_objs,
 					int concurrentCons,
-					const char *progname, bool echo, bool quiet)
+					const char *progname)
 {
 	PQExpBufferData sql;
 	PGconn	   *conn;
@@ -192,7 +191,7 @@ vacuum_one_database(ConnParams *cparams,
 	Assert(stage == ANALYZE_NO_STAGE ||
 		   (stage >= 0 && stage < ANALYZE_NUM_STAGES));
 
-	conn = connectDatabase(cparams, progname, echo, false, true);
+	conn = connectDatabase(cparams, progname, vacopts->echo, false, true);
 
 	if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600)
 	{
@@ -281,7 +280,7 @@ vacuum_one_database(ConnParams *cparams,
 	/* skip_database_stats is used automatically if server supports it */
 	vacopts->skip_database_stats = (PQserverVersion(conn) >= 160000);
 
-	if (!quiet)
+	if (!vacopts->quiet)
 	{
 		if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
 			printf(_("%s: processing database \"%s\": %s\n"),
@@ -302,7 +301,7 @@ vacuum_one_database(ConnParams *cparams,
 		retobjs = *found_objs;
 	else
 	{
-		retobjs = retrieve_objects(conn, vacopts, objects, echo);
+		retobjs = retrieve_objects(conn, vacopts, objects);
 		if (found_objs)
 			*found_objs = retobjs;
 		else
@@ -341,7 +340,7 @@ vacuum_one_database(ConnParams *cparams,
 	if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
 	{
 		initcmd = stage_commands[stage];
-		executeCommand(conn, initcmd, echo);
+		executeCommand(conn, initcmd, vacopts->echo);
 	}
 	else
 		initcmd = NULL;
@@ -351,7 +350,8 @@ vacuum_one_database(ConnParams *cparams,
 	 * for the first slot.  If not in parallel mode, the first slot in the
 	 * array contains the connection.
 	 */
-	sa = ParallelSlotsSetup(concurrentCons, cparams, progname, echo, initcmd);
+	sa = ParallelSlotsSetup(concurrentCons, cparams, progname,
+							vacopts->echo, initcmd);
 	ParallelSlotsAdoptConn(sa, conn);
 
 	initPQExpBuffer(&sql);
@@ -383,8 +383,7 @@ vacuum_one_database(ConnParams *cparams,
 		 * through ParallelSlotsGetIdle.
 		 */
 		ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL);
-		run_vacuum_command(free_slot->connection, sql.data,
-						   echo, tabname);
+		run_vacuum_command(free_slot->connection, vacopts, sql.data, tabname);
 
 		cell = cell->next;
 	} while (cell != NULL);
@@ -408,7 +407,7 @@ vacuum_one_database(ConnParams *cparams,
 		}
 
 		ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL);
-		run_vacuum_command(free_slot->connection, cmd, echo, NULL);
+		run_vacuum_command(free_slot->connection, vacopts, cmd, NULL);
 
 		if (!ParallelSlotsWaitCompletion(sa))
 			ret = EXIT_FAILURE; /* error already reported by handler */
@@ -436,17 +435,17 @@ vacuum_all_databases(ConnParams *cparams,
 					 vacuumingOptions *vacopts,
 					 SimpleStringList *objects,
 					 int concurrentCons,
-					 const char *progname, bool echo, bool quiet)
+					 const char *progname)
 {
 	int			ret = EXIT_SUCCESS;
 	PGconn	   *conn;
 	PGresult   *result;
 	int			numdbs;
 
-	conn = connectMaintenanceDatabase(cparams, progname, echo);
+	conn = connectMaintenanceDatabase(cparams, progname, vacopts->echo);
 	result = executeQuery(conn,
 						  "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
-						  echo);
+						  vacopts->echo);
 	numdbs = PQntuples(result);
 	PQfinish(conn);
 
@@ -474,7 +473,7 @@ vacuum_all_databases(ConnParams *cparams,
 										  objects,
 										  vacopts->missing_stats_only ? &found_objs[i] : NULL,
 										  concurrentCons,
-										  progname, echo, quiet);
+										  progname);
 				if (ret != EXIT_SUCCESS)
 					break;
 			}
@@ -499,7 +498,7 @@ vacuum_all_databases(ConnParams *cparams,
 									  objects,
 									  NULL,
 									  concurrentCons,
-									  progname, echo, quiet);
+									  progname);
 			if (ret != EXIT_SUCCESS)
 				break;
 		}
@@ -524,7 +523,7 @@ vacuum_all_databases(ConnParams *cparams,
  */
 static SimpleStringList *
 retrieve_objects(PGconn *conn, vacuumingOptions *vacopts,
-				 SimpleStringList *objects, bool echo)
+				 SimpleStringList *objects)
 {
 	PQExpBufferData buf;
 	PQExpBufferData catalog_query;
@@ -776,10 +775,10 @@ retrieve_objects(PGconn *conn, vacuumingOptions *vacopts,
 	 * query for consistency with table lookups done elsewhere by the user.
 	 */
 	appendPQExpBufferStr(&catalog_query, " ORDER BY c.relpages DESC;");
-	executeCommand(conn, "RESET search_path;", echo);
-	res = executeQuery(conn, catalog_query.data, echo);
+	executeCommand(conn, "RESET search_path;", vacopts->echo);
+	res = executeQuery(conn, catalog_query.data, vacopts->echo);
 	termPQExpBuffer(&catalog_query);
-	PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));
+	PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, vacopts->echo));
 
 	/*
 	 * Build qualified identifiers for each table, including the column list
@@ -1001,12 +1000,12 @@ prepare_vacuum_command(PGconn *conn, PQExpBuffer sql,
  * Any errors during command execution are reported to stderr.
  */
 static void
-run_vacuum_command(PGconn *conn, const char *sql, bool echo,
-				   const char *table)
+run_vacuum_command(PGconn *conn, vacuumingOptions *vacopts,
+				   const char *sql, const char *table)
 {
 	bool		status;
 
-	if (echo)
+	if (vacopts->echo)
 		printf("%s\n", sql);
 
 	status = PQsendQuery(conn, sql) == 1;
diff --git a/src/bin/scripts/vacuuming.h b/src/bin/scripts/vacuuming.h
index 49f968b32e5..90db4fa1a64 100644
--- a/src/bin/scripts/vacuuming.h
+++ b/src/bin/scripts/vacuuming.h
@@ -51,6 +51,8 @@ typedef struct vacuumingOptions
 	bool		skip_database_stats;
 	char	   *buffer_usage_limit;
 	bool		missing_stats_only;
+	bool		echo;
+	bool		quiet;
 } vacuumingOptions;
 
 /* Valid values for vacuumingOptions->objfilter */
@@ -65,7 +67,7 @@ extern int	vacuuming_main(ConnParams *cparams, const char *dbname,
 						   SimpleStringList *objects,
 						   unsigned int tbl_count,
 						   int concurrentCons,
-						   const char *progname, bool echo, bool quiet);
+						   const char *progname);
 
 extern char *escape_quotes(const char *src);
 
-- 
2.39.5 (Apple Git-154)

