From 8918017134fea7b996a5aedc63a11c14e2947d9b Mon Sep 17 00:00:00 2001 From: Ibrar Ahmed Date: Mon, 2 Nov 2020 11:21:47 +0000 Subject: [PATCH] Issue (#59): Build failed using gcc-9 and 10. The issue only occurs when compiled using -fno-common flag. --- guc.c | 10 +++++++++- hash_query.c | 20 ++++++-------------- pg_stat_monitor.c | 39 +++++++++++++++++++++++++++++++-------- pg_stat_monitor.h | 34 ++++++++++++++++++---------------- 4 files changed, 64 insertions(+), 39 deletions(-) diff --git a/guc.c b/guc.c index f9a3022..405490a 100644 --- a/guc.c +++ b/guc.c @@ -13,6 +13,8 @@ #include "pg_stat_monitor.h" +GucVariable conf[12]; + /* * Define (or redefine) custom GUC variables. */ @@ -57,7 +59,7 @@ init_guc(void) conf[i++] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_normalized_query", .guc_desc = "Selects whether save query in normalized format.", - .guc_default = 0, + .guc_default = 1, .guc_min = 0, .guc_max = 0, .guc_restart = false @@ -275,3 +277,9 @@ init_guc(void) } +GucVariable* +get_conf(int i) +{ + return &conf[i]; +} + diff --git a/hash_query.c b/hash_query.c index fb6dcdf..758fe37 100644 --- a/hash_query.c +++ b/hash_query.c @@ -33,21 +33,12 @@ hash_init(const char *hash_name, int key_size, int entry_size, int hash_size) return ShmemInitHash(hash_name, hash_size, hash_size, &info, HASH_ELEM | HASH_BLOBS); } -/* - * shmem_startup hook: allocate or attach to shared memory, - * then load any pre-existing statistics from file. - * Also create and load the query-texts file, which is expected to exist - * (even if empty) while the module is enabled. - */ void -pgss_shmem_startup(void) +pgss_startup(void) { bool found = false; int32 i; - if (prev_shmem_startup_hook) - prev_shmem_startup_hook(); - /* reset in case this is a restart within the postmaster */ pgss = NULL; pgss_hash = NULL; @@ -73,11 +64,11 @@ pgss_shmem_startup(void) for (i = 0; i < PGSM_MAX_BUCKETS; i++) { - unsigned char *buf; - pgss_qbuf[i] = (unsigned char *) ShmemAlloc(pgss->query_buf_size_bucket); - buf = pgss_qbuf[i]; + unsigned char *buf = (unsigned char *)ShmemAlloc(pgss->query_buf_size_bucket); + set_qbuf(i, buf); memset(buf, 0, sizeof (uint64)); } + pgss_hash = hash_init("pg_stat_monitor: Queries hashtable", sizeof(pgssHashKey), sizeof(pgssEntry),PGSM_MAX); pgss_waiteventshash = hash_init("pg_stat_monitor: Wait Event hashtable", sizeof(pgssWaitEventKey), sizeof(pgssWaitEventEntry), 100); @@ -330,7 +321,8 @@ hash_create_query_entry(unsigned int queryid, return entry; } -bool IsHashInitialize(void) +bool +IsHashInitialize(void) { return (pgss || pgss_hash || pgss_object_hash || pgss_buckethash || pgss_waiteventshash); } diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 50d9f3d..6769c19 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -36,6 +36,7 @@ static struct rusage rusage_start; static struct rusage rusage_end; static volatile sig_atomic_t sigterm = false; static void handle_sigterm(SIGNAL_ARGS); +static unsigned char *pgss_qbuf[MAX_BUCKETS]; /* Saved hook values in case of unload */ static planner_hook_type planner_hook_next = NULL; @@ -45,7 +46,7 @@ static ExecutorRun_hook_type prev_ExecutorRun = NULL; static ExecutorFinish_hook_type prev_ExecutorFinish = NULL; static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; static ProcessUtility_hook_type prev_ProcessUtility = NULL; - +static shmem_startup_hook_type prev_shmem_startup_hook = NULL; PG_FUNCTION_INFO_V1(pg_stat_monitor_version); PG_FUNCTION_INFO_V1(pg_stat_monitor_reset); @@ -197,6 +198,22 @@ _PG_fini(void) hash_entry_reset(); } +/* + * shmem_startup hook: allocate or attach to shared memory, + * then load any pre-existing statistics from file. + * Also create and load the query-texts file, which is expected to exist + * (even if empty) while the module is enabled. + */ +void +pgss_shmem_startup(void) +{ + if (prev_shmem_startup_hook) + prev_shmem_startup_hook(); + + pgss_startup(); +} + + /* * Select the version of pg_stat_monitor. */ @@ -2422,16 +2439,22 @@ pg_stat_monitor_settings(PG_FUNCTION_ARGS) memset(values, 0, sizeof(values)); memset(nulls, 0, sizeof(nulls)); - values[j++] = CStringGetTextDatum(conf[i].guc_name); - values[j++] = Int64GetDatumFast(conf[i].guc_variable); - values[j++] = Int64GetDatumFast(conf[i].guc_default); - values[j++] = CStringGetTextDatum(conf[i].guc_desc); - values[j++] = Int64GetDatumFast(conf[i].guc_min); - values[j++] = Int64GetDatumFast(conf[i].guc_max); - values[j++] = Int64GetDatumFast(conf[i].guc_restart); + values[j++] = CStringGetTextDatum(get_conf(i)->guc_name); + values[j++] = Int64GetDatumFast(get_conf(i)->guc_variable); + values[j++] = Int64GetDatumFast(get_conf(i)->guc_default); + values[j++] = CStringGetTextDatum(get_conf(i)->guc_desc); + values[j++] = Int64GetDatumFast(get_conf(i)->guc_min); + values[j++] = Int64GetDatumFast(get_conf(i)->guc_max); + values[j++] = Int64GetDatumFast(get_conf(i)->guc_restart); tuplestore_putvalues(tupstore, tupdesc, values, nulls); } /* clean up and return the tuplestore */ tuplestore_donestoring(tupstore); return (Datum)0; } + +void +set_qbuf(int i, unsigned char *buf) +{ + pgss_qbuf[i] = buf; +} diff --git a/pg_stat_monitor.h b/pg_stat_monitor.h index 73fb43d..39da015 100644 --- a/pg_stat_monitor.h +++ b/pg_stat_monitor.h @@ -252,7 +252,6 @@ do { \ -unsigned char *pgss_qbuf[MAX_BUCKETS]; /* * Struct for tracking locations/lengths of constants during normalization @@ -292,6 +291,7 @@ typedef struct pgssJumbleState /* guc.c */ void init_guc(void); +GucVariable *get_conf(int i); /* hash_create.c */ void hash_alloc_object_entry(uint64 queryid, char *objects); @@ -299,7 +299,6 @@ void hash_dealloc_object_entry(uint64 queryid, char *objects); bool IsHashInitialize(void); void pgss_shmem_startup(void); void pgss_shmem_shutdown(int code, Datum arg); -shmem_startup_hook_type prev_shmem_startup_hook; int pgsm_get_bucket_size(void); pgssSharedState* pgsm_get_ss(void); HTAB* pgsm_get_wait_event_hash(void); @@ -311,19 +310,22 @@ pgssEntry* hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key, int encodin Size hash_memsize(void); pgssEntry* hash_create_query_entry(unsigned int queryid, unsigned int userid, unsigned int dbid, unsigned int bucket_id, unsigned int ip); -/*---- GUC variables ----*/ -#define PGSM_MAX conf[0].guc_variable -#define PGSM_QUERY_MAX_LEN conf[1].guc_variable -#define PGSM_ENABLED conf[2].guc_variable -#define PGSM_TRACK_UTILITY conf[3].guc_variable -#define PGSM_NORMALIZED_QUERY conf[4].guc_variable -#define PGSM_MAX_BUCKETS conf[5].guc_variable -#define PGSM_BUCKET_TIME conf[6].guc_variable -#define PGSM_OBJECT_CACHE conf[7].guc_variable -#define PGSM_RESPOSE_TIME_LOWER_BOUND conf[8].guc_variable -#define PGSM_RESPOSE_TIME_STEP conf[9].guc_variable -#define PGSM_QUERY_BUF_SIZE conf[10].guc_variable -#define PGSM_TRACK_PLANNING conf[11].guc_variable +/* hash_query.c */ +void pgss_startup(void); +void set_qbuf(int i, unsigned char *); + +/*---- GUC variables ----*/ +#define PGSM_MAX get_conf(0)->guc_variable +#define PGSM_QUERY_MAX_LEN get_conf(1)->guc_variable +#define PGSM_ENABLED get_conf(2)->guc_variable +#define PGSM_TRACK_UTILITY get_conf(3)->guc_variable +#define PGSM_NORMALIZED_QUERY get_conf(4)->guc_variable +#define PGSM_MAX_BUCKETS get_conf(5)->guc_variable +#define PGSM_BUCKET_TIME get_conf(6)->guc_variable +#define PGSM_OBJECT_CACHE get_conf(7)->guc_variable +#define PGSM_RESPOSE_TIME_LOWER_BOUND get_conf(8)->guc_variable +#define PGSM_RESPOSE_TIME_STEP get_conf(9)->guc_variable +#define PGSM_QUERY_BUF_SIZE get_conf(10)->guc_variable +#define PGSM_TRACK_PLANNING get_conf(11)->guc_variable -GucVariable conf[12]; #endif