From ad16e3d7a7ff7e168168b96dc251cf7691cbfc1c Mon Sep 17 00:00:00 2001 From: Anthonin Bonnefoy Date: Wed, 2 Jul 2025 09:58:52 +0200 Subject: Don't keep closed WAL segments in page cache after replay The recovery process read the WAL segments, apply changes and close the segment. When closed, the segments will still be in page cache memory until they are evicted due to inactivity. This can create unnecessary memory pressure as once replayed, a WAL segment shouldn't be accessed anymore on a replica. This patch issues a POSIX_FADV_DONTNEED before closing a replayed WAL segment to avoid keeping them in the page cache. --- src/backend/access/transam/xlogrecovery.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c index 6ce979f2d8b..212a2719e3c 100644 --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -3341,6 +3341,13 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, } } + /* + * Once replayed, WAL segment files won't be re-read in normal + * operation. Signal the kernel to release any cached pages. + */ +#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED) + (void) posix_fadvise(readFile, 0, 0, POSIX_FADV_DONTNEED); +#endif close(readFile); readFile = -1; readSource = XLOG_FROM_ANY; -- 2.50.0