From 27958bc3ba69279719996f8495bfc63c83c8f0e1 Mon Sep 17 00:00:00 2001 From: Mark Wong Date: Tue, 13 Oct 2009 19:29:47 -0700 Subject: [PATCH] Fix use of posix_fadvise in xlog. Currently posix_fadvise() is called right before a log file is closed so its benefit is hardly realized. This fix moves posix_fadvise() into 3 new locations within XLogFileInit(). The first case is where an existing file handle is returned. The next case is when a file is zeroed out. The third case is returning a file handle, which may be the file that was just zeroed out. --- src/backend/access/transam/xlog.c | 27 ++++++++++++++++++++++----- 1 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 622d796..1b1794e 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2255,6 +2255,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. */ int XLogFileInit(uint32 log, uint32 seg, @@ -2287,7 +2293,13 @@ XLogFileInit(uint32 log, uint32 seg, path, log, seg))); } else + { +#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED) + if (!XLogIsNeeded()) + (void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); +#endif return fd; + } } /* @@ -2310,6 +2322,11 @@ 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 (!XLogIsNeeded()) + (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 @@ -2390,6 +2407,11 @@ 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 (!XLogIsNeeded()) + (void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); +#endif + elog(DEBUG2, "done creating and filling new WAL file"); return fd; @@ -2756,11 +2778,6 @@ XLogFileClose(void) * or streaming is active, because archiver and walsender process could * use the cache to read the WAL segment. */ -#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED) - if (!XLogIsNeeded()) - (void) posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED); -#endif - if (close(openLogFile)) ereport(PANIC, (errcode_for_file_access(), -- 1.6.0.6