Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG-592 Treat queries with different parent queries as separate entries #403

Merged
merged 9 commits into from
Aug 6, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*.mod*
*.cmd
.tmp_versions/
.deps/
modules.order
Module.symvers
Mkfile.old
Expand Down
31 changes: 22 additions & 9 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1438,14 +1438,13 @@ pgsm_update_entry(pgsmEntry * entry,
e->counters.info.num_relations = num_relations;
_snprintf2(e->counters.info.relations, relations, num_relations, REL_LEN);

if (exec_nested_level > 0 && e->counters.info.parentid == 0 && pgsm_track == PGSM_TRACK_ALL)
if (exec_nested_level > 0 && exec_nested_level < max_stack_depth && e->key.parentid != 0 && pgsm_track == PGSM_TRACK_ALL)
{
if (exec_nested_level >= 0 && exec_nested_level < max_stack_depth)
if (!DsaPointerIsValid(e->counters.info.parent_query))
{
int parent_query_len = nested_query_txts[exec_nested_level - 1] ?
strlen(nested_query_txts[exec_nested_level - 1]) : 0;

e->counters.info.parentid = nested_queryids[exec_nested_level - 1];
e->counters.info.parent_query = InvalidDsaPointer;
/* If we have a parent query, store it in the raw dsa area */
if (parent_query_len > 0)
Expand Down Expand Up @@ -1473,8 +1472,7 @@ pgsm_update_entry(pgsmEntry * entry,
}
else
{
e->counters.info.parentid = UINT64CONST(0);
e->counters.info.parent_query = InvalidDsaPointer;
Assert(!DsaPointerIsValid(e->counters.info.parent_query));
}
}

Expand Down Expand Up @@ -1693,6 +1691,7 @@ pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo * plan_info)
entry->key.dbid = MyDatabaseId;
entry->key.queryid = queryid;
entry->key.bucket_id = bucket_id;
entry->key.parentid = 0;

#if PG_VERSION_NUM < 140000
entry->key.toplevel = 1;
Expand Down Expand Up @@ -1807,9 +1806,20 @@ pgsm_store(pgsmEntry * entry)
memcpy(&jitusage.optimization_counter, &entry->counters.jitinfo.instr_optimization_counter, sizeof(instr_time));
memcpy(&jitusage.emission_counter, &entry->counters.jitinfo.instr_emission_counter, sizeof(instr_time));


// Update parent id if needed
if(pgsm_track == PGSM_TRACK_ALL && exec_nested_level > 0 && exec_nested_level < max_stack_depth)
{
entry->key.parentid = nested_queryids[exec_nested_level - 1];
}
else
{
entry->key.parentid = UINT64CONST(0);
}

/*
* Acquire a share lock to start with. We'd have to acquire exclusive if
* we need ot create the entry.
* we need to create the entry.
*/
LWLockAcquire(pgsm->lock, LW_SHARED);
shared_hash_entry = (pgsmEntry *) pgsm_hash_find(get_pgsmHash(), &entry->key, &found);
Expand Down Expand Up @@ -1900,6 +1910,7 @@ pgsm_store(pgsmEntry * entry)
shared_hash_entry->pgsm_query_id = entry->pgsm_query_id;
shared_hash_entry->encoding = entry->encoding;
shared_hash_entry->counters.info.cmd_type = entry->counters.info.cmd_type;
shared_hash_entry->counters.info.parent_query = InvalidDsaPointer;

snprintf(shared_hash_entry->datname, sizeof(shared_hash_entry->datname), "%s", entry->datname);
snprintf(shared_hash_entry->username, sizeof(shared_hash_entry->username), "%s", entry->username);
Expand Down Expand Up @@ -2061,6 +2072,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
bool nulls[PG_STAT_MONITOR_COLS] = {0};
int i = 0;
Counters tmp;
pgsmHashKey tmpkey;
double stddev;
uint64 queryid = entry->key.queryid;
int64 bucketid = entry->key.bucket_id;
Expand Down Expand Up @@ -2097,6 +2109,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,

SpinLockAcquire(&e->mutex);
tmp = e->counters;
tmpkey = e->key;
SpinLockRelease(&e->mutex);
}

Expand All @@ -2113,7 +2126,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
}

/* read the parent query text if any */
if (tmp.info.parentid != UINT64CONST(0))
if (tmpkey.parentid != UINT64CONST(0))
{
if (DsaPointerIsValid(tmp.info.parent_query))
{
Expand Down Expand Up @@ -2198,9 +2211,9 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
nulls[i++] = true;

/* parentid at column number 9 */
if (tmp.info.parentid != UINT64CONST(0))
if (tmpkey.parentid != UINT64CONST(0))
{
values[i++] = UInt64GetDatum(tmp.info.parentid);
values[i++] = UInt64GetDatum(tmpkey.parentid);
values[i++] = CStringGetTextDatum(parent_query_txt);
}
else
Expand Down
2 changes: 1 addition & 1 deletion pg_stat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ typedef struct pgsmHashKey
Oid dbid; /* database OID */
uint32 ip; /* client ip address */
bool toplevel; /* query executed at top level */
uint64 parentid; /* parent queryid of current query */
} pgsmHashKey;

typedef struct QueryInfo
{
uint64 parentid; /* parent queryid of current query */
dsa_pointer parent_query;
int64 type; /* type of query, options are query, info,
* warning, error, fatal */
Expand Down
Loading