From ff35ea06bc8d10833e26e72daffdc566de9b2060 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Thu, 26 Nov 2026 17:19:43 +0300
Subject: [PATCH v1 2/7] Share the code that sends CopyBothResponse

Physical and logical replication both send the same CopyBothResponse
when they start streaming. Use one function to build and send that
message. The new feedback command will use it too.
---
 src/backend/replication/walsender.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 4822619d925..33c914b606e 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -855,6 +855,19 @@ HandleUploadManifestPacket(StringInfo buf, off_t *offset,
 	return false;
 }
 
+/* Enter bidirectional COPY mode with no column metadata. */
+static void
+SendCopyBothResponse(void)
+{
+	StringInfoData buf;
+
+	pq_beginmessage(&buf, PqMsg_CopyBothResponse);
+	pq_sendbyte(&buf, 0);
+	pq_sendint16(&buf, 0);
+	pq_endmessage(&buf);
+	pq_flush();
+}
+
 /*
  * Handle START_REPLICATION command.
  *
@@ -864,7 +877,6 @@ HandleUploadManifestPacket(StringInfo buf, off_t *offset,
 static void
 StartReplication(StartReplicationCmd *cmd)
 {
-	StringInfoData buf;
 	XLogRecPtr	FlushPtr;
 	TimeLineID	FlushTLI;
 
@@ -995,12 +1007,7 @@ StartReplication(StartReplicationCmd *cmd)
 		 */
 		WalSndSetState(WALSNDSTATE_CATCHUP);
 
-		/* Send a CopyBothResponse message, and start streaming */
-		pq_beginmessage(&buf, PqMsg_CopyBothResponse);
-		pq_sendbyte(&buf, 0);
-		pq_sendint16(&buf, 0);
-		pq_endmessage(&buf);
-		pq_flush();
+		SendCopyBothResponse();
 
 		/*
 		 * Don't allow a request to stream from a future point in WAL that
@@ -1539,7 +1546,6 @@ AlterReplicationSlot(AlterReplicationSlotCmd *cmd)
 static void
 StartLogicalReplication(StartReplicationCmd *cmd)
 {
-	StringInfoData buf;
 	QueryCompletion qc;
 
 	/* make sure that our requirements are still fulfilled */
@@ -1579,12 +1585,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
 
 	WalSndSetState(WALSNDSTATE_CATCHUP);
 
-	/* Send a CopyBothResponse message, and start streaming */
-	pq_beginmessage(&buf, PqMsg_CopyBothResponse);
-	pq_sendbyte(&buf, 0);
-	pq_sendint16(&buf, 0);
-	pq_endmessage(&buf);
-	pq_flush();
+	SendCopyBothResponse();
 
 	/* Start reading WAL from the oldest required WAL. */
 	XLogBeginRead(logical_decoding_ctx->reader,
-- 
2.47.3

