From b9daa0b24a8d6f0ec12c7ee2df79fb66029b75f1 Mon Sep 17 00:00:00 2001 From: Artem Gavrilov Date: Tue, 24 Mar 2026 17:10:01 +0200 Subject: [PATCH 1/2] Remove unused argument from pgsm_create_hash_entry function Everywhere where we invoke this function we pass just zero. This argument has no sence as we always fill bucket id later. --- pg_stat_monitor.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index dcdee2fb..de66443c 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -197,7 +197,7 @@ DECLARE_HOOK(void pgsm_ProcessUtility, PlannedStmt *pstmt, const char *queryStri static int64 pgsm_hash_string(const char *str, int len); char *unpack_sql_state(int sql_state); -static pgsmEntry *pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info); +static pgsmEntry *pgsm_create_hash_entry(int64 queryid, PlanInfo *plan_info); static void pgsm_add_to_list(pgsmEntry *entry, char *query_text, int query_len); static void pgsm_delete_entry(uint64 queryid); static pgsmEntry *pgsm_get_entry_for_query(int64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create, CmdType cmd_type); @@ -458,7 +458,7 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState * * bucket value. The correct bucket value will be needed then to search * the hash table, or create the appropriate entry. */ - entry = pgsm_create_hash_entry(0, query->queryId, NULL); + entry = pgsm_create_hash_entry(query->queryId, NULL); /* * Update other member that are not counters, so that we don't have to @@ -1091,7 +1091,7 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString, BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start); /* Create an entry for this query */ - entry = pgsm_create_hash_entry(0, queryId, NULL); + entry = pgsm_create_hash_entry(queryId, NULL); location = pstmt->stmt_location; query_len = pstmt->stmt_len; @@ -1572,7 +1572,7 @@ pgsm_store_error(const char *query, ErrorData *edata) queryid = pgsm_hash_string(query, len); - entry = pgsm_create_hash_entry(0, queryid, NULL); + entry = pgsm_create_hash_entry(queryid, NULL); entry->query_text.query_pointer = pnstrdup(query, len); entry->pgsm_query_id = get_pgsm_query_id_hash(query, len); @@ -1663,7 +1663,7 @@ pgsm_get_entry_for_query(int64 queryid, PlanInfo *plan_info, const char *query_t * current bucket value. The correct bucket value will be needed then * to search the hash table, or create the appropriate entry. */ - entry = pgsm_create_hash_entry(0, queryid, plan_info); + entry = pgsm_create_hash_entry(queryid, plan_info); /* * Update other member that are not counters, so that we don't have to @@ -1689,10 +1689,9 @@ pgsm_cleanup_callback(void *arg) /* * Function encapsulating some external calls for filling up the hash key data structure. - * The bucket_id may not be known at this stage. So pass any value that you may wish. */ static pgsmEntry * -pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info) +pgsm_create_hash_entry(int64 queryid, PlanInfo *plan_info) { pgsmEntry *entry; int sec_ctx; @@ -1730,7 +1729,6 @@ pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info) /* Set remaining data */ entry->key.dbid = MyDatabaseId; entry->key.queryid = queryid; - entry->key.bucket_id = bucket_id; entry->key.parentid = 0; #if PG_VERSION_NUM >= 170000 From 0a03600489fcb312e01d6516c56b1037dd88abf7 Mon Sep 17 00:00:00 2001 From: Artem Gavrilov Date: Tue, 24 Mar 2026 18:24:19 +0200 Subject: [PATCH 2/2] Cleanup header file Move declarations that used in single file to that file. No need to have them in main project header file. --- hash_query.c | 16 ++++++++++++++-- pg_stat_monitor.c | 3 ++- pg_stat_monitor.h | 28 ++++------------------------ 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/hash_query.c b/hash_query.c index a5782158..fe960772 100644 --- a/hash_query.c +++ b/hash_query.c @@ -19,7 +19,19 @@ #include "pg_stat_monitor.h" +typedef struct pgsmLocalState +{ + pgsmSharedState *shared_pgsmState; + dsa_area *dsa; /* local dsa area for backend attached to the + * dsa area created by postmaster at startup. */ + PGSM_HASH_TABLE *shared_hash; + MemoryContext pgsm_mem_cxt; + +} pgsmLocalState; + static pgsmLocalState pgsmStateLocal; +static void pgsm_attach_shmem(void); +static void pgsm_shmem_shutdown(int code, Datum arg); static PGSM_HASH_TABLE_HANDLE pgsm_create_bucket_hash(pgsmSharedState *pgsm, dsa_area *dsa); static Size pgsm_get_shared_area_size(void); static void InitializeSharedState(pgsmSharedState *pgsm); @@ -205,7 +217,7 @@ pgsm_create_bucket_hash(pgsmSharedState *pgsm, dsa_area *dsa) * different virtual address in this process. * */ -void +static void pgsm_attach_shmem(void) { MemoryContext oldcontext; @@ -273,7 +285,7 @@ pgsm_get_ss(void) * Note: we don't bother with acquiring lock, because there should be no * other processes running when this is called. */ -void +static void pgsm_shmem_shutdown(int code, Datum arg) { /* Don't try to dump during a crash. */ diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index de66443c..a17cce76 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -127,6 +127,7 @@ static int app_name_len; /* Query buffer, store queries' text. */ static char *pgsm_explain(QueryDesc *queryDesc); +static void pgsm_shmem_startup(void); static void extract_query_comments(const char *query, char *comments, size_t max_len); static void set_histogram_bucket_timings(void); static void histogram_bucket_timings(int index, double *b_start, double *b_end); @@ -331,7 +332,7 @@ _PG_init(void) * Also create and load the query-texts file, which is expected to exist * (even if empty) while the module is enabled. */ -void +static void pgsm_shmem_startup(void) { if (prev_shmem_startup_hook) diff --git a/pg_stat_monitor.h b/pg_stat_monitor.h index 5f70b0eb..caf4903a 100644 --- a/pg_stat_monitor.h +++ b/pg_stat_monitor.h @@ -310,40 +310,20 @@ typedef struct pgsmSharedState TimestampTz bucket_start_time[]; /* start time of the bucket */ } pgsmSharedState; -typedef struct pgsmLocalState -{ - pgsmSharedState *shared_pgsmState; - dsa_area *dsa; /* local dsa area for backend attached to the - * dsa area created by postmaster at startup. */ - PGSM_HASH_TABLE *shared_hash; - MemoryContext pgsm_mem_cxt; - -} pgsmLocalState; - /* guc.c */ void init_guc(void); -/* hash_create.c */ +/* hash_query.c */ +void pgsm_startup(void); +MemoryContext GetPgsmMemoryContext(void); dsa_area *get_dsa_area_for_query_text(void); PGSM_HASH_TABLE *get_pgsmHash(void); - -void pgsm_attach_shmem(void); bool IsHashInitialize(void); bool IsSystemOOM(void); -void pgsm_shmem_startup(void); -void pgsm_shmem_shutdown(int code, Datum arg); +Size pgsm_ShmemSize(void); pgsmSharedState *pgsm_get_ss(void); void hash_entry_dealloc(int new_bucket_id, int old_bucket_id, unsigned char *query_buffer); pgsmEntry *hash_entry_alloc(pgsmSharedState *pgsm, pgsmHashKey *key, int encoding); -Size pgsm_ShmemSize(void); -void pgsm_startup(void); - -/* hash_query.c */ -void pgsm_startup(void); -MemoryContext GetPgsmMemoryContext(void); - -/* guc.c */ -void init_guc(void); /* GUC variables*/ /*---- GUC variables ----*/