PG-296: Fix application name.
If a backend would change the application name during execution, pg_stat_monitor would then fail to read the updated value, as it caches the result in order to avoid calling the expensive functions pgstat_fetch_stat_numbackends() and pgstat_fetch_stat_local_beentry(). A workaround was found, we can just read an exported GUC from PostgreSQL backend itself, namely application_name, from utils/guc.h, thus saving us from having to call those expensive functions.pull/184/head
parent
1d41f62294
commit
24ae3fa66f
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
#include "access/parallel.h"
|
#include "access/parallel.h"
|
||||||
|
#include "utils/guc.h"
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#ifdef BENCHMARK
|
#ifdef BENCHMARK
|
||||||
#include <time.h> /* clock() */
|
#include <time.h> /* clock() */
|
||||||
|
@ -199,6 +200,8 @@ static uint64 get_query_id(JumbleState *jstate, Query *query);
|
||||||
|
|
||||||
/* Daniel J. Bernstein's hash algorithm: see http://www.cse.yorku.ca/~oz/hash.html */
|
/* Daniel J. Bernstein's hash algorithm: see http://www.cse.yorku.ca/~oz/hash.html */
|
||||||
static uint64 djb2_hash(unsigned char *str, size_t len);
|
static uint64 djb2_hash(unsigned char *str, size_t len);
|
||||||
|
/* Same as above, but stores the calculated string length into *out_len (small optimization) */
|
||||||
|
static uint64 djb2_hash_str(unsigned char *str, int *out_len);
|
||||||
/*
|
/*
|
||||||
* Module load callback
|
* Module load callback
|
||||||
*/
|
*/
|
||||||
|
@ -1172,8 +1175,6 @@ static int
|
||||||
pg_get_application_name(char *application_name, bool *ok)
|
pg_get_application_name(char *application_name, bool *ok)
|
||||||
{
|
{
|
||||||
PgBackendStatus *beentry = pg_get_backend_status();
|
PgBackendStatus *beentry = pg_get_backend_status();
|
||||||
if (!beentry)
|
|
||||||
return snprintf(application_name, APPLICATIONNAME_LEN, "%s", "postmaster");
|
|
||||||
|
|
||||||
if (!beentry)
|
if (!beentry)
|
||||||
return snprintf(application_name, APPLICATIONNAME_LEN, "%s", "unknown");
|
return snprintf(application_name, APPLICATIONNAME_LEN, "%s", "unknown");
|
||||||
|
@ -1412,22 +1413,23 @@ pgss_store(uint64 queryid,
|
||||||
pgssStoreKind kind)
|
pgssStoreKind kind)
|
||||||
{
|
{
|
||||||
HTAB *pgss_hash;
|
HTAB *pgss_hash;
|
||||||
pgssHashKey key;
|
pgssHashKey key;
|
||||||
pgssEntry *entry;
|
pgssEntry *entry;
|
||||||
pgssSharedState *pgss = pgsm_get_ss();
|
pgssSharedState *pgss = pgsm_get_ss();
|
||||||
static char application_name[APPLICATIONNAME_LEN] = "";
|
char *app_name_ptr;
|
||||||
static int application_name_len = 0;
|
char app_name[APPLICATIONNAME_LEN] = "";
|
||||||
bool reset = false;
|
int app_name_len = 0;
|
||||||
uint64 bucketid;
|
bool reset = false;
|
||||||
uint64 prev_bucket_id;
|
uint64 bucketid;
|
||||||
uint64 userid;
|
uint64 prev_bucket_id;
|
||||||
uint64 planid;
|
uint64 userid;
|
||||||
uint64 appid;
|
uint64 planid;
|
||||||
char comments[512] = "";
|
uint64 appid = 0;
|
||||||
|
char comments[512] = "";
|
||||||
char *norm_query = NULL;
|
char *norm_query = NULL;
|
||||||
static bool found_app_name = false;
|
bool found_app_name = false;
|
||||||
static bool found_client_addr = false;
|
bool found_client_addr = false;
|
||||||
static uint client_addr = 0;
|
uint client_addr = 0;
|
||||||
|
|
||||||
/* Monitoring is disabled */
|
/* Monitoring is disabled */
|
||||||
if (!PGSM_ENABLED)
|
if (!PGSM_ENABLED)
|
||||||
|
@ -1474,14 +1476,24 @@ pgss_store(uint64 queryid,
|
||||||
else
|
else
|
||||||
userid = GetUserId();
|
userid = GetUserId();
|
||||||
|
|
||||||
if (!found_app_name)
|
/* Try to read application name from GUC directly */
|
||||||
application_name_len = pg_get_application_name(application_name, &found_app_name);
|
if (application_name && *application_name)
|
||||||
|
{
|
||||||
|
app_name_ptr = application_name;
|
||||||
|
appid = djb2_hash_str((unsigned char *)application_name, &app_name_len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
app_name_len = pg_get_application_name(app_name, &found_app_name);
|
||||||
|
if (found_app_name)
|
||||||
|
appid = djb2_hash((unsigned char *)app_name, app_name_len);
|
||||||
|
app_name_ptr = app_name;
|
||||||
|
}
|
||||||
|
|
||||||
if (!found_client_addr)
|
if (!found_client_addr)
|
||||||
client_addr = pg_get_client_addr(&found_client_addr);
|
client_addr = pg_get_client_addr(&found_client_addr);
|
||||||
|
|
||||||
planid = plan_info ? plan_info->planid : 0;
|
planid = plan_info ? plan_info->planid : 0;
|
||||||
appid = djb2_hash((unsigned char *)application_name, application_name_len);
|
|
||||||
|
|
||||||
extract_query_comments(query, comments, sizeof(comments));
|
extract_query_comments(query, comments, sizeof(comments));
|
||||||
|
|
||||||
|
@ -1610,8 +1622,9 @@ pgss_store(uint64 queryid,
|
||||||
walusage, /* walusage */
|
walusage, /* walusage */
|
||||||
reset, /* reset */
|
reset, /* reset */
|
||||||
kind, /* kind */
|
kind, /* kind */
|
||||||
application_name,
|
app_name_ptr,
|
||||||
application_name_len);
|
app_name_len);
|
||||||
|
}
|
||||||
|
|
||||||
LWLockRelease(pgss->lock);
|
LWLockRelease(pgss->lock);
|
||||||
if (norm_query)
|
if (norm_query)
|
||||||
|
@ -3729,7 +3742,24 @@ static uint64 djb2_hash(unsigned char *str, size_t len)
|
||||||
uint64 hash = 5381LLU;
|
uint64 hash = 5381LLU;
|
||||||
|
|
||||||
while (len--)
|
while (len--)
|
||||||
hash = ((hash << 5) + hash) ^ *str++; // hash(i - 1) * 33 ^ str[i]
|
hash = ((hash << 5) + hash) ^ *str++; // hash(i - 1) * 33 ^ str[i]
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64 djb2_hash_str(unsigned char *str, int *out_len)
|
||||||
|
{
|
||||||
|
uint64 hash = 5381LLU;
|
||||||
|
unsigned char *start = str;
|
||||||
|
unsigned char c;
|
||||||
|
|
||||||
|
while ((c = *str) != '\0')
|
||||||
|
{
|
||||||
|
hash = ((hash << 5) + hash) ^ c; // hash(i - 1) * 33 ^ str[i]
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_len = str - start;
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue