mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
Add client_hostname field to pg_stat_activity.
Peter Eisentraut, reviewed by Steve Singer, Alvaro Herrera, and me.
This commit is contained in:
@ -495,6 +495,7 @@ CREATE VIEW pg_stat_activity AS
|
||||
U.rolname AS usename,
|
||||
S.application_name,
|
||||
S.client_addr,
|
||||
S.client_hostname,
|
||||
S.client_port,
|
||||
S.backend_start,
|
||||
S.xact_start,
|
||||
@ -512,6 +513,7 @@ CREATE VIEW pg_stat_replication AS
|
||||
U.rolname AS usename,
|
||||
S.application_name,
|
||||
S.client_addr,
|
||||
S.client_hostname,
|
||||
S.client_port,
|
||||
S.backend_start,
|
||||
W.state,
|
||||
|
@ -2223,6 +2223,7 @@ pgstat_fetch_global(void)
|
||||
|
||||
static PgBackendStatus *BackendStatusArray = NULL;
|
||||
static PgBackendStatus *MyBEEntry = NULL;
|
||||
static char *BackendClientHostnameBuffer = NULL;
|
||||
static char *BackendAppnameBuffer = NULL;
|
||||
static char *BackendActivityBuffer = NULL;
|
||||
|
||||
@ -2240,11 +2241,13 @@ BackendStatusShmemSize(void)
|
||||
mul_size(NAMEDATALEN, MaxBackends));
|
||||
size = add_size(size,
|
||||
mul_size(pgstat_track_activity_query_size, MaxBackends));
|
||||
size = add_size(size,
|
||||
mul_size(NAMEDATALEN, MaxBackends));
|
||||
return size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the shared status array and activity/appname string buffers
|
||||
* Initialize the shared status array and several string buffers
|
||||
* during postmaster startup.
|
||||
*/
|
||||
void
|
||||
@ -2286,6 +2289,24 @@ CreateSharedBackendStatus(void)
|
||||
}
|
||||
}
|
||||
|
||||
/* Create or attach to the shared client hostname buffer */
|
||||
size = mul_size(NAMEDATALEN, MaxBackends);
|
||||
BackendClientHostnameBuffer = (char *)
|
||||
ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
|
||||
|
||||
if (!found)
|
||||
{
|
||||
MemSet(BackendClientHostnameBuffer, 0, size);
|
||||
|
||||
/* Initialize st_clienthostname pointers. */
|
||||
buffer = BackendClientHostnameBuffer;
|
||||
for (i = 0; i < MaxBackends; i++)
|
||||
{
|
||||
BackendStatusArray[i].st_clienthostname = buffer;
|
||||
buffer += NAMEDATALEN;
|
||||
}
|
||||
}
|
||||
|
||||
/* Create or attach to the shared activity buffer */
|
||||
size = mul_size(pgstat_track_activity_query_size, MaxBackends);
|
||||
BackendActivityBuffer = (char *)
|
||||
@ -2386,16 +2407,21 @@ pgstat_bestart(void)
|
||||
beentry->st_databaseid = MyDatabaseId;
|
||||
beentry->st_userid = userid;
|
||||
beentry->st_clientaddr = clientaddr;
|
||||
beentry->st_clienthostname[0] = '\0';
|
||||
beentry->st_waiting = false;
|
||||
beentry->st_appname[0] = '\0';
|
||||
beentry->st_activity[0] = '\0';
|
||||
/* Also make sure the last byte in each string area is always 0 */
|
||||
beentry->st_clienthostname[NAMEDATALEN - 1] = '\0';
|
||||
beentry->st_appname[NAMEDATALEN - 1] = '\0';
|
||||
beentry->st_activity[pgstat_track_activity_query_size - 1] = '\0';
|
||||
|
||||
beentry->st_changecount++;
|
||||
Assert((beentry->st_changecount & 1) == 0);
|
||||
|
||||
if (MyProcPort && MyProcPort->remote_hostname)
|
||||
strlcpy(beentry->st_clienthostname, MyProcPort->remote_hostname, NAMEDATALEN);
|
||||
|
||||
/* Update app name to current GUC setting */
|
||||
if (application_name)
|
||||
pgstat_report_appname(application_name);
|
||||
|
@ -506,7 +506,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
|
||||
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
||||
|
||||
tupdesc = CreateTemplateTupleDesc(11, false);
|
||||
tupdesc = CreateTemplateTupleDesc(12, false);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
|
||||
@ -517,7 +517,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "query_start", TIMESTAMPTZOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 9, "backend_start", TIMESTAMPTZOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_addr", INETOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_port", INT4OID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "client_hostname", TEXTOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 12, "client_port", INT4OID, -1, 0);
|
||||
|
||||
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
|
||||
|
||||
@ -569,8 +570,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
if (funcctx->call_cntr < funcctx->max_calls)
|
||||
{
|
||||
/* for each row */
|
||||
Datum values[11];
|
||||
bool nulls[11];
|
||||
Datum values[12];
|
||||
bool nulls[12];
|
||||
HeapTuple tuple;
|
||||
PgBackendStatus *beentry;
|
||||
SockAddr zero_clientaddr;
|
||||
@ -647,6 +648,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
{
|
||||
nulls[9] = true;
|
||||
nulls[10] = true;
|
||||
nulls[11] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -671,13 +673,18 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
{
|
||||
nulls[9] = true;
|
||||
nulls[10] = true;
|
||||
nulls[11] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
|
||||
values[9] = DirectFunctionCall1(inet_in,
|
||||
CStringGetDatum(remote_host));
|
||||
values[10] = Int32GetDatum(atoi(remote_port));
|
||||
if (beentry->st_clienthostname)
|
||||
values[10] = CStringGetTextDatum(beentry->st_clienthostname);
|
||||
else
|
||||
nulls[10] = true;
|
||||
values[11] = Int32GetDatum(atoi(remote_port));
|
||||
}
|
||||
}
|
||||
else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
|
||||
@ -689,13 +696,15 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
* errors.
|
||||
*/
|
||||
nulls[9] = true;
|
||||
values[10] = DatumGetInt32(-1);
|
||||
nulls[10] = true;
|
||||
values[11] = DatumGetInt32(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Unknown address type, should never happen */
|
||||
nulls[9] = true;
|
||||
nulls[10] = true;
|
||||
nulls[11] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -709,6 +718,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
nulls[8] = true;
|
||||
nulls[9] = true;
|
||||
nulls[10] = true;
|
||||
nulls[11] = true;
|
||||
}
|
||||
|
||||
tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
|
||||
|
Reference in New Issue
Block a user