Small WAL replay perf improvements (smgropen()+reduce membarriers for LSN waiters)

From: Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Small WAL replay perf improvements (smgropen()+reduce membarriers for LSN waiters)
Date: 2026-11-30 07:28:18
Message-ID: CAKZiRmzEs+S60pW_gbjBjSrRYLKfKRf08dTcmN-7+qh8aDFPsw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v27112026-0001-Avoid-redundant-smgropen-in-XLogReadBuffer.patch text/x-patch 3.0 KB
v27112026-0002-Cache-the-most-recently-opened-relation-in.patch text/x-patch 2.1 KB
v27112026-0003-WAIT-FOR-LSN-reduce-per-record-overhead-du.patch text/x-patch 3.6 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message xnervwang 2026-11-30 07:14:35 Re: GetNewOidWithIndex() can reuse a TOAST value ID while its chunks are still in the heap