pgsql: Improve pgstat_get_entry_ref_cached() behavior on out-of-memory

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Improve pgstat_get_entry_ref_cached() behavior on out-of-memory
Date: 2026-07-20 06:52:32
Message-ID: E1wlhrd-000EOz-0T@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Improve pgstat_get_entry_ref_cached() behavior on out-of-memory errors

A failure in allocating a new cache entry in the backend-level hash
table holding references to shared stats entries would leave the table
in an inconsistent state, crash or FATAL at session exit, depending on
if there are pending stats.

Rather than leaving things in an inconsistent state on OOM, the code is
switched to use MemoryContextAllocExtended(MCXT_ALLOC_NO_OOM), so as an
allocation failure leads to a cleanup of the hash table before issuing
the allocation error.

The problem is unlikely going to show up in practice, so no backpatch is
done. Note that shared memory is not impacted, only a backend-level
hash table.

Reported-by: Alex Masterov <amasterov(at)gmail(dot)com>
Discussion: https://postgr.es/m/CA+8z=zumV9sscgK=j1Es+-564maVoO9CMDdB9CsW9=FCziCj3w@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/16ebc196d8926823bc8d64931557b679cecd67aa

Modified Files
--------------
src/backend/utils/activity/pgstat_shmem.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2026-07-20 10:13:47 pgsql: Move code to get_tables_to_repack_partitioned
Previous Message Peter Eisentraut 2026-07-20 06:50:52 pgsql: Fix RLS checks for FOR PORTION OF leftover rows