During recent review/analysis of WAL replay patch, I've got the following
picture in the perf report no-children from startup/recovery:
9.09% postgres postgres [.] hash_bytes
---hash_bytes
tag_hash
|--7.02%--hash_search
| smgropen
| |--4.95%--ReadBufferWithoutRelcache
| | XLogReadBufferExtended
| | |--3.60%--XLogReadBufferForRedoExtended
[..] | | |
| | --1.35%--XLogRecordPageWithFreeSpace
| --2.07%--XLogReadBufferExtended
| XLogReadBufferForRedoExtended
| XLogReadBufferForRedo
| heap_xlog_insert
--2.07%--get_hash_value
[..]
so clearly the pretty well known smgropen()->hash_bytes() hotspot [1], but
quite unexpectedly when looking at this, it struck me that smgropen() could be
simply reused: today XLogReadBufferExtended() does smgropen()/smgrcreate() and
after a few lines calls ReadBufferWithoutRelcache() to again (!) call
smgropen().. - see 0001.
Another thing while at it, is that even simplest one element smgr cache could
yield substantion benefit for the same row-by-row (we hit smgropen() quite
often), see 0002.
There's even third thing that surpised me: I've noticed there's new code from
PG 19 onwards (WAIT FOR LSN), and this seems to be (?) slowing us down too
potentially (by how much I don't haven't measured from last version PG18 vs
PG19), but we seem to do now 3x separate pg_atomic_read_membarrier_u64()
from the main redo loop (PerformWalRecovery()), so quick attempted to do just
one read barrier read seems to work and there gives some boost too. But I
haven't tested this on like ARM (where without such barriers CPUs are more
free to reoder stuff - right?), so I just hope it's safe (it's first time
for me to play with barrier(s), maybe better one could be used there, dunno).
Other interesting thing (not pursued covered in patch) is perhaps why do
we even check for the WaitLSNWakeups() in case of doing pure (closed) crash
recovery (when DB is inaccessible, way before anything could issue register
to be waiting for anything)?
Benchmarks: 3 workloads were used, each with 2GB of WAL, 3 runs, and those are
averages:
a. CREATE TABLE t (id bigint) with INSERT INTO t SELECT generate_series(..)
b. CREATE TABLE tpk (id bigint primary key) with INSERT INTO tpk SELECT
generate_series(..)
c. pgbench -i -s 20, with pgbench -c 4 -j 4
First, marketing ;) numbers to show % gain to master, that was ofc with noturbo/
performance CPU governor to get stable measurements:
workload 0001 0002 0003 combined
a: t (id bigint) insert -5% -9% -4% -14%
b: tpk (id bigint primary key) insert -5% -4% -4% -9.5%
c: pgbench -i -s 20, -c 4 -j 0% 0% -2.4% -4.0%
Same results, but just in seconds for WAL recovery:
workload master 0001 0002 0003 combined
a 16.90 16.02 15.35 16.24 14.57
b 15.78 14.95 15.14 15.18 14.26
c 25.31 25.16 25.32 24.71 24.30
2048MB / 14.57 seconds =~ 140 MB/s and ofc, disabling Intel's turbo is bit
artificial CPU slowdown (on real production nobody uses such slow CPU cores),
but when I enable it again, I've been able to breach 300MB/s of WAL for
workload "a" (table alone without PK) on commodity CPU (yay! ;)):
workload master 0001 0002 0033 combined
a 7.74 7.29 7.10 7.51 6.72
2048 MB / 7.74 =~ 264 MB/s (master)
2048 MB / 6.72 =~ 305 MB/s (combined), and that's ~2.44Gbit/s, so we could claim
that we are (at least partially) 2.5GbE ready now ;]
-J.
[1] -
https://www.postgresql.org/message-id/CA%2BhUKG%2B67j4fUiuSP2d8U5r0gROBpx%3DiPgUQd-9DT%3Dy-zv-pCA%40mail.gmail.com
From 130fcfe3a4940227ac3c3758e7f8c78ee857c40a Mon Sep 17 00:00:00 2001
From: Jakub Wartak <[email protected]>
Date: Fri, 27 Nov 2026 14:15:12 +0100
Subject: [PATCH v27112026 1/3] Avoid redundant smgropen() in
XLogReadBufferExtended()
XLogReadBufferExtended() today performs smgropen()/smgrcreate() and few lines
later calls ReadBufferWithoutRelcache() which again calls smgropen() that
performs the same hash table lookup calculation again.
Add ReadBufferWithoutRelcacheSmgr() to optimize that and avoid second hash
table lookup for every block read during WAL replay.
Author: Jakub Wartak <[email protected]>
Discussion:
---
src/backend/access/transam/xlogutils.c | 4 ++--
src/backend/storage/buffer/bufmgr.c | 13 +++++++++++++
src/include/storage/bufmgr.h | 4 ++++
3 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index ff71e14015d..551c4c30769 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -518,8 +518,8 @@ XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum,
if (blkno < lastblock)
{
/* page exists in file */
- buffer = ReadBufferWithoutRelcache(rlocator, forknum, blkno,
- mode, NULL, true);
+ buffer = ReadBufferWithoutRelcacheSmgr(smgr, forknum, blkno,
+ mode, NULL, true);
}
else
{
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index c17fc3590f0..ff0544cbe67 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -959,6 +959,19 @@ ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum,
{
SMgrRelation smgr = smgropen(rlocator, INVALID_PROC_NUMBER);
+ return ReadBufferWithoutRelcacheSmgr(smgr, forkNum, blockNum,
+ mode, strategy, permanent);
+}
+
+/*
+ * ReadBufferWithoutRelcacheSmgr -- like ReadBufferWithoutRelcache, but
+ * save smgropen() hash lookup.
+ */
+Buffer
+ReadBufferWithoutRelcacheSmgr(SMgrRelation smgr, ForkNumber forkNum,
+ BlockNumber blockNum, ReadBufferMode mode,
+ BufferAccessStrategy strategy, bool permanent)
+{
return ReadBuffer_common(NULL, smgr,
permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
forkNum, blockNum,
diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h
index 6837b35fc6d..1d05afcac9b 100644
--- a/src/include/storage/bufmgr.h
+++ b/src/include/storage/bufmgr.h
@@ -241,6 +241,10 @@ extern Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator,
ForkNumber forkNum, BlockNumber blockNum,
ReadBufferMode mode, BufferAccessStrategy strategy,
bool permanent);
+extern Buffer ReadBufferWithoutRelcacheSmgr(SMgrRelation smgr,
+ ForkNumber forkNum, BlockNumber blockNum,
+ ReadBufferMode mode, BufferAccessStrategy strategy,
+ bool permanent);
extern bool StartReadBuffer(ReadBuffersOperation *operation,
Buffer *buffer,
--
2.43.0
From 983bbd267460d7f52020ef6cb7eb975114e28dba Mon Sep 17 00:00:00 2001
From: Jakub Wartak <[email protected]>
Date: Fri, 27 Nov 2026 14:15:26 +0100
Subject: [PATCH v27112026 2/3] Cache the most recently opened relation in
smgropen()
Cache the SMgrRelation returned by the previous call of smgropen() and return
it directly if the next call asks for the same relation and same backend.
Author: Jakub Wartak <[email protected]>
Discussion:
---
src/backend/storage/smgr/smgr.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index 5391640d861..34e65059d20 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -159,6 +159,12 @@ static const int NSmgr = lengthof(smgrsw);
*/
static HTAB *SMgrRelationHash = NULL;
+/*
+ * The SMgrRelation cache. WAL replay tends to open the same relation quite
+ * frequently, so caching this avoids lots of smgr hash table lookups.
+ */
+static SMgrRelation smgr_last_opened_cache = NULL;
+
static dlist_head unpinned_relns;
/* local function prototypes */
@@ -245,6 +251,12 @@ smgropen(RelFileLocator rlocator, ProcNumber backend)
Assert(RelFileNumberIsValid(rlocator.relNumber));
+ /* Fast path smgr cache: is it the same relation as last time? */
+ if (smgr_last_opened_cache != NULL &&
+ RelFileLocatorEquals(smgr_last_opened_cache->smgr_rlocator.locator, rlocator) &&
+ smgr_last_opened_cache->smgr_rlocator.backend == backend)
+ return smgr_last_opened_cache;
+
HOLD_INTERRUPTS();
if (SMgrRelationHash == NULL)
@@ -283,6 +295,9 @@ smgropen(RelFileLocator rlocator, ProcNumber backend)
smgrsw[reln->smgr_which].smgr_open(reln);
}
+ /* Fast path smgr cache: set */
+ smgr_last_opened_cache = reln;
+
RESUME_INTERRUPTS();
return reln;
@@ -333,6 +348,10 @@ smgrdestroy(SMgrRelation reln)
dlist_delete(&reln->node);
+ /* Fast path smgr cache: reset */
+ if (smgr_last_opened_cache == reln)
+ smgr_last_opened_cache = NULL;
+
if (hash_search(SMgrRelationHash,
&(reln->smgr_rlocator),
HASH_REMOVE, NULL) == NULL)
--
2.43.0
From 7880bc6d59ac78474daeaf8cf6cbfeb75857c431 Mon Sep 17 00:00:00 2001
From: Jakub Wartak <[email protected]>
Date: Fri, 27 Nov 2026 14:15:30 +0100
Subject: [PATCH v27112026 3/3] WAIT FOR LSN: reduce per record overhead during
WAL replay
Replace the three separate WaitLSNWakeup() calls during WAL replay with single
WaitLSNWakeupStandby() which does a just single memory barrier (e.g. x86 "lock"
instruction) followed by plain reads of the three minWaitedLSN values.
Author: Jakub Wartak <[email protected]>
Discussion:
---
src/backend/access/transam/xlogrecovery.c | 7 +-----
src/backend/access/transam/xlogwait.c | 26 +++++++++++++++++++++++
src/include/access/xlogwait.h | 1 +
3 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index e091a3e178b..4e0c03af07d 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -1809,12 +1809,7 @@ PerformWalRecovery(void)
* WAL was already written and flushed to disk, so write and flush
* waiters can be woken at the replay position too.
*/
- WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY,
- XLogRecoveryCtl->lastReplayedEndRecPtr);
- WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE,
- XLogRecoveryCtl->lastReplayedEndRecPtr);
- WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH,
- XLogRecoveryCtl->lastReplayedEndRecPtr);
+ WaitLSNWakeupStandby(XLogRecoveryCtl->lastReplayedEndRecPtr);
/* Exit loop if we reached inclusive recovery target */
if (recoveryStopsAfter(xlogreader))
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 7170921ecdf..855fea3780f 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -376,6 +376,32 @@ WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN)
wakeupWaiters(lsnType, currentLSN);
}
+/*
+ * Wake up the all the standby replay, write and flush waiters for currentLSN.
+ *
+ * Speedy variant: use just single memory barrier for read (e.g. single "lock"
+ * x86 instruction) rather than three as it would happen with three
+ * (WAIT_LSN_TYPE_COUNT) separate WaitLSNWakeup() calls.
+ */
+void
+WaitLSNWakeupStandby(XLogRecPtr currentLSN)
+{
+ if (!XLogRecPtrIsValid(currentLSN))
+ return;
+
+ /* Pairs with pg_atomic_write_membarrier_u64() in updateMinWaitedLSN(). */
+ pg_memory_barrier();
+
+ if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_REPLAY]) <= currentLSN)
+ wakeupWaiters(WAIT_LSN_TYPE_STANDBY_REPLAY, currentLSN);
+
+ if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_WRITE]) <= currentLSN)
+ wakeupWaiters(WAIT_LSN_TYPE_STANDBY_WRITE, currentLSN);
+
+ if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_FLUSH]) <= currentLSN)
+ wakeupWaiters(WAIT_LSN_TYPE_STANDBY_FLUSH, currentLSN);
+}
+
/*
* Clean up any LSN wait state for the current process.
*/
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 2bf0263e9e2..1f9862f5a69 100644
--- a/src/include/access/xlogwait.h
+++ b/src/include/access/xlogwait.h
@@ -102,6 +102,7 @@ extern PGDLLIMPORT WaitLSNState *waitLSNState;
extern XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType);
extern void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN);
+extern void WaitLSNWakeupStandby(XLogRecPtr currentLSN);
extern void WaitLSNCleanup(void);
extern WaitLSNResult WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN,
int timeout);
--
2.43.0