From ba3e38495b038bdfc176e25d3e1b721f09d0053b Mon Sep 17 00:00:00 2001
From: Nisha Moond <nisha.moond412@gmail.com>
Date: Wed, 10 Jan 2024 12:04:18 +0530
Subject: [PATCH v2] 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     | 4 ++--
 src/backend/replication/logical/tablesync.c | 3 ++-
 src/backend/replication/logical/worker.c    | 3 ++-
 src/backend/replication/walreceiver.c       | 9 +++++----
 src/test/regress/expected/subscription.out  | 2 +-
 5 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 75e6cd8ae3..3477769a74 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -742,7 +742,7 @@ 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();
 		{
@@ -875,7 +875,7 @@ 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();
 	{
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 06d5b3df33..1e4b878bf4 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1335,7 +1335,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 911835c5cb..5553a6becd 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -4518,7 +4518,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/walreceiver.c b/src/backend/replication/walreceiver.c
index e00395ff2b..9d744dd5d5 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -198,6 +198,7 @@ WalReceiverMain(void)
 	char	   *err;
 	char	   *sender_host = NULL;
 	int			sender_port = 0;
+	char	   *appname = NULL;
 
 	/*
 	 * WalRcv should be set up already (if we are a backend, we inherit this
@@ -295,14 +296,14 @@ WalReceiverMain(void)
 	/* 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, false, false,
-							cluster_name[0] ? cluster_name : "walreceiver",
-							&err);
+	wrconn = walrcv_connect(conninfo, false, false, appname, &err);
 	if (!wrconn)
 		ereport(ERROR,
 				(errcode(ERRCODE_CONNECTION_FAILURE),
-				 errmsg("could not connect to the primary server: %s", err)));
+				 errmsg("\"%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 b15eddbff3..3eb9e287d0 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

