From 6902b207cf7497396493aef369a9e275900a86e7 Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Wed, 12 Jun 2024 14:46:16 +0000 Subject: [PATCH v2 1/2] Use WALReadFromBuffers in more places Commit 91f2cae introduced WALReadFromBuffers, and used it only for physical replication walsenders. There are couple of other callers that use read_local_xlog_page page_read callback and logical replication walsenders can also benefit reading WAL from WAL buffers using the new function. This commit uses the new function for these callers. Author: Bharath Rupireddy Reviewed-by: Jingtang Zhang, Nitin Jadhav Discussion: https://www.postgresql.org/message-id/CALj2ACVfF2Uj9NoFy-5m98HNtjHpuD17EDE9twVeJng-jTAe7A%40mail.gmail.com --- src/backend/access/transam/xlogutils.c | 10 +++++++++- src/backend/replication/walsender.c | 13 ++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 5295b85fe0..24a7ef0479 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -892,6 +892,7 @@ read_local_xlog_page_guts(XLogReaderState *state, XLogRecPtr targetPagePtr, int count; WALReadError errinfo; TimeLineID currTLI; + Size rbytes; loc = targetPagePtr + reqLen; @@ -1004,7 +1005,14 @@ read_local_xlog_page_guts(XLogReaderState *state, XLogRecPtr targetPagePtr, count = read_upto - targetPagePtr; } - if (!WALRead(state, cur_page, targetPagePtr, count, tli, + /* attempt to read WAL from WAL buffers first */ + rbytes = WALReadFromBuffers(cur_page, targetPagePtr, count, currTLI); + cur_page += rbytes; + targetPagePtr += rbytes; + + /* now read the remaining WAL from WAL file */ + if ((count - rbytes) > 0 && + !WALRead(state, cur_page, targetPagePtr, (count - rbytes), tli, &errinfo)) WALReadRaiseError(&errinfo); diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index c623b07cf0..bd0decef3d 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -1056,6 +1056,7 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req WALReadError errinfo; XLogSegNo segno; TimeLineID currTLI; + Size rbytes; /* * Make sure we have enough WAL available before retrieving the current @@ -1092,11 +1093,17 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req else count = flushptr - targetPagePtr; /* part of the page available */ - /* now actually read the data, we know it's there */ - if (!WALRead(state, + /* attempt to read WAL from WAL buffers first */ + rbytes = WALReadFromBuffers(cur_page, targetPagePtr, count, currTLI); + cur_page += rbytes; + targetPagePtr += rbytes; + + /* now read the remaining WAL from WAL file */ + if ((count - rbytes) > 0 && + !WALRead(state, cur_page, targetPagePtr, - count, + (count - rbytes), currTLI, /* Pass the current TLI because only * WalSndSegmentOpen controls whether new TLI * is needed. */ -- 2.34.1