On Thu, 17 Jul 2025 at 11:18, Álvaro Herrera <alvhe...@kurilemu.de> wrote:
>
> Hi,
>
> Shouldn't we be using a notice receiver rather than a notice processor?

I saw the following comment in code regarding PQsetNoticeProcessor
should be deprecated:
/*
 * The default notice message receiver just gets the standard notice text
 * and sends it to the notice processor.  This two-level setup exists
 * mostly for backwards compatibility; perhaps we should deprecate use of
 * PQsetNoticeProcessor?
 */

So I changed it to PQsetNoticeReceiver. The attached v5 version patch
has the changes for the same.

Regards,
Vignesh
From 8a0d5392f482572ebd57a6eae30c715c81b31963 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignes...@gmail.com>
Date: Wed, 16 Jul 2025 15:26:12 +0530
Subject: [PATCH v5 1/3] Add custom PQsetNoticeReceiver handlers for
 replication connection

This patch introduces a custom notice receiver for replication
connections. The notice receiver captures messages and routes
them through ereport(), making them visible in local logs with
a prefix making it easy for diagnosis.
---
 .../libpqwalreceiver/libpqwalreceiver.c       | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index f7b5d093681..7d1031139ac 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -114,6 +114,7 @@ static WalReceiverFunctionsType PQWalReceiverFunctions = {
 };
 
 /* Prototypes for private functions */
+static void notice_receiver(void *arg, const PGresult *result);
 static char *stringlist_to_identifierstr(PGconn *conn, List *strings);
 
 /*
@@ -232,6 +233,13 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
 				 errhint("Target server's authentication method must be changed, or set password_required=false in the subscription parameters.")));
 	}
 
+	/*
+	 * Set a custom notice receiver so that NOTICEs, WARNINGs, and similar
+	 * messages from the replication connection are reported via ereport(),
+	 * instead of being printed to stderr.
+	 */
+	PQsetNoticeReceiver(conn->streamConn, notice_receiver, NULL);
+
 	/*
 	 * Set always-secure search path for the cases where the connection is
 	 * used to run SQL queries, so malicious users can't get control.
@@ -1195,6 +1203,28 @@ libpqrcv_exec(WalReceiverConn *conn, const char *query,
 	return walres;
 }
 
+/*
+ * Custom notice receiver for replication connection.
+ */
+static void
+notice_receiver(void *arg, const PGresult *result)
+{
+	char	   *message;
+	int			len;
+
+	/*
+	 * Trim the trailing newline from the message text passed to the notice
+	 * receiver, as it always includes one, to produce cleaner log output.
+	 */
+	message = PQresultErrorMessage(result);
+	len = strlen(message);
+	if (len > 0 && message[len - 1] == '\n')
+		len--;
+
+	ereport(LOG,
+			errmsg("received message via replication: %.*s", len, message));
+}
+
 /*
  * Given a List of strings, return it as single comma separated
  * string, quoting identifiers as needed.
-- 
2.43.0

From c4f76b90d6be701e50bd3fff271f77565cd8d212 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignes...@gmail.com>
Date: Wed, 16 Jul 2025 15:32:41 +0530
Subject: [PATCH v5 3/3] Add custom PQsetNoticeReceiver handlers for remote
 server connection in fdw

This patch introduces a custom notice receiver for remote
server connection in fdw.  The notice receiver captures
messages and routes them through ereport(), making them visible
in local logs with a prefix making it easy for diagnosis.
---
 contrib/postgres_fdw/connection.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index 304f3c20f83..babd78914fb 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -472,6 +472,28 @@ pgfdw_security_check(const char **keywords, const char **values, UserMapping *us
 			 errhint("Target server's authentication method must be changed or password_required=false set in the user mapping attributes.")));
 }
 
+/*
+ * Custom notice receiver for remote server connection.
+ */
+static void
+notice_receiver(void *arg, const PGresult *result)
+{
+	char	   *message;
+	int			len;
+
+	/*
+	 * Trim the trailing newline from the message text passed to the notice
+	 * receiver, as it always includes one, to produce cleaner log output.
+	 */
+	message = PQresultErrorMessage(result);
+	len = strlen(message);
+	if (len > 0 && message[len - 1] == '\n')
+		len--;
+
+	ereport(LOG,
+			errmsg("received message from remote server: %.*s", len, message));
+}
+
 /*
  * Connect to remote server using specified server and user mapping properties.
  */
@@ -625,6 +647,13 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
 							server->servername),
 					 errdetail_internal("%s", pchomp(PQerrorMessage(conn)))));
 
+		/*
+		 * Set a custom notice receiver so that NOTICEs, WARNINGs, and similar
+		 * messages from the remote server connection are reported via
+		 * ereport(), instead of being printed to stderr.
+		 */
+		PQsetNoticeReceiver(conn, notice_receiver, NULL);
+
 		/* Perform post-connection security checks. */
 		pgfdw_security_check(keywords, values, user, conn);
 
-- 
2.43.0

From 815d21cc9ba385376622a9cbab7a9807c5e9c726 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignes...@gmail.com>
Date: Wed, 16 Jul 2025 15:31:54 +0530
Subject: [PATCH v5 2/3] Add custom PQsetNoticeReceiver handlers for remote
 server connection in dblink

This patch introduces a custom notice receiver for remote
server connection in dblink.  The notice receiver 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 | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 8a0b112a7ff..a2d1e407137 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_receiver(void *arg, const PGresult *result);
 
 /* 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 receiver so that NOTICEs, WARNINGs, and similar
+		 * messages from the remote server connection are reported via
+		 * ereport(), instead of being printed to stderr.
+		 */
+		PQsetNoticeReceiver(conn, notice_receiver, NULL);
+
 		dblink_security_check(conn, NULL, connstr);
 		if (PQclientEncoding(conn) != GetDatabaseEncoding())
 			PQsetClientEncoding(conn, GetDatabaseEncodingName());
@@ -338,6 +347,13 @@ dblink_connect(PG_FUNCTION_ARGS)
 				 errdetail_internal("%s", msg)));
 	}
 
+	/*
+	 * Set a custom notice receiver so that NOTICEs, WARNINGs, and similar
+	 * messages from the remote server connection are reported via ereport(),
+	 * instead of being printed to stderr.
+	 */
+	PQsetNoticeReceiver(conn, notice_receiver, NULL);
+
 	/* check password actually used if not superuser */
 	dblink_security_check(conn, connname, connstr);
 
@@ -2670,6 +2686,28 @@ dblink_connstr_has_required_scram_options(const char *connstr)
 	return (has_scram_keys && has_require_auth);
 }
 
+/*
+ * Custom notice receiver for remote server connection.
+ */
+static void
+notice_receiver(void *arg, const PGresult *result)
+{
+	char	   *message;
+	int			len;
+
+	/*
+	 * Trim the trailing newline from the message text passed to the notice
+	 * receiver, as it always includes one, to produce cleaner log output.
+	 */
+	message = PQresultErrorMessage(result);
+	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

Reply via email to