From cb6ecaa3a2d6d65dc6f95743fdb1d16c457927ea Mon Sep 17 00:00:00 2001 From: Takashi Menjo Date: Mon, 16 Mar 2020 11:14:02 +0900 Subject: [PATCH v3 04/10] Lazy-unmap WAL segments --- src/backend/access/transam/xlog.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index a20fadbb55..7d9d2dc06a 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -798,7 +798,9 @@ static const char *const xlogSourceNames[] = {"any", "archive", "pg_wal", "strea */ static int openLogFile = -1; static XLogSegNo openLogSegNo = 0; +static XLogSegNo beingClosedLogSegNo = 0; static char *mappedPages = NULL; +static char *beingUnmappedPages = NULL; static bool pmemMapped = 0; /* 2MiB hugepage mask used by XLogFileMapHint */ @@ -1215,6 +1217,14 @@ XLogInsertRecord(XLogRecData *rdata, } } + /* Lazy-unmap */ + if (beingUnmappedPages != NULL) + { + XLogFileUnmap(beingUnmappedPages, beingClosedLogSegNo); + beingUnmappedPages = NULL; + beingClosedLogSegNo = 0; + } + #ifdef WAL_DEBUG if (XLOG_DEBUG) { @@ -1857,9 +1867,23 @@ GetXLogBuffer(XLogRecPtr ptr) XLByteToSeg(ptr, segno, wal_segment_size); if (segno != openLogSegNo) { - /* Unmap the current segment if mapped */ + /* + * We do not want to unmap the current segment here because we are in + * a critial section and unmap is time-consuming operation. So we + * just mark it to be unmapped later. + */ if (mappedPages != NULL) - XLogFileUnmap(mappedPages, openLogSegNo); + { + /* + * If there is another being-unmapped segment, it cannot be helped; + * we unmap it here. + */ + if (beingUnmappedPages != NULL) + XLogFileUnmap(beingUnmappedPages, beingClosedLogSegNo); + + beingUnmappedPages = mappedPages; + beingClosedLogSegNo = openLogSegNo; + } /* Map the segment we need */ mappedPages = XLogFileMap(segno, &pmemMapped); -- 2.25.1