Skip to content

Commit 3516ea7

Browse files
committed
Add local-address escape "%L" to log_line_prefix.
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 <htamfids@gmail.com> Reviewed-by: Cary Huang <cary.huang@highgo.ca> Reviewed-by: David Steele <david@pgmasters.net> Reviewed-by: Jim Jones <jim.jones@uni-muenster.de> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAKAnmmK-U+UicE-qbNU23K--Q5XTLdM6bj+gbkZBZkjyjrd3Ow@mail.gmail.com
1 parent 8f5e419 commit 3516ea7

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

doc/src/sgml/config.sgml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7689,6 +7689,12 @@ local0.* /var/log/postgresql
76897689
<entry>Remote host name or IP address</entry>
76907690
<entry>yes</entry>
76917691
</row>
7692+
<row>
7693+
<entry><literal>%L</literal></entry>
7694+
<entry>Local address (the IP address on the server that the
7695+
client connected to)</entry>
7696+
<entry>yes</entry>
7697+
</row>
76927698
<row>
76937699
<entry><literal>%b</literal></entry>
76947700
<entry>Backend type</entry>

src/backend/utils/error/elog.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#endif
6868

6969
#include "access/xact.h"
70+
#include "common/ip.h"
7071
#include "libpq/libpq.h"
7172
#include "libpq/pqformat.h"
7273
#include "mb/pg_wchar.h"
@@ -3084,6 +3085,38 @@ log_status_format(StringInfo buf, const char *format, ErrorData *edata)
30843085
appendStringInfoSpaces(buf,
30853086
padding > 0 ? padding : -padding);
30863087
break;
3088+
case 'L':
3089+
{
3090+
const char *local_host;
3091+
3092+
if (MyProcPort)
3093+
{
3094+
if (MyProcPort->local_host[0] == '\0')
3095+
{
3096+
/*
3097+
* First time through: cache the lookup, since it
3098+
* might not have trivial cost.
3099+
*/
3100+
(void) pg_getnameinfo_all(&MyProcPort->laddr.addr,
3101+
MyProcPort->laddr.salen,
3102+
MyProcPort->local_host,
3103+
sizeof(MyProcPort->local_host),
3104+
NULL, 0,
3105+
NI_NUMERICHOST | NI_NUMERICSERV);
3106+
}
3107+
local_host = MyProcPort->local_host;
3108+
}
3109+
else
3110+
{
3111+
/* Background process, or connection not yet made */
3112+
local_host = "[none]";
3113+
}
3114+
if (padding != 0)
3115+
appendStringInfo(buf, "%*s", padding, local_host);
3116+
else
3117+
appendStringInfoString(buf, local_host);
3118+
}
3119+
break;
30873120
case 'r':
30883121
if (MyProcPort && MyProcPort->remote_host)
30893122
{

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@
604604
# %d = database name
605605
# %r = remote host and port
606606
# %h = remote host
607+
# %L = local address
607608
# %b = backend type
608609
# %p = process ID
609610
# %P = process ID of parallel group leader

src/include/libpq/libpq-be.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ typedef struct Port
139139
int remote_hostname_errcode; /* see above */
140140
char *remote_port; /* text rep of remote port */
141141

142+
/* local_host is filled only if needed (see log_status_format) */
143+
char local_host[64]; /* ip addr of local socket for client conn */
144+
142145
/*
143146
* Information that needs to be saved from the startup packet and passed
144147
* into backend execution. "char *" fields are NULL if not set.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy