Skip to content

Commit 4a25bc1

Browse files
committed
Add client_hostname field to pg_stat_activity.
Peter Eisentraut, reviewed by Steve Singer, Alvaro Herrera, and me.
1 parent a3e8486 commit 4a25bc1

File tree

8 files changed

+69
-23
lines changed

8 files changed

+69
-23
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,17 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
245245
<entry><structname>pg_stat_activity</><indexterm><primary>pg_stat_activity</primary></indexterm></entry>
246246
<entry>One row per server process, showing database OID, database
247247
name, process <acronym>ID</>, user OID, user name, application name,
248-
client's address and port number, times at which the server process,
249-
current transaction, and current query began execution, process's waiting
250-
status, and text of the current query.
248+
client's address, hostname (if available), and port number, times at
249+
which the server process, current transaction, and current query began
250+
execution, process's waiting status, and text of the current query.
251251
The columns that report data on the current query are available unless
252252
the parameter <varname>track_activities</varname> has been turned off.
253253
Furthermore, these columns are only visible if the user examining
254254
the view is a superuser or the same as the user owning the process
255-
being reported on.
255+
being reported on. The client's hostname will be available only if
256+
<xref linkend="guc-log-hostname"> is set or if the user's hostname
257+
needed to be looked up during <filename>pg_hba.conf</filename>
258+
processing.
256259
</entry>
257260
</row>
258261

@@ -297,14 +300,18 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
297300
<row>
298301
<entry><structname>pg_stat_replication</><indexterm><primary>pg_stat_replication</primary></indexterm></entry>
299302
<entry>One row per WAL sender process, showing process <acronym>ID</>,
300-
user OID, user name, application name, client's address and port number,
301-
time at which the server process began execution, and the current WAL
302-
sender state and transaction log location. In addition, the standby
303-
reports the last transaction log position it received and wrote, the last
304-
position it flushed to disk, and the last position it replayed, and this
305-
information is also displayed here. The columns detailing what exactly
306-
the connection is doing are only visible if the user examining the view
307-
is a superuser.
303+
user OID, user name, application name, client's address, hostname
304+
(if available) and port number, time at which the server process began
305+
execution, and the current WAL sender state and transaction log
306+
location. In addition, the standby reports the last transaction log
307+
position it received and wrote, the last position it flushed to disk,
308+
and the last position it replayed, and this information is also
309+
displayed here. The columns detailing what exactly the connection is
310+
doing are only visible if the user examining the view is a superuser.
311+
The client's hostname will be available only if
312+
<xref linkend="guc-log-hostname"> is set or if the user's hostname
313+
needed to be looked up during <filename>pg_hba.conf</filename>
314+
processing.
308315
</entry>
309316
</row>
310317

src/backend/catalog/system_views.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ CREATE VIEW pg_stat_activity AS
495495
U.rolname AS usename,
496496
S.application_name,
497497
S.client_addr,
498+
S.client_hostname,
498499
S.client_port,
499500
S.backend_start,
500501
S.xact_start,
@@ -512,6 +513,7 @@ CREATE VIEW pg_stat_replication AS
512513
U.rolname AS usename,
513514
S.application_name,
514515
S.client_addr,
516+
S.client_hostname,
515517
S.client_port,
516518
S.backend_start,
517519
W.state,

src/backend/postmaster/pgstat.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,7 @@ pgstat_fetch_global(void)
22232223

22242224
static PgBackendStatus *BackendStatusArray = NULL;
22252225
static PgBackendStatus *MyBEEntry = NULL;
2226+
static char *BackendClientHostnameBuffer = NULL;
22262227
static char *BackendAppnameBuffer = NULL;
22272228
static char *BackendActivityBuffer = NULL;
22282229

@@ -2240,11 +2241,13 @@ BackendStatusShmemSize(void)
22402241
mul_size(NAMEDATALEN, MaxBackends));
22412242
size = add_size(size,
22422243
mul_size(pgstat_track_activity_query_size, MaxBackends));
2244+
size = add_size(size,
2245+
mul_size(NAMEDATALEN, MaxBackends));
22432246
return size;
22442247
}
22452248

22462249
/*
2247-
* Initialize the shared status array and activity/appname string buffers
2250+
* Initialize the shared status array and several string buffers
22482251
* during postmaster startup.
22492252
*/
22502253
void
@@ -2286,6 +2289,24 @@ CreateSharedBackendStatus(void)
22862289
}
22872290
}
22882291

2292+
/* Create or attach to the shared client hostname buffer */
2293+
size = mul_size(NAMEDATALEN, MaxBackends);
2294+
BackendClientHostnameBuffer = (char *)
2295+
ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
2296+
2297+
if (!found)
2298+
{
2299+
MemSet(BackendClientHostnameBuffer, 0, size);
2300+
2301+
/* Initialize st_clienthostname pointers. */
2302+
buffer = BackendClientHostnameBuffer;
2303+
for (i = 0; i < MaxBackends; i++)
2304+
{
2305+
BackendStatusArray[i].st_clienthostname = buffer;
2306+
buffer += NAMEDATALEN;
2307+
}
2308+
}
2309+
22892310
/* Create or attach to the shared activity buffer */
22902311
size = mul_size(pgstat_track_activity_query_size, MaxBackends);
22912312
BackendActivityBuffer = (char *)
@@ -2386,16 +2407,21 @@ pgstat_bestart(void)
23862407
beentry->st_databaseid = MyDatabaseId;
23872408
beentry->st_userid = userid;
23882409
beentry->st_clientaddr = clientaddr;
2410+
beentry->st_clienthostname[0] = '\0';
23892411
beentry->st_waiting = false;
23902412
beentry->st_appname[0] = '\0';
23912413
beentry->st_activity[0] = '\0';
23922414
/* Also make sure the last byte in each string area is always 0 */
2415+
beentry->st_clienthostname[NAMEDATALEN - 1] = '\0';
23932416
beentry->st_appname[NAMEDATALEN - 1] = '\0';
23942417
beentry->st_activity[pgstat_track_activity_query_size - 1] = '\0';
23952418

23962419
beentry->st_changecount++;
23972420
Assert((beentry->st_changecount & 1) == 0);
23982421

2422+
if (MyProcPort && MyProcPort->remote_hostname)
2423+
strlcpy(beentry->st_clienthostname, MyProcPort->remote_hostname, NAMEDATALEN);
2424+
23992425
/* Update app name to current GUC setting */
24002426
if (application_name)
24012427
pgstat_report_appname(application_name);

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
506506

507507
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
508508

509-
tupdesc = CreateTemplateTupleDesc(11, false);
509+
tupdesc = CreateTemplateTupleDesc(12, false);
510510
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
511511
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
512512
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
@@ -517,7 +517,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
517517
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "query_start", TIMESTAMPTZOID, -1, 0);
518518
TupleDescInitEntry(tupdesc, (AttrNumber) 9, "backend_start", TIMESTAMPTZOID, -1, 0);
519519
TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_addr", INETOID, -1, 0);
520-
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_port", INT4OID, -1, 0);
520+
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_hostname", TEXTOID, -1, 0);
521+
TupleDescInitEntry(tupdesc, (AttrNumber) 12, "client_port", INT4OID, -1, 0);
521522

522523
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
523524

@@ -569,8 +570,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
569570
if (funcctx->call_cntr < funcctx->max_calls)
570571
{
571572
/* for each row */
572-
Datum values[11];
573-
bool nulls[11];
573+
Datum values[12];
574+
bool nulls[12];
574575
HeapTuple tuple;
575576
PgBackendStatus *beentry;
576577
SockAddr zero_clientaddr;
@@ -647,6 +648,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
647648
{
648649
nulls[9] = true;
649650
nulls[10] = true;
651+
nulls[11] = true;
650652
}
651653
else
652654
{
@@ -671,13 +673,18 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
671673
{
672674
nulls[9] = true;
673675
nulls[10] = true;
676+
nulls[11] = true;
674677
}
675678
else
676679
{
677680
clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
678681
values[9] = DirectFunctionCall1(inet_in,
679682
CStringGetDatum(remote_host));
680-
values[10] = Int32GetDatum(atoi(remote_port));
683+
if (beentry->st_clienthostname)
684+
values[10] = CStringGetTextDatum(beentry->st_clienthostname);
685+
else
686+
nulls[10] = true;
687+
values[11] = Int32GetDatum(atoi(remote_port));
681688
}
682689
}
683690
else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
@@ -689,13 +696,15 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
689696
* errors.
690697
*/
691698
nulls[9] = true;
692-
values[10] = DatumGetInt32(-1);
699+
nulls[10] = true;
700+
values[11] = DatumGetInt32(-1);
693701
}
694702
else
695703
{
696704
/* Unknown address type, should never happen */
697705
nulls[9] = true;
698706
nulls[10] = true;
707+
nulls[11] = true;
699708
}
700709
}
701710
}
@@ -709,6 +718,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
709718
nulls[8] = true;
710719
nulls[9] = true;
711720
nulls[10] = true;
721+
nulls[11] = true;
712722
}
713723

714724
tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201102161
56+
#define CATALOG_VERSION_NO 201102171
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3073,7 +3073,7 @@ DATA(insert OID = 3057 ( pg_stat_get_autoanalyze_count PGNSP PGUID 12 1 0 0 f f
30733073
DESCR("statistics: number of auto analyzes for a table");
30743074
DATA(insert OID = 1936 ( pg_stat_get_backend_idset PGNSP PGUID 12 1 100 0 f f f t t s 0 0 23 "" _null_ _null_ _null_ _null_ pg_stat_get_backend_idset _null_ _null_ _null_ ));
30753075
DESCR("statistics: currently active backend IDs");
3076-
DATA(insert OID = 2022 ( pg_stat_get_activity PGNSP PGUID 12 1 100 0 f f f f t s 1 0 2249 "23" "{23,26,23,26,25,25,16,1184,1184,1184,869,23}" "{i,o,o,o,o,o,o,o,o,o,o,o}" "{pid,datid,procpid,usesysid,application_name,current_query,waiting,xact_start,query_start,backend_start,client_addr,client_port}" _null_ pg_stat_get_activity _null_ _null_ _null_ ));
3076+
DATA(insert OID = 2022 ( pg_stat_get_activity PGNSP PGUID 12 1 100 0 f f f f t s 1 0 2249 "23" "{23,26,23,26,25,25,16,1184,1184,1184,869,25,23}" "{i,o,o,o,o,o,o,o,o,o,o,o,o}" "{pid,datid,procpid,usesysid,application_name,current_query,waiting,xact_start,query_start,backend_start,client_addr,client_hostname,client_port}" _null_ pg_stat_get_activity _null_ _null_ _null_ ));
30773077
DESCR("statistics: information about currently active backends");
30783078
DATA(insert OID = 3099 ( pg_stat_get_wal_senders PGNSP PGUID 12 1 10 0 f f f f t s 0 0 2249 "" "{23,25,25,25,25,25}" "{o,o,o,o,o,o}" "{procpid,state,sent_location,write_location,flush_location,apply_location}" _null_ pg_stat_get_wal_senders _null_ _null_ _null_ ));
30793079
DESCR("statistics: information about currently active replication");

src/include/pgstat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ typedef struct PgBackendStatus
628628
Oid st_databaseid;
629629
Oid st_userid;
630630
SockAddr st_clientaddr;
631+
char *st_clienthostname; /* MUST be null-terminated */
631632

632633
/* Is backend currently waiting on an lmgr lock? */
633634
bool st_waiting;

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