From e953e2465a73259eb15553cadbaf6d689cdc53d0 Mon Sep 17 00:00:00 2001 From: Takashi Menjo Date: Tue, 1 Jun 2021 19:29:22 +0900 Subject: [PATCH v9 08/10] Preallocate and initialize more WAL if wal_pmem_map=true Fixes introduced in patchset v9: - Fix wrong handling of missingContrecPtr --- src/backend/access/transam/xlog.c | 39 ++++++++++++++++++++++++++----- src/backend/utils/misc/guc.c | 2 +- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index afbb439dfc..11b0b7aec5 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -1676,7 +1676,9 @@ GetXLogBuffer(XLogRecPtr ptr, TimeLineID tli) { openLogTLI = tli; openLogSegNo = PmemXLogEnsurePrevMapped(endptr, tli); - cachedPos = PmemXLogGetBufferPages() + idx * (Size) XLOG_BLCKSZ; + cachedPos = PmemXLogGetBufferPages() + + (Size) XLogSegmentOffset(endptr - XLOG_BLCKSZ, + wal_segment_size); } else cachedPos = XLogCtl->pages + idx * (Size) XLOG_BLCKSZ; @@ -1917,7 +1919,9 @@ AdvanceXLInsertBuffer(XLogRecPtr upto, TimeLineID tli, bool opportunistic) { openLogTLI = tli; openLogSegNo = PmemXLogEnsurePrevMapped(NewPageEndPtr, tli); - NewPage = (XLogPageHeader) (PmemXLogGetBufferPages() + nextidx * (Size) XLOG_BLCKSZ); + NewPage = (XLogPageHeader) + (PmemXLogGetBufferPages() + + (Size) XLogSegmentOffset(NewPageBeginPtr, wal_segment_size)); } else NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * (Size) XLOG_BLCKSZ); @@ -4444,15 +4448,25 @@ XLOGShmemSize(void) /* * If we use WAL segment files as WAL buffers, we don't use the given * value of wal_buffers. Instead, we set it to the value based on the - * segment size and the page size. This should be done before calculating + * min_wal_size and the page size. This should be done before calculating * the size of xlblocks array. + * + * TODO Do not allow changing min_wal_size by SIGHUP if wal_pmem_map=true. + * + * TODO Move validations to check_hook functions. */ if (wal_pmem_map) { int npages; char buf[32]; - npages = wal_segment_size / XLOG_BLCKSZ; + if (min_wal_size_mb % (wal_segment_size / (1024 * 1024)) != 0) + elog(PANIC, "min_wal_size should be multiple of wal_segment_size when wal_pmem_map=true"); + + if (min_wal_size_mb / (XLOG_BLCKSZ / 1024) > INT_MAX / 1024) + elog(PANIC, "too many wal buffer pages"); + + npages = min_wal_size_mb / (XLOG_BLCKSZ / 1024) * 1024; snprintf(buf, sizeof(buf), "%d", (int) npages); SetConfigOption("wal_buffers", buf, PGC_POSTMASTER, PGC_S_OVERRIDE); } @@ -5585,7 +5599,9 @@ StartupXLOG(void) */ openLogTLI = newTLI; openLogSegNo = PmemXLogEnsurePrevMapped(EndOfLog, newTLI); - page = PmemXLogGetBufferPages() + firstIdx * (Size) XLOG_BLCKSZ; + page = PmemXLogGetBufferPages() + + (Size) XLogSegmentOffset(endOfRecoveryInfo->lastPageBeginPtr, + wal_segment_size); memset(page + len, 0, XLOG_BLCKSZ - len); /* Cache-flush and sync now */ @@ -5623,7 +5639,18 @@ StartupXLOG(void) /* * Preallocate additional log files, if wanted. */ - PreallocXlogFiles(EndOfLog, newTLI); + if (wal_pmem_map) + { + /* + * AdvanceXLInsertBuffer may reset missingContrecPtr, so we + * save and restore it for CreateOverwriteContrecordRecord. + */ + XLogRecPtr savedMissingContrecPtr = missingContrecPtr; + AdvanceXLInsertBuffer(InvalidXLogRecPtr, newTLI, true); + missingContrecPtr = savedMissingContrecPtr; + } + else + PreallocXlogFiles(EndOfLog, newTLI); /* * Okay, we're officially UP. diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 5268cd95f4..776a8f7618 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -2886,7 +2886,7 @@ static struct config_int ConfigureNamesInt[] = GUC_UNIT_XBLOCKS }, &XLOGbuffers, - -1, -1, (INT_MAX / XLOG_BLCKSZ), + -1, -1, INT_MAX, check_wal_buffers, NULL, NULL }, -- 2.25.1