>From a1e54ccba738cc339647bba0bafd7df7e92915c3 Mon Sep 17 00:00:00 2001
From: Takashi Menjo <takashi.menjou.vg@hco.ntt.co.jp>
Date: Mon, 10 Feb 2020 17:53:15 +0900
Subject: [msync 4/5] Speculative-map WAL segments

---
 src/backend/access/transam/xlog.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 317816a0b9..9b3caa63a4 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -976,6 +976,8 @@ XLogInsertRecord(XLogRecData *rdata,
 							   info == XLOG_SWITCH);
 	XLogRecPtr	StartPos;
 	XLogRecPtr	EndPos;
+	XLogRecPtr	ProbablyInsertPos;
+	XLogSegNo	ProbablyInsertSegNo;
 	bool		prevDoPageWrites = doPageWrites;
 
 	/* we assume that all of the record header is in the first chunk */
@@ -985,6 +987,23 @@ XLogInsertRecord(XLogRecData *rdata,
 	if (!XLogInsertAllowed())
 		elog(ERROR, "cannot make new WAL entries during recovery");
 
+	/* Speculatively map a segment we probably need */
+	ProbablyInsertPos = GetInsertRecPtr();
+	XLByteToSeg(ProbablyInsertPos, ProbablyInsertSegNo, wal_segment_size);
+	if (ProbablyInsertSegNo != openLogSegNo)
+	{
+		if (mappedPages != NULL)
+		{
+			Assert(beingUnmappedPages == NULL);
+			Assert(beingClosedLogSegNo == 0);
+			beingUnmappedPages = mappedPages;
+			beingClosedLogSegNo = openLogSegNo;
+		}
+		mappedPages = XLogFileMap(ProbablyInsertSegNo, &pmemMapped);
+		Assert(mappedPages != NULL);
+		openLogSegNo = ProbablyInsertSegNo;
+	}
+
 	/*----------
 	 *
 	 * We have now done all the preparatory work we can without holding a
-- 
2.20.1

