PG-186: Add support to monitor query execution plan.

This commit is contained in:
Ibrar Ahmed
2021-03-17 18:56:39 +05:00
parent aa0ca050d5
commit e0fc683810
18 changed files with 499 additions and 533 deletions

View File

@@ -21,7 +21,6 @@
static pgssSharedState *pgss;
static HTAB *pgss_hash;
static HTAB *pgss_query_hash;
static HTAB *pgss_plan_hash;
static HTAB* hash_init(const char *hash_name, int key_size, int entry_size, int hash_size);
@@ -71,8 +70,7 @@ pgss_startup(void)
}
pgss_hash = hash_init("pg_stat_monitor: bucket hashtable", sizeof(pgssHashKey), sizeof(pgssEntry), MAX_BUCKET_ENTRIES);
pgss_query_hash = hash_init("pg_stat_monitor: query hashtable", sizeof(pgssQueryHashKey), sizeof(pgssQueryEntry),500000);
pgss_plan_hash = hash_init("pg_stat_monitor: plan hashtable", sizeof(pgssPlanHashKey), sizeof(pgssPlanEntry), MAX_BUCKET_ENTRIES);
pgss_query_hash = hash_init("pg_stat_monitor: query hashtable", sizeof(pgssQueryHashKey), sizeof(pgssQueryEntry),MAX_BUCKET_ENTRIES);
LWLockRelease(AddinShmemInitLock);
@@ -83,12 +81,6 @@ pgss_startup(void)
on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
}
HTAB*
pgsm_get_plan_hash(void)
{
return pgss_plan_hash;
}
pgssSharedState*
pgsm_get_ss(void)
{
@@ -101,6 +93,12 @@ pgsm_get_hash(void)
return pgss_hash;
}
HTAB*
pgsm_get_query_hash(void)
{
return pgss_query_hash;
}
/*
* shmem_shutdown hook: Dump statistics into file.
*
@@ -128,33 +126,11 @@ hash_memsize(void)
size = MAXALIGN(sizeof(pgssSharedState));
size += MAXALIGN(MAX_QUERY_BUF);
size = add_size(size, hash_estimate_size(MAX_BUCKET_ENTRIES, sizeof(pgssEntry)));
size = add_size(size, hash_estimate_size(MAX_BUCKET_ENTRIES, sizeof(pgssPlanEntry)));
size = add_size(size, hash_estimate_size(500000, sizeof(pgssQueryEntry)));
size = add_size(size, hash_estimate_size(MAX_BUCKET_ENTRIES, sizeof(pgssQueryEntry)));
return size;
}
pgssPlanEntry *
hash_plan_entry_alloc(pgssSharedState *pgss, pgssPlanHashKey *key)
{
pgssPlanEntry *entry = NULL;
bool found = false;
if (hash_get_num_entries(pgss_plan_hash) >= MAX_BUCKET_ENTRIES)
return NULL;
/* Find or create an entry with desired hash code */
entry = (pgssPlanEntry *) hash_search(pgss_plan_hash, key, HASH_ENTER, &found);
if (!found)
{
memset(&entry->plan_info, 0, sizeof(PlanInfo));
SpinLockInit(&entry->mutex);
}
if (entry == NULL)
elog(FATAL, "%s", "pg_stat_monitor: out of memory");
return entry;
}
pgssEntry *
hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key,int encoding)
{
@@ -163,7 +139,7 @@ hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key,int encoding)
if (hash_get_num_entries(pgss_hash) >= MAX_BUCKET_ENTRIES)
{
elog(DEBUG2, "%s", "pg_stat_monitor: out of memory");
elog(DEBUG1, "%s", "pg_stat_monitor: out of memory");
return NULL;
}
/* Find or create an entry with desired hash code */
@@ -181,7 +157,7 @@ hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key,int encoding)
entry->encoding = encoding;
}
if (entry == NULL)
elog(FATAL, "%s", "pg_stat_monitor: out of memory");
elog(DEBUG1, "%s", "pg_stat_monitor: out of memory");
return entry;
}
@@ -199,7 +175,7 @@ hash_query_entry_dealloc(int bucket)
hash_seq_init(&hash_seq, pgss_query_hash);
while ((entry = hash_seq_search(&hash_seq)) != NULL)
{
if (entry->key.bucket_id == bucket)
if (entry->key.bucket_id == bucket || bucket < 0)
entry = hash_search(pgss_query_hash, &entry->key, HASH_REMOVE, NULL);
}
}
@@ -247,8 +223,8 @@ hash_entry_reset()
}
/* Caller must accuire lock */
bool
hash_create_query_entry(uint64 bucket_id, uint64 queryid)
pgssQueryEntry*
hash_create_query_entry(uint64 bucket_id, uint64 queryid, uint64 dbid, uint64 userid, uint64 ip)
{
pgssQueryHashKey key;
pgssQueryEntry *entry;
@@ -256,14 +232,17 @@ hash_create_query_entry(uint64 bucket_id, uint64 queryid)
key.queryid = queryid;
key.bucket_id = bucket_id;
key.dbid = dbid;
key.userid = userid;
key.ip = ip;
entry = (pgssQueryEntry *) hash_search(pgss_query_hash, &key, HASH_ENTER, &found);
return (entry != NULL);
return entry;
}
/* Caller must accuire lock */
bool
hash_find_query_entry(uint64 bucket_id, uint64 queryid)
pgssQueryEntry*
hash_find_query_entry(uint64 bucket_id, uint64 queryid, uint64 dbid, uint64 userid, uint64 ip)
{
pgssQueryHashKey key;
pgssQueryEntry *entry;
@@ -271,10 +250,13 @@ hash_find_query_entry(uint64 bucket_id, uint64 queryid)
key.queryid = queryid;
key.bucket_id = bucket_id;
key.dbid = dbid;
key.userid = userid;
key.ip = ip;
/* Lookup the hash table entry with shared lock. */
entry = (pgssQueryEntry *) hash_search(pgss_query_hash, &key, HASH_FIND, &found);
return ((entry != NULL) && found);
return entry;
}
bool