Re: DSA_ALLOC_NO_OOM vs dsm_create ERROR leaving a half-initialized pgstats hash entry

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Grigorev Jurij <ju(dot)grigorev(at)ftdata(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: DSA_ALLOC_NO_OOM vs dsm_create ERROR leaving a half-initialized pgstats hash entry
Date: 2026-09-17 03:44:41
Message-ID: aqtiKTvl519bu8-V@paquier.xyz
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Sep 17, 2026 at 11:48:26AM +0900, Michael Paquier wrote:
> + /*
> + * Allocate the stats body before inserting a hash entry. Creating a
> + * new DSA segment can raise ERROR (e.g. ENOSPC on posix shm); doing
> + * that after the insert would leave a live hash entry with an
> + * invalid body.
> + */
> + chunk = pgstat_alloc_entry_body(kind);
>
> Hmm. This still leaves a local entry_ref if pgstat_alloc_entry_body()
> itself fails. Compared to the case of a corrupted shmem area. I think
> that I can live with that. And if I'm reading that right, the backend
> reference that may still be around self-heals on re-entry if a backend
> tries to insert again the same entry?

Ah. 4069df21beb8 points exactly at that case. Perhaps we should
extend pgstat_gc_entry_refs() so as it is able to handle gracefully a
partial reference then? I would imagine something like that, that
forces a release of the local entry if we don't have a shared_entry,
as of:
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -839,6 +839,15 @@ pgstat_gc_entry_refs(void)
Assert(!entry_ref->shared_stats ||
entry_ref->shared_stats->magic == 0xdeadbeef);

+ /* NULL shared_entry marks a partial reference */
+ if (entry_ref->shared_entry == NULL)
+ {
+ Assert(entry_ref->shared_stats == NULL);
+ Assert(entry_ref->pending == NULL);
+ pgstat_release_entry_ref(ent->key, entry_ref, false);
+ continue;
+ }

What do you think about the attached? That would be an independent
safety measure.
--
Michael

Attachment Content-Type Size
0001-pgstat-Add-safety-measure-for-partially-initialized-.patch text/plain 1.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message ZizhuanLiu X-MAN 2026-09-17 03:53:36 Re: Optimize MCV stats for sortable types and utilize sorted-order properties
Previous Message Xuneng Zhou 2026-09-17 03:41:43 Re: WAIT FOR NO_THROW option could use some documentation