From 70ff20ba997b3e2ee33036dd86fef2f06a72c0e3 Mon Sep 17 00:00:00 2001
From: Ben Wheatley <contact@benwh.com>
Date: Tue, 17 Sep 2019 17:30:12 +0100
Subject: [PATCH] Fix pg_rewind when divergence is at end of WAL

---
 src/bin/pg_rewind/parsexlog.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c
index 8641dce306..831398310d 100644
--- a/src/bin/pg_rewind/parsexlog.c
+++ b/src/bin/pg_rewind/parsexlog.c
@@ -195,12 +195,18 @@ findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
 		if (record == NULL)
 		{
 			if (errormsg)
-				pg_fatal("could not find previous WAL record at %X/%X: %s\n",
+				pg_log(PG_WARNING, "could not find previous WAL record at %X/%X: %s\n",
 						 (uint32) (searchptr >> 32), (uint32) (searchptr),
 						 errormsg);
 			else
-				pg_fatal("could not find previous WAL record at %X/%X\n",
+				pg_log(PG_WARNING, "could not find previous WAL record at %X/%X\n",
 						 (uint32) (searchptr >> 32), (uint32) (searchptr));
+
+			/* Skip backwards 8 bytes, as XLogRecords will always
+			 * be aligned on an 8-byte boundary */
+			searchptr -= 8;
+
+			continue;
 		}
 
 		/*
-- 
2.22.0

