From 8a0d5392f482572ebd57a6eae30c715c81b31963 Mon Sep 17 00:00:00 2001 From: Vignesh C 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