From 9a0c0ce671d3860f1611c4ca1ec7cf55ab43e6e4 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Wed, 16 Jul 2025 15:31:54 +0530
Subject: [PATCH v4 2/3] Add custom PQsetNoticeProcessor handlers for dblink
 connection

This patch introduces a custom notice processor for libpq-based
connections in dblink connection.  The notice processor captures
messages and routes them through ereport(), making them visible
in local logs with a prefix making it easy for diagnosis.
---
 contrib/dblink/dblink.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 8a0b112a7ff..30509f9ff32 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -137,6 +137,7 @@ static void appendSCRAMKeysInfo(StringInfo buf);
 static bool is_valid_dblink_fdw_option(const PQconninfoOption *options, const char *option,
 									   Oid context);
 static bool dblink_connstr_has_required_scram_options(const char *connstr);
+static void notice_processor(void *arg, const char *message);
 
 /* Global */
 static remoteConn *pconn = NULL;
@@ -240,6 +241,14 @@ dblink_get_conn(char *conname_or_str,
 					 errmsg("could not establish connection"),
 					 errdetail_internal("%s", msg)));
 		}
+
+		/*
+		 * Set a custom notice processor so that NOTICEs, WARNINGs, and
+		 * similar messages from the remote server connection are reported via
+		 * ereport(), instead of being printed to stderr.
+		 */
+		PQsetNoticeProcessor(conn, notice_processor, NULL);
+
 		dblink_security_check(conn, NULL, connstr);
 		if (PQclientEncoding(conn) != GetDatabaseEncoding())
 			PQsetClientEncoding(conn, GetDatabaseEncodingName());
@@ -338,6 +347,8 @@ dblink_connect(PG_FUNCTION_ARGS)
 				 errdetail_internal("%s", msg)));
 	}
 
+	PQsetNoticeProcessor(conn, notice_processor, NULL);
+
 	/* check password actually used if not superuser */
 	dblink_security_check(conn, connname, connstr);
 
@@ -2670,6 +2681,26 @@ dblink_connstr_has_required_scram_options(const char *connstr)
 	return (has_scram_keys && has_require_auth);
 }
 
+/*
+ * Custom notice processor for remote server connection.
+ */
+static void
+notice_processor(void *arg, const char *message)
+{
+	int			len;
+
+	/*
+	 * Trim the trailing newline from the message text passed to the notice
+	 * processor, as it always includes one, to produce cleaner log output.
+	 */
+	len = strlen(message);
+	if (len > 0 && message[len - 1] == '\n')
+		len--;
+
+	ereport(LOG,
+			errmsg("received message from remote server: %.*s", len, message));
+}
+
 /*
  * We need to make sure that the connection made used credentials
  * which were provided by the user, so check what credentials were
-- 
2.43.0

