From 95e438ee448c1686c946909f1fc84ec95ee6c7d4 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyoga.ntt@gmail.com>
Date: Fri, 18 Dec 2020 13:45:29 +0900
Subject: [PATCH v5 1/2] Avoid archiving a WAL segment that continues to the
 next segment

If the last record of a finshed segment continues to the next segment,
we need to defer archiving of the segment until the record is flushed
to the end. Otherwise crash recovery can overwrite the last record of
a segment and history diverges between archive and pg_wal.
---
 src/backend/access/transam/xlog.c   | 221 +++++++++++++++++++++++++++-
 src/backend/replication/walsender.c |  14 +-
 src/include/access/xlog.h           |   1 +
 3 files changed, 224 insertions(+), 12 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index b1e5d2dbff..b0d3ba2c5a 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -733,6 +733,16 @@ typedef struct XLogCtlData
 	 */
 	XLogRecPtr	lastFpwDisableRecPtr;
 
+	/* The last segment notified to be archived. Protected by WALWriteLock */
+	XLogSegNo	lastNotifiedSeg;
+
+	/*
+	 * Remember the region we need to consider refraining from archiving
+	 * finished segments immediately. Protected by info_lck.
+	 */
+	XLogRecPtr	firstSegContRecStart;
+	XLogRecPtr	lastSegContRecEnd;
+	
 	slock_t		info_lck;		/* locks shared variables shown above */
 } XLogCtlData;
 
@@ -1170,6 +1180,9 @@ XLogInsertRecord(XLogRecData *rdata,
 	 */
 	if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
 	{
+		XLogSegNo startseg;
+		XLogSegNo endseg;
+
 		SpinLockAcquire(&XLogCtl->info_lck);
 		/* advance global request to include new block(s) */
 		if (XLogCtl->LogwrtRqst.Write < EndPos)
@@ -1177,6 +1190,22 @@ XLogInsertRecord(XLogRecData *rdata,
 		/* update local result copy while I have the chance */
 		LogwrtResult = XLogCtl->LogwrtResult;
 		SpinLockRelease(&XLogCtl->info_lck);
+
+		/*
+		 * Remember the range of segment boundaries that are connected by a
+		 * continuation record.
+		 */
+		XLByteToSeg(StartPos, startseg, wal_segment_size);
+		XLByteToPrevSeg(EndPos, endseg, wal_segment_size);
+
+		if (startseg != endseg)
+		{
+			SpinLockAcquire(&XLogCtl->info_lck);
+			if (XLogCtl->firstSegContRecStart == InvalidXLogRecPtr)
+				XLogCtl->firstSegContRecStart = StartPos;
+			XLogCtl->lastSegContRecEnd = EndPos;
+			SpinLockRelease(&XLogCtl->info_lck);
+		}
 	}
 
 	/*
@@ -2410,6 +2439,50 @@ XLogCheckpointNeeded(XLogSegNo new_segno)
 	return false;
 }
 
+/*
+ * Returns last notified segment.
+ */
+static XLogSegNo
+GetLastNotifiedSegment(void)
+{
+	XLogSegNo last_notified;
+
+	SpinLockAcquire(&XLogCtl->info_lck);
+	last_notified = XLogCtl->lastNotifiedSeg;
+	SpinLockRelease(&XLogCtl->info_lck);
+
+	return last_notified;
+}
+
+/*
+ * Notify segments that are not yet notified.
+ */
+static void
+NotifySegmentsUpTo(XLogSegNo notifySegNo)
+{
+	XLogSegNo last_notified = GetLastNotifiedSegment();
+	XLogSegNo i;
+
+	if (notifySegNo <= last_notified)
+		return;
+
+	for (i = XLogCtl->lastNotifiedSeg + 1 ; i <= notifySegNo ; i++)
+		XLogArchiveNotifySeg(i);
+
+	/* Don't go back in the case someone else has made it go further. */
+	SpinLockAcquire(&XLogCtl->info_lck);
+	if (XLogCtl->lastNotifiedSeg < notifySegNo)
+		XLogCtl->lastNotifiedSeg = notifySegNo;
+
+	/* Reset the locations if we have got out of the continuation region. */
+	if (XLogCtl->lastSegContRecEnd <= XLogCtl->LogwrtRqst.Flush)
+	{
+		XLogCtl->firstSegContRecStart = InvalidXLogRecPtr;
+		XLogCtl->lastSegContRecEnd = InvalidXLogRecPtr;
+	}
+	SpinLockRelease(&XLogCtl->info_lck);
+}
+
 /*
  * Write and/or fsync the log at least as far as WriteRqst indicates.
  *
@@ -2433,6 +2506,8 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
 	int			npages;
 	int			startidx;
 	uint32		startoffset;
+	bool		notify_seg = false;
+	bool		force_continue = false;
 
 	/* We should always be inside a critical section here */
 	Assert(CritSectionCount > 0);
@@ -2589,15 +2664,97 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
 			 */
 			if (finishing_seg)
 			{
+				XLogRecPtr firstSegContRecStart;
+				XLogRecPtr lastSegContRecEnd;
+
 				issue_xlog_fsync(openLogFile, openLogSegNo);
 
 				/* signal that we need to wakeup walsenders later */
 				WalSndWakeupRequest();
 
-				LogwrtResult.Flush = LogwrtResult.Write;	/* end of page */
+				SpinLockAcquire(&XLogCtl->info_lck);
+				firstSegContRecStart = XLogCtl->firstSegContRecStart;
+				lastSegContRecEnd = XLogCtl->lastSegContRecEnd;
+				SpinLockRelease(&XLogCtl->info_lck);
 
-				if (XLogArchivingActive())
-					XLogArchiveNotifySeg(openLogSegNo);
+				/*
+				 * If we may be on a continuation record spans over segments,
+				 * don't archive the segment until the record is written to the
+				 * end. If we do, we could have corrupt archive having
+				 * different records at the boundary after a server crash
+				 * around here.  For the same reason, also for replication,
+				 * don't expose flush location until the record is written to
+				 * the end so that an incomplete record at segment boundary
+				 * won't be sent to standby.
+				 */
+				if (firstSegContRecStart == InvalidXLogRecPtr ||
+					LogwrtResult.Write < firstSegContRecStart ||
+					lastSegContRecEnd <= LogwrtResult.Write)
+				{
+					LogwrtResult.Flush = LogwrtResult.Write; /* end of page */
+
+					if (XLogArchivingActive())
+						NotifySegmentsUpTo(openLogSegNo);
+
+					/* Already notified */
+					notify_seg = false;
+					force_continue = false;
+				}
+				else
+				{
+					/*
+					 * The last record in the finishing segment may be
+					 * continuing into the next segment. Don't archive the
+					 * segment until we are sure that the continuation record
+					 * is completely written out.
+					 */
+
+					XLogSegNo reqendseg;
+					XLogSegNo contendseg;
+
+					force_continue = true;
+
+					XLByteToSeg(WriteRqst.Write, reqendseg, wal_segment_size);
+					XLByteToSeg(lastSegContRecEnd, contendseg,
+								wal_segment_size);
+
+					/*
+					 * There's a case where we are told to flush up not to the
+					 * end of a record but to a page boundary. Advance the
+					 * request LSN to the end of the record at the boundary if
+					 * any.
+					 */
+					if (reqendseg == contendseg)
+					{
+						/*
+						 * We finished the last segment in the region. Extend
+						 * the write request to the end of the recrod.
+						 */
+						if (WriteRqst.Write < lastSegContRecEnd)
+							WriteRqst.Write = lastSegContRecEnd;
+
+						/*
+						 * Flush up to at least the same location immediately
+						 * so that the segment can be archived.
+						 */
+						if (WriteRqst.Flush < lastSegContRecEnd)
+							WriteRqst.Flush = lastSegContRecEnd;
+
+						/* We know the record will be finished */
+						notify_seg = true;
+					}
+					else
+					{
+						/* We don't know the exact placement of the record
+						 * around the requested write LSN. Do not archive this
+						 * segment since it might ends with an incomplete
+						 * record. In this case we will end up with archiving
+						 * the segment soon since we have written up to further
+						 * segments.
+						 */
+						notify_seg = false;
+					}
+				}
 
 				XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
 				XLogCtl->lastSegSwitchLSN = LogwrtResult.Flush;
@@ -2626,8 +2783,11 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
 		}
 		curridx = NextBufIdx(curridx);
 
-		/* If flexible, break out of loop as soon as we wrote something */
-		if (flexible && npages == 0)
+		/*
+		 * If flexible, break out of loop as soon as we wrote something.
+		 * However, we don't leave the loop if we have to write a bit more.
+		 */
+		if (flexible && !force_continue && npages == 0)
 			break;
 	}
 
@@ -2685,6 +2845,14 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
 			XLogCtl->LogwrtRqst.Flush = LogwrtResult.Flush;
 		SpinLockRelease(&XLogCtl->info_lck);
 	}
+
+	/*
+	 * We have extended the write request to the next segment if the record at
+	 * the initial WriteRqst.Write continues to the next segment.  In that case
+	 * need to notify the last segment here.
+	 */
+	if (notify_seg)
+		NotifySegmentsUpTo(openLogSegNo - 1);
 }
 
 /*
@@ -7716,6 +7884,18 @@ StartupXLOG(void)
 	XLogCtl->LogwrtRqst.Write = EndOfLog;
 	XLogCtl->LogwrtRqst.Flush = EndOfLog;
 
+	/*
+	 * We have archived up to the previous segment of EndOfLog so far.
+	 * Initialize lastNotifiedSeg if needed.
+	 */
+	if (XLogArchivingActive())
+	{
+		XLogSegNo	endLogSegNo;
+
+		XLByteToSeg(EndOfLog, endLogSegNo, wal_segment_size);
+		XLogCtl->lastNotifiedSeg = endLogSegNo - 1;
+	}
+
 	/*
 	 * Update full_page_writes in shared memory and write an XLOG_FPW_CHANGE
 	 * record before resource manager writes cleanup WAL records or checkpoint
@@ -8440,6 +8620,37 @@ GetFlushRecPtr(void)
 	return LogwrtResult.Flush;
 }
 
+/*
+ * GetReplicationTargetRecPtr -- Returns the latest position that is safe to
+ * replicate.
+ */
+XLogRecPtr
+GetReplicationTargetRecPtr(void)
+{
+	static XLogRecPtr	lastTargetRecPtr = InvalidXLogRecPtr;
+	XLogRecPtr	firstSegContRecStart;
+	XLogRecPtr	lastSegContRecEnd;
+
+	SpinLockAcquire(&XLogCtl->info_lck);
+	LogwrtResult = XLogCtl->LogwrtResult;
+	firstSegContRecStart = XLogCtl->firstSegContRecStart;
+	lastSegContRecEnd = XLogCtl->lastSegContRecEnd;
+	SpinLockRelease(&XLogCtl->info_lck);
+
+	/*
+	 * Don't move forward if the current flush position may be within a
+	 * continuation record that spans over segments.
+	 */
+	if (lastTargetRecPtr == InvalidXLogRecPtr ||
+		firstSegContRecStart == InvalidXLogRecPtr ||
+		XLogSegmentOffset(LogwrtResult.Flush, wal_segment_size) != 0 ||
+		LogwrtResult.Flush < firstSegContRecStart ||
+		lastSegContRecEnd <= LogwrtResult.Flush)
+		lastTargetRecPtr = LogwrtResult.Flush;
+
+	return lastTargetRecPtr;
+}
+
 /*
  * GetLastImportantRecPtr -- Returns the LSN of the last important record
  * inserted. All records not explicitly marked as unimportant are considered
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index d5c9bc31d8..c13cca1e64 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -2631,14 +2631,14 @@ XLogSendPhysical(void)
 		/*
 		 * Streaming the current timeline on a primary.
 		 *
-		 * Attempt to send all data that's already been written out and
-		 * fsync'd to disk.  We cannot go further than what's been written out
-		 * given the current implementation of WALRead().  And in any case
-		 * it's unsafe to send WAL that is not securely down to disk on the
-		 * primary: if the primary subsequently crashes and restarts, standbys
-		 * must not have applied any WAL that got lost on the primary.
+		 * Attempt to send all data that's can be replicated.  We cannot go
+		 * further than what's been written out given the current
+		 * implementation of WALRead().  And in any case it's unsafe to send
+		 * WAL that is not securely down to disk on the primary: if the primary
+		 * subsequently crashes and restarts, standbys must not have applied
+		 * any WAL that got lost on the primary.
 		 */
-		SendRqstPtr = GetFlushRecPtr();
+		SendRqstPtr = GetReplicationTargetRecPtr();
 	}
 
 	/*
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 221af87e71..94876f628c 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -338,6 +338,7 @@ extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
 extern XLogRecPtr GetRedoRecPtr(void);
 extern XLogRecPtr GetInsertRecPtr(void);
 extern XLogRecPtr GetFlushRecPtr(void);
+extern XLogRecPtr GetReplicationTargetRecPtr(void);
 extern XLogRecPtr GetLastImportantRecPtr(void);
 extern void RemovePromoteSignalFiles(void);
 
-- 
2.27.0

