Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions hash_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
17 changes: 8 additions & 9 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -197,7 +198,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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -458,7 +459,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
Expand Down Expand Up @@ -1091,7 +1092,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;
Expand Down Expand Up @@ -1572,7 +1573,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);
Expand Down Expand Up @@ -1663,7 +1664,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
Expand All @@ -1689,10 +1690,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;
Expand Down Expand Up @@ -1730,7 +1730,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
Expand Down
28 changes: 4 additions & 24 deletions pg_stat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----*/
Expand Down
Loading