From 57f4711d444c844071ba2c237458a690882021f6 Mon Sep 17 00:00:00 2001 From: Dennis Kovalenko Date: Sat, 27 Jul 2024 12:45:08 +0000 Subject: [PATCH] Force IsGroupInRedZone to do all checks --- src/backend/utils/resgroup/resgroup.c | 29 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/backend/utils/resgroup/resgroup.c b/src/backend/utils/resgroup/resgroup.c index 65e52db46ca9..3d831f9c2b24 100644 --- a/src/backend/utils/resgroup/resgroup.c +++ b/src/backend/utils/resgroup/resgroup.c @@ -4614,28 +4614,33 @@ IsGroupInRedZone(void) /* * IsGroupInRedZone is called frequently, we should put the * condition which returns with higher probability in front. - * - * safe: global shared memory is not in redzone + * + * We're in RedZone if number of remained chunks on segment host + * is less than totalChunks * (100 - runaway_detector_activation_percent) */ remainGlobalSharedMem = (uint32) pg_atomic_read_u32(&pResGroupControl->freeChunks); safeChunksThreshold100 = (uint32) pg_atomic_read_u32(&pResGroupControl->safeChunksThreshold100); - if (remainGlobalSharedMem * 100 >= safeChunksThreshold100) - return false; + if (remainGlobalSharedMem * 100 < safeChunksThreshold100) + return true; AssertImply(slot != NULL, group != NULL); if (!slot) return false; - /* safe: slot memory is not used up */ - if (slot->memQuota > slot->memUsage) - return false; + /* We're in RedZone if group's shared memory is exceeded */ + if (group->memSharedGranted < group->memSharedUsage) + return true; - /* safe: group shared memory is not in redzone */ - if (group->memSharedGranted > group->memSharedUsage) - return false; + /* + * This check is here for the cases when a single process + * solely consumes the entire group quota. Not sure if it's + * a common case, so let's put it in the end + */ + if (slot->memUsage > group->memQuotaGranted + group->memSharedGranted) + return true; - /* memory usage in this group is in RedZone */ - return true; + /* All checks are passed, we're not in RedZone */ + return false; } /*