From 0619925d04488f9b04b72eeaa7dea64eeda5e37f Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Fri, 31 Jul 2026 13:51:03 +0200
Subject: [PATCH v06082026 05/10] basebackup: issue posix_fadvise() for more
 efficient cold-cache handling

By informing the OS that we are going need whole file being backed up quite
soon, it can perform more efficient read-ahead. This in turns allows our
synchronous basebackup_read_file()->pread64() calls to get lower latencies
(as they fetch faster just from page-cache more often) and therefore this
increases the efficiency of the whole data transfer.

Author: Jakub Wartak <jakub.wartak@enterprisedb.com>
---
 src/backend/backup/basebackup.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c
index d609355f429..dedfde74f90 100644
--- a/src/backend/backup/basebackup.c
+++ b/src/backend/backup/basebackup.c
@@ -1600,6 +1600,14 @@ sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
 				 errmsg("could not open file \"%s\": %m", readfilename)));
 	}
 
+	/*
+	 * Let the OS know that we are going to read the whole file. It's just an
+	 * hint, but it helps avoid longer synchronous read stalls.
+	 */
+#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_SEQUENTIAL)
+	(void) posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
+#endif
+
 	_tarWriteHeader(sink, tarfilename, NULL, statbuf, false);
 
 	/*
-- 
2.43.0

