From 880d19658fdc3f9d4e31b1e6f1be670e1cb3dd46 Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Wed, 16 Jul 2025 15:32:41 +0530 Subject: [PATCH v4 3/3] Add custom PQsetNoticeProcessor handlers for fdw connection This patch introduces a custom notice processor for libpq-based connections in fdw 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/postgres_fdw/connection.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 304f3c20f83..53264ecbf93 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -472,6 +472,26 @@ 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 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)); +} + /* * Connect to remote server using specified server and user mapping properties. */ @@ -625,6 +645,13 @@ connect_pg_server(ForeignServer *server, UserMapping *user) server->servername), errdetail_internal("%s", pchomp(PQerrorMessage(conn))))); + /* + * 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); + /* Perform post-connection security checks. */ pgfdw_security_check(keywords, values, user, conn); -- 2.43.0