From 2c0832c54bc65f51931611cea4eb1c32970e07de Mon Sep 17 00:00:00 2001 From: Jakub Wartak Date: Thu, 23 Jun 2022 08:18:26 +0000 Subject: [PATCH] Use fadvise to prefetch WAL in xlogrecovery --- src/backend/access/transam/xlogrecovery.c | 12 ++++++++++++ src/backend/utils/activity/wait_event.c | 3 +++ src/include/utils/wait_event.h | 1 + 3 files changed, 16 insertions(+) diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c index 6eba626420..24fd873962 100644 --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -3237,6 +3237,18 @@ retry: Assert(targetPageOff == readOff); Assert(reqLen <= readLen); +#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED) + /* + * Prefetch next wal blocks to avoid page misses on next read iterations. + */ +#define RACHUNK (128*1024) + if (readOff % RACHUNK == 0) { + pgstat_report_wait_start(WAIT_EVENT_WAL_PREFETCH); + posix_fadvise(readFile, readOff + RACHUNK, RACHUNK, POSIX_FADV_WILLNEED); + pgstat_report_wait_end(); + } +#endif + xlogreader->seg.ws_tli = curFileTLI; /* diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 87c15b9c6f..bde5fa9106 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -732,6 +732,9 @@ pgstat_get_wait_io(WaitEventIO w) case WAIT_EVENT_WAL_READ: event_name = "WALRead"; break; + case WAIT_EVENT_WAL_PREFETCH: + event_name = "WALPrefetch"; + break; case WAIT_EVENT_WAL_SYNC: event_name = "WALSync"; break; diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index b578e2ec75..d0d68b9887 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -227,6 +227,7 @@ typedef enum WAIT_EVENT_WAL_INIT_SYNC, WAIT_EVENT_WAL_INIT_WRITE, WAIT_EVENT_WAL_READ, + WAIT_EVENT_WAL_PREFETCH, WAIT_EVENT_WAL_SYNC, WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN, WAIT_EVENT_WAL_WRITE -- 2.32.0