diff --git a/README.md b/README.md index 2d5605ae..b93ddc25 100644 --- a/README.md +++ b/README.md @@ -69,20 +69,20 @@ make USE_PGXS=1 install Here is the complete list of configuration parameters. ``` -postgres=# select name,value, minimum, maximum from pg_stat_monitor_settings; - name | value | minimum | maximum ------------------------------------------------+--------+---------+------------ - pg_stat_monitor.pgsm_max | 5000 | 5000 | 2147483647 - pg_stat_monitor.pgsm_query_max_len | 1024 | 1024 | 2147483647 - pg_stat_monitor.pgsm_enable | 1 | 0 | 0 - pg_stat_monitor.pgsm_track_utility | 1 | 0 | 0 - pg_stat_monitor.pgsm_normalized_query | 1 | 0 | 0 - pg_stat_monitor.pgsm_max_buckets | 10 | 1 | 10 - pg_stat_monitor.pgsm_bucket_time | 60 | 1 | 2147483647 - pg_stat_monitor.pgsm_object_cache | 500000 | 5 | 10 - pg_stat_monitor.pgsm_respose_time_lower_bound | 5 | 1 | 2147483647 - pg_stat_monitor.pgsm_respose_time_step | 1 | 1 | 2147483647 - pg_stat_monitor.pgsm_query_shared_buffer | 1 | 500000 | 2147483647 +postgres=# select * from pg_stat_monitor_settings; + name | value | default_value | description | minimum | maximum | restart +-----------------------------------------------+--------+---------------+-------------------------------------------------------------------+---------+------------+--------- + pg_stat_monitor.pgsm_max | 5000 | 5000 | Sets the maximum number of statements tracked by pg_stat_monitor. | 5000 | 2147483647 | 1 + pg_stat_monitor.pgsm_query_max_len | 1024 | 1024 | Sets the maximum length of query. | 1024 | 2147483647 | 1 + pg_stat_monitor.pgsm_enable | 1 | 1 | Enable/Disable statistics collector. | 0 | 0 | 1 + pg_stat_monitor.pgsm_track_utility | 1 | 0 | Selects whether utility commands are tracked. | 0 | 0 | 0 + pg_stat_monitor.pgsm_normalized_query | 1 | 0 | Selects whether save query in normalized format. | 0 | 0 | 0 + pg_stat_monitor.pgsm_max_buckets | 10 | 10 | Sets the maximum number of buckets. | 1 | 10 | 1 + pg_stat_monitor.pgsm_bucket_time | 60 | 60 | Sets the time in seconds per bucket. | 1 | 2147483647 | 1 + pg_stat_monitor.pgsm_object_cache | 50 | 50 | Sets the maximum number of object cache | 50 | 2147483647 | 1 + pg_stat_monitor.pgsm_respose_time_lower_bound | 1 | 1 | Sets the time in millisecond. | 1 | 2147483647 | 1 + pg_stat_monitor.pgsm_respose_time_step | 1 | 1 | Sets the respose time steps in millisecond. | 1 | 2147483647 | 1 + pg_stat_monitor.pgsm_query_shared_buffer | 500000 | 500000 | Sets the query shared_buffer size. | 500000 | 2147483647 | 1 (11 rows) ``` diff --git a/hash_query.c b/hash_query.c index c69e9a86..5333d9bb 100644 --- a/hash_query.c +++ b/hash_query.c @@ -3,7 +3,7 @@ * hash_query.c * Track statement execution times across a whole database cluster. * - * Copyright (c) 2008-2018, PostgreSQL Global Development Group + * Copyright (c) 2008-2020, PostgreSQL Global Development Group * * IDENTIFICATION * contrib/pg_stat_monitor/hash_query.c @@ -57,8 +57,8 @@ pgss_shmem_startup(void) pgss_waiteventshash = NULL; /* - * Create or attach to the shared memory state, including hash table - */ + * Create or attach to the shared memory state, including hash table + */ LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE); pgss = ShmemInitStruct("pg_stat_monitor", sizeof(pgssSharedState), &found); @@ -71,28 +71,17 @@ pgss_shmem_startup(void) } pgss->query_buf_size_bucket = PGSM_QUERY_BUF_SIZE / PGSM_MAX_BUCKETS; + for (i = 0; i < PGSM_MAX_BUCKETS; i++) pgss_qbuf[i] = (unsigned char *) ShmemAlloc(pgss->query_buf_size_bucket); - pgss_hash = hash_init("pg_stat_monitor: Queries hashtable", - sizeof(pgssHashKey), - sizeof(pgssEntry), - PGSM_MAX); + pgss_hash = hash_init("pg_stat_monitor: Queries hashtable", sizeof(pgssHashKey), sizeof(pgssEntry),PGSM_MAX); - pgss_buckethash = hash_init("pg_stat_monitor: Bucket hashtable", - sizeof(pgssBucketHashKey), - sizeof(pgssBucketEntry), - PGSM_MAX_BUCKETS); + pgss_buckethash = hash_init("pg_stat_monitor: Bucket hashtable", sizeof(pgssBucketHashKey), sizeof(pgssBucketEntry), PGSM_MAX_BUCKETS); - pgss_waiteventshash = hash_init("pg_stat_monitor: Wait Event hashtable", - sizeof(pgssWaitEventKey), - sizeof(pgssWaitEventEntry), - 100); + pgss_waiteventshash = hash_init("pg_stat_monitor: Wait Event hashtable", sizeof(pgssWaitEventKey), sizeof(pgssWaitEventEntry), 100); - pgss_object_hash = hash_init("pg_stat_monitor: Object hashtable", - sizeof(pgssObjectHashKey), - sizeof(pgssObjectEntry), - PGSM_OBJECT_CACHE); + pgss_object_hash = hash_init("pg_stat_monitor: Object hashtable", sizeof(pgssObjectHashKey), sizeof(pgssObjectEntry), PGSM_OBJECT_CACHE); Assert(IsHashInitialize()); @@ -133,9 +122,9 @@ pgss_shmem_startup(void) LWLockRelease(AddinShmemInitLock); /* - * If we're in the postmaster (or a standalone backend...), set up a shmem - * exit hook to dump the statistics to disk. - */ + * If we're in the postmaster (or a standalone backend...), set up a shmem + * exit hook to dump the statistics to disk. + */ if (!IsUnderPostmaster) on_shmem_exit(pgss_shmem_shutdown, (Datum) 0); } @@ -143,7 +132,6 @@ pgss_shmem_startup(void) int pgsm_get_bucket_size(void) { - Assert(pgss->query_buf_size_bucket <= 0); return pgss->query_buf_size_bucket; } @@ -155,26 +143,31 @@ pgssSharedState* pgsm_get_ss(void) HTAB* pgsm_get_hash(void) { + Assert(pgss_hash); return pgss_hash; } pgssBucketEntry** pgsm_get_bucket_entries(void) { + Assert(pgssBucketEntries); return pgssBucketEntries; } HTAB* pgsm_get_wait_event_hash(void) { + Assert(pgss_waiteventshash); return pgss_waiteventshash; } pgssBucketEntry** pgsm_get_bucket(void) { + Assert(pgssBucketEntries); return pgssBucketEntries; } pgssWaitEventEntry** pgsm_get_wait_event_entry(void) { + Assert(pgssWaitEventEntries); return pgssWaitEventEntries; } @@ -308,7 +301,7 @@ hash_entry_reset() } void -add_object_entry(uint64 queryid, char *objects) +hash_alloc_object_entry(uint64 queryid, char *objects) { pgssObjectEntry *entry = NULL; bool found; @@ -328,7 +321,7 @@ add_object_entry(uint64 queryid, char *objects) /* De-alocate memory */ void -remove_object_entry(uint64 queryid, char *objects) +hash_dealloc_object_entry(uint64 queryid, char *objects) { pgssObjectHashKey key; pgssObjectEntry *entry; @@ -346,7 +339,7 @@ remove_object_entry(uint64 queryid, char *objects) } pgssEntry* -pgsm_create_query_entry(unsigned int queryid, +hash_create_query_entry(unsigned int queryid, unsigned int userid, unsigned int dbid, unsigned int bucket_id, diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 7a015fb3..d854ece5 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -259,7 +259,7 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query) } } } - add_object_entry(query->queryId, tables_name); + hash_alloc_object_entry(query->queryId, tables_name); } /* @@ -711,7 +711,7 @@ static void pgss_store(const char *query, uint64 queryId, if (queryId == UINT64CONST(0)) queryId = pgss_hash_string(query, query_len); - remove_object_entry(queryId, tables_name); + hash_dealloc_object_entry(queryId, tables_name); len = strlen(tables_name); /* Set up key for hashtable search */ diff --git a/pg_stat_monitor.h b/pg_stat_monitor.h index efc87785..351a2279 100644 --- a/pg_stat_monitor.h +++ b/pg_stat_monitor.h @@ -310,8 +310,8 @@ typedef struct pgssJumbleState void init_guc(void); /* hash_create.c */ -void add_object_entry(uint64 queryid, char *objects); -void remove_object_entry(uint64 queryid, char *objects); +void hash_alloc_object_entry(uint64 queryid, char *objects); +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); @@ -327,7 +327,7 @@ void hash_entry_reset(void); void hash_entry_dealloc(int bucket); pgssEntry* hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key, int encoding); Size hash_memsize(void); -pgssEntry* pgsm_create_query_entry(unsigned int queryid, unsigned int userid, unsigned int dbid, unsigned int bucket_id, unsigned int ip); +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