Re: [RFC PATCH v1] On-demand WAL replay: accept connections before crash recovery has applied the WAL

From: Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com>
To: Srinath Reddy Sadipiralla <srinath2133(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [RFC PATCH v1] On-demand WAL replay: accept connections before crash recovery has applied the WAL
Date: 2026-11-23 11:20:56
Message-ID: CAKZiRmxL2TShnyckmQ3UQX7uKySO3TPUzx6AH-ZZR9n7kh6ojw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Oct 10, 2026 at 12:45 PM Srinath Reddy Sadipiralla
<srinath2133(at)gmail(dot)com> wrote:

Hi Srinath, sorry for posting so late, but here it finally is:

> After a crash, the server cannot accept connections until all WAL written
> since the last checkpoint has been replayed and, for crash recovery, until
> the end-of-recovery checkpoint has written the result back to disk. Both
> are proportional to the amount of WAL, so time-to-connect after a crash is
> bound by WAL volume.
>
> The attached patches let the startup process scan that WAL without
> applying the records that modify relation pages. It replays everything
> else eagerly (transaction status, multixacts, smgr and database
> operations, ... anything without a block reference, which is cheap and has
> no later page read that could trigger it), and records, per page, the LSNs
> of the records that touch it, in a dshash table in dynamic shared memory.

It's big feature, probably even controversial and risky, up to the point
You haven't got any responses so far, so I'll play devil advocate to at least
provide some amount of feedback. I have loads of my own doubts, and I've tried
to think and experiment a bit with this concept (more on this later).

Main counterargument is that for folks wishing to have fast crash consistency
restart, it is better today to simply failover to another node rather than wait
for it. So it would only happen for single instances (without HA) and/or PITR.

> - The feature applies to crash recovery under the postmaster only. Archive
> recovery, standby mode and single-user mode log a message and replay WAL
> as usual.

1. That's my big fear, it is not universal. Normally the standby is allowed
to open when consistent recovery state is reached, right? (backup_label.
XLOG_BACKUP_END or pg_control.minRecoveryPoint) and the hot standby got
WAL XLOG_RUNNING_XACTS? (it knows which transactions were in progress on
primary => so we can build MVCC snapshots). So we open with potential lag
still there normally and we allow reads, yet this patch doesn't work in this
conditions. It could take minutes for standby to open for reads (reach
consistent state) and in theory we could minimize that time too. It's not
implemented probably because it is hard/challening?

> Known limitations and also unknown :), which I would like to discuss:
[..]
> 2. Also the fast crash recovery worker could be parallel, which makes
> replaying all the pending pages recovered , so the queries won't be slow.

2. Yes, fast crash recovery worker could be parallel, which make, but if I
think there's should be some debate at least on how the grand architecural
vision would like like for parallelism in the startup&recovery; there's was
least couple of patches floating: Koichi's parallel recovery, more recent
"WAL pipelined recovery" and now this). I cannot help, but maybe others will
do.

3. Speaking of lazily WAL scanning, if we are doing it (and have plenty of
code from You for this already, thanks :D), why just don't have some filter
to filter WAL records applied to just interesting for us relations? That could
be building block for 'recover single table' for restoring/recovering single
table/schema/DB in PITR scenarios? Today we need to have all of data restored
from let's say 50TB to just to see 1 table (after WAL recovery) from 1 day
ago, which is hardly scalable. So with your's LSNIndexer we could have:

recovery_target_relation_filter = 'schemaoid.tableoid'
recovery_target_db_filter = 'dboid'

(the idea is that one would simply restore few segments and not whole cluster,
then StartReadBuffersImpl() -> ReadAndReplayPendingBuffer() looks like almost
ready to do it, filter out anything not need, simply said ignore blocks not in
those two GUCs). This is kind of dragons den, because recovering just base/
<DBOID>/<tableOID> is happy path, and restoring related-objects seems to be
harder (think of scenarios like FKs, catalog, custom extension's objects,
partitioned tables - e.g. if PITR and catalog has entries to other partitions
and I've just restored *some* what should happen)? *IF* that sounds
interesting then probably patch should be split into 3 sections (lsnindexer-
foundation infrastrcture, impl. of fast-crash, and impl. of
recovery_target_*_filters?). If this should be done, no idea, it's just that
there's additional usecase supporitng this approach/code.

===========================================================================

Probably without answers to the above design-like questions, it doesn't make
sense to fix the below findings, but just as it happened that I've played with
patch, I'm posing them (hopefully they are useful):

4. I've played with the patch and it indeed opened faster, which is cool to
see for real.

5. As stated code needs to re-read all the WAL (if there's lots of IT , it's
going to be a lot of IO, we are assuming expected 2x here), but detailed
measurement and comparisiong of the same crashed cluster indicate that
something odd is happening:

fast_crash_recovery=off / startup process:
while : ; do cat /proc/`pgrep -f "startup recovering"`/io; done
rchar: 620503048
wchar: 197158239
syscr: 75748
syscw: 24080
read_bytes: 0
write_bytes: 197566464
cancelled_write_bytes: 0

fast_crash_recovery=on // startup recovering (process "A")
while : ; do cat /proc/`pgrep -f "startup recovering"`/io; done
rchar: 620740616
wchar: 9138
syscr: 75777
syscw: 10
read_bytes: 0
write_bytes: 8192
cancelled_write_bytes: 0

fast_crash_recovery=on // fast recovery worker (process "B")
while : ; do cat /proc/`pgrep -f "fast recovery worker"`/io; done
rchar: 1673109504
wchar: 199418022
syscr: 204237
syscw: 24345
read_bytes: 0
write_bytes: 332218368
cancelled_write_bytes: 0

"Blind" Claude AI analysis of the above:
Metric OFF ON (A + B) Change
rchar 620.5 MB 2,293.9 MB 3.7× // not2x?
wchar 197.2 MB 199.4 MB ≈ same (+1.1%)
syscr 75,748 280,014 3.7× // not2x?
syscw 24,080 24,355 ≈ same
read_bytes 0 0 none
write_bytes 197.6 MB 332.2 MB +68% (+134.7 MB)

So the picture tells that writing has been done by process B (syscw), good.
However, turning the feature causes way more reading (not just expected 2x,
but 3.7x). Potentially it's eviction, but I haven't drilled more into this,
anyway sample stack of fast recovery pid doing writing looked like this:
pwrite64(79, ""..., 8192, 0) = 8192
> /usr/lib/x86_64-linux-gnu/libc.so.6(pwrite+0x17) [0xfa5d7]
> /usr/pgsql20/bin/postgres(pg_pwritev+0x39) [0x5e7aab]
> /usr/pgsql20/bin/postgres(FileWriteV+0x188) [0x5ea086]
> /usr/pgsql20/bin/postgres(mdwritev+0x1c2) [0x627000]
> /usr/pgsql20/bin/postgres(smgrwritev+0x81) [0x629e02]
> /usr/pgsql20/bin/postgres(smgrwrite+0x3f) [0x5d59e4]
> /usr/pgsql20/bin/postgres(FlushBuffer+0x17f) [0x5dc82a]
> /usr/pgsql20/bin/postgres(GetVictimBuffer+0x13d) [0x5d9a76]
/////// Evictions
> /usr/pgsql20/bin/postgres(BufferAlloc+0x12b) [0x5d94f8]
> /usr/pgsql20/bin/postgres(PinBufferForBlock+0x68) [0x5d7d82]
> /usr/pgsql20/bin/postgres(StartReadBuffersImpl+0x216) [0x5d85e0]
> /usr/pgsql20/bin/postgres(StartReadBuffer+0x53) [0x5d88c6]
> /usr/pgsql20/bin/postgres(ReadBuffer_common+0x2bb) [0x5d811f]
> /usr/pgsql20/bin/postgres(ReadBufferWithoutRelcache+0x82) [0x5d76ab]
> /usr/pgsql20/bin/postgres(FastRecoveryMain+0x280) [0x5374c2]
> /usr/pgsql20/bin/postgres(FastRecoveryWorkerMain+0xc6) [0x53762e]
> /usr/pgsql20/bin/postgres(postmaster_child_launch+0x160) [0x537a42]

6. XLogFileRead()
+#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
+
+ /*
+ * Fast crash recovery decodes WAL faster than the
kernel grows its
+ * readahead window for 8 kB reads, and then waits
at nearly every
+ * window. Ask for the whole segment at once; the
kernel reads it in
+ * the background while we decode what has already arrived.
+ */
+ if (LSNIndexIsActive())
+ (void) posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
+#endif

This looks like independent change, we could have it independently and
it doesn't feel that connected to this patch alone. Trouble is potentially
demonstrating the real benefit of such such alone.

7. With normal crash recovery, I get this (~1.5s checkpoint):
2026-11-19 09:46:13.915 CET [76368] LOG: checkpoint starting:
end-of-recovery fast wait
2026-11-19 09:46:15.419 CET [76368] LOG: checkpoint complete:
end-of-recovery fast wait: [..] total=1.506 s; [..]

but with vs fast_crash_recovery=on I get 33s?

2026-11-19 09:46:37.538 CET [76454] LOG: fast crash recovery complete
2026-11-19 09:46:37.539 CET [76448] LOG: checkpoint starting: force
2026-11-19 09:47:10.630 CET [76448] LOG: checkpoint complete: force:
wrote 2067 buffers[..], total=33.092 s; [..]
2026-11-19 09:47:10.635 CET [76448] LOG: checkpoint starting: wal

Shouldn't the checkpoint after fast crash recovery be also executed as fast as
possible to minimize the chances of subsequent crash forcing the thing to redo
all the work again? (so to save work, before next crash happen; imagine
constantly SIGSEGVing postgres).

8. The patch seems to be lacking bypassing of opening data relation segments
in lazy scan? If they are not needed we do we open them and ask for advises
there? It might pollute causes VFS cache. Below pg_wal fd = 6, and normal
segment = 921 && 922, so it shows that we are scanning WAL and opening/
fadvising a lot of normal segmetns, where it is too early for that:

[pid 373215] openat(AT_FDCWD, "base/5/16389.23334", O_RDWR|O_CLOEXEC) = 921
[pid 373215] fadvise64(921, 0, 8192, POSIX_FADV_WILLNEED) = 0
[pid 373215] pread64(6, "", 8192, 13328384) = 8192 (DELAYED)
[pid 373215] pread64(6, "", 8192, 13336576) = 8192 (DELAYED)
[pid 373215] fadvise64(921, 8192, 8192, POSIX_FADV_WILLNEED) = 0
[pid 373215] pread64(6, "", 8192, 13344768) = 8192 (DELAYED)
[pid 373215] fadvise64(921, 16384, 8192, POSIX_FADV_WILLNEED) = 0
[pid 373215] pread64(6, "", 8192, 13352960) = 8192 (DELAYED)
[pid 373215] pread64(6, "", 8192, 13361152) = 8192 (DELAYED)
[pid 373215] fadvise64(921, 24576, 8192, POSIX_FADV_WILLNEED) = 0
[pid 373215] pread64(6, """, 8192, 13369344) = 8192 (DELAYED)
[pid 373215] pread64(6, "", 8192, 13377536) = 8192 (DELAYED)
[pid 373215] fadvise64(921, 32768, 8192, POSIX_FADV_WILLNEED) = 0
[pid 373215] pread64(6, "", 8192, 13385728) = 8192 (DELAYED)
[pid 373215] pread64(6, "", 8192, 13393920) = 8192 (DELAYED)
[pid 373215] fadvise64(921, 40960, 8192, POSIX_FADV_WILLNEED) = 0
[pid 373215] pread64(6, "", 8192, 13402112) = 8192 (DELAYED)
[pid 373215] openat(AT_FDCWD, "base/5/16389.23335", O_RDWR|O_CLOEXEC) = 922
[pid 373215] fadvise64(922, 0, 8192, POSIX_FADV_WILLNEED) = 0

sample stack from startup/recovery when this happens is showing
XLogPrefetcherNextBlock() as culprint:

fadvise64(974, 40960, 8192, POSIX_FADV_WILLNEED) = 0
> /usr/lib/x86_64-linux-gnu/libc.so.6(posix_fadvise64+0xe) [0x11b7be]
> /usr/pgsql20/bin/postgres(FilePrefetch+0x73) [0x5e9d20]
> /usr/pgsql20/bin/postgres(mdprefetch+0x108) [0x6266dd]
> /usr/pgsql20/bin/postgres(smgrprefetch+0x67) [0x629bc1]
> /usr/pgsql20/bin/postgres(PrefetchSharedBuffer+0xc1) [0x5d72fd]
> /usr/pgsql20/bin/postgres(XLogPrefetcherNextBlock+0x5cb) [0x1ff0c8] <- bad?
> /usr/pgsql20/bin/postgres(lrq_prefetch+0x47) [0x1fe50f]
> /usr/pgsql20/bin/postgres(lrq_complete_lsn+0xe8) [0x1fe6d8]
> /usr/pgsql20/bin/postgres(XLogPrefetcherReadRecord+0x10a) [0x1ff75d]
> /usr/pgsql20/bin/postgres(ReadRecord+0xb1) [0x2054fd]
> /usr/pgsql20/bin/postgres(PerformWalRecovery+0x51e) [0x202d88]
> /usr/pgsql20/bin/postgres(StartupXLOG+0xa29) [0x1f2000]
> /usr/pgsql20/bin/postgres(StartupProcessMain+0x109) [0x53fb21]

So perhaps somehow disarm prefetcher during inital fast_crash_recovery scan?
(PerformWalRecovery?) There should be no openat, no pread64, no
fadvise64/posix_fadvise64 for stuff we dont need there.

9. XLogFileClose() has this:
/*
* WAL segment files will not be re-read in normal operation, so we advise
* the OS to release any cached pages. But do not do so if WAL archiving
* or streaming is active, because archiver and walsender process could
* use the cache to read the WAL segment.
*/
#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
if (!XLogIsNeeded() && (io_direct_flags & IO_DIRECT_WAL) !=0 && ...)
(void) posix_fadvise(openLogFile, 0, 0,
POSIX_FADV_DONTNEED);

but here with fast_crash_recovery=on we actually *ARE* going to re-read it
(fixing this may result in even faster recovery, if all WAL would cached
in VFS cache)

10. How and when are the prepared transactions recovered? Because:

LOG: redo done at 1/0CE26300 system usage: CPU: user: 0.94 s, system:
0.42 s, elapsed: 1.37 s
LOG: fast crash recovery: deferred 1556253 records for 123968 pages
(46 MB of index), [..]
LOG: recovering prepared transaction 690 of epoch 0 from shared memory
LOG: recovering prepared transaction 694 of epoch 0 from shared memory
[..]
LOG: recovering prepared transaction 822 of epoch 0 from shared memory
LOG: database system is ready to accept connections
LOG: fast recovery worker started
LOG: fast recovery worker replaying 123968 pending pages
LOG: fast crash recovery complete

question is, shouldn't it be replayed also from under the fast recovery
worker itself? (Transaction/PREPARE is reference-less, maybe just messsage is
wrong, btw: it got silenced rightfuly down to DEBUG2 recently with c11eb5de01)

-J.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2026-11-23 11:43:56 Re: [PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost
Previous Message Virender Singla 2026-11-23 11:19:30 [PATCH] TRUNCATE's reset of n_live_tup/n_dead_tup is lost