pgsql: Fix local pgstat entry leak on OOM during entry creation

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix local pgstat entry leak on OOM during entry creation
Date: 2026-08-07 05:24:12
Message-ID: E1wsD40-00000000XAu-0MBt@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix local pgstat entry leak on OOM during entry creation

When pgstat_init_entry() fails due to an OOM in the DSA allocation,
pgstat_get_entry_ref() cleaned up the shared hashtable but forgot to
remove the local reference that pgstat_get_entry_ref_cached() had
already inserted into pgStatEntryRefHash.

Missing this cleanup would leave a backend with a stale local cache
entry whose entry_ref points to a NULL shared_stats. If
pgstat_gc_entry_refs() runs with this reference still around, it would
crash due to a pointer dereference.

The local reference is now removed before removing the shared entry,
the order being sensitive to pending interrupts.

Oversight in 8191e0c16a03.

Author: Niall Newman <nn(at)turacolabs(dot)com>
Discussion: https://postgr.es/m/2FDAA194-9CF3-4FD7-A450-F1A4BEB125F6@turacolabs.com
Backpatch-through: 15

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/89fc6a7c12f2ee6d75e6ebab06844edd4737c348

Modified Files
--------------
src/backend/utils/activity/pgstat_shmem.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-08-07 05:52:21 pgsql: Fix shared refcount leak in pgstat entry acquisition
Previous Message Fujii Masao 2026-08-07 03:32:27 pgsql: Make 030_pg_recvlogical robust against PID reuse