From 2460fbd0a5c6a1be425cdaf92daa2800407e4fcb Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Wed, 16 Jul 2025 15:26:12 +0530 Subject: [PATCH v4 1/3] Add custom PQsetNoticeProcessor handlers for replication connection This patch introduces a custom notice processor for libpq-based connections in replication 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. --- .../libpqwalreceiver/libpqwalreceiver.c | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c index f7b5d093681..689333d7c52 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_processor(void *arg, const char *message); 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 processor so that NOTICEs, WARNINGs, and similar + * messages from the replication connection are reported via ereport(), + * instead of being printed to stderr. + */ + PQsetNoticeProcessor(conn->streamConn, notice_processor, 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,26 @@ libpqrcv_exec(WalReceiverConn *conn, const char *query, return walres; } +/* + * Custom notice processor for replication 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 via replication: %.*s", len, message)); +} + /* * Given a List of strings, return it as single comma separated * string, quoting identifiers as needed. -- 2.43.0