diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index d17d660f460..9064fa19837 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -831,6 +831,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr,
 	XLogRecPtr	read_upto,
 				loc;
 	TimeLineID	tli;
+	TimeLineID	saved_ThisTimeLineID = ThisTimeLineID;
 	int			count;
 	WALReadError errinfo;
 
@@ -915,6 +916,13 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr,
 		}
 	}
 
+	/*
+	 * We want subsequent writes to go to the latest timeline. Thus, resync
+	 * ThisTimeLineID, which may now be pointing to an older timeline, if we are
+	 * currently in recovery.
+	 */
+	ThisTimeLineID = saved_ThisTimeLineID;
+
 	if (targetPagePtr + XLOG_BLCKSZ <= read_upto)
 	{
 		/*
