Fixing issue with data not showing when database or the user is no longer valid

PG-588
Hamid Akhtar 2023-02-18 20:48:45 +05:00
parent 549451fe51
commit 446b88dbc7
4 changed files with 59 additions and 29 deletions

View File

@ -12,7 +12,9 @@ CREATE FUNCTION pg_stat_monitor_internal(
IN showtext boolean,
OUT bucket int8, -- 0
OUT userid oid,
OUT username text,
OUT dbid oid,
OUT datname text,
OUT client_ip int8,
OUT queryid int8, -- 4
@ -113,7 +115,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -155,7 +157,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
cpu_user_time,
cpu_sys_time,
bucket_done
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
@ -169,7 +171,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -222,7 +224,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
max_plan_time,
mean_plan_time,
stddev_plan_time
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
@ -235,7 +237,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -288,7 +290,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
max_plan_time,
mean_plan_time,
stddev_plan_time
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
@ -301,7 +303,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -367,7 +369,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
jit_emission_count,
jit_emission_time
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;

View File

@ -84,7 +84,9 @@ CREATE FUNCTION pg_stat_monitor_internal(
IN showtext boolean,
OUT bucket int8, -- 0
OUT userid oid,
OUT username text,
OUT dbid oid,
OUT datname text,
OUT client_ip int4,
OUT queryid int8, -- 4
@ -169,7 +171,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -211,7 +213,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
cpu_user_time,
cpu_sys_time,
bucket_done
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
@ -225,7 +227,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -278,7 +280,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
max_plan_time,
mean_plan_time,
stddev_plan_time
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
@ -291,7 +293,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -344,7 +346,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
max_plan_time,
mean_plan_time,
stddev_plan_time
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
@ -357,7 +359,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
userid::regrole AS user,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
@ -423,7 +425,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
jit_emission_count,
jit_emission_time
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;

View File

@ -23,6 +23,7 @@
#include "utils/palloc.h"
#include <regex.h>
#include "pgstat.h"
#include "commands/dbcommands.h"
#include "commands/explain.h"
#include "pg_stat_monitor.h"
@ -41,8 +42,8 @@ PG_MODULE_MAGIC;
/* Number of output arguments (columns) for various API versions */
#define PG_STAT_MONITOR_COLS_V1_0 52
#define PG_STAT_MONITOR_COLS_V2_0 62
#define PG_STAT_MONITOR_COLS 62 /* maximum of above */
#define PG_STAT_MONITOR_COLS_V2_0 64
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_0 /* maximum of above */
#define PGSM_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat_monitor_query"
@ -446,11 +447,9 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
}
}
if (!pgsm_enabled(exec_nested_level))
return;
/*
* Clear queryId for prepared statements related utility, as those will
* inherit from the underlying statement's one (except DEALLOCATE which is
@ -759,7 +758,7 @@ pgsm_ExecutorEnd(QueryDesc *queryDesc)
queryDesc->totaltime->total * 1000.0, /* exec_total_time */
queryDesc->estate->es_processed, /* rows */
&queryDesc->totaltime->bufusage, /* bufusage */
#if PG_VERSION_NUM >= 130000
#if PG_VERSION_NU >= 130000
&queryDesc->totaltime->walusage, /* walusage */
#else
NULL,
@ -1420,7 +1419,8 @@ pgsm_update_entry(pgsmEntry *entry,
e->counters.time.max_time = exec_total_time;
}
index = get_histogram_bucket(exec_total_time);
/* Get the histogram timing in microseconds */
index = get_histogram_bucket(exec_total_time * 1000);
e->counters.resp_calls[index]++;
}
@ -1657,6 +1657,8 @@ pgsm_create_hash_entry(MemoryContext context, uint64 bucket_id, uint64 queryid,
char *app_name_ptr = app_name;
int app_name_len = 0;
MemoryContext oldctx;
char *datname = NULL;
char *username = NULL;
/* Create an entry in the TopMemoryContext */
oldctx = MemoryContextSwitchTo(context);
@ -1690,6 +1692,20 @@ pgsm_create_hash_entry(MemoryContext context, uint64 bucket_id, uint64 queryid,
entry->key.toplevel = ((exec_nested_level + plan_nested_level) == 0);
#endif
datname = get_database_name(entry->key.dbid);
if (!datname)
datname = pnstrdup("<database name not available>", sizeof(entry->datname) - 1);
username = GetUserNameFromId(entry->key.userid, true);
if (!username)
username = pnstrdup("<user name not available>", sizeof(entry->username) - 1);
snprintf(entry->datname, sizeof(entry->datname), "%s", datname);
snprintf(entry->username, sizeof(entry->username), "%s", username);
pfree(datname);
pfree(username);
return entry;
}
@ -1806,8 +1822,10 @@ 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;
}
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);
}
/* bufusage */
bufusage.shared_blks_hit = entry->counters.blocks.shared_blks_hit;
@ -2057,9 +2075,15 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
/* userid at column number 1 */
values[i++] = ObjectIdGetDatum(userid);
/* userid at column number 1 */
values[i++] = CStringGetTextDatum(entry->username);
/* dbid at column number 2 */
values[i++] = ObjectIdGetDatum(dbid);
/* userid at column number 1 */
values[i++] = CStringGetTextDatum(entry->datname);
/*
* ip address at column number 3, Superusers or members of
* pg_read_all_stats members are allowed

View File

@ -382,14 +382,16 @@ typedef struct Counters
*/
typedef struct pgsmEntry
{
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
uint64 pgsm_query_id; /* pgsm generate normalized query hash */
Counters counters; /* the statistics for this query */
int encoding; /* query text encoding */
slock_t mutex; /* protects the counters only */
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
uint64 pgsm_query_id; /* pgsm generate normalized query hash */
char datname[NAMEDATALEN]; /* database name */
char username[NAMEDATALEN]; /* user name */
Counters counters; /* the statistics for this query */
int encoding; /* query text encoding */
slock_t mutex; /* protects the counters only */
union
{
dsa_pointer query_pos; /* query location within query buffer */
dsa_pointer query_pos; /* query location within query buffer */
char* query_pointer;
}query_text;
} pgsmEntry;