From cae95a1db4caf35e68697383ab0416dc86173c38 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Fri, 31 Jan 2025 11:51:30 +0800
Subject: [PATCH v13 1/1] minor coesmetic change based on v13

---
 doc/src/sgml/ref/pg_dumpall.sgml         |  2 +-
 src/bin/pg_dump/common_dumpall_restore.h |  2 ++
 src/bin/pg_dump/pg_backup_utils.c        |  4 +--
 src/bin/pg_dump/pg_dumpall.c             |  2 +-
 src/bin/pg_dump/pg_restore.c             | 46 ++++++++++++++----------
 5 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/doc/src/sgml/ref/pg_dumpall.sgml b/doc/src/sgml/ref/pg_dumpall.sgml
index 429808373b..508d5ac57a 100644
--- a/doc/src/sgml/ref/pg_dumpall.sgml
+++ b/doc/src/sgml/ref/pg_dumpall.sgml
@@ -126,7 +126,7 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
-<varlistentry>
+     <varlistentry>
       <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
       <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
       <listitem>
diff --git a/src/bin/pg_dump/common_dumpall_restore.h b/src/bin/pg_dump/common_dumpall_restore.h
index 7fe1c00ab7..a0dcdbe080 100644
--- a/src/bin/pg_dump/common_dumpall_restore.h
+++ b/src/bin/pg_dump/common_dumpall_restore.h
@@ -16,6 +16,8 @@
 
 #include "pg_backup.h"
 
+/* TODO: increasing this to keep 100 db restoring by single pg_restore command. */
+#define MAX_ON_EXIT_NICELY				100
 extern PGconn *connectDatabase(const char *dbname, const char *connection_string, const char *pghost,
 							   const char *pgport, const char *pguser,
 							   trivalue prompt_password, bool fail_on_error,
diff --git a/src/bin/pg_dump/pg_backup_utils.c b/src/bin/pg_dump/pg_backup_utils.c
index f70ea9233f..47589cca90 100644
--- a/src/bin/pg_dump/pg_backup_utils.c
+++ b/src/bin/pg_dump/pg_backup_utils.c
@@ -13,6 +13,7 @@
  */
 #include "postgres_fe.h"
 
+#include "common_dumpall_restore.h"
 #ifdef WIN32
 #include "parallel.h"
 #endif
@@ -21,9 +22,6 @@
 /* Globals exported by this file */
 const char *progname = NULL;
 
-/* TODO: increasing this to keep 100 db restoring by single pg_restore command. */
-#define MAX_ON_EXIT_NICELY				100
-
 static struct
 {
 	on_exit_nicely_callback function;
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 3e022ecdeb..09b61dab00 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -674,7 +674,7 @@ help(void)
 	printf(_("\nGeneral options:\n"));
 	printf(_("  -f, --file=FILENAME          output file name\n"));
 	printf(_("  -F, --format=c|d|t|p         output file format (custom, directory, tar,\n"
-			 "                                plain text (default))\n"));
+			 "                               plain text (default))\n"));
 	printf(_("  -v, --verbose                verbose mode\n"));
 	printf(_("  -V, --version                output version information, then exit\n"));
 	printf(_("  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock\n"));
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index fc248a441e..10d1553e48 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -77,15 +77,16 @@ simple_db_oid_list_append(SimpleDatabaseOidList *list, Oid db_oid, const char *d
 static void usage(const char *progname);
 static void read_restore_filters(const char *filename, RestoreOptions *opts);
 static bool IsFileExistsInDirectory(const char *dir, const char *filename);
-static int restoreOneDatabase(const char *inputFileSpec,
-		RestoreOptions *opts, int numWorkers, bool append_data);
+static int restoreOneDatabase(const char *inputFileSpec, RestoreOptions *opts,
+							  int numWorkers, bool append_data);
 static int ReadOneStatement(StringInfo inBuf, FILE *pfile);
 static int restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
 							   SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers);
 static void execute_global_sql_commands(PGconn *conn, const char *dumpdirpath,
 										const char *outfile);
 static int filter_dbnames_for_restore(PGconn *conn,
-		SimpleDatabaseOidList *dbname_oid_list, SimpleStringList db_exclude_patterns);
+									  SimpleDatabaseOidList *dbname_oid_list,
+									  SimpleStringList db_exclude_patterns);
 static int get_dbname_oid_list_from_mfile(const char *dumpdirpath,
 										  SimpleDatabaseOidList *dbname_oid_list);
 static void simple_db_oid_list_append(SimpleDatabaseOidList *list, Oid db_oid,
@@ -510,7 +511,9 @@ main(int argc, char **argv)
 			{
 				/* Now restore all the databases from map.dat file. */
 				exit_code = restoreAllDatabases(conn, inputFileSpec,
-						db_exclude_patterns, opts, numWorkers);
+												db_exclude_patterns,
+												opts,
+												numWorkers);
 			}
 
 			/* Free db pattern list. */
@@ -821,8 +824,9 @@ ReadOneStatement(StringInfo inBuf, FILE *pfile)
  * returns number of dbnames those will be restored.
  */
 static int
-filter_dbnames_for_restore(PGconn *conn, SimpleDatabaseOidList *dbname_oid_list,
-		SimpleStringList db_exclude_patterns)
+filter_dbnames_for_restore(PGconn *conn,
+						   SimpleDatabaseOidList *dbname_oid_list,
+						   SimpleStringList db_exclude_patterns)
 {
 	SimpleDatabaseOidListCell	*dboid_cell = dbname_oid_list->head;
 	SimpleDatabaseOidListCell	*dboidprecell = NULL;
@@ -833,6 +837,7 @@ filter_dbnames_for_restore(PGconn *conn, SimpleDatabaseOidList *dbname_oid_list,
 	/* Return 0 if there is no db to restore. */
 	if (dboid_cell == NULL)
 		return 0;
+	Assert(conn);
 
 	query = createPQExpBuffer();
 
@@ -860,7 +865,7 @@ filter_dbnames_for_restore(PGconn *conn, SimpleDatabaseOidList *dbname_oid_list,
 			 * If we don't have db connection, then consider patterns as NAME
 			 * only.
 			 */
-			if (!conn && (pg_strcasecmp(dboid_cell->db_name, celldb->val) == 0))
+			if (pg_strcasecmp(dboid_cell->db_name, celldb->val) == 0)
 			{
 				/*
 				 * As we need to skip this dbname so set flag to remove it from
@@ -875,13 +880,13 @@ filter_dbnames_for_restore(PGconn *conn, SimpleDatabaseOidList *dbname_oid_list,
 
 				appendPQExpBufferStr(query, "SELECT 1 ");
 				processSQLNamePattern(conn, query, celldb->val, false,
-						false, NULL, quote_literal_cstr(dboid_cell->db_name),
-						NULL, NULL, NULL, &dotcnt);
+									  false, NULL, quote_literal_cstr(dboid_cell->db_name),
+									  NULL, NULL, NULL, &dotcnt);
 
 				if (dotcnt > 0)
 				{
 					pg_log_error("improper qualified name (too many dotted names): %s",
-							celldb->val);
+								  celldb->val);
 					PQfinish(conn);
 					exit_nicely(1);
 				}
@@ -891,7 +896,7 @@ filter_dbnames_for_restore(PGconn *conn, SimpleDatabaseOidList *dbname_oid_list,
 				if ((PQresultStatus(res) == PGRES_TUPLES_OK) && PQntuples(res))
 				{
 					skip_db_restore = true;
-					pg_log_info("\"%s\" database is matching with exclude \"%s\" pattern", dboid_cell->db_name, celldb->val);
+					pg_log_info("database \"%s\" is matching with exclude pattern: \"%s\"", dboid_cell->db_name, celldb->val);
 				}
 
 				PQclear(res);
@@ -1000,13 +1005,13 @@ get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimpleDatabaseOidList *d
  */
 static int
 restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
-		SimpleStringList db_exclude_patterns, RestoreOptions *opts,
-		int numWorkers)
+					SimpleStringList db_exclude_patterns, RestoreOptions *opts,
+					int numWorkers)
 {
 	SimpleDatabaseOidList			dbname_oid_list = {NULL, NULL};
 	SimpleDatabaseOidListCell		*dboid_cell;
 	int								exit_code = 0;
-	int								num_db_restore;
+	int								num_db_restore = 0;
 	int								num_total_db;
 
 	num_total_db = get_dbname_oid_list_from_mfile(dumpdirpath, &dbname_oid_list);
@@ -1042,8 +1047,9 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
 	/*
 	 * processing pg_retsore --exclude-database=PATTERN.
 	 */
-	num_db_restore = filter_dbnames_for_restore(conn, &dbname_oid_list,
-			db_exclude_patterns);
+	if (conn)
+		num_db_restore = filter_dbnames_for_restore(conn, &dbname_oid_list,
+													db_exclude_patterns);
 
 	/* Close the db connection as we are done globals and patterns. */
 	if (conn)
@@ -1063,10 +1069,12 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
 		pg_log_info("restoring dump of pg_dumpall without -C option, there might be multiple databases in directory.");
 
 	/* TODO: MAX_ON_EXIT_NICELY is 100 now... max AH handle register on exit .*/
-	if (num_db_restore > 100)
+	if (num_db_restore > MAX_ON_EXIT_NICELY)
 	{
 		simple_db_oid_full_list_delete(&dbname_oid_list);
-		pg_fatal("cound not restore more than 100 databases by single pg_restore, here total db:%d", num_db_restore);
+		pg_fatal("cound not restore more than %d databases by single pg_restore, here total db:%d",
+				  MAX_ON_EXIT_NICELY,
+				  num_db_restore);
 	}
 
 	/*
@@ -1102,7 +1110,7 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
 			exit_code = dbexit_code;
 
 		dboid_cell = dboid_cell->next;
-	} /* end while */
+	}
 
 	/* Log number of processed databases.*/
 	pg_log_info("number of restored databases are %d", num_db_restore);
-- 
2.34.1

