>From 0e34f41ac611cf0f4e5bdc3428b71e0f81d33cb0 Mon Sep 17 00:00:00 2001
From: Takashi Menjo <takashi.menjou.vg@hco.ntt.co.jp>
Date: Mon, 10 Feb 2020 17:53:16 +0900
Subject: [msync 5/5] Allocate WAL segments to utilize hugepage

See also https://nvdimm.wiki.kernel.org/2mib_fs_dax
---
 src/backend/access/transam/xlog.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 9b3caa63a4..d3ef7bf6e5 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2904,8 +2904,21 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
 	memset(zbuffer.data, 0, XLOG_BLCKSZ);
 
 	pgstat_report_wait_start(WAIT_EVENT_WAL_INIT_WRITE);
-	save_errno = 0;
-	if (wal_init_zero)
+
+	/*
+	 * Allocate the file by posix_allocate(3) to utilize hugepage and reduce
+	 * overhead of page fault.  Note that posix_fallocate(3) do not set errno
+	 * on error.  Instead, it returns an error number directly.
+	 */
+	save_errno = posix_fallocate(fd, 0, wal_segment_size);
+
+	if (save_errno)
+	{
+		/*
+		 * Do nothing on error.  Go to pgstat_report_wait_end().
+		 */
+	}
+	else if (wal_init_zero)
 	{
 		XLogCtlInsert  *Insert = &XLogCtl->Insert;
 		XLogPageHeader	NewPage = (XLogPageHeader) zbuffer.data;
-- 
2.20.1

