From 576d7ae83aa4700496919303c1b731a4f612ca60 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 v4] 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     |  6 +++---
 src/backend/replication/logical/slotsync.c  |  2 +-
 src/backend/replication/logical/tablesync.c |  3 ++-
 src/backend/replication/logical/worker.c    |  3 ++-
 src/backend/replication/slotfuncs.c         |  2 +-
 src/backend/replication/walreceiver.c       | 10 ++++++----
 src/test/regress/expected/subscription.out  |  2 +-
 7 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 5a47fa984d..f40c16e0d7 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -762,7 +762,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();
 		{
@@ -910,7 +910,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();
 	{
@@ -1537,7 +1537,7 @@ 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 7b180bdb5c..ae544e8e81 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -1259,7 +1259,7 @@ 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("\"%s\" could not connect to the primary server: %s", app_name.data, err));
 
 	/*
 	 * Register the failure callback once we have the connection.
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 1061d5b61b..341e905e72 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 b5a80fe3e8..85609bbd86 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 ad79e1fccd..0b3714b59d 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -993,7 +993,7 @@ 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("\"%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..2c1329b867 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("\"%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 1eee6b17b8..5c9f8e47de 100644
--- a/src/test/regress/expected/subscription.out
+++ b/src/test/regress/expected/subscription.out
@@ -141,7 +141,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

