From 383e2d9b0d24e02ca468b32f8bd3775bbecf1e86 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Tue, 1 Nov 2022 23:29:38 +1300 Subject: [PATCH v2 1/9] Allow palloc_extended(NO_OOM) in critical sections. Commit 4a170ee9e0e banned palloc() and similar in critical sections, because an allocation failure would produce a panic. Make an exception for allocation with NULL on failure, for code that has a backup plan. Discussion: https://postgr.es/m/CA%2BhUKGJHudZMG_vh6GiPB61pE%2BGgiBk5jxzd7inijqx5nEZLCw%40mail.gmail.com --- src/backend/utils/mmgr/mcxt.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index f526ca82c1..460fa9d6c0 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -1112,7 +1112,8 @@ MemoryContextAllocExtended(MemoryContext context, Size size, int flags) void *ret; Assert(MemoryContextIsValid(context)); - AssertNotInCriticalSection(context); + if ((flags & MCXT_ALLOC_NO_OOM) == 0) + AssertNotInCriticalSection(context); if (!((flags & MCXT_ALLOC_HUGE) != 0 ? AllocHugeSizeIsValid(size) : AllocSizeIsValid(size))) @@ -1267,7 +1268,8 @@ palloc_extended(Size size, int flags) MemoryContext context = CurrentMemoryContext; Assert(MemoryContextIsValid(context)); - AssertNotInCriticalSection(context); + if ((flags & MCXT_ALLOC_NO_OOM) == 0) + AssertNotInCriticalSection(context); if (!((flags & MCXT_ALLOC_HUGE) != 0 ? AllocHugeSizeIsValid(size) : AllocSizeIsValid(size))) @@ -1368,7 +1370,8 @@ repalloc_extended(void *pointer, Size size, int flags) AllocSizeIsValid(size))) elog(ERROR, "invalid memory alloc request size %zu", size); - AssertNotInCriticalSection(context); + if ((flags & MCXT_ALLOC_NO_OOM) == 0) + AssertNotInCriticalSection(context); /* isReset must be false already */ Assert(!context->isReset); -- 2.37.0 (Apple Git-136)