>From b407a8ca516f7693d6a5166b06e7d9137c7c5d50 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 24 Sep 2015 15:50:01 +0200
Subject: [PATCH 3/3] Remove legacy multixact truncation support.

---
 src/backend/access/transam/multixact.c | 77 +++++++---------------------------
 src/backend/commands/vacuum.c          |  2 +-
 src/include/access/multixact.h         |  2 +-
 src/include/access/xlog_internal.h     |  2 +-
 4 files changed, 17 insertions(+), 66 deletions(-)

diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index d19b4c2..43f6e99 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -220,14 +220,6 @@ typedef struct MultiXactStateData
 	MultiXactOffset oldestOffset;
 	bool		oldestOffsetKnown;
 
-	/*
-	 * True if a multixact truncation WAL record was replayed since the last
-	 * checkpoint. This is used to trigger 'legacy truncations', i.e. truncate
-	 * by looking at the data directory during WAL replay, when the primary is
-	 * too old to generate truncation records.
-	 */
-	bool		sawTruncationInCkptCycle;
-
 	/* support for anti-wraparound measures */
 	MultiXactId multiVacLimit;
 	MultiXactId multiWarnLimit;
@@ -2384,28 +2376,7 @@ MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
 	Assert(InRecovery);
 
 	if (MultiXactIdPrecedes(MultiXactState->oldestMultiXactId, oldestMulti))
-	{
-		/*
-		 * If there has been a truncation on the master, detected by seeing a
-		 * moving oldestMulti, without a corresponding truncation record, we
-		 * know that the primary is still running an older version of postgres
-		 * that doesn't yet log multixact truncations. So perform the
-		 * truncation ourselves.
-		 */
-		if (!MultiXactState->sawTruncationInCkptCycle)
-		{
-			ereport(LOG,
-					(errmsg("performing legacy multixact truncation"),
-					 errdetail("Legacy truncations are sometimes performed when replaying WAL from an older primary."),
-					 errhint("Upgrade the primary, it is susceptible to data corruption.")));
-			TruncateMultiXact(oldestMulti, oldestMultiDB, true);
-		}
-
 		SetMultiXactIdLimit(oldestMulti, oldestMultiDB);
-	}
-
-	/* only looked at in the startup process, no lock necessary */
-	MultiXactState->sawTruncationInCkptCycle = false;
 }
 
 /*
@@ -2750,8 +2721,7 @@ find_multixact_start(MultiXactId multi, MultiXactOffset *result)
 	int			slotno;
 	MultiXactOffset *offptr;
 
-	/* XXX: Remove || AmStartupProcess() after WAL page magic bump */
-	Assert(MultiXactState->finishedStartup || AmStartupProcess());
+	Assert(MultiXactState->finishedStartup);
 
 	pageno = MultiXactIdToOffsetPage(multi);
 	entryno = MultiXactIdToOffsetEntry(multi);
@@ -2944,18 +2914,15 @@ PerformOffsetsTruncation(MultiXactId oldestMulti, MultiXactId newOldestMulti)
  * Remove all MultiXactOffset and MultiXactMember segments before the oldest
  * ones still of interest.
  *
- * On a primary this is called as part of vacuum (via
- * vac_truncate_clog()). During recovery truncation is normally done by
- * replaying truncation WAL records instead of this routine; the exception is
- * when replaying records from an older primary that doesn't yet generate
- * truncation WAL records. In that case truncation is triggered by
- * MultiXactAdvanceOldest().
+ * This is only called on a primary as part of vacuum (via
+ * vac_truncate_clog()). During recovery truncation is done by replaying
+ * truncation WAL records logged here.
  *
  * newOldestMulti is the oldest currently required multixact, newOldestMultiDB
  * is one of the databases preventing newOldestMulti from increasing.
  */
 void
-TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB, bool in_recovery)
+TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
 {
 	MultiXactId oldestMulti;
 	MultiXactId nextMulti;
@@ -2965,13 +2932,8 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB, bool in_reco
 	mxtruncinfo trunc;
 	MultiXactId earliest;
 
-	/*
-	 * Need to allow being called in recovery for backwards compatibility,
-	 * when an updated standby replays WAL generated by a non-updated primary.
-	 */
-	Assert(in_recovery || !RecoveryInProgress());
-	Assert(!in_recovery || AmStartupProcess());
-	Assert(in_recovery || MultiXactState->finishedStartup);
+	Assert(!RecoveryInProgress());
+	Assert(MultiXactState->finishedStartup);
 
 	/*
 	 * We can only allow one truncation to happen at once. Otherwise parts of
@@ -3084,22 +3046,15 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB, bool in_reco
 	 * Prevent checkpoints from being scheduled concurrently. This is critical
 	 * because otherwise a truncation record might not be replayed after a
 	 * crash/basebackup, even though the state of the data directory would
-	 * require it.  It's not possible (startup process doesn't have a PGXACT
-	 * entry), and not needed, to do this during recovery, when performing an
-	 * old-style truncation, though. There the entire scheduling depends on
-	 * the replayed WAL records which be the same after a possible crash.
+	 * require it.
 	 */
-	if (!in_recovery)
-	{
-		Assert(!MyPgXact->delayChkpt);
-		MyPgXact->delayChkpt = true;
-	}
+	Assert(!MyPgXact->delayChkpt);
+	MyPgXact->delayChkpt = true;
 
 	/* WAL log truncation */
-	if (!in_recovery)
-		WriteMTruncateXlogRec(newOldestMultiDB,
-							  oldestMulti, newOldestMulti,
-							  oldestOffset, newOldestOffset);
+	WriteMTruncateXlogRec(newOldestMultiDB,
+						  oldestMulti, newOldestMulti,
+						  oldestOffset, newOldestOffset);
 
 	/*
 	 * Update in-memory limits before performing the truncation, while inside
@@ -3120,8 +3075,7 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB, bool in_reco
 	/* Then offsets */
 	PerformOffsetsTruncation(oldestMulti, newOldestMulti);
 
-	if (!in_recovery)
-		MyPgXact->delayChkpt = false;
+	MyPgXact->delayChkpt = false;
 
 	END_CRIT_SECTION();
 	LWLockRelease(MultiXactTruncationLock);
@@ -3369,9 +3323,6 @@ multixact_redo(XLogReaderState *record)
 		PerformOffsetsTruncation(xlrec.startTruncOff, xlrec.endTruncOff);
 
 		LWLockRelease(MultiXactTruncationLock);
-
-		/* only looked at in the startup process, no lock necessary */
-		MultiXactState->sawTruncationInCkptCycle = true;
 	}
 	else
 		elog(PANIC, "multixact_redo: unknown op code %u", info);
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 698bb35..6d55148 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -1141,7 +1141,7 @@ vac_truncate_clog(TransactionId frozenXID,
 	 */
 	TruncateCLOG(frozenXID);
 	TruncateCommitTs(frozenXID, true);
-	TruncateMultiXact(minMulti, minmulti_datoid, false);
+	TruncateMultiXact(minMulti, minmulti_datoid);
 
 	/*
 	 * Update the wrap limit for GetNewTransactionId and creation of new
diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h
index 47ef38d..6ef8ba9 100644
--- a/src/include/access/multixact.h
+++ b/src/include/access/multixact.h
@@ -136,7 +136,7 @@ extern void MultiXactGetCheckptMulti(bool is_shutdown,
 						 Oid *oldestMultiDB);
 extern void CheckPointMultiXact(void);
 extern MultiXactId GetOldestMultiXactId(void);
-extern void TruncateMultiXact(MultiXactId oldestMulti, Oid oldestMultiDB, bool in_recovery);
+extern void TruncateMultiXact(MultiXactId oldestMulti, Oid oldestMultiDB);
 extern void MultiXactSetNextMXact(MultiXactId nextMulti,
 					  MultiXactOffset nextMultiOffset);
 extern void MultiXactAdvanceNextMXact(MultiXactId minMulti,
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index 590bf17..5096c17 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -31,7 +31,7 @@
 /*
  * Each page of XLOG file has a header like this:
  */
-#define XLOG_PAGE_MAGIC 0xD086	/* can be used as WAL version indicator */
+#define XLOG_PAGE_MAGIC 0xD087	/* can be used as WAL version indicator */
 
 typedef struct XLogPageHeaderData
 {
-- 
2.5.0.400.gff86faf

