From 590592ae1c32d5661ed93f3fa2e41dd3c80b963b Mon Sep 17 00:00:00 2001 From: Matt Blewitt Date: Tue, 22 Sep 2026 18:34:22 +0100 Subject: [PATCH v1] basebackup: use sequential access advice for full-file reads --- src/backend/backup/basebackup.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c index f8c3520e5b8..3819aa65a23 100644 --- a/src/backend/backup/basebackup.c +++ b/src/backend/backup/basebackup.c @@ -12,6 +12,7 @@ */ #include "postgres.h" +#include #include #include #include @@ -127,6 +128,7 @@ static void perform_base_backup(basebackup_options *opt, bbsink *sink, IncrementalBackupInfo *ib); static void parse_basebackup_options(List *options, basebackup_options *opt); static int compareWalFileNames(const ListCell *a, const ListCell *b); +static void basebackup_fadvise_sequential(int fd); static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset, const char *filename, bool partial_read_ok); @@ -574,6 +576,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink, (errcode_for_file_access(), errmsg("could not open file \"%s\": %m", pathbuf))); } + basebackup_fadvise_sequential(fd); if (fstat(fd, &statbuf) != 0) ereport(ERROR, @@ -1625,6 +1628,8 @@ sendFile(bbsink *sink, const char *readfilename, const char *tarfilename, (errcode_for_file_access(), errmsg("could not open file \"%s\": %m", readfilename))); } + if (incremental_blocks == NULL) + basebackup_fadvise_sequential(fd); /* * Non-relation files may be replaced by a rename(), making irrelevant the @@ -2204,6 +2209,16 @@ convert_link_to_directory(const char *pathbuf, struct stat *statbuf) statbuf->st_mode = S_IFDIR | pg_dir_create_mode; } +static void +basebackup_fadvise_sequential(int fd) +{ +#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_SEQUENTIAL) + (void) posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); +#else + (void) fd; +#endif +} + /* * Read some data from a file, setting a wait event and reporting any error * encountered. -- 2.53.0