Skip to content

Commit d48f41e

Browse files
author
Commitfest Bot
committed
[CF 5745] v7 - Log prefix missing for subscriber log messages received from publisher
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5745 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CALDaNm1J4i5Ryg2WksxPpsqr7od9qCqueQh5L0B=YvRXLdf0VQ@mail.gmail.com Author(s): vigneshwaran C
2 parents cb937e4 + e52ecbb commit d48f41e

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static inline void libpqsrv_connect_prepare(void);
5252
static inline void libpqsrv_connect_internal(PGconn *conn, uint32 wait_event_info);
5353
static inline PGresult *libpqsrv_get_result_last(PGconn *conn, uint32 wait_event_info);
5454
static inline PGresult *libpqsrv_get_result(PGconn *conn, uint32 wait_event_info);
55+
static inline void libpqsrv_notice_receiver(void *arg, const PGresult *res);
5556

5657

5758
/*
@@ -454,4 +455,34 @@ exit: ;
454455
return error;
455456
}
456457

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

0 commit comments

Comments
 (0)