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 <[email protected]>
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/
/*
 * sh_oom_test.c - reproduce simplehash inconsistency after an allocation
 * failure inside SH_GROW (PostgreSQL 16 simplehash.h).
 *
 * The element array allocator fails on demand (longjmp, like an
 * elog(ERROR) "out of memory" would do in the backend).  After the
 * failure the table must still be usable: tb->size must match the array
 * really allocated, iteration must visit exactly tb->members entries and
 * new inserts must work.
 *
 * Build (frontend, against a configured PostgreSQL source tree):
 *   gcc -g -fsanitize=address -DFRONTEND -I$PGSRC/src/include sh_oom_test.c \
 *       -L$PGSRC/src/common -L$PGSRC/src/port -lpgcommon -lpgport -o sh_oom_test
 */
#include "postgres_fe.h"
#include <setjmp.h>

#include "common/hashfn.h"
#include "common/logging.h"

typedef struct TestEntry
{
	uint32		key;
	char		status;
} TestEntry;

static jmp_buf oom_jmp;
static bool fail_next_alloc = false;
static uint64 allocated_elems = 0;	/* size of the last array really allocated */

#define SH_PREFIX th
#define SH_ELEMENT_TYPE TestEntry
#define SH_KEY_TYPE uint32
#define SH_KEY key
#define SH_HASH_KEY(tb, key) murmurhash32(key)
#define SH_EQUAL(tb, a, b) ((a) == (b))
#define SH_SCOPE static inline
#define SH_RAW_ALLOCATOR pg_malloc0
#define SH_USE_NONDEFAULT_ALLOCATOR
#define SH_DECLARE
#define SH_DEFINE
#include "lib/simplehash.h"

static inline void *
th_allocate(th_hash *tb, Size size)
{
	if (fail_next_alloc)
	{
		fail_next_alloc = false;
		longjmp(oom_jmp, 1);	/* simulates ereport(ERROR, out of memory) */
	}
	allocated_elems = size / sizeof(TestEntry);
	return pg_malloc0(size);
}

static inline void
th_free(th_hash *tb, void *pointer)
{
	pg_free(pointer);
}

int
main(void)
{
	th_hash    *tb = th_create(16, NULL);
	th_iterator it;
	TestEntry  *e;
	volatile uint32 next_key = 1;
	uint64		size_before;
	uint32		count = 0;
	bool		found;
	int			rc = 0;

	setvbuf(stdout, NULL, _IONBF, 0);

	/* fill the table up to its grow threshold */
	while (tb->members < tb->grow_threshold)
		th_insert(tb, next_key++, &found);

	size_before = tb->size;
	printf("before: size=%llu members=%u allocated=%llu\n",
		   (unsigned long long) tb->size, tb->members,
		   (unsigned long long) allocated_elems);

	/* next insert must grow the table: make that allocation fail */
	fail_next_alloc = true;
	if (setjmp(oom_jmp) == 0)
	{
		th_insert(tb, next_key, &found);
		printf("unexpected: grow did not fail\n");
		return 2;
	}
	printf("after OOM in SH_GROW: size=%llu sizemask=0x%x members=%u allocated=%llu\n",
		   (unsigned long long) tb->size, tb->sizemask, tb->members,
		   (unsigned long long) allocated_elems);

	if (tb->size != allocated_elems || tb->size != size_before)
	{
		printf("FAIL: tb->size (%llu) does not match the allocated array (%llu)\n",
			   (unsigned long long) tb->size, (unsigned long long) allocated_elems);
		rc = 1;
	}

	/* what pgstat_release_matching_entry_refs() does at backend exit */
	th_start_iterate(tb, &it);
	while ((e = th_iterate(tb, &it)) != NULL)
		count++;
	printf("iteration visited %u entries (members=%u)\n", count, tb->members);
	if (count != tb->members)
		rc = 1;

	/* the table must remain usable: the next grow succeeds */
	th_insert(tb, next_key++, &found);
	printf("insert after OOM: size=%llu members=%u\n",
		   (unsigned long long) tb->size, tb->members);

	th_destroy(tb);
	printf(rc == 0 ? "PASS\n" : "FAIL\n");
	return rc;
}
From a46ef62207c5befeec20a348fac0ac847d47a0d0 Mon Sep 17 00:00:00 2001
From: Darold <[email protected]>
Date: Fri, 20 Nov 2026 18:21:37 +0300
Subject: [PATCH] simplehash: preserve consistency in case of OOM (backport to
 16)

Backport of upstream commit b282fa88df ("simplehash: preserve
consistency in case of OOM."), present in v17 and later but not
back-patched to REL_16_STABLE.

Split SH_COMPUTE_PARAMETERS() into SH_COMPUTE_SIZE() and
SH_UPDATE_PARAMETERS(), and only update the table parameters once the
allocation has succeeded, in both SH_CREATE() and SH_GROW().  Only the
functional hunks of the upstream commit are included.
---
 src/include/lib/simplehash.h | 42 ++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/src/include/lib/simplehash.h b/src/include/lib/simplehash.h
index 1dc817ac..60fe6103 100644
--- a/src/include/lib/simplehash.h
+++ b/src/include/lib/simplehash.h
@@ -128,7 +128,8 @@
 #define SH_STAT SH_MAKE_NAME(stat)
 
 /* internal helper functions (no externally visible prototypes) */
-#define SH_COMPUTE_PARAMETERS SH_MAKE_NAME(compute_parameters)
+#define SH_COMPUTE_SIZE SH_MAKE_NAME(compute_size)
+#define SH_UPDATE_PARAMETERS SH_MAKE_NAME(update_parameters)
 #define SH_NEXT SH_MAKE_NAME(next)
 #define SH_PREV SH_MAKE_NAME(prev)
 #define SH_DISTANCE_FROM_OPTIMAL SH_MAKE_NAME(distance)
@@ -303,11 +304,11 @@ SH_SCOPE void SH_STAT(SH_TYPE * tb);
 #endif
 
 /*
- * Compute sizing parameters for hashtable. Called when creating and growing
- * the hashtable.
+ * Compute allocation size for hashtable. Result can be passed to
+ * SH_UPDATE_PARAMETERS.
  */
-static inline void
-SH_COMPUTE_PARAMETERS(SH_TYPE * tb, uint64 newsize)
+static inline uint64
+SH_COMPUTE_SIZE(uint64 newsize)
 {
 	uint64		size;
 
@@ -325,6 +326,18 @@ SH_COMPUTE_PARAMETERS(SH_TYPE * tb, uint64 newsize)
 	if (unlikely((((uint64) sizeof(SH_ELEMENT_TYPE)) * size) >= SIZE_MAX / 2))
 		sh_error("hash table too large");
 
+	return size;
+}
+
+/*
+ * Update sizing parameters for hashtable. Called when creating and growing
+ * the hashtable.
+ */
+static inline void
+SH_UPDATE_PARAMETERS(SH_TYPE * tb, uint64 newsize)
+{
+	uint64		size = SH_COMPUTE_SIZE(newsize);
+
 	/* now set size */
 	tb->size = size;
 	tb->sizemask = (uint32) (size - 1);
@@ -446,10 +459,11 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data)
 	/* increase nelements by fillfactor, want to store nelements elements */
 	size = Min((double) SH_MAX_SIZE, ((double) nelements) / SH_FILLFACTOR);
 
-	SH_COMPUTE_PARAMETERS(tb, size);
+	size = SH_COMPUTE_SIZE(size);
 
-	tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size);
+	tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * size);
 
+	SH_UPDATE_PARAMETERS(tb, size);
 	return tb;
 }
 
@@ -490,10 +504,15 @@ SH_GROW(SH_TYPE * tb, uint64 newsize)
 	Assert(oldsize != SH_MAX_SIZE);
 	Assert(oldsize < newsize);
 
-	/* compute parameters for new table */
-	SH_COMPUTE_PARAMETERS(tb, newsize);
+	newsize = SH_COMPUTE_SIZE(newsize);
 
-	tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size);
+	tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * newsize);
+
+	/*
+	 * Update parameters for new table after allocation succeeds to avoid
+	 * inconsistent state on OOM.
+	 */
+	SH_UPDATE_PARAMETERS(tb, newsize);
 
 	newdata = tb->data;
 
@@ -1176,7 +1195,8 @@ SH_STAT(SH_TYPE * tb)
 #undef SH_STAT
 
 /* internal function names */
-#undef SH_COMPUTE_PARAMETERS
+#undef SH_COMPUTE_SIZE
+#undef SH_UPDATE_PARAMETERS
 #undef SH_COMPARE_KEYS
 #undef SH_INITIAL_BUCKET
 #undef SH_NEXT
-- 
2.43.0

Reply via email to