From e1553523cafe538d3684351b83bfc3e77705cec7 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 sense 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 5ebe348a..e59fc512 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 @@ -1029,7 +1029,7 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString, BufferUsage bufusage_start = pgBufferUsage; WalUsage walusage; WalUsage walusage_start = pgWalUsage; - pgsmEntry *entry = pgsm_create_hash_entry(0, queryId, NULL); + pgsmEntry *entry = pgsm_create_hash_entry(queryId, NULL); if (getrusage(RUSAGE_SELF, &rusage_start) != 0) elog(DEBUG1, "[pg_stat_monitor] pgsm_ProcessUtility: Failed to execute getrusage."); @@ -1569,7 +1569,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); @@ -1660,7 +1660,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 @@ -1686,10 +1686,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; @@ -1727,7 +1726,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 11c8f128550e385bf9a15ed829b56451c9f4a87c 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 e59fc512..4f06f0b5 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 9d059ec5..05a732a5 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 ----*/