Peter, thank you for the feedback. Attached is a new patch with "address"
rather than "interface", plus a new default of "local" if there is no
address. I also removed the questionable comment, and updated the
commitfest title.

Cheers,
Greg
From bfa69fc2fffcb29dee0c6acfa4fc3749f987b272 Mon Sep 17 00:00:00 2001
From: Greg Sabino Mullane <greg@turnstep.com>
Date: Fri, 24 May 2024 11:25:48 -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 698169afdb..d0b5e4d9ea 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7470,6 +7470,11 @@ local0.*    /var/log/postgresql
              <entry>Remote host name or IP address</entry>
              <entry>yes</entry>
             </row>
+            <row>
+             <entry><literal>%L</literal></entry>
+             <entry>Local address</entry>
+             <entry>yes</entry>
+            </row>
             <row>
              <entry><literal>%b</literal></entry>
              <entry>Backend type</entry>
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index d91a85cb2d..b1525d901c 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"
@@ -3023,6 +3025,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 83d5df8e46..85a9c59116 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

Reply via email to