From 0f7482733a90a2e0d8917a41d823306975f291ee Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 24 Jun 2014 16:11:06 -0400
Subject: [PATCH] pg_upgrade:  remove pg_multixact files left by initdb

This fixes a bug that caused vacuum to fail when the '0000' files left
by initdb were accessed as part of vacuum's cleanup of old pg_multixact
files.

Backpatch through 9.3
---
 contrib/pg_upgrade/pg_upgrade.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c
index 773bb07e04e..c7386cdd3d3 100644
--- a/contrib/pg_upgrade/pg_upgrade.c
+++ b/contrib/pg_upgrade/pg_upgrade.c
@@ -363,22 +363,35 @@ create_new_objects(void)
 }
 
 /*
- * Delete the given subdirectory contents from the new cluster, and copy the
- * files from the old cluster into it.
+ * Delete the given subdirectory contents from the new cluster
  */
 static void
-copy_subdir_files(char *subdir)
+remove_new_subdir(char *subdir, bool rmtopdir)
 {
-	char		old_path[MAXPGPATH];
 	char		new_path[MAXPGPATH];
 
 	prep_status("Deleting files from new %s", subdir);
 
-	snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
 	snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
-	if (!rmtree(new_path, true))
+	if (!rmtree(new_path, rmtopdir))
 		pg_fatal("could not delete directory \"%s\"\n", new_path);
+
 	check_ok();
+}
+
+/*
+ * Copy the files from the old cluster into it
+ */
+static void
+copy_subdir_files(char *subdir)
+{
+	char		old_path[MAXPGPATH];
+	char		new_path[MAXPGPATH];
+
+	remove_new_subdir(subdir, true);
+
+	snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
+	snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
 
 	prep_status("Copying old %s to new server", subdir);
 
@@ -419,6 +432,7 @@ copy_clog_xlog_xid(void)
 	{
 		copy_subdir_files("pg_multixact/offsets");
 		copy_subdir_files("pg_multixact/members");
+
 		prep_status("Setting next multixact ID and offset for new cluster");
 
 		/*
@@ -436,6 +450,13 @@ copy_clog_xlog_xid(void)
 	}
 	else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
 	{
+		/*
+		 * Remove files created by initdb that no longer match the
+		 * new multi-xid value.
+		 */
+		remove_new_subdir("pg_multixact/offsets", false);
+		remove_new_subdir("pg_multixact/members", false);
+
 		prep_status("Setting oldest multixact ID on new cluster");
 
 		/*
-- 
GitLab