Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 94a5c4a

Browse files
committed
Use posix_fadvise() to avoid kernel caching of WAL contents on WAL file
close. ITAGAKI Takahiro
1 parent a584c12 commit 94a5c4a

File tree

1 file changed

+33
-25
lines changed
  • src/backend/access/transam

1 file changed

+33
-25
lines changed

src/backend/access/transam/xlog.c

+33-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.237 2006/04/20 04:07:38 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.238 2006/06/15 19:15:00 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -478,6 +478,7 @@ static bool InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
478478
bool use_lock);
479479
static int XLogFileOpen(uint32 log, uint32 seg);
480480
static int XLogFileRead(uint32 log, uint32 seg, int emode);
481+
static void XLogFileClose(void);
481482
static bool RestoreArchivedFile(char *path, const char *xlogfname,
482483
const char *recovername, off_t expectedSize);
483484
static int PreallocXlogFiles(XLogRecPtr endptr);
@@ -1384,14 +1385,7 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
13841385
*/
13851386
Assert(npages == 0);
13861387
if (openLogFile >= 0)
1387-
{
1388-
if (close(openLogFile))
1389-
ereport(PANIC,
1390-
(errcode_for_file_access(),
1391-
errmsg("could not close log file %u, segment %u: %m",
1392-
openLogId, openLogSeg)));
1393-
openLogFile = -1;
1394-
}
1388+
XLogFileClose();
13951389
XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
13961390

13971391
/* create/use new log file */
@@ -1567,14 +1561,7 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
15671561
{
15681562
if (openLogFile >= 0 &&
15691563
!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
1570-
{
1571-
if (close(openLogFile))
1572-
ereport(PANIC,
1573-
(errcode_for_file_access(),
1574-
errmsg("could not close log file %u, segment %u: %m",
1575-
openLogId, openLogSeg)));
1576-
openLogFile = -1;
1577-
}
1564+
XLogFileClose();
15781565
if (openLogFile < 0)
15791566
{
15801567
XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
@@ -2152,6 +2139,34 @@ XLogFileRead(uint32 log, uint32 seg, int emode)
21522139
return -1;
21532140
}
21542141

2142+
/*
2143+
* Close the current logfile segment for writing.
2144+
*/
2145+
static void
2146+
XLogFileClose(void)
2147+
{
2148+
Assert(openLogFile >= 0);
2149+
2150+
#ifdef _POSIX_ADVISORY_INFO
2151+
/*
2152+
* WAL caches will not be accessed in the future, so we advise OS to
2153+
* free them. But we will not do so if WAL archiving is active,
2154+
* because archivers might use the caches to read the WAL segment.
2155+
* While O_DIRECT works for O_SYNC, posix_fadvise() works for fsync()
2156+
* and O_SYNC, and some platforms only have posix_fadvise().
2157+
*/
2158+
if (!XLogArchivingActive())
2159+
posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED);
2160+
#endif
2161+
2162+
if (close(openLogFile))
2163+
ereport(PANIC,
2164+
(errcode_for_file_access(),
2165+
errmsg("could not close log file %u, segment %u: %m",
2166+
openLogId, openLogSeg)));
2167+
openLogFile = -1;
2168+
}
2169+
21552170
/*
21562171
* Attempt to retrieve the specified file from off-line archival storage.
21572172
* 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)
56095624
errmsg("could not fsync log file %u, segment %u: %m",
56105625
openLogId, openLogSeg)));
56115626
if (open_sync_bit != new_sync_bit)
5612-
{
5613-
if (close(openLogFile))
5614-
ereport(PANIC,
5615-
(errcode_for_file_access(),
5616-
errmsg("could not close log file %u, segment %u: %m",
5617-
openLogId, openLogSeg)));
5618-
openLogFile = -1;
5619-
}
5627+
XLogFileClose();
56205628
}
56215629
sync_method = new_sync_method;
56225630
open_sync_bit = new_sync_bit;

0 commit comments

Comments
 (0)