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