diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 820b439..7d5c277 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2111,6 +2111,12 @@ XLogNeedsFlush(XLogRecPtr record)
  * inside a critical section (eg, during checkpoint there is no reason to
  * take down the system on failure).  They will promote to PANIC if we are
  * in a critical section.
+ *
+ * WAL segment files will not be re-read in normal operation, so we advise
+ * the OS to release any cached pages.  But do not do so if WAL archiving
+ * is active, because archiver process could use the cache to read the WAL
+ * segment.  Also, don't bother with it if we are using O_DIRECT, since
+ * the kernel is presumably not caching in that case.
  */
 static int
 XLogFileInit(uint32 log, uint32 seg,
@@ -2143,7 +2149,14 @@ XLogFileInit(uint32 log, uint32 seg,
 								path, log, seg)));
 		}
 		else
+		{
+#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
+			if (!XLogArchivingActive() &&
+					(get_sync_bit(sync_method) & PG_O_DIRECT) == 0)
+				(void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
+#endif
 			return fd;
+		}
 	}
 
 	/*
@@ -2166,6 +2179,12 @@ XLogFileInit(uint32 log, uint32 seg,
 				(errcode_for_file_access(),
 				 errmsg("could not create file \"%s\": %m", tmppath)));
 
+#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
+	if (!XLogArchivingActive() &&
+			(get_sync_bit(sync_method) & PG_O_DIRECT) == 0)
+		(void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
+#endif
+
 	/*
 	 * Zero-fill the file.	We have to do this the hard way to ensure that all
 	 * the file space has really been allocated --- on platforms that allow
@@ -2244,6 +2263,12 @@ XLogFileInit(uint32 log, uint32 seg,
 		   errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
 				  path, log, seg)));
 
+#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
+	if (!XLogArchivingActive() &&
+			(get_sync_bit(sync_method) & PG_O_DIRECT) == 0)
+		(void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
+#endif
+
 	return fd;
 }
 
@@ -2563,19 +2588,6 @@ XLogFileClose(void)
 {
 	Assert(openLogFile >= 0);
 
-	/*
-	 * WAL segment files will not be re-read in normal operation, so we advise
-	 * the OS to release any cached pages.  But do not do so if WAL archiving
-	 * is active, because archiver process could use the cache to read the WAL
-	 * segment.  Also, don't bother with it if we are using O_DIRECT, since
-	 * the kernel is presumably not caching in that case.
-	 */
-#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
-	if (!XLogArchivingActive() &&
-		(get_sync_bit(sync_method) & PG_O_DIRECT) == 0)
-		(void) posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED);
-#endif
-
 	if (close(openLogFile))
 		ereport(PANIC,
 				(errcode_for_file_access(),
