Fixing issue with data not showing when database or the user is no longer valid
parent
549451fe51
commit
446b88dbc7
|
@ -12,7 +12,9 @@ CREATE FUNCTION pg_stat_monitor_internal(
|
||||||
IN showtext boolean,
|
IN showtext boolean,
|
||||||
OUT bucket int8, -- 0
|
OUT bucket int8, -- 0
|
||||||
OUT userid oid,
|
OUT userid oid,
|
||||||
|
OUT username text,
|
||||||
OUT dbid oid,
|
OUT dbid oid,
|
||||||
|
OUT datname text,
|
||||||
OUT client_ip int8,
|
OUT client_ip int8,
|
||||||
|
|
||||||
OUT queryid int8, -- 4
|
OUT queryid int8, -- 4
|
||||||
|
@ -113,7 +115,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'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_user_time,
|
||||||
cpu_sys_time,
|
cpu_sys_time,
|
||||||
bucket_done
|
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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
@ -169,7 +171,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||||
|
@ -222,7 +224,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
max_plan_time,
|
max_plan_time,
|
||||||
mean_plan_time,
|
mean_plan_time,
|
||||||
stddev_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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
@ -235,7 +237,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||||
|
@ -288,7 +290,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
max_plan_time,
|
max_plan_time,
|
||||||
mean_plan_time,
|
mean_plan_time,
|
||||||
stddev_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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
@ -301,7 +303,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'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_count,
|
||||||
jit_emission_time
|
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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
|
|
@ -84,7 +84,9 @@ CREATE FUNCTION pg_stat_monitor_internal(
|
||||||
IN showtext boolean,
|
IN showtext boolean,
|
||||||
OUT bucket int8, -- 0
|
OUT bucket int8, -- 0
|
||||||
OUT userid oid,
|
OUT userid oid,
|
||||||
|
OUT username text,
|
||||||
OUT dbid oid,
|
OUT dbid oid,
|
||||||
|
OUT datname text,
|
||||||
OUT client_ip int4,
|
OUT client_ip int4,
|
||||||
|
|
||||||
OUT queryid int8, -- 4
|
OUT queryid int8, -- 4
|
||||||
|
@ -169,7 +171,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'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_user_time,
|
||||||
cpu_sys_time,
|
cpu_sys_time,
|
||||||
bucket_done
|
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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
@ -225,7 +227,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||||
|
@ -278,7 +280,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
max_plan_time,
|
max_plan_time,
|
||||||
mean_plan_time,
|
mean_plan_time,
|
||||||
stddev_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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
@ -291,7 +293,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||||
|
@ -344,7 +346,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
max_plan_time,
|
max_plan_time,
|
||||||
mean_plan_time,
|
mean_plan_time,
|
||||||
stddev_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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
@ -357,7 +359,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time AS bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
userid::regrole AS user,
|
username,
|
||||||
dbid,
|
dbid,
|
||||||
datname,
|
datname,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'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_count,
|
||||||
jit_emission_time
|
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;
|
ORDER BY bucket_start_time;
|
||||||
RETURN 0;
|
RETURN 0;
|
||||||
END;
|
END;
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include "utils/palloc.h"
|
#include "utils/palloc.h"
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#include "pgstat.h"
|
#include "pgstat.h"
|
||||||
|
#include "commands/dbcommands.h"
|
||||||
#include "commands/explain.h"
|
#include "commands/explain.h"
|
||||||
#include "pg_stat_monitor.h"
|
#include "pg_stat_monitor.h"
|
||||||
|
|
||||||
|
@ -41,8 +42,8 @@ PG_MODULE_MAGIC;
|
||||||
|
|
||||||
/* Number of output arguments (columns) for various API versions */
|
/* Number of output arguments (columns) for various API versions */
|
||||||
#define PG_STAT_MONITOR_COLS_V1_0 52
|
#define PG_STAT_MONITOR_COLS_V1_0 52
|
||||||
#define PG_STAT_MONITOR_COLS_V2_0 62
|
#define PG_STAT_MONITOR_COLS_V2_0 64
|
||||||
#define PG_STAT_MONITOR_COLS 62 /* maximum of above */
|
#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"
|
#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))
|
if (!pgsm_enabled(exec_nested_level))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Clear queryId for prepared statements related utility, as those will
|
* Clear queryId for prepared statements related utility, as those will
|
||||||
* inherit from the underlying statement's one (except DEALLOCATE which is
|
* 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->totaltime->total * 1000.0, /* exec_total_time */
|
||||||
queryDesc->estate->es_processed, /* rows */
|
queryDesc->estate->es_processed, /* rows */
|
||||||
&queryDesc->totaltime->bufusage, /* bufusage */
|
&queryDesc->totaltime->bufusage, /* bufusage */
|
||||||
#if PG_VERSION_NUM >= 130000
|
#if PG_VERSION_NU– >= 130000
|
||||||
&queryDesc->totaltime->walusage, /* walusage */
|
&queryDesc->totaltime->walusage, /* walusage */
|
||||||
#else
|
#else
|
||||||
NULL,
|
NULL,
|
||||||
|
@ -1420,7 +1419,8 @@ pgsm_update_entry(pgsmEntry *entry,
|
||||||
e->counters.time.max_time = exec_total_time;
|
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]++;
|
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;
|
char *app_name_ptr = app_name;
|
||||||
int app_name_len = 0;
|
int app_name_len = 0;
|
||||||
MemoryContext oldctx;
|
MemoryContext oldctx;
|
||||||
|
char *datname = NULL;
|
||||||
|
char *username = NULL;
|
||||||
|
|
||||||
/* Create an entry in the TopMemoryContext */
|
/* Create an entry in the TopMemoryContext */
|
||||||
oldctx = MemoryContextSwitchTo(context);
|
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);
|
entry->key.toplevel = ((exec_nested_level + plan_nested_level) == 0);
|
||||||
#endif
|
#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;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1806,8 +1822,10 @@ pgsm_store(pgsmEntry *entry)
|
||||||
shared_hash_entry->pgsm_query_id = entry->pgsm_query_id;
|
shared_hash_entry->pgsm_query_id = entry->pgsm_query_id;
|
||||||
shared_hash_entry->encoding = entry->encoding;
|
shared_hash_entry->encoding = entry->encoding;
|
||||||
shared_hash_entry->counters.info.cmd_type = entry->counters.info.cmd_type;
|
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 */
|
||||||
bufusage.shared_blks_hit = entry->counters.blocks.shared_blks_hit;
|
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 */
|
/* userid at column number 1 */
|
||||||
values[i++] = ObjectIdGetDatum(userid);
|
values[i++] = ObjectIdGetDatum(userid);
|
||||||
|
|
||||||
|
/* userid at column number 1 */
|
||||||
|
values[i++] = CStringGetTextDatum(entry->username);
|
||||||
|
|
||||||
/* dbid at column number 2 */
|
/* dbid at column number 2 */
|
||||||
values[i++] = ObjectIdGetDatum(dbid);
|
values[i++] = ObjectIdGetDatum(dbid);
|
||||||
|
|
||||||
|
/* userid at column number 1 */
|
||||||
|
values[i++] = CStringGetTextDatum(entry->datname);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ip address at column number 3, Superusers or members of
|
* ip address at column number 3, Superusers or members of
|
||||||
* pg_read_all_stats members are allowed
|
* pg_read_all_stats members are allowed
|
||||||
|
|
|
@ -384,6 +384,8 @@ typedef struct pgsmEntry
|
||||||
{
|
{
|
||||||
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
|
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
|
||||||
uint64 pgsm_query_id; /* pgsm generate normalized query hash */
|
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 */
|
Counters counters; /* the statistics for this query */
|
||||||
int encoding; /* query text encoding */
|
int encoding; /* query text encoding */
|
||||||
slock_t mutex; /* protects the counters only */
|
slock_t mutex; /* protects the counters only */
|
||||||
|
|
Loading…
Reference in New Issue