From 812ced024a7edea7dda1634fe926936c20a22f93 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Thu, 5 Apr 2018 10:31:53 +0900
Subject: [PATCH 2/2] Enforce ordering of data chunks fetched in pg_rewind for
 libpq transfer

This is good for performance, as this gives a sequential behavior to the
rewind operation, and also limits the number of files opened and closed
when applying data chunks.

Author: Christian H.
Author: Michael Paquier

Discussion: https://postgr.es/m/20180104200633.17004.16377%40wrigleys.postgresql.org
---
 src/bin/pg_rewind/libpq_fetch.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_rewind/libpq_fetch.c b/src/bin/pg_rewind/libpq_fetch.c
index 5914b15017..880c6553ea 100644
--- a/src/bin/pg_rewind/libpq_fetch.c
+++ b/src/bin/pg_rewind/libpq_fetch.c
@@ -501,12 +501,18 @@ libpq_executeFileMap(filemap_t *map)
 
 	/*
 	 * We've now copied the list of file ranges that we need to fetch to the
-	 * temporary table. Now, actually fetch all of those ranges.
+	 * temporary table. Now, actually fetch all of those ranges.  The elements
+	 * fetched are ordered to ensure that the set of chunks applying to one
+	 * are processed successively.  This gives a sequential behavior to the
+	 * rewind, which is good for performance especially on large files, and
+	 * limites the back-and-forth movements to open and close multiple times
+	 * the same file.
 	 */
 	sql =
 		"SELECT path, begin,\n"
 		"  pg_read_binary_file(path, begin, len, true) AS chunk\n"
-		"FROM fetchchunks\n";
+		"FROM fetchchunks\n"
+		"ORDER BY path, begin\n";
 
 	receiveFileChunks(sql);
 }
-- 
2.16.3

