From 9d2efb8913e9217d9e5d9129fd1a6722f22730a7 Mon Sep 17 00:00:00 2001 From: Hamid Akhtar Date: Thu, 23 Feb 2023 13:18:38 +0500 Subject: [PATCH] PG-542: Performance improvement of pg_stat_monitor. Refining the code for storing ip locally. --- pg_stat_monitor--2.0.sql | 2 +- pg_stat_monitor.c | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pg_stat_monitor--2.0.sql b/pg_stat_monitor--2.0.sql index e9957c1..6182fa4 100644 --- a/pg_stat_monitor--2.0.sql +++ b/pg_stat_monitor--2.0.sql @@ -87,7 +87,7 @@ CREATE FUNCTION pg_stat_monitor_internal( OUT username text, OUT dbid oid, OUT datname text, - OUT client_ip int4, + OUT client_ip int8, OUT queryid int8, -- 4 OUT planid int8, diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index ee6e235..0811aa6 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -63,6 +63,11 @@ do \ strlcpy((char *)_str_dst[i], _str_src[i], _len2); \ }while(0) +#define PGSM_INVALID_IP_MASK 0xFFFFFFFF + +#define pgsm_client_ip_is_valid() \ + (pgsm_client_ip != PGSM_INVALID_IP_MASK) + /*---- Initicalization Function Declarations ----*/ void _PG_init(void); @@ -81,6 +86,8 @@ static int hist_bucket_count_user; static int hist_bucket_count_total; int64 hist_bucket_timings[MAX_RESPONSE_BUCKET + 2][2]; /* Start and end timings */ +static uint32 pgsm_client_ip = PGSM_INVALID_IP_MASK; + /* The array to store outer layer query id*/ uint64 *nested_queryids; char **nested_query_txts; @@ -1382,7 +1389,7 @@ pgsm_update_entry(pgsmEntry *entry, e->counters.time.max_time = exec_total_time; } - index = get_histogram_bucket(exec_total_time); + index = get_histogram_bucket(exec_total_time * 1000.0); e->counters.resp_calls[index]++; } @@ -1615,8 +1622,6 @@ pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo *plan_info) MemoryContext oldctx; char *datname = NULL; char *username = NULL; - static bool is_ip_set = false; - static uint32 ip = 0; /* Create an entry in the pgsm memory context */ oldctx = MemoryContextSwitchTo(pgsm_get_ss()->pgsm_mem_cxt); @@ -1633,13 +1638,10 @@ pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo *plan_info) entry->key.appid = pgsm_hash_string((const char *)app_name_ptr, app_name_len); /* client address */ - if (!is_ip_set) - { - ip = pg_get_client_addr(&found_client_addr); - is_ip_set = true; - } + if (!pgsm_client_ip_is_valid()) + pgsm_client_ip = pg_get_client_addr(&found_client_addr); - entry->key.ip = ip; + entry->key.ip = pgsm_client_ip; /* PlanID, if there is one */ entry->key.planid = plan_info ? plan_info->planid : 0; @@ -2012,7 +2014,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, int64 bucketid = entry->key.bucket_id; Oid dbid = entry->key.dbid; Oid userid = entry->key.userid; - uint32 ip = entry->key.ip; + uint64 ip = (uint64)entry->key.ip; uint64 planid = entry->key.planid; uint64 pgsm_query_id = entry->pgsm_query_id; dsa_area *query_dsa_area;