From bd823e11fa52a79f6116e1538f861cccd8823f3f Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 15 May 2010 21:41:16 +0000
Subject: [PATCH] Ensure that pg_restore -l will output DATABASE entries
 whether or not -C is specified.  Per bug report from Russell Smith and
 ensuing discussion. Since this is a corner case behavioral change, I'm going
 to be conservative and not back-patch it.

In passing, also rename the RestoreOptions field for the -C switch to
something less generic than "create".
---
 src/bin/pg_dump/pg_backup.h          |  4 ++--
 src/bin/pg_dump/pg_backup_archiver.c | 15 +++++++++------
 src/bin/pg_dump/pg_dump.c            |  8 ++++----
 src/bin/pg_dump/pg_restore.c         |  4 ++--
 4 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
index 3ca39d7ba0b..5a73779d215 100644
--- a/src/bin/pg_dump/pg_backup.h
+++ b/src/bin/pg_dump/pg_backup.h
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.52 2009/06/11 14:49:07 momjian Exp $
+ *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.53 2010/05/15 21:41:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -96,7 +96,7 @@ typedef int (*DataDumperPtr) (Archive *AH, void *userArg);
 
 typedef struct _restoreOptions
 {
-	int			create;			/* Issue commands to create the database */
+	int			createDB;		/* Issue commands to create the database */
 	int			noOwner;		/* Don't try to match original object owner */
 	int			noTablespace;	/* Don't issue tablespace-related commands */
 	int			disable_triggers;		/* disable triggers during data-only
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index bd723a0c74a..d0e30ad5a21 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.184 2010/04/23 23:21:44 rhaas Exp $
+ *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.185 2010/05/15 21:41:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -211,19 +211,19 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
 	/*
 	 * Check for nonsensical option combinations.
 	 *
-	 * NB: create+dropSchema is useless because if you're creating the DB,
+	 * NB: createDB+dropSchema is useless because if you're creating the DB,
 	 * there's no need to drop individual items in it.  Moreover, if we tried
 	 * to do that then we'd issue the drops in the database initially
 	 * connected to, not the one we will create, which is very bad...
 	 */
-	if (ropt->create && ropt->dropSchema)
+	if (ropt->createDB && ropt->dropSchema)
 		die_horribly(AH, modulename, "-C and -c are incompatible options\n");
 
 	/*
-	 * -1 is not compatible with -C, because we can't create a database inside
+	 * -C is not compatible with -1, because we can't create a database inside
 	 * a transaction block.
 	 */
-	if (ropt->create && ropt->single_txn)
+	if (ropt->createDB && ropt->single_txn)
 		die_horribly(AH, modulename, "-C and -1 are incompatible options\n");
 
 	/*
@@ -815,6 +815,9 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
 
 	ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
 
+	/* We should print DATABASE entries whether or not -C was specified */
+	ropt->createDB = 1;
+
 	for (te = AH->toc->next; te != AH->toc; te = te->next)
 	{
 		if (ropt->verbose || _tocEntryRequired(te, ropt, true) != 0)
@@ -2257,7 +2260,7 @@ _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
 		return 0;
 
 	/* Ignore DATABASE entry unless we should create it */
-	if (!ropt->create && strcmp(te->desc, "DATABASE") == 0)
+	if (!ropt->createDB && strcmp(te->desc, "DATABASE") == 0)
 		return 0;
 
 	/* Check options for selective dump/restore */
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 2ff4e0c8748..1df0aa12fd6 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -25,7 +25,7 @@
  *	http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.579 2010/03/18 20:00:51 petere Exp $
+ *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.580 2010/05/15 21:41:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -243,7 +243,7 @@ main(int argc, char **argv)
 	int			compressLevel = -1;
 	int			plainText = 0;
 	int			outputClean = 0;
-	int			outputCreate = 0;
+	int			outputCreateDB = 0;
 	bool		outputBlobs = false;
 	int			outputNoOwner = 0;
 	char	   *outputSuperuser = NULL;
@@ -352,7 +352,7 @@ main(int argc, char **argv)
 				break;
 
 			case 'C':			/* Create DB */
-				outputCreate = 1;
+				outputCreateDB = 1;
 				break;
 
 			case 'E':			/* Dump encoding */
@@ -766,7 +766,7 @@ main(int argc, char **argv)
 		ropt->dropSchema = outputClean;
 		ropt->aclsSkip = aclsSkip;
 		ropt->superuser = outputSuperuser;
-		ropt->create = outputCreate;
+		ropt->createDB = outputCreateDB;
 		ropt->noOwner = outputNoOwner;
 		ropt->noTablespace = outputNoTablespaces;
 		ropt->disable_triggers = disable_triggers;
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index c5a196c366d..4927471dce4 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -34,7 +34,7 @@
  *
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.101 2009/11/19 22:05:48 petere Exp $
+ *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.102 2010/05/15 21:41:16 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -154,7 +154,7 @@ main(int argc, char **argv)
 				opts->dropSchema = 1;
 				break;
 			case 'C':
-				opts->create = 1;
+				opts->createDB = 1;
 				break;
 			case 'd':
 				opts->dbname = strdup(optarg);
-- 
GitLab