PG-153: Log application name.
parent
33b0b4a3e0
commit
6ec5c2b4ed
|
@ -22,6 +22,7 @@ CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
|
||||||
|
|
||||||
OUT queryid text,
|
OUT queryid text,
|
||||||
OUT query text,
|
OUT query text,
|
||||||
|
OUT application_name text,
|
||||||
OUT relations text,
|
OUT relations text,
|
||||||
OUT cmd_type text,
|
OUT cmd_type text,
|
||||||
OUT elevel int,
|
OUT elevel int,
|
||||||
|
@ -97,6 +98,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||||
queryid,
|
queryid,
|
||||||
query,
|
query,
|
||||||
|
application_name,
|
||||||
(string_to_array(relations, ',')) relations,
|
(string_to_array(relations, ',')) relations,
|
||||||
(string_to_array(cmd_type, ',')) cmd_type,
|
(string_to_array(cmd_type, ',')) cmd_type,
|
||||||
elevel,
|
elevel,
|
||||||
|
|
|
@ -62,6 +62,8 @@ PG_FUNCTION_INFO_V1(pg_stat_monitor);
|
||||||
PG_FUNCTION_INFO_V1(pg_stat_monitor_settings);
|
PG_FUNCTION_INFO_V1(pg_stat_monitor_settings);
|
||||||
|
|
||||||
static uint pg_get_client_addr(void);
|
static uint pg_get_client_addr(void);
|
||||||
|
static int pg_get_application_name(char* application_name);
|
||||||
|
static PgBackendStatus *pg_get_backend_status(void);
|
||||||
static Datum textarray_get_datum(char arr[][CMD_LEN], int len);
|
static Datum textarray_get_datum(char arr[][CMD_LEN], int len);
|
||||||
static Datum intarray_get_datum(int32 arr[], int len);
|
static Datum intarray_get_datum(int32 arr[], int len);
|
||||||
|
|
||||||
|
@ -655,36 +657,61 @@ pgss_hash_string(const char *str, int len)
|
||||||
len, 0));
|
len, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint
|
static PgBackendStatus*
|
||||||
pg_get_client_addr(void)
|
pg_get_backend_status(void)
|
||||||
{
|
{
|
||||||
char remote_host[NI_MAXHOST];
|
LocalPgBackendStatus *local_beentry;
|
||||||
int num_backends = pgstat_fetch_stat_numbackends();
|
int num_backends = pgstat_fetch_stat_numbackends();
|
||||||
int ret;
|
int i;
|
||||||
int i;
|
|
||||||
|
|
||||||
memset(remote_host, 0x0, NI_MAXHOST);
|
|
||||||
for (i = 1; i <= num_backends; i++)
|
for (i = 1; i <= num_backends; i++)
|
||||||
{
|
{
|
||||||
LocalPgBackendStatus *local_beentry;
|
|
||||||
PgBackendStatus *beentry;
|
PgBackendStatus *beentry;
|
||||||
|
|
||||||
local_beentry = pgstat_fetch_stat_local_beentry(i);
|
local_beentry = pgstat_fetch_stat_local_beentry(i);
|
||||||
beentry = &local_beentry->backendStatus;
|
beentry = &local_beentry->backendStatus;
|
||||||
|
|
||||||
if (beentry->st_procpid == MyProcPid)
|
if (beentry->st_procpid == MyProcPid)
|
||||||
{
|
return beentry;
|
||||||
ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
pg_get_application_name(char *application_name)
|
||||||
|
{
|
||||||
|
PgBackendStatus *beentry = pg_get_backend_status();
|
||||||
|
|
||||||
|
snprintf(application_name, APPLICATIONNAME_LEN, "%s", beentry->st_appname);
|
||||||
|
return strlen(application_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Store some statistics for a statement.
|
||||||
|
*
|
||||||
|
* If queryId is 0 then this is a utility statement and we should compute
|
||||||
|
* a suitable queryId internally.
|
||||||
|
*
|
||||||
|
* If jstate is not NULL then we're trying to create an entry for which
|
||||||
|
* we have no statistics as yet; we just want to record the normalized
|
||||||
|
*/
|
||||||
|
|
||||||
|
static uint
|
||||||
|
pg_get_client_addr(void)
|
||||||
|
{
|
||||||
|
PgBackendStatus *beentry = pg_get_backend_status();
|
||||||
|
char remote_host[NI_MAXHOST];
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
memset(remote_host, 0x0, NI_MAXHOST);
|
||||||
|
ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
|
||||||
beentry->st_clientaddr.salen,
|
beentry->st_clientaddr.salen,
|
||||||
remote_host, sizeof(remote_host),
|
remote_host, sizeof(remote_host),
|
||||||
NULL, 0,
|
NULL, 0,
|
||||||
NI_NUMERICHOST | NI_NUMERICSERV);
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
||||||
if (ret == 0)
|
if (ret != 0)
|
||||||
break;
|
return ntohl(inet_addr("127.0.0.1"));
|
||||||
else
|
|
||||||
return ntohl(inet_addr("127.0.0.1"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (strcmp(remote_host, "[local]") == 0)
|
if (strcmp(remote_host, "[local]") == 0)
|
||||||
return ntohl(inet_addr("127.0.0.1"));
|
return ntohl(inet_addr("127.0.0.1"));
|
||||||
return ntohl(inet_addr(remote_host));
|
return ntohl(inet_addr(remote_host));
|
||||||
|
@ -731,10 +758,13 @@ static void pgss_store(uint64 queryId,
|
||||||
HTAB *pgss_hash = pgsm_get_hash();
|
HTAB *pgss_hash = pgsm_get_hash();
|
||||||
int message_len = message ? strlen(message) : 0;
|
int message_len = message ? strlen(message) : 0;
|
||||||
int cmd_len[CMD_LST];
|
int cmd_len[CMD_LST];
|
||||||
|
char application_name[APPLICATIONNAME_LEN];
|
||||||
|
int application_name_len;
|
||||||
Assert(query != NULL);
|
Assert(query != NULL);
|
||||||
Assert(PGSM_ENABLED);
|
Assert(PGSM_ENABLED);
|
||||||
|
|
||||||
|
application_name_len = pg_get_application_name(application_name);
|
||||||
|
|
||||||
/* Safety check... */
|
/* Safety check... */
|
||||||
if (!IsSystemInitialized() || !pgss_qbuf[pgss->current_wbucket])
|
if (!IsSystemInitialized() || !pgss_qbuf[pgss->current_wbucket])
|
||||||
return;
|
return;
|
||||||
|
@ -899,6 +929,9 @@ static void pgss_store(uint64 queryId,
|
||||||
e->counters.resp_calls[MAX_RESPONSE_BUCKET - 1]++;
|
e->counters.resp_calls[MAX_RESPONSE_BUCKET - 1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < application_name_len; i++)
|
||||||
|
e->counters.info.application_name[i] = application_name[i];
|
||||||
|
|
||||||
for (i = 0; i < REL_LST; i++)
|
for (i = 0; i < REL_LST; i++)
|
||||||
if (e->counters.info.relations[i] != 0)
|
if (e->counters.info.relations[i] != 0)
|
||||||
found = true;
|
found = true;
|
||||||
|
@ -1112,6 +1145,10 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
|
||||||
else
|
else
|
||||||
nulls[i++] = true;
|
nulls[i++] = true;
|
||||||
}
|
}
|
||||||
|
if (strlen(tmp.info.application_name) <= 0)
|
||||||
|
nulls[i++] = true;
|
||||||
|
else
|
||||||
|
values[i++] = CStringGetTextDatum(tmp.info.application_name);
|
||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
for (j = 0; j < REL_LST; j++)
|
for (j = 0; j < REL_LST; j++)
|
||||||
|
|
|
@ -64,6 +64,7 @@
|
||||||
#define REL_LST 10
|
#define REL_LST 10
|
||||||
#define CMD_LST 10
|
#define CMD_LST 10
|
||||||
#define CMD_LEN 20
|
#define CMD_LEN 20
|
||||||
|
#define APPLICATIONNAME_LEN 100
|
||||||
|
|
||||||
typedef struct GucVariables
|
typedef struct GucVariables
|
||||||
{
|
{
|
||||||
|
@ -121,6 +122,7 @@ typedef struct QueryInfo
|
||||||
Oid dbid; /* database OID */
|
Oid dbid; /* database OID */
|
||||||
uint host; /* client IP */
|
uint host; /* client IP */
|
||||||
int64 type; /* type of query, options are query, info, warning, error, fatal */
|
int64 type; /* type of query, options are query, info, warning, error, fatal */
|
||||||
|
char application_name[APPLICATIONNAME_LEN];
|
||||||
int32 relations[REL_LST];
|
int32 relations[REL_LST];
|
||||||
char cmd_type[CMD_LST][CMD_LEN]; /* query command type SELECT/UPDATE/DELETE/INSERT */
|
char cmd_type[CMD_LST][CMD_LEN]; /* query command type SELECT/UPDATE/DELETE/INSERT */
|
||||||
} QueryInfo;
|
} QueryInfo;
|
||||||
|
|
Loading…
Reference in New Issue