Skip to content

Commit 112faf1

Browse files
committed
Log remote NOTICE, WARNING, and similar messages using ereport().
Previously, NOTICE, WARNING, and similar messages received from remote servers over replication, postgres_fdw, or dblink connections were printed directly to stderr on the local server (e.g., the subscriber). As a result, these messages lacked log prefixes (e.g., timestamp), making them harder to trace and correlate with other log entries. This commit addresses the issue by introducing a custom notice receiver for replication, postgres_fdw, and dblink connections. These messages are now logged via ereport(), ensuring they appear in the logs with proper formatting and context, which improves clarity and aids in debugging. Author: Vignesh C <vignesh21@gmail.com> Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/CALDaNm2xsHpWRtLm-VL_HJCsaE3+1Y_n-jDEAr3-suxVqc3xoQ@mail.gmail.com
1 parent 1b8bbee commit 112faf1

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

contrib/dblink/dblink.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ dblink_get_conn(char *conname_or_str,
240240
errmsg("could not establish connection"),
241241
errdetail_internal("%s", msg)));
242242
}
243+
244+
PQsetNoticeReceiver(conn, libpqsrv_notice_receiver,
245+
gettext_noop("received message via remote connection"));
246+
243247
dblink_security_check(conn, NULL, connstr);
244248
if (PQclientEncoding(conn) != GetDatabaseEncoding())
245249
PQsetClientEncoding(conn, GetDatabaseEncodingName());
@@ -338,6 +342,9 @@ dblink_connect(PG_FUNCTION_ARGS)
338342
errdetail_internal("%s", msg)));
339343
}
340344

345+
PQsetNoticeReceiver(conn, libpqsrv_notice_receiver,
346+
gettext_noop("received message via remote connection"));
347+
341348
/* check password actually used if not superuser */
342349
dblink_security_check(conn, connname, connstr);
343350

contrib/postgres_fdw/connection.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
625625
server->servername),
626626
errdetail_internal("%s", pchomp(PQerrorMessage(conn)))));
627627

628+
PQsetNoticeReceiver(conn, libpqsrv_notice_receiver,
629+
gettext_noop("received message via remote connection"));
630+
628631
/* Perform post-connection security checks. */
629632
pgfdw_security_check(keywords, values, user, conn);
630633

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
232232
errhint("Target server's authentication method must be changed, or set password_required=false in the subscription parameters.")));
233233
}
234234

235+
PQsetNoticeReceiver(conn->streamConn, libpqsrv_notice_receiver,
236+
gettext_noop("received message via replication"));
237+
235238
/*
236239
* Set always-secure search path for the cases where the connection is
237240
* used to run SQL queries, so malicious users can't get control.

src/include/libpq/libpq-be-fe-helpers.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,34 @@ exit: ;
454454
return error;
455455
}
456456

457+
/*
458+
* libpqsrv_notice_receiver
459+
*
460+
* Custom notice receiver for libpq connections.
461+
*
462+
* This function is intended to be set via PQsetNoticeReceiver() so that
463+
* NOTICE, WARNING, and similar messages from the connection are reported via
464+
* ereport(), instead of being printed to stderr.
465+
*/
466+
static inline void
467+
libpqsrv_notice_receiver(void *arg, const PGresult *res)
468+
{
469+
char *message;
470+
int len;
471+
char *prefix = (char *) arg;
472+
473+
/*
474+
* Trim the trailing newline from the message text returned from
475+
* PQresultErrorMessage(), as it always includes one, to produce cleaner
476+
* log output.
477+
*/
478+
message = PQresultErrorMessage(res);
479+
len = strlen(message);
480+
if (len > 0 && message[len - 1] == '\n')
481+
len--;
482+
483+
ereport(LOG,
484+
errmsg_internal("%s: %.*s", _(prefix), len, message));
485+
}
486+
457487
#endif /* LIBPQ_BE_FE_HELPERS_H */

0 commit comments

Comments
 (0)