From fb26a48eb81d28c34b10e8d116b70e503635d07f Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Thu, 11 Jul 2024 12:07:40 -0400 Subject: [PATCH] Add local address to log_line_prefix --- doc/src/sgml/config.sgml | 5 ++++ src/backend/utils/error/elog.c | 25 +++++++++++++++++++ src/backend/utils/misc/postgresql.conf.sample | 1 + 3 files changed, 31 insertions(+) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index b14c5d81a1..905ce0615a 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -7489,6 +7489,11 @@ local0.* /var/log/postgresql Remote host name or IP address yes + + %L + Local address + yes + %b Backend type diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 3e42f5754f..09b9003083 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -67,6 +67,7 @@ #endif #include "access/xact.h" +#include "common/ip.h" #include "libpq/libpq.h" #include "libpq/pqformat.h" #include "mb/pg_wchar.h" @@ -79,6 +80,7 @@ #include "storage/ipc.h" #include "storage/proc.h" #include "tcop/tcopprot.h" +#include "utils/builtins.h" #include "utils/guc_hooks.h" #include "utils/memutils.h" #include "utils/ps_status.h" @@ -3037,6 +3039,29 @@ log_status_format(StringInfo buf, const char *format, ErrorData *edata) appendStringInfoSpaces(buf, padding > 0 ? padding : -padding); break; + case 'L': + if (MyProcPort + && (MyProcPort->laddr.addr.ss_family == AF_INET + || + MyProcPort->laddr.addr.ss_family == AF_INET6) + ) + { + Port *port = MyProcPort; + char local_host[NI_MAXHOST]; + + local_host[0] = '\0'; + + if (0 == pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen, + local_host, sizeof(local_host), + NULL, 0, + NI_NUMERICHOST | NI_NUMERICSERV) + ) + appendStringInfo(buf, "%s", local_host); + } + else + appendStringInfo(buf, "[local]"); + + break; case 'r': if (MyProcPort && MyProcPort->remote_host) { diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 9ec9f97e92..923beae8fc 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -587,6 +587,7 @@ # %d = database name # %r = remote host and port # %h = remote host + # %L = local address # %b = backend type # %p = process ID # %P = process ID of parallel group leader -- 2.30.2