v15-0001-make-pg_restore-file-option-using-connect-for.no-cfbot

application/octet-stream

Filename: v15-0001-make-pg_restore-file-option-using-connect-for.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Non-text mode for pg_dumpall
From a46d28371f62b51712f24f687dedfa1cfdcca342 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Tue, 4 Feb 2025 15:24:59 +0800
Subject: [PATCH v15 1/1] make pg_restore --file option using \connect for
 retsoring multiple databases.

This patch solves problems mentioned in [1].
so pg_restore --file output is sane.

we print out the database name we are restoring through
```ahprintf(AH, "--\n-- Database \"%s\" dump\n--\n\n", dbname);```.

So the pg_restore --file output comments make it easy to distinguish which database contents we are dumping.

overall it will be like:
---------------------------------------------------------------
--
-- Database "template1" dump
--

-- Dumped from database version 18devel_debug_build_622f678c10
-- Dumped by pg_dump version 18devel_debug_build_622f678c10

-- Started on 2025-02-04 14:34:44 CST

\connect template1
.....

-- Completed on 2025-02-04 14:34:53 CST

--
-- Database "template1" dump complete
--

---------------------------------------------------------------
[1] https://postgr.es/m/CACJufxFrzYJ0oZNm=v9hg10UpPQNe+p0+2ydNirHxyhUT_JtXw@mail.gmail.com
---
 src/bin/pg_dump/pg_backup.h          |  2 +-
 src/bin/pg_dump/pg_backup_archiver.c | 21 ++++++++++++++-------
 src/bin/pg_dump/pg_backup_tar.c      |  2 +-
 src/bin/pg_dump/pg_dump.c            |  2 +-
 src/bin/pg_dump/pg_restore.c         | 12 +++++++-----
 5 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
index 65000e5a08..729ffc9e12 100644
--- a/src/bin/pg_dump/pg_backup.h
+++ b/src/bin/pg_dump/pg_backup.h
@@ -306,7 +306,7 @@ extern void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ro
 
 extern void ProcessArchiveRestoreOptions(Archive *AHX);
 
-extern void RestoreArchive(Archive *AHX, bool append_data);
+extern void RestoreArchive(Archive *AHX, bool append_data, const char *dbname);
 
 /* Open an existing archive */
 extern Archive *OpenArchive(const char *FileSpec, const ArchiveFormat fmt);
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index e91f4b836f..fd6fd16642 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -336,9 +336,11 @@ ProcessArchiveRestoreOptions(Archive *AHX)
  *
  * If append_data is set, then append data into file as we are restoring dump
  * of multiple databases which was taken by pg_dumpall.
+ * If dbname is not NULL, then pg_restore restore archive to file will have
+ * comments about which database currently is being dumped.
  */
 void
-RestoreArchive(Archive *AHX, bool append_data)
+RestoreArchive(Archive *AHX, bool append_data, const char *dbname)
 {
 	ArchiveHandle *AH = (ArchiveHandle *) AHX;
 	RestoreOptions *ropt = AH->public.ropt;
@@ -457,7 +459,10 @@ RestoreArchive(Archive *AHX, bool append_data)
 	if (ropt->filename || ropt->compression_spec.algorithm != PG_COMPRESSION_NONE)
 		SetOutput(AH, ropt->filename, ropt->compression_spec, append_data);
 
-	ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
+	if (append_data && dbname != NULL)
+		ahprintf(AH, "--\n-- Database \"%s\" dump\n--\n\n", dbname);
+	else
+		ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
 
 	if (AH->archiveRemoteVersion)
 		ahprintf(AH, "-- Dumped from database version %s\n",
@@ -797,8 +802,10 @@ RestoreArchive(Archive *AHX, bool append_data)
 	if (AH->public.verbose)
 		dumpTimestamp(AH, "Completed on", time(NULL));
 
-	ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n");
-
+	if (append_data && dbname != NULL)
+		ahprintf(AH, "--\n-- Database \"%s\" dump complete\n--\n\n", dbname);
+	else
+		ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
 	/*
 	 * Clean up & we're done.
 	 */
@@ -2926,13 +2933,13 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
 
 	/*
 	 * DATABASE and DATABASE PROPERTIES also have a special rule: they are
-	 * restored in createDB mode, and not restored otherwise, independently of
-	 * all else.
+	 * restored in createDB mode or restored format is not plain file, and not
+	 * restored otherwise, independently of all else.
 	 */
 	if (strcmp(te->desc, "DATABASE") == 0 ||
 		strcmp(te->desc, "DATABASE PROPERTIES") == 0)
 	{
-		if (ropt->createDB)
+		if (ropt->createDB || AH->format != archNull)
 			return REQ_SCHEMA;
 		else
 			return 0;
diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c
index d94d0de2a5..45f0fb46e0 100644
--- a/src/bin/pg_dump/pg_backup_tar.c
+++ b/src/bin/pg_dump/pg_backup_tar.c
@@ -826,7 +826,7 @@ _CloseArchive(ArchiveHandle *AH)
 		savVerbose = AH->public.verbose;
 		AH->public.verbose = 0;
 
-		RestoreArchive((Archive *) AH, false);
+		RestoreArchive((Archive *) AH, false, NULL);
 
 		SetArchiveOptions((Archive *) AH, savDopt, savRopt);
 
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 61067e1542..51c595a7a5 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -1148,7 +1148,7 @@ main(int argc, char **argv)
 	 * right now.
 	 */
 	if (plainText)
-		RestoreArchive(fout, false);
+		RestoreArchive(fout, false, NULL);
 
 	CloseArchive(fout);
 
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 444415d2ee..b216873773 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -75,7 +75,8 @@ 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);
+							  int numWorkers, bool append_data,
+							  const char *dbname);
 static int ReadOneStatement(StringInfo inBuf, FILE *pfile);
 static int restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
 							   SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers);
@@ -525,16 +526,17 @@ main(int argc, char **argv)
 		}
 	}
 
-	return restoreOneDatabase(inputFileSpec, opts, numWorkers, false);
+	return restoreOneDatabase(inputFileSpec, opts, numWorkers, false, NULL);
 }
 /*
  * restoreOneDatabase
  *
  * This will restore one database using toc.dat file.
+ * dbname is the current to be restored database name.
  */
 static int
 restoreOneDatabase(const char *inputFileSpec, RestoreOptions *opts,
-				   int numWorkers, bool append_data)
+				   int numWorkers, bool append_data, const char *dbname)
 {
 	Archive		*AH;
 	int			exit_code;
@@ -568,7 +570,7 @@ restoreOneDatabase(const char *inputFileSpec, RestoreOptions *opts,
 	else
 	{
 		ProcessArchiveRestoreOptions(AH);
-		RestoreArchive(AH, append_data);
+		RestoreArchive(AH, append_data, dbname);
 	}
 
 	/* done, print a summary of ignored errors */
@@ -1138,7 +1140,7 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
 
 		pg_log_info("restoring database \"%s\"", dboid_cell->db_name);
 
-		dbexit_code = restoreOneDatabase(subdirpath, opts, numWorkers, true);
+		dbexit_code = restoreOneDatabase(subdirpath, opts, numWorkers, true, dboid_cell->db_name);
 
 		/* Store exit_code to report it back. */
 		if (exit_code == 0 && dbexit_code != 0)
-- 
2.34.1