Re: Backpatch for fix simplehash to preserve consistency in case of OOM

From: Gilles Darold <gilles(at)darold(dot)net>
To: Andres Freund <andres(at)anarazel(dot)de>, Gilles Darold <gilles(at)darold(dot)net>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Jeff Davis <pgsql(at)j-davis(dot)com>
Subject: Re: Backpatch for fix simplehash to preserve consistency in case of OOM
Date: 2026-11-20 18:36:23
Message-ID: fd4522c6-0f91-4150-bee8-602aa2de09ad@darold.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Le 20/11/2026 à 20:09, Andres Freund a écrit :
> Hi,
>
> On 2026-11-20 19:59:09 +0300, Gilles Darold wrote:
>> I have experienced a PG 16.14 backend crashed with SIGSEGV during an
>> out-of-memory episode. Analysis of the core dump shows that this is the bug
>> fixed on master by commit b282fa88df ("simplehash: preserve consistency
>> in case of OOM.", 2023-11-17). That commit is in v17 and later, but it was
>> never back-patched to REL_16_STABLE. Here are the details of the commit
>> message:
> I think Jeff didn't backpatch it because the use where that he newly
> introduced that could be problematic wasn't in 16:
>
> Author: Jeff Davis <jdavis(at)postgresql(dot)org>
> Branch: master Release: REL_17_BR [f26c2368d] 2023-11-14 17:53:30 -0800
>
> Add cache for recomputeNamespacePath().
>
>
> Can you provide a bit more detail about what you encountered?
>
> Greetings,
>
> Andres Freund

The computer was very short of commit space (Committed_AS at
CommitLimit), so a lot of allocations failed with "out of memory".

  ERROR:  out of memory
  DETAIL:  Failed on request of size 66125824 in memory context
"Backend Status Snapshot".

When the client went away, the backend crashed on exit:

  LOG:  server process (PID 2990127) was terminated by signal 11:
Segmentation fault
  LOG:  all server processes terminated; reinitializing

Stack trace of the core dump:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000008f42a1 in pgstat_release_matching_entry_refs ()

===== 1. bt
#0  0x00000000008f42a1 in pgstat_release_matching_entry_refs ()
#1  0x00000000008f4324 in pgstat_detach_shmem ()
#2  0x00000000008af941 in shmem_exit ()
#3  0x00000000008afacc in proc_exit_prepare ()
#4  0x00000000008afb8f in proc_exit ()
#5  0x00000000008d3087 in PostgresMain ()
#6  0x0000000000846029 in ServerLoop.lto_priv.0 ()
#7  0x000000000083d79d in PostmasterMain ()
#8  0x000000000051f0fc in main ()

The attached sh_oom_test.c is a frontend program that instantiates
simplehash with an allocator that fails on demand (a longjmp, which
mimics elog(ERROR)). It fills the table up to its grow threshold, makes
the grow allocation fail, and then iterates over the table and inserts
into it again.

Built with -fsanitize=address against the 16.14 simplehash.h:

  before: size=32 members=28 allocated=32
  after OOM in SH_GROW: size=64 sizemask=0x3f members=28 allocated=32
  FAIL: tb->size (64) does not match the allocated array (32)
  ERROR: AddressSanitizer: heap-buffer-overflow ...
  READ of size 1 ... in th_iterate simplehash.h:1040

With b282fa88df applied:

  before: size=32 members=28 allocated=32
  after OOM in SH_GROW: size=32 sizemask=0x1f members=28 allocated=32
  iteration visited 28 entries (members=28)
  insert after OOM: size=64 members=29
  PASS

I've attached the patch I use for backport. Builds without warnings
(--enable-cassert --enable-debug) and make check passes (All 224 tests
passed);

--
Gilles Darold
http://www.darold.net/

Attachment Content-Type Size
sh_oom_test.c text/x-csrc 3.3 KB
0001-simplehash-preserve-consistency-in-case-of-OOM-backport.patch text/x-patch 3.6 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Melanie Plageman 2026-11-20 18:53:59 Stop masking visibility information during WAL consistency checking
Previous Message Masahiko Sawada 2026-11-20 18:35:00 Re: Add a hook for handling logical decoding messages on subscribers.