Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2DTv-00Eolb-GJ for pgsql-general@arkaria.postgresql.org; Tue, 08 Apr 2025 18:15:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u2DTt-00DfwX-Sg for pgsql-general@arkaria.postgresql.org; Tue, 08 Apr 2025 18:15:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2DTt-00Dfw1-6v for pgsql-general@lists.postgresql.org; Tue, 08 Apr 2025 18:15:29 +0000 Received: from mail-vk1-xa2e.google.com ([2607:f8b0:4864:20::a2e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u2DTq-004BTr-2T for pgsql-general@lists.postgresql.org; Tue, 08 Apr 2025 18:15:29 +0000 Received: by mail-vk1-xa2e.google.com with SMTP id 71dfb90a1353d-525b44b7720so2671964e0c.0 for ; Tue, 08 Apr 2025 11:15:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; t=1744136125; x=1744740925; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=PPmopnCkgt/Ct6CFXAp/mKhbv/1SDIK2FaS+fzQFpRQ=; b=Uf8gPDGE58v7/QpVQsZQBlDn4Gkvv7VqfcD/Fl9wqdJRKTuSBY5rlphUwlRJIESKvw piTKTxmp+ww9p/Hw2Rl0tZ95Zi89F86N9R3HH6xMK3Pw0R7yE8BQIi494LD0kQ4nSRz3 2zfCkZ4ee0I2BMQsZG5+2/JDxQpoq2y74HXSPKcWfo+K6GeCXBKZA0vBhViniUxS+XPD A9TEgCKyzHb6BSPghzazvhTk095Dmanukw9N0K3Dr5EgrGNw5ACan3G682J/UfNP4f6n dSWNNyhIig2cxHkUTTynJggnYfVKLs1EC+UKTpfftkKnGCDecMPVwKtrr6OirxkeQV8K f24w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744136125; x=1744740925; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PPmopnCkgt/Ct6CFXAp/mKhbv/1SDIK2FaS+fzQFpRQ=; b=V4EdZETiI/S8HkzIWwOEa6QtTfQy4/THWIK9NT19/px/zSywjNhBiS063QlJ14rZvj Ig9O2gn4+H60mHKwcC6/zrgQTYDLP3+9ktR4+l2b3WrgHV/76ApISSEjoVM7xT3ZRHul NkhJfn0M4bKypox3gigt2Y533pCLeBwdStfgmPowIMWgTPilv9aZepwFmMEOcUHR+Jgh LCROYNg/dwhcvtiqPswlRRL0XFayy2+rVJ4pCLlXmUF2qvWYsVQ5cI453kDA8gZ7rwQG HmchPLX/OcrDPeCpJu8wuOvrMtYGnTa+ABS0WCcWXWBaR34+I+vSIXejV9oEEdvVgbDb Ysrg== X-Forwarded-Encrypted: i=1; AJvYcCU/z+U3sQPD4FDW3vwV+ZznuYgIL3/tI5H+/vb+nYmML540uXIDbO956sgIQbgioE2CkvXo2hP6naVW0eEj@lists.postgresql.org X-Gm-Message-State: AOJu0Yx7z2GvAR6OfVeiQsSvO2xKWCcgb3COAeA7VXcP68po5eyBKTNO xmP8aJ0ITvBos7Uy3S0oghe+Mr6vNRgO8oU1j7u1JwHZyKSsgboWjP1l5DWCCK4= X-Gm-Gg: ASbGncuaLeQ7yVf/u/xx9uowdI1hm2Dv1uMSx4oD41vtw/c4qCmYzf/I2al9RROveGu oljnshX41zwoa/MNw2ARBwUF0Ba6fySzyqQiQuZ+PPDMkCarrZ4gsXQlEWpUssKtwBR2hY3SL9f iFo5xg73qXztq0p0YTHeSK19SojcBPqWQU/sg5cVotYCMkB+w8ZeypHXa/RPIWFJ4o35TLjCEj7 OAup3bR29tu9KpKfi8MlvradLn+fs/GXWiGN+myRyamAHRw+jH2k66WzHGGYJx6K8vayDqJ1xxB EXgQvfANTH4lofcxYghGQwh92ECoUVQMzXqhmepf3U7ahXIBoqKyjvxY0ZJtBWD6XU+p0/wI6Z3 vCFMqIuhJFBqbw2hfmI7QZxQb X-Google-Smtp-Source: AGHT+IFPjAXvYchxH8IJODeB/S1KYrytFuosJuXMzcoxj/W2gWu432tsSTj74p3G7W/oyAHXpkf9dQ== X-Received: by 2002:a05:6122:886:b0:516:18cd:c1fc with SMTP id 71dfb90a1353d-5276458254amr11505804e0c.8.1744136124777; Tue, 08 Apr 2025 11:15:24 -0700 (PDT) Received: from [192.168.4.41] (162-239-31-113.lightspeed.dybhfl.sbcglobal.net. [162.239.31.113]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5279b5b28ffsm490130e0c.22.2025.04.08.11.15.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 08 Apr 2025 11:15:24 -0700 (PDT) Message-ID: <2992983d-b237-4cc0-91d7-e3bc8de25006@joeconway.com> Date: Tue, 8 Apr 2025 14:15:23 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Kubernetes, cgroups v2 and OOM killer - how to avoid? To: Ancoron Luciferis , pgsql-general@lists.postgresql.org References: Content-Language: en-US From: Joe Conway Autocrypt: addr=mail@joeconway.com; keydata= xsFNBEpXMCsBEADDnXUQzjlyi/cX02Gtdy2CLcroE5CsC7DJKdOBDbfgn0kfiIYoV5JniG4l VyzZUodY8yUAagqLYolh0UkBzs9N+qkm7erde4ypw3jzVQ37Buz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wAQ//bjcWnZg/jjRQ9gbZUGMqniItZYRglBMKIqt4 Fia379JmHwTvavnFkJ8XMZ56UB0FIrgS+sUkRH6cPRQR+7Qi392LD021DXgSsz9CwFHjFyBG HwLEOTRcfYQbtJy0shHDJB4aQTOX3ERDH1PsvJNuevmQMzS0DWFav9+xMz9rKP4N+HffoBIZ E0C1xIE43nD4eLsbycte9sVIrmlNuUti3qUxJAQw8HwfJ6ZbBInHxquApR16uD1u99o6Xlnd FrDlY22tRmHCM0bR81GfGNdcU3Uo+rG/R/k4qa7s9/dgKvMbyH3fHhp/ceKag80Xo8IFurRl 0ZJP3sHJ2QDHCVLat7jRZ+43hi1WlIhFbrgn6IyI0i7XR/W8JjrC5MsKq4TUwGH077sU/kcH YebVJZRbUUst2hAGHDFVBcG12qoKf+ltL9qXJc1y7BGeCoUW6QjOpljpq6ZL4FQUsM0RSRjs 5egE3szPcIf5SyPK6WDOApoAq6M7BBFMGDZwEylYMtr0YekA1u86UA9D2xwLHEbBBp/uiby1 c9JbPJ1Pn8zJP8WZNeRw4Q9TtqVK09+oLirMUSpIDd6KdZ1VgRxOK2re7tjDvkVuYsSrsiJ+ 1iJNEnp9iK0ok0DlJpSCe6KhkxpaTdeoWMXdKuJWec0NIqoAd54ZgBPnr+UPxTixgPq/p6Q= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/8/25 13:58, Ancoron Luciferis wrote: > On 2025-04-07 15:21, Joe Conway wrote: >> On 4/5/25 07:53, Ancoron Luciferis wrote: >>> I've been investigating this topic every now and then but to this day >>> have not come to a setup that consistently leads to a PostgreSQL backend >>> process receiving an allocation error instead of being killed externally >>> by the OOM killer. >>> >>> Why this is a problem for me? Because while applications are accessing >>> their DBs (multiple services having their own DBs, some high-frequency), >>> the whole server goes into recovery and kills all backends/connections. >>> >>> While my applications are written to tolerate that, it also means that >>> at that time, esp. for the high-frequency apps, events are piling up, >>> which then leads to a burst as soon as connectivity is restored. This in >>> turn leads to peaks in resource usage in other places (event store, >>> in-memory buffers from apps, ...), which sometimes leads to a series of >>> OOM killer events being triggered, just because some analytics query >>> went overboard. >>> >>> Ideally, I'd find a configuration that only terminates one backend but >>> leaves the others working. >>> >>> I am wondering whether there is any way to receive a real ENOMEM inside >>> a cgroup as soon as I try to allocate beyond its memory.max, instead of >>> relying on the OOM killer. >>> >>> I know the recommendation is to have vm.overcommit_memory set to 2, but >>> then that affects all workloads on the host, including critical infra >>> like the kubelet, CNI, CSI, monitoring, ... >>> >>> I have already gone through and tested the obvious: >>> >>> https://www.postgresql.org/docs/current/kernel-resources.html#LINUX- >>> MEMORY-OVERCOMMIT >> >> Importantly vm.overcommit_memory set to 2 only matters when memory is >> constrained at the host level. >> >> As soon as you are running in a cgroup with a hard memory limit, >> vm.overcommit_memory is irrelevant. >> >> You can have terabytes of free memory on the host, but if cgroup memory >> usage exceeds memory.limit (cgv1) or memory.max (cgv2) the OOM killer >> will pick the process in the cgroup with the highest oom_score and whack >> it. >> >> Unfortunately there is no equivalent to vm.overcommit_memory within the >> cgroup. >> >>> And yes, I know that Linux cgroups v2 memory.max is not an actual hard >>> limit: >>> >>> https://www.kernel.org/doc/html/latest/admin-guide/cgroup- >>> v2.html#memory-interface-files >> >> Read that again -- memory.max *is* a hard limit (same as memory.limit in >> cgv1). >> >>   "memory.max >> >>     A read-write single value file which exists on non-root cgroups. The >>     default is “max”. >> >>     Memory usage hard limit. This is the main mechanism to limit memory >>     usage of a cgroup. If a cgroup’s memory usage reaches this limit and >>     can’t be reduced, the OOM killer is invoked in the cgroup." > > Yes, I know it says "hard limit", but then any app still can go beyond > (might just be on me here to assume any "hard limit" to imply an actual > error when trying to go beyond). The OOM killer then will kick in > eventually, but not in any way that any process inside the cgroup could > prevent. So there is no signal that the app could react to saying "hey, > you just went beyond what you're allowed, please adjust before I kill you". No, that really is a hard limit and the OOM killer is *really* fast. Once that is hit there is no time to intervene. The soft limit (memory.high) is the one you want for that. Or you can monitor PSI and try to anticipate problems, but that is difficult at best. If you want to see how that is done, check out senpai: https://github.com/facebookincubator/senpai/blob/main/README.md >> If you want a soft limit use memory.high. >> >>   "memory.high >> >>     A read-write single value file which exists on non-root cgroups. The >>     default is “max”. >> >>     Memory usage throttle limit. If a cgroup’s usage goes over the high >>     boundary, the processes of the cgroup are throttled and put under >>     heavy reclaim pressure. >> >>     Going over the high limit never invokes the OOM killer and under >>     extreme conditions the limit may be breached. The high limit should >>     be used in scenarios where an external process monitors the limited >>     cgroup to alleviate heavy reclaim pressure. >> >> You want to be using memory.high rather than memory.max. > > Hm, so solely relying on reclaim? I think that'll just get the whole > cgroup into ultra-slow mode and would not actually prevent too much > memory allocation. While this may work out just fine for the PostgreSQL > instance, it'll for sure have effects on the other workloads on the same > node (which I have apparently, more PG instances). > > Apparently, I also don't see a way to even try this out in a Kubernetes > environment, since there doesn't seem to be a way to set this field > through some workload manifests field. Yeah, that part I have no idea about. I quit looking at kubernetes related things about 3 years ago. Although, this link seems to indicate there is a way related to how it does QoS: https://kubernetes.io/blog/2023/05/05/qos-memory-resources/#:~:text=memory.high%20formula >> Also, I don't know what kubernetes recommends these days, but it used to >> require you to disable swap. In more recent versions of kubernetes you >> are able to run with swap enabled but I have no idea what the default is >> -- make sure you run with swap enabled. > > Yes, this is what I wanna try out next. Seriously -- this is *way* more than half the battle. If you do nothing else, be sure to do this... -- Joe Conway PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com