From 87ff3f9cccc4768f55e7390b27789ca47bccd025 Mon Sep 17 00:00:00 2001
From: Rui Zhao <xiyuan.zr@alibaba-inc.com>
Date: Sat, 29 Jul 2023 22:49:42 +0800
Subject: [PATCH] Fix pg_upgrade failing with in-place tablespace.

---
 src/backend/utils/adt/misc.c           | 3 ++-
 src/backend/utils/misc/guc_tables.c    | 2 +-
 src/bin/pg_upgrade/t/002_pg_upgrade.pl | 7 +++++++
 src/bin/pg_upgrade/tablespace.c        | 4 +++-
 4 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index 5d78d6dc06..baed55281b 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -341,8 +341,9 @@ pg_tablespace_location(PG_FUNCTION_ARGS)
 						sourcepath)));
 	}
 
+	/* The path of the in-place tablespace is always pg_tblspc/<oid>. */
 	if (!S_ISLNK(st.st_mode))
-		PG_RETURN_TEXT_P(cstring_to_text(sourcepath));
+		PG_RETURN_TEXT_P(cstring_to_text(""));
 
 	/*
 	 * In presence of a link or a junction point, return the path pointing to.
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 15b51f2c5b..c433e0ce9b 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -1834,7 +1834,7 @@ struct config_bool ConfigureNamesBool[] =
 			GUC_NOT_IN_SAMPLE
 		},
 		&allow_in_place_tablespaces,
-		false,
+		true,
 		NULL, NULL, NULL
 	},
 
diff --git a/src/bin/pg_upgrade/t/002_pg_upgrade.pl b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
index a5688a1cf2..e8db54ae79 100644
--- a/src/bin/pg_upgrade/t/002_pg_upgrade.pl
+++ b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
@@ -212,6 +212,13 @@ else
 	is($rc, 0, 'regression tests pass');
 }
 
+# Test with in-place tablespace
+$oldnode->append_conf('postgresql.conf', 'allow_in_place_tablespaces = on');
+$oldnode->reload;
+$oldnode->safe_psql('postgres', "CREATE TABLESPACE space_test LOCATION ''");
+$oldnode->append_conf('postgresql.conf', 'allow_in_place_tablespaces = off');
+$oldnode->reload;
+
 # Initialize a new node for the upgrade.
 my $newnode = PostgreSQL::Test::Cluster->new('new_node');
 
diff --git a/src/bin/pg_upgrade/tablespace.c b/src/bin/pg_upgrade/tablespace.c
index 69cef7fa6b..2a6ad6b910 100644
--- a/src/bin/pg_upgrade/tablespace.c
+++ b/src/bin/pg_upgrade/tablespace.c
@@ -45,11 +45,13 @@ get_tablespace_paths(void)
 	int			i_spclocation;
 	char		query[QUERY_ALLOC];
 
+	/* Only check the tablespace with an absolute path. */
 	snprintf(query, sizeof(query),
 			 "SELECT pg_catalog.pg_tablespace_location(oid) AS spclocation "
 			 "FROM	pg_catalog.pg_tablespace "
 			 "WHERE	spcname != 'pg_default' AND "
-			 "		spcname != 'pg_global'");
+			 "		spcname != 'pg_global' AND "
+			 "		pg_catalog.pg_tablespace_location(oid) ~ '^/'");
 
 	res = executeQueryOrDie(conn, "%s", query);
 
-- 
2.32.0.3.g01195cf9f

