Skip to content

Commit

Permalink
PG-646: pg_stat_monitor hangs in pgsm_store
Browse files Browse the repository at this point in the history
A potential lock contention could've been caused when an OOM warning
was being emitted by the pgsm_store function. This could lead the
pg_store_error function calling pgsm_store function and thereby trying
to acquire and exclusive lock when a shared was already by the same
process. This warning is now guarded by protection block.
  • Loading branch information
Hamid Akhtar authored and codeforall committed Nov 24, 2023
1 parent 0a8ac38 commit 4863020
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,10 +1776,6 @@ pgsm_store(pgsmEntry * entry)

pgsm = pgsm_get_ss();

/*
* We should lock the hash table here what if the bucket is removed; e.g.
* reset is called - HAMID
*/
prev_bucket_id = pg_atomic_read_u64(&pgsm->current_wbucket);
bucketid = get_next_wbucket(pgsm);

Expand Down Expand Up @@ -1882,6 +1878,11 @@ pgsm_store(pgsmEntry * entry)

if (shared_hash_entry == NULL)
{
LWLockRelease(pgsm->lock);

if (DsaPointerIsValid(dsa_query_pointer))
dsa_free(query_dsa_area, dsa_query_pointer);

/*
* Out of memory; report only if the state has changed now.
* Otherwise we risk filling up the log file with these message.
Expand All @@ -1890,18 +1891,16 @@ pgsm_store(pgsmEntry * entry)
{
pgsm->pgsm_oom = true;

ereport(WARNING,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
PGSM_DISABLE_ERROR_CAPUTRE();
{
ereport(WARNING,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
} PGSM_END_DISABLE_ERROR_CAPTURE();
}

LWLockRelease(pgsm->lock);

if (DsaPointerIsValid(dsa_query_pointer))
dsa_free(query_dsa_area, dsa_query_pointer);

return;
}
else
Expand Down

0 comments on commit 4863020

Please sign in to comment.