From 2eb5d39bc38474f3fe956d0f3344c47257133afd Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Tue, 20 Oct 2020 17:30:54 -0500
Subject: [PATCH v3] Avoid failure if tablespace dirs exist and empty..

See also: 3c0471b5fdd847febb30ec805a3e44f3eac5b66f
---
 src/bin/pg_upgrade/check.c      | 23 ++++++++++++++++-------
 src/bin/pg_upgrade/pg_upgrade.c | 13 +++++++++++++
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index 05e6bf7f2c..57f9fa9ad6 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -531,9 +531,9 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
 
 
 /*
- * A previous run of pg_upgrade might have failed and the new cluster
- * directory recreated, but they might have forgotten to remove
- * the new cluster's tablespace directories.  Therefore, check that
+ * A previous run of pg_upgrade might have failed.
+ * The admin might ahve recreated the new cluster directory, but forgotten to
+ * remove the new cluster's tablespace directories.  Therefore, check that
  * new cluster tablespace directories do not already exist.  If
  * they do, it would cause an error while restoring global objects.
  * This allows the failure to be detected at check time, rather than
@@ -554,15 +554,24 @@ check_for_new_tablespace_dir(ClusterInfo *new_cluster)
 
 	for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
 	{
-		struct stat statbuf;
-
 		snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
 				os_info.old_tablespaces[tblnum],
 				new_cluster->tablespace_suffix);
 
-		if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
-			pg_fatal("new cluster tablespace directory already exists: \"%s\"\n",
+		/*
+		 * New tablepace dirs are expected to not exist; if they
+		 * do exist but are empty, we avoid failing here, and remove them
+		 * before creating tablespaces while "restoring global objects".
+		 */
+		switch (pg_check_dir(new_tablespace_dir))
+		{
+			case 0: /* Nonextant */
+			case 1: /* Empty */
+				break;
+			default:
+				pg_fatal("new cluster tablespace directory already exists and nonempty\"%s\"\n",
 					 new_tablespace_dir);
+		}
 	}
 
 	check_ok();
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 1bc86e4205..6b7f8a690a 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -300,6 +300,19 @@ prepare_new_globals(void)
 	 */
 	set_frozenxids(false);
 
+	/*
+	 * Remove any *empty* tablespace dirs left behind by a previous, failed
+	 * upgrade.
+	 */
+	for (int tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
+	{
+		char	new_tablespace_dir[MAXPGPATH];
+		snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
+				os_info.old_tablespaces[tblnum],
+				new_cluster.tablespace_suffix);
+		(void) rmdir(new_tablespace_dir);
+	}
+
 	/*
 	 * Now restore global objects (roles and tablespaces).
 	 */
-- 
2.17.0

