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
Diego Fronza 2021-12-23 11:15:38 -03:00 committed by Hamid Akhtar
parent 1d41f62294
commit 24ae3fa66f
1 changed files with 51 additions and 21 deletions

View File

@ -17,6 +17,7 @@
#include "postgres.h"
#include "access/parallel.h"
#include "utils/guc.h"
#include <regex.h>
#ifdef BENCHMARK
#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 */
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
*/
@ -1172,8 +1175,6 @@ static int
pg_get_application_name(char *application_name, bool *ok)
{
PgBackendStatus *beentry = pg_get_backend_status();
if (!beentry)
return snprintf(application_name, APPLICATIONNAME_LEN, "%s", "postmaster");
if (!beentry)
return snprintf(application_name, APPLICATIONNAME_LEN, "%s", "unknown");
@ -1415,19 +1416,20 @@ pgss_store(uint64 queryid,
pgssHashKey key;
pgssEntry *entry;
pgssSharedState *pgss = pgsm_get_ss();
static char application_name[APPLICATIONNAME_LEN] = "";
static int application_name_len = 0;
char *app_name_ptr;
char app_name[APPLICATIONNAME_LEN] = "";
int app_name_len = 0;
bool reset = false;
uint64 bucketid;
uint64 prev_bucket_id;
uint64 userid;
uint64 planid;
uint64 appid;
uint64 appid = 0;
char comments[512] = "";
char *norm_query = NULL;
static bool found_app_name = false;
static bool found_client_addr = false;
static uint client_addr = 0;
bool found_app_name = false;
bool found_client_addr = false;
uint client_addr = 0;
/* Monitoring is disabled */
if (!PGSM_ENABLED)
@ -1474,14 +1476,24 @@ pgss_store(uint64 queryid,
else
userid = GetUserId();
if (!found_app_name)
application_name_len = pg_get_application_name(application_name, &found_app_name);
/* Try to read application name from GUC directly */
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)
client_addr = pg_get_client_addr(&found_client_addr);
planid = plan_info ? plan_info->planid : 0;
appid = djb2_hash((unsigned char *)application_name, application_name_len);
extract_query_comments(query, comments, sizeof(comments));
@ -1610,8 +1622,9 @@ pgss_store(uint64 queryid,
walusage, /* walusage */
reset, /* reset */
kind, /* kind */
application_name,
application_name_len);
app_name_ptr,
app_name_len);
}
LWLockRelease(pgss->lock);
if (norm_query)
@ -3734,6 +3747,23 @@ static uint64 djb2_hash(unsigned char *str, size_t len)
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;
}
#ifdef BENCHMARK
void init_hook_stats(void)
{