Skip to content

Commit

Permalink
Issue - (percona#51): Postgres process is taking too much CPU.
Browse files Browse the repository at this point in the history
Jira: PG-141

There is lock conflict, so used LW_EXCLUSIVE instead of LW_SHARED. This
need to be investigated again and check the possibility to use a shared lock.
  • Loading branch information
ibrarahmad committed Oct 12, 2020
1 parent b23df84 commit 166ee0a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
13 changes: 2 additions & 11 deletions hash_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,24 +248,15 @@ hash_entry_dealloc(int bucket)
{
HASH_SEQ_STATUS hash_seq;
pgssEntry *entry;
pgssEntry **entries;
int i;
int nvictims = 0;

pgss->bucket_entry[bucket] = 0;

entries = palloc(hash_get_num_entries(pgss_hash) * sizeof(pgssEntry *));
hash_seq_init(&hash_seq, pgss_hash);
while ((entry = hash_seq_search(&hash_seq)) != NULL)
{
if (entry->key.bucket_id == bucket || bucket < 0)
entries[nvictims++] = entry;
entry = hash_search(pgss_hash, &entry->key, HASH_REMOVE, NULL);
}

for (i = 0; i < nvictims; i++)
entry = hash_search(pgss_hash, &entries[i]->key, HASH_REMOVE, NULL);

pfree(entries);
}

/*
Expand Down Expand Up @@ -332,7 +323,7 @@ hash_dealloc_object_entry(uint64 queryid, char *objects)

key.queryid = queryid;

LWLockAcquire(pgss->lock, LW_SHARED);
LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
entry = (pgssObjectEntry *) hash_search(pgss_object_hash, &key, HASH_FIND, NULL);
if (entry != NULL)
{
Expand Down
3 changes: 3 additions & 0 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,12 +875,15 @@ static void pgss_store(const char *query, uint64 queryId,
Datum
pg_stat_monitor_reset(PG_FUNCTION_ARGS)
{
pgssSharedState *pgss = pgsm_get_ss();
/* Safety check... */
if (!IsHashInitialize())
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("pg_stat_monitor: must be loaded via shared_preload_libraries")));
LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
hash_entry_dealloc(-1);
LWLockRelease(pgss->lock);
PG_RETURN_VOID();
}

Expand Down

0 comments on commit 166ee0a

Please sign in to comment.