Add local-address escape "%L" to log_line_prefix.
authorTom Lane <[email protected]>
Mon, 7 Apr 2025 14:57:26 +0000 (10:57 -0400)
committerTom Lane <[email protected]>
Mon, 7 Apr 2025 15:06:05 +0000 (11:06 -0400)
This escape shows the numeric server IP address that the client
has connected to.  Unix-socket connections will show "[local]".
Non-client processes (e.g. background processes) will show "[none]".

We expect that this option will be of interest to only a fairly
small number of users.  Therefore the implementation is optimized
for the case where it's not used (that is, we don't do the string
conversion until we have to), and we've not added the field to
csvlog or jsonlog formats.

Author: Greg Sabino Mullane <[email protected]>
Reviewed-by: Cary Huang <[email protected]>
Reviewed-by: David Steele <[email protected]>
Reviewed-by: Jim Jones <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/CAKAnmmK-U+UicE-qbNU23K--Q5XTLdM6bj+gbkZBZkjyjrd3Ow@mail.gmail.com

doc/src/sgml/config.sgml
src/backend/utils/error/elog.c
src/backend/utils/misc/postgresql.conf.sample
src/include/libpq/libpq-be.h

index fea683cb49ce0c17920c48568db43beb2b208cde..a8542fe41cecad974774e5bf0028a8212c5c992f 100644 (file)
@@ -7689,6 +7689,12 @@ 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 (the IP address on the server that the
+             client connected to)</entry>
+             <entry>yes</entry>
+            </row>
             <row>
              <entry><literal>%b</literal></entry>
              <entry>Backend type</entry>
index 8a6b6905079d427fac825a08f6ee4e8f4dc00bf7..d6299633ab792b04dbea75f2e5e0e565d157d2dd 100644 (file)
@@ -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"
@@ -3084,6 +3085,38 @@ log_status_format(StringInfo buf, const char *format, ErrorData *edata)
                    appendStringInfoSpaces(buf,
                                           padding > 0 ? padding : -padding);
                break;
+           case 'L':
+               {
+                   const char *local_host;
+
+                   if (MyProcPort)
+                   {
+                       if (MyProcPort->local_host[0] == '\0')
+                       {
+                           /*
+                            * First time through: cache the lookup, since it
+                            * might not have trivial cost.
+                            */
+                           (void) pg_getnameinfo_all(&MyProcPort->laddr.addr,
+                                                     MyProcPort->laddr.salen,
+                                                     MyProcPort->local_host,
+                                                     sizeof(MyProcPort->local_host),
+                                                     NULL, 0,
+                                                     NI_NUMERICHOST | NI_NUMERICSERV);
+                       }
+                       local_host = MyProcPort->local_host;
+                   }
+                   else
+                   {
+                       /* Background process, or connection not yet made */
+                       local_host = "[none]";
+                   }
+                   if (padding != 0)
+                       appendStringInfo(buf, "%*s", padding, local_host);
+                   else
+                       appendStringInfoString(buf, local_host);
+               }
+               break;
            case 'r':
                if (MyProcPort && MyProcPort->remote_host)
                {
index bcd4e67f43e006283b2008d0cd573ad060e44d24..25fe90a430f4f84b430cc3ba4e3198ad9c174f7f 100644 (file)
                    #   %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
index 0d1f1838f73d36a0376f15e52df8b692e16be7c1..d6e671a638257093e3eafc4db7e44ce86e975a5c 100644 (file)
@@ -139,6 +139,9 @@ typedef struct Port
    int         remote_hostname_errcode;    /* see above */
    char       *remote_port;    /* text rep of remote port */
 
+   /* local_host is filled only if needed (see log_status_format) */
+   char        local_host[64]; /* ip addr of local socket for client conn */
+
    /*
     * Information that needs to be saved from the startup packet and passed
     * into backend execution.  "char *" fields are NULL if not set.