PG-579: Querying pg_stat_monitor crashes the server ... (#351)

pgsm_get_ss() must only be called when pg_stat_monitor.so is loaded.
Fix is to move the pgsm_get_ss() call after checking if the pg_stat_monotor
library is loaded or not.
pull/354/head
Muhammad Usama 2023-01-17 15:49:38 +05:00 committed by GitHub
parent 2c5e12af0a
commit caeb5f5e73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions

View File

@ -1411,7 +1411,7 @@ pgss_store(uint64 queryid,
{
pgssHashKey key;
pgssEntry *entry;
pgssSharedState *pgss = pgsm_get_ss();
pgssSharedState *pgss;
char *app_name_ptr;
char app_name[APPLICATIONNAME_LEN] = "";
int app_name_len = 0;
@ -1434,6 +1434,8 @@ pgss_store(uint64 queryid,
if (!IsSystemInitialized())
return;
pgss = pgsm_get_ss();
#if PG_VERSION_NUM >= 140000
/*
@ -1652,13 +1654,15 @@ pgss_store(uint64 queryid,
Datum
pg_stat_monitor_reset(PG_FUNCTION_ARGS)
{
pgssSharedState *pgss = pgsm_get_ss();
pgssSharedState *pgss;
/* Safety check... */
if (!IsSystemInitialized())
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("pg_stat_monitor: must be loaded via shared_preload_libraries")));
pgss = pgsm_get_ss();
LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
hash_entry_dealloc(-1, -1, NULL);
@ -1721,7 +1725,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
PGSM_HASH_SEQ_STATUS hstat;
pgssEntry *entry;
char parentid_txt[32];
pgssSharedState *pgss = pgsm_get_ss();
pgssSharedState *pgss;
char *query_txt = NULL;
char *parent_query_txt = NULL;
int expected_columns = (api_version >= PGSM_V2_0)?PG_STAT_MONITOR_COLS_V2_0:PG_STAT_MONITOR_COLS_V1_0;
@ -1743,6 +1747,8 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
errmsg("pg_stat_monitor: materialize mode required, but it is not " \
"allowed in this context")));
pgss = pgsm_get_ss();
/* Switch into long-lived context to construct returned data structures */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);