From 3f260ed2e617c3f27f5a2176e8a172c79186c6de Mon Sep 17 00:00:00 2001
From: Nisha Moond <nisha.moond412@gmail.com>
Date: Fri, 31 May 2024 17:52:04 +0530
Subject: [PATCH v6] Improve the connection failure error messages

Enhances the error messages for walrcv_connect() connection failures,
offering more details on the processes that failed to establish a connection.
---
 src/backend/commands/subscriptioncmds.c     |  9 ++++++---
 src/backend/replication/logical/slotsync.c  |  3 ++-
 src/backend/replication/logical/tablesync.c |  3 ++-
 src/backend/replication/logical/worker.c    |  3 ++-
 src/backend/replication/slotfuncs.c         |  3 ++-
 src/backend/replication/walreceiver.c       | 10 ++++++----
 src/test/regress/expected/subscription.out  |  2 +-
 7 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index e407428dbc..16d83b3253 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -755,7 +755,8 @@ CreateSubscription(ParseState *pstate, CreateSubscriptionStmt *stmt,
 		if (!wrconn)
 			ereport(ERROR,
 					(errcode(ERRCODE_CONNECTION_FAILURE),
-					 errmsg("could not connect to the publisher: %s", err)));
+					 errmsg("subscription \"%s\" could not connect to the publisher: %s",
+							stmt->subname, err)));
 
 		PG_TRY();
 		{
@@ -888,7 +889,8 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data,
 	if (!wrconn)
 		ereport(ERROR,
 				(errcode(ERRCODE_CONNECTION_FAILURE),
-				 errmsg("could not connect to the publisher: %s", err)));
+				 errmsg("subscription \"%s\" could not connect to the publisher: %s",
+						sub->name, err)));
 
 	PG_TRY();
 	{
@@ -1521,7 +1523,8 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt,
 		if (!wrconn)
 			ereport(ERROR,
 					(errcode(ERRCODE_CONNECTION_FAILURE),
-					 errmsg("could not connect to the publisher: %s", err)));
+					 errmsg("subscription \"%s\" could not connect to the publisher: %s",
+							sub->name, err)));
 
 		PG_TRY();
 		{
diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index fe2f07cf44..2d1914ce08 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -1463,7 +1463,8 @@ ReplSlotSyncWorkerMain(char *startup_data, size_t startup_data_len)
 	if (!wrconn)
 		ereport(ERROR,
 				errcode(ERRCODE_CONNECTION_FAILURE),
-				errmsg("could not connect to the primary server: %s", err));
+				errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
+					   app_name.data, err));
 
 	/*
 	 * Register the disconnection callback.
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index b00267f042..7bd0f9d7e0 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1342,7 +1342,8 @@ LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
 	if (LogRepWorkerWalRcvConn == NULL)
 		ereport(ERROR,
 				(errcode(ERRCODE_CONNECTION_FAILURE),
-				 errmsg("could not connect to the publisher: %s", err)));
+				 errmsg("logical replication table synchronization worker for subscription \"%s\" could not connect to the publisher: %s",
+						MySubscription->name, err)));
 
 	Assert(MyLogicalRepWorker->relstate == SUBREL_STATE_INIT ||
 		   MyLogicalRepWorker->relstate == SUBREL_STATE_DATASYNC ||
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 3b285894db..4d2f3d63d1 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -4500,7 +4500,8 @@ run_apply_worker()
 	if (LogRepWorkerWalRcvConn == NULL)
 		ereport(ERROR,
 				(errcode(ERRCODE_CONNECTION_FAILURE),
-				 errmsg("could not connect to the publisher: %s", err)));
+				 errmsg("logical replication apply worker for subscription \"%s\" could not connect to the publisher: %s",
+						MySubscription->name, err)));
 
 	/*
 	 * We don't really use the output identify_system for anything but it does
diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c
index 38595b3a47..c7bfbb15e0 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -898,7 +898,8 @@ pg_sync_replication_slots(PG_FUNCTION_ARGS)
 	if (!wrconn)
 		ereport(ERROR,
 				errcode(ERRCODE_CONNECTION_FAILURE),
-				errmsg("could not connect to the primary server: %s", err));
+				errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
+					   app_name.data, err));
 
 	SyncReplicationSlots(wrconn);
 
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index acda5f68d9..7506774f75 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -195,6 +195,7 @@ WalReceiverMain(char *startup_data, size_t startup_data_len)
 	char	   *err;
 	char	   *sender_host = NULL;
 	int			sender_port = 0;
+	char	   *appname = NULL;
 
 	Assert(startup_data_len == 0);
 
@@ -297,14 +298,15 @@ WalReceiverMain(char *startup_data, size_t startup_data_len)
 	/* Unblock signals (they were blocked when the postmaster forked us) */
 	sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
 
+	appname = cluster_name[0] ? cluster_name : "walreceiver";
+
 	/* Establish the connection to the primary for XLOG streaming */
-	wrconn = walrcv_connect(conninfo, true, false, false,
-							cluster_name[0] ? cluster_name : "walreceiver",
-							&err);
+	wrconn = walrcv_connect(conninfo, true, false, false, appname, &err);
+
 	if (!wrconn)
 		ereport(ERROR,
 				(errcode(ERRCODE_CONNECTION_FAILURE),
-				 errmsg("could not connect to the primary server: %s", err)));
+				 errmsg("streaming replication \"%s\" could not connect to the primary server: %s", appname, err)));
 
 	/*
 	 * Save user-visible connection string.  This clobbers the original
diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out
index 0f2a25cdc1..5c2f1ee517 100644
--- a/src/test/regress/expected/subscription.out
+++ b/src/test/regress/expected/subscription.out
@@ -139,7 +139,7 @@ ERROR:  invalid connection string syntax: invalid connection option "i_dont_exis
 -- fail, connection string parses, but doesn't work (and does so without
 -- connecting, so this is reliable and safe)
 CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'port=-1' PUBLICATION testpub;
-ERROR:  could not connect to the publisher: invalid port number: "-1"
+ERROR:  subscription "regress_testsub5" could not connect to the publisher: invalid port number: "-1"
 -- fail - invalid connection string during ALTER
 ALTER SUBSCRIPTION regress_testsub CONNECTION 'foobar';
 ERROR:  invalid connection string syntax: missing "=" after "foobar" in connection info string
-- 
2.34.1

