From 0bb356aa0cd1582112926fbcf0b5370222c2db6d Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Fri, 3 Nov 2023 15:57:44 +0200 Subject: [PATCH] Update WAL buffers when restoring WAL at compute needed for LR (#325) * Update WAL buffers when restoring WAL at compute needed for LR * Fix copying data in WAL buffers --------- Co-authored-by: Konstantin Knizhnik --- src/backend/access/transam/xlog.c | 26 ++++++++++++++++++++++++++ src/include/access/xlog.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index e88ada5bfd7..c39f5af32e7 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -13755,3 +13755,29 @@ XLogRequestWalReceiverReply(void) { doRequestWalReceiverReply = true; } + +void +XLogUpdateWalBuffers(char* data, XLogRecPtr start, size_t len) +{ + XLogRecPtr end; + int idx; + XLogRecPtr pagebegptr; + + LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE); + + end = start + len; + idx = XLogRecPtrToBufIdx(end); + pagebegptr = XLogCtl->xlblocks[idx] - XLOG_BLCKSZ; + + if (pagebegptr + XLOG_BLCKSZ >= end && pagebegptr < end) + { + /* Last page of the segment is present in WAL buffers */ + char* page = &XLogCtl->pages[idx * XLOG_BLCKSZ]; + size_t overlap = end - pagebegptr; + if (overlap <= len) + memcpy(page, data + len - overlap, overlap); + else + memcpy(page + overlap - len, data, len); + } + LWLockRelease(WALBufMappingLock); +} diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index cf7b4e7c4c3..32e5728eb15 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -377,6 +377,8 @@ extern void SetWalWriterSleeping(bool sleeping); extern void StartupRequestWalReceiverRestart(void); extern void XLogRequestWalReceiverReply(void); +extern void XLogUpdateWalBuffers(char* data, XLogRecPtr start, size_t len); + extern void assign_max_wal_size(int newval, void *extra); extern void assign_checkpoint_completion_target(double newval, void *extra);