Use posix_fadvise() to avoid kernel caching of WAL contents on WAL file
authorBruce Momjian <bruce@momjian.us>
Thu, 15 Jun 2006 19:15:00 +0000 (19:15 +0000)
committerBruce Momjian <bruce@momjian.us>
Thu, 15 Jun 2006 19:15:00 +0000 (19:15 +0000)
close.

ITAGAKI Takahiro

src/backend/access/transam/xlog.c

index ad6767e92fb5a35b018214f21bef9d5530de616b..49e71aa12a42618959716ca453d0cd2d1ac4688c 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.237 2006/04/20 04:07:38 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.238 2006/06/15 19:15:00 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -478,6 +478,7 @@ static bool InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
                       bool use_lock);
 static int XLogFileOpen(uint32 log, uint32 seg);
 static int XLogFileRead(uint32 log, uint32 seg, int emode);
+static void    XLogFileClose(void);
 static bool RestoreArchivedFile(char *path, const char *xlogfname,
                    const char *recovername, off_t expectedSize);
 static int PreallocXlogFiles(XLogRecPtr endptr);
@@ -1384,14 +1385,7 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
             */
            Assert(npages == 0);
            if (openLogFile >= 0)
-           {
-               if (close(openLogFile))
-                   ereport(PANIC,
-                           (errcode_for_file_access(),
-                       errmsg("could not close log file %u, segment %u: %m",
-                              openLogId, openLogSeg)));
-               openLogFile = -1;
-           }
+               XLogFileClose();
            XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
 
            /* create/use new log file */
@@ -1567,14 +1561,7 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
        {
            if (openLogFile >= 0 &&
                !XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
-           {
-               if (close(openLogFile))
-                   ereport(PANIC,
-                           (errcode_for_file_access(),
-                       errmsg("could not close log file %u, segment %u: %m",
-                              openLogId, openLogSeg)));
-               openLogFile = -1;
-           }
+               XLogFileClose();
            if (openLogFile < 0)
            {
                XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
@@ -2152,6 +2139,34 @@ XLogFileRead(uint32 log, uint32 seg, int emode)
    return -1;
 }
 
+/*
+ * Close the current logfile segment for writing.
+ */
+static void
+XLogFileClose(void)
+{
+   Assert(openLogFile >= 0);
+
+#ifdef _POSIX_ADVISORY_INFO
+   /*
+    * WAL caches will not be accessed in the future, so we advise OS to
+    * free them. But we will not do so if WAL archiving is active,
+    * because archivers might use the caches to read the WAL segment.
+    * While O_DIRECT works for O_SYNC, posix_fadvise() works for fsync()
+    * and O_SYNC, and some platforms only have posix_fadvise().
+    */
+   if (!XLogArchivingActive())
+       posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED);
+#endif
+
+   if (close(openLogFile))
+       ereport(PANIC,
+           (errcode_for_file_access(),
+           errmsg("could not close log file %u, segment %u: %m",
+                  openLogId, openLogSeg)));
+   openLogFile = -1;
+}
+
 /*
  * Attempt to retrieve the specified file from off-line archival storage.
  * If successful, fill "path" with its complete path (note that this will be
@@ -5609,14 +5624,7 @@ assign_xlog_sync_method(const char *method, bool doit, GucSource source)
                         errmsg("could not fsync log file %u, segment %u: %m",
                                openLogId, openLogSeg)));
            if (open_sync_bit != new_sync_bit)
-           {
-               if (close(openLogFile))
-                   ereport(PANIC,
-                           (errcode_for_file_access(),
-                       errmsg("could not close log file %u, segment %u: %m",
-                              openLogId, openLogSeg)));
-               openLogFile = -1;
-           }
+               XLogFileClose();
        }
        sync_method = new_sync_method;
        open_sync_bit = new_sync_bit;