From 8575bbd0b0a27ca999d41c771af72f2e738bdb6f Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Thu, 19 Nov 2026 16:35:38 +0300
Subject: [PATCH v1 3/7] Pass the replication mode to WalSndLoop()

A physical walsender waits for more WAL in WalSndLoop() when it has
caught up. A logical walsender does that in WalSndWaitForWal(). The
loop currently tells the two apart by comparing the send callback to
XLogSendLogical().

Pass the mode explicitly instead. This also lets us add a feedback
mode that uses the same loop but has no WAL to send.
---
 src/backend/replication/walsender.c | 47 +++++++++++++++++++----------
 src/tools/pgindent/typedefs.list    |  2 +-
 2 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 33c914b606e..74f6e8613b8 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -279,19 +279,25 @@ typedef struct
 
 static LagTracker *lag_tracker;
 
+/* What this COPY session does */
+typedef enum
+{
+	WALSND_LOOP_PHYSICAL,
+	WALSND_LOOP_LOGICAL,
+} WalSndLoopMode;
+
 /* Signal handlers */
 static void WalSndLastCycleHandler(SIGNAL_ARGS);
 
 /* Prototypes for private functions */
-typedef void (*WalSndSendDataCallback) (void);
-static void WalSndLoop(WalSndSendDataCallback send_data);
+static void WalSndLoop(WalSndLoopMode mode);
 static void InitWalSenderSlot(void);
 static void WalSndKill(int code, Datum arg);
 pg_noreturn static void WalSndShutdown(void);
 static void XLogSendPhysical(void);
 static void XLogSendLogical(void);
 pg_noreturn static void WalSndDoneImmediate(void);
-static void WalSndDone(WalSndSendDataCallback send_data);
+static void WalSndDone(WalSndLoopMode mode);
 static void IdentifySystem(void);
 static void UploadManifest(void);
 static bool HandleUploadManifestPacket(StringInfo buf, off_t *offset,
@@ -1034,7 +1040,7 @@ StartReplication(StartReplicationCmd *cmd)
 		/* Main loop of walsender */
 		replication_active = true;
 
-		WalSndLoop(XLogSendPhysical);
+		WalSndLoop(WALSND_LOOP_PHYSICAL);
 
 		replication_active = false;
 		if (got_STOPPING)
@@ -1607,7 +1613,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
 	SyncRepInitConfig();
 
 	/* Main loop of walsender */
-	WalSndLoop(XLogSendLogical);
+	WalSndLoop(WALSND_LOOP_LOGICAL);
 
 	FreeDecodingContext(logical_decoding_ctx);
 	logical_decoding_ctx = NULL;
@@ -3079,9 +3085,20 @@ WalSndCheckShutdownTimeout(void)
 		WalSndDoneImmediate();
 }
 
+/* Try to send more WAL in a streaming session. */
+static inline void
+WalSndSendData(WalSndLoopMode mode)
+{
+	Assert(mode == WALSND_LOOP_PHYSICAL || mode == WALSND_LOOP_LOGICAL);
+	if (mode == WALSND_LOOP_PHYSICAL)
+		XLogSendPhysical();
+	else
+		XLogSendLogical();
+}
+
 /* Main loop of walsender process that streams the WAL over Copy messages. */
 static void
-WalSndLoop(WalSndSendDataCallback send_data)
+WalSndLoop(WalSndLoopMode mode)
 {
 	TimestampTz last_flush = 0;
 
@@ -3119,13 +3136,11 @@ WalSndLoop(WalSndSendDataCallback send_data)
 			break;
 
 		/*
-		 * If we don't have any pending data in the output buffer, try to send
-		 * some more.  If there is some, we don't bother to call send_data
-		 * again until we've flushed it ... but we'd better assume we are not
-		 * caught up.
+		 * Call WalSndSendData() only when the output buffer is empty.  While
+		 * output is pending, assume we are not caught up.
 		 */
 		if (!pq_is_send_pending())
-			send_data();
+			WalSndSendData(mode);
 		else
 			WalSndCaughtUp = false;
 
@@ -3160,7 +3175,7 @@ WalSndLoop(WalSndSendDataCallback send_data)
 			 * is not sure which.
 			 */
 			if (got_SIGUSR2)
-				WalSndDone(send_data);
+				WalSndDone(mode);
 		}
 
 		/* Check for replication timeout. */
@@ -3180,12 +3195,12 @@ WalSndLoop(WalSndSendDataCallback send_data)
 		 * Block if we have unsent data.  XXX For logical replication, let
 		 * WalSndWaitForWal() handle any other blocking; idle receivers need
 		 * its additional actions.  For physical replication, also block if
-		 * caught up; its send_data does not block.
+		 * caught up, since XLogSendPhysical() does not block.
 		 *
 		 * The IO statistics are reported in WalSndWaitForWal() for the
 		 * logical WAL senders.
 		 */
-		if ((WalSndCaughtUp && send_data != XLogSendLogical &&
+		if ((WalSndCaughtUp && mode == WALSND_LOOP_PHYSICAL &&
 			 !streamingDoneSending) ||
 			pq_is_send_pending())
 		{
@@ -3847,12 +3862,12 @@ WalSndDoneImmediate(void)
  * function will return control to the caller.
  */
 static void
-WalSndDone(WalSndSendDataCallback send_data)
+WalSndDone(WalSndLoopMode mode)
 {
 	XLogRecPtr	replicatedPtr;
 
 	/* ... let's just be real sure we're caught up ... */
-	send_data();
+	WalSndSendData(mode);
 
 	/*
 	 * To figure out whether all WAL has successfully been replicated, check
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index f10eb469266..3d332ff45b9 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3433,7 +3433,7 @@ WalReceiverConn
 WalReceiverFunctionsType
 WalSnd
 WalSndCtlData
-WalSndSendDataCallback
+WalSndLoopMode
 WalSndState
 WalSummarizerData
 WalSummaryFile
-- 
2.47.3

