From 3a0f498c2b21146352fb21897b250ded0861a13f Mon Sep 17 00:00:00 2001
From: Greg Sabino Mullane
Date: Wed, 26 Mar 2025 12:56:49 -0400
Subject: [PATCH] Add local address to log_line_prefix with %L
This shows the server IP that a client has connected to.
Socket connections will show "[local]"
Non-client connections (e.g. background processes) will show "[none]"
---
doc/src/sgml/config.sgml | 5 +++++
src/backend/utils/error/elog.c | 19 +++++++++++++++++++
src/backend/utils/misc/postgresql.conf.sample | 1 +
3 files changed, 25 insertions(+)
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 69fc93dffc4..646b5e29965 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7674,6 +7674,11 @@ local0.* /var/log/postgresql
Remote host name or IP address
yes
+
+ %L
+ Local address (the IP address on the server that the client connected to)
+ yes
+
%b
Backend type
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 860bbd40d42..16367208552 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"
@@ -3059,6 +3061,23 @@ log_status_format(StringInfo buf, const char *format, ErrorData *edata)
appendStringInfoSpaces(buf,
padding > 0 ? padding : -padding);
break;
+ case 'L':
+ {
+ char local_host[NI_MAXHOST];
+
+ strlcpy(local_host, "[none]", sizeof(local_host));
+
+ if (MyProcPort)
+ pg_getnameinfo_all(&MyProcPort->laddr.addr, MyProcPort->laddr.salen,
+ local_host, sizeof(local_host),
+ NULL, 0,
+ NI_NUMERICHOST | NI_NUMERICSERV);
+ if (padding != 0)
+ appendStringInfo(buf, "%*s", padding, local_host);
+ else
+ appendStringInfoString(buf, local_host);
+ }
+ 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 0b9e3066bde..5e2b64cf774 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -602,6 +602,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