From 26ea49eac8f4f16b896928dfb5be96504c057649 Mon Sep 17 00:00:00 2001
From: shipixian <shipixian_yewu@cmss.chinamobile.com>
Date: Mon, 19 Aug 2024 12:08:08 +0800
Subject: [PATCH] Fix walreceiver set incorrect flushedUpto when switching
 primary

Signed-off-by: shipixian <shipixian_yewu@cmss.chinamobile.com>
---
 src/backend/replication/walreceiverfuncs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c
index 90798b9d53..bc2ae2ae61 100644
--- a/src/backend/replication/walreceiverfuncs.c
+++ b/src/backend/replication/walreceiverfuncs.c
@@ -298,10 +298,10 @@ RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo,
 	walrcv->startTime = now;
 
 	/*
-	 * If this is the first startup of walreceiver (on this timeline),
+	 * If this is the first startup of walreceiver (on this timeline) or recptr lt last flushedLsn,
 	 * initialize flushedUpto and latestChunkStart to the starting point.
 	 */
-	if (walrcv->receiveStart == 0 || walrcv->receivedTLI != tli)
+	if (walrcv->receiveStart == 0 || walrcv->receivedTLI != tli || recptr < walrcv->flushedUpto)
 	{
 		walrcv->flushedUpto = recptr;
 		walrcv->receivedTLI = tli;
-- 
2.39.3 (Apple Git-145)

