v2-0005-Streamify-log_newpage_range-WAL-logging-path.patch
application/octet-stream
Filename: v2-0005-Streamify-log_newpage_range-WAL-logging-path.patch
Type: application/octet-stream
Part: 4
Message:
Re: Streamify more code paths
From d719cbc20c28634f07106e89b5de0c89cf098a8e Mon Sep 17 00:00:00 2001
From: alterego655 <824662526@qq.com>
Date: Sun, 28 Dec 2025 18:29:07 +0800
Subject: [PATCH v2 5/6] Streamify log_newpage_range() WAL logging path
Refactor log_newpage_range() to use the Read Stream. This allows
prefetching of upcoming relation blocks during bulk WAL logging
perations, overlapping I/O with CPU-intensive XLogInsert and
WAL-writing work.
---
src/backend/access/transam/xloginsert.c | 28 +++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index a56d5a55282..04c1a46143a 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -39,6 +39,7 @@
#include "replication/origin.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
+#include "storage/read_stream.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
@@ -1295,6 +1296,8 @@ log_newpage_range(Relation rel, ForkNumber forknum,
{
int flags;
BlockNumber blkno;
+ BlockRangeReadStreamPrivate p;
+ ReadStream *stream;
flags = REGBUF_FORCE_IMAGE;
if (page_std)
@@ -1307,6 +1310,23 @@ log_newpage_range(Relation rel, ForkNumber forknum,
*/
XLogEnsureRecordSpace(XLR_MAX_BLOCK_ID - 1, 0);
+ /* Set up a streaming read for the range of blocks */
+ p.current_blocknum = startblk;
+ p.last_exclusive = endblk;
+
+ /*
+ * It is safe to use batchmode as block_range_read_stream_cb takes no
+ * locks.
+ */
+ stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE |
+ READ_STREAM_USE_BATCHING,
+ NULL,
+ rel,
+ forknum,
+ block_range_read_stream_cb,
+ &p,
+ 0);
+
blkno = startblk;
while (blkno < endblk)
{
@@ -1321,8 +1341,10 @@ log_newpage_range(Relation rel, ForkNumber forknum,
nbufs = 0;
while (nbufs < XLR_MAX_BLOCK_ID && blkno < endblk)
{
- Buffer buf = ReadBufferExtended(rel, forknum, blkno,
- RBM_NORMAL, NULL);
+ Buffer buf = read_stream_next_buffer(stream, NULL);
+
+ if (!BufferIsValid(buf))
+ break;
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
@@ -1361,6 +1383,8 @@ log_newpage_range(Relation rel, ForkNumber forknum,
}
END_CRIT_SECTION();
}
+
+ read_stream_end(stream);
}
/*
--
2.51.0