Backpatch for fix simplehash to preserve consistency in case of OOM

Поиск
Список
Период
Сортировка
Искать

Backpatch for fix simplehash to preserve consistency in case of OOM

От:
Gilles Darold <gilles@darold.net>
Дата:
Hi,

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:

commit b282fa88df78a05643d2df84a2b8e81cd9c1705d
Author: Jeff Davis 
Date:   Fri Nov 17 13:58:16 2023 -0800

     simplehash: preserve consistency in case of OOM.

     Compute size first, then allocate, then update the structure.

     Previously, an out-of-memory when growing could leave the hashtable in
     an inconsistent state.

     Discussion: 
https://postgr.es/m/20231117201334.eyb542qr5yk4gilq@awork3.anarazel.de
     Reviewed-by: Andres Freund
     Reviewed-by: Gurjeet Singh

Is it possible to back-patch it?

-- 
Gilles Darold



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

От:
Andres Freund <andres@anarazel.de>
Дата:
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 
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


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

От:
Gilles Darold <gilles@darold.net>
Дата:
Вложения:
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 
> 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/
FAQ