Skip to content

Commit 4863020

Browse files
Hamid Akhtarcodeforall
authored andcommitted
PG-646: pg_stat_monitor hangs in pgsm_store
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.
1 parent 0a8ac38 commit 4863020

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

pg_stat_monitor.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,10 +1776,6 @@ pgsm_store(pgsmEntry * entry)
17761776

17771777
pgsm = pgsm_get_ss();
17781778

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

@@ -1882,6 +1878,11 @@ pgsm_store(pgsmEntry * entry)
18821878

18831879
if (shared_hash_entry == NULL)
18841880
{
1881+
LWLockRelease(pgsm->lock);
1882+
1883+
if (DsaPointerIsValid(dsa_query_pointer))
1884+
dsa_free(query_dsa_area, dsa_query_pointer);
1885+
18851886
/*
18861887
* Out of memory; report only if the state has changed now.
18871888
* Otherwise we risk filling up the log file with these message.
@@ -1890,18 +1891,16 @@ pgsm_store(pgsmEntry * entry)
18901891
{
18911892
pgsm->pgsm_oom = true;
18921893

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

1900-
LWLockRelease(pgsm->lock);
1901-
1902-
if (DsaPointerIsValid(dsa_query_pointer))
1903-
dsa_free(query_dsa_area, dsa_query_pointer);
1904-
19051904
return;
19061905
}
19071906
else

0 commit comments

Comments
 (0)