>From 72728138ef92b744b64464d21ba35d4b717a55bb Mon Sep 17 00:00:00 2001
From: Takashi Menjo <takashi.menjou.vg@hco.ntt.co.jp>
Date: Mon, 10 Feb 2020 17:53:11 +0900
Subject: [msync 1/5] Preallocate more WAL segments

Please run ./configure with LIBS=-lpmem to build this patchset.
---
 src/backend/access/transam/xlog.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 77ad765989..e2cd34057f 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -891,7 +891,7 @@ static bool WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
 										bool fetching_ckpt, XLogRecPtr tliRecPtr);
 static int	emode_for_corrupt_record(int emode, XLogRecPtr RecPtr);
 static void XLogFileClose(void);
-static void PreallocXlogFiles(XLogRecPtr endptr);
+static void PreallocXlogFiles(XLogRecPtr RedoRecPtr, XLogRecPtr endptr);
 static void RemoveTempXlogFiles(void);
 static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr RedoRecPtr, XLogRecPtr endptr);
 static void RemoveXlogFile(const char *segname, XLogRecPtr RedoRecPtr, XLogRecPtr endptr);
@@ -3801,27 +3801,20 @@ XLogFileClose(void)
 
 /*
  * Preallocate log files beyond the specified log endpoint.
- *
- * XXX this is currently extremely conservative, since it forces only one
- * future log segment to exist, and even that only if we are 75% done with
- * the current one.  This is only appropriate for very low-WAL-volume systems.
- * High-volume systems will be OK once they've built up a sufficient set of
- * recycled log segments, but the startup transient is likely to include
- * a lot of segment creations by foreground processes, which is not so good.
  */
 static void
-PreallocXlogFiles(XLogRecPtr endptr)
+PreallocXlogFiles(XLogRecPtr RedoRecPtr, XLogRecPtr endptr)
 {
 	XLogSegNo	_logSegNo;
+	XLogSegNo	endSegNo;
+	XLogSegNo	recycleSegNo;
 	int			lf;
 	bool		use_existent;
-	uint64		offset;
 
-	XLByteToPrevSeg(endptr, _logSegNo, wal_segment_size);
-	offset = XLogSegmentOffset(endptr - 1, wal_segment_size);
-	if (offset >= (uint32) (0.75 * wal_segment_size))
+	XLByteToPrevSeg(endptr, endSegNo, wal_segment_size);
+	recycleSegNo = XLOGfileslop(RedoRecPtr);
+	for (_logSegNo = endSegNo + 1; _logSegNo <= recycleSegNo; _logSegNo++)
 	{
-		_logSegNo++;
 		use_existent = true;
 		lf = XLogFileInit(_logSegNo, &use_existent, true);
 		close(lf);
@@ -7692,7 +7685,7 @@ StartupXLOG(void)
 	/*
 	 * Preallocate additional log files, if wanted.
 	 */
-	PreallocXlogFiles(EndOfLog);
+	PreallocXlogFiles(RedoRecPtr, EndOfLog);
 
 	/*
 	 * Okay, we're officially UP.
@@ -8905,7 +8898,7 @@ CreateCheckPoint(int flags)
 	 * segments, since that may supply some of the needed files.)
 	 */
 	if (!shutdown)
-		PreallocXlogFiles(recptr);
+		PreallocXlogFiles(RedoRecPtr, recptr);
 
 	/*
 	 * Truncate pg_subtrans if possible.  We can throw away all data before
@@ -9255,7 +9248,7 @@ CreateRestartPoint(int flags)
 	 * Make more log segments if needed.  (Do this after recycling old log
 	 * segments, since that may supply some of the needed files.)
 	 */
-	PreallocXlogFiles(endptr);
+	PreallocXlogFiles(RedoRecPtr, endptr);
 
 	/*
 	 * ThisTimeLineID is normally not set when we're still in recovery.
-- 
2.20.1

