PG-254: Postpone variable initialization.

There were many variables being initialized in pgss_store() before
checking if the module was actually active, this would waste cpu
processor if the module is disabled.

To fix that, declare variables and initialize them only after check
that pg_stat_monitor is active.
This commit is contained in:
Diego Fronza
2021-10-06 14:44:57 -03:00
parent a93ba37ac3
commit fc9e630497

View File

@@ -1489,33 +1489,40 @@ pgss_store(uint64 queryid,
pgssEntry *entry;
pgssSharedState *pgss = pgsm_get_ss();
char application_name[APPLICATIONNAME_LEN];
int application_name_len = pg_get_application_name(application_name);
int application_name_len;
bool reset = false;
uint64 bucketid;
uint64 prev_bucket_id;
uint64 userid;
int con;
uint64 dbid = MyDatabaseId;
uint64 ip = pg_get_client_addr();
uint64 planid = plan_info ? plan_info->planid: 0;
uint64 appid = djb2_hash((unsigned char *)application_name, application_name_len);
uint64 dbid;
uint64 ip;
uint64 planid;
uint64 appid;
char comments[512] = "";
bool out_of_memory = false;
/* Monitoring is disabled */
if (!PGSM_ENABLED)
return;
/* Safety check... */
if (!IsSystemInitialized() || !pgss_qbuf[pg_atomic_read_u64(&pgss->current_wbucket)])
return;
Assert(query != NULL);
if (kind == PGSS_ERROR)
GetUserIdAndSecContext((unsigned int *)&userid, &con);
else
userid = GetUserId();
extract_query_comments(query, comments, sizeof(comments));
dbid = MyDatabaseId;
application_name_len = pg_get_application_name(application_name);
ip = pg_get_client_addr();
planid = plan_info ? plan_info->planid: 0;
appid = djb2_hash((unsigned char *)application_name, application_name_len);
/* Safety check... */
if (!IsSystemInitialized() || !pgss_qbuf[pg_atomic_read_u64(&pgss->current_wbucket)])
return;
extract_query_comments(query, comments, sizeof(comments));
prev_bucket_id = pg_atomic_read_u64(&pgss->current_wbucket);
bucketid = get_next_wbucket(pgss);
@@ -1717,7 +1724,6 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
if (query_entry == NULL)
continue;
if (read_query(buf, bucketid, queryid, query_txt) == 0)
{
int len;