From ce03fb3489f6683da8c0ace7c33b5a95b2f399ae Mon Sep 17 00:00:00 2001
From: Jeevan Ladhe <jeevan.ladhe@enterprisedb.com>
Date: Tue, 13 Jul 2021 18:52:38 +0530
Subject: [PATCH] Improve the pg_upgrade error message.

---
 src/bin/pg_upgrade/check.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index 0c47a6b8cc..e6b52aa3e8 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -700,9 +700,13 @@ check_proper_datallowconn(ClusterInfo *cluster)
 	int			ntups;
 	int			i_datname;
 	int			i_datallowconn;
+	FILE	   *script = NULL;
+	char		output_path[MAXPGPATH];
 
 	prep_status("Checking database connection settings");
 
+	snprintf(output_path, sizeof(output_path), "databases_with_datallowconn_false.txt");
+
 	conn_template1 = connectToServer(cluster, "template1");
 
 	/* get database names */
@@ -733,8 +737,13 @@ check_proper_datallowconn(ClusterInfo *cluster)
 			 * restore
 			 */
 			if (strcmp(datallowconn, "f") == 0)
-				pg_fatal("All non-template0 databases must allow connections, "
-						 "i.e. their pg_database.datallowconn must be true\n");
+			{
+				if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
+					pg_fatal("could not open file \"%s\": %s\n",
+							 output_path, strerror(errno));
+
+				fprintf(script, "%s\n", datname);
+			}
 		}
 	}
 
@@ -742,7 +751,22 @@ check_proper_datallowconn(ClusterInfo *cluster)
 
 	PQfinish(conn_template1);
 
-	check_ok();
+	if (script)
+	{
+		fclose(script);
+
+		pg_log(PG_REPORT, "fatal\n");
+		pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
+				 "pg_database.datallowconn must be true. Your installation contains\n"
+				 "non-template0 databases with their pg_database.datallowconn set to\n"
+				 "false. Consider allowing connection for all non-template0 databases\n"
+				 "using:\n"
+				 "    UPDATE pg_catalog.pg_database SET datallowconn='true' WHERE datname NOT LIKE 'template0';\n"
+				 "A list of databases with the problem is given in the file:\n"
+				 "    %s\n\n", output_path);
+	}
+	else
+		check_ok();
 }
 
 
-- 
2.25.1

