v20240401-0002-WIP-prefetch-blocks-when-reconstructing-fi.patch

text/x-patch

Filename: v20240401-0002-WIP-prefetch-blocks-when-reconstructing-fi.patch
Type: text/x-patch
Part: 1
Message: Re: pg_combinebackup --copy-file-range

Patch

Format: format-patch
Series: patch v20240401-0002
Subject: WIP: prefetch blocks when reconstructing file
File+
src/bin/pg_combinebackup/reconstruct.c 18 0
From 0f43f3f8a96fc7292f880a9827297f5c8e46eb9a Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Sun, 31 Mar 2024 22:39:19 +0200
Subject: [PATCH v20240401 2/7] WIP: prefetch blocks when reconstructing file

Prefetch up to 128 blocks ahead when reconstructing backups from
multiple files.
---
 src/bin/pg_combinebackup/reconstruct.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/bin/pg_combinebackup/reconstruct.c b/src/bin/pg_combinebackup/reconstruct.c
index e6cb9a206e2..2a8482c5083 100644
--- a/src/bin/pg_combinebackup/reconstruct.c
+++ b/src/bin/pg_combinebackup/reconstruct.c
@@ -539,6 +539,10 @@ write_reconstructed_file(char *input_filename,
 	unsigned	i;
 	unsigned	zero_blocks = 0;
 
+	unsigned	prefetch_index = 0;
+	unsigned	prefetch_distance = 1;
+	unsigned	prefetch_max = 128;
+
 	/* Debugging output. */
 	if (debug)
 	{
@@ -648,6 +652,20 @@ write_reconstructed_file(char *input_filename,
 		{
 			int			rb;
 
+			/* maybe do some prefetching */
+			prefetch_distance = Min(prefetch_distance + 1, prefetch_max);
+			while (prefetch_index < Min(block_length, i + prefetch_distance))
+			{
+				rfile *ps = sourcemap[prefetch_index];
+
+				if (ps != NULL)
+				{
+					 posix_fadvise(ps->fd, offsetmap[prefetch_index], BLCKSZ, POSIX_FADV_WILLNEED);
+				}
+
+				prefetch_index++;
+			}
+
 			/* Read the block from the correct source, except if dry-run. */
 			rb = pg_pread(s->fd, buffer, BLCKSZ, offsetmap[i]);
 			if (rb != BLCKSZ)
-- 
2.44.0