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 1ujZoI-009NEY-FH for pgsql-performance@arkaria.postgresql.org; Wed, 06 Aug 2025 08:47:47 +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 1ujZoH-00ERkF-D5 for pgsql-performance@arkaria.postgresql.org; Wed, 06 Aug 2025 08:47:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ujZoG-00ERjy-R8 for pgsql-performance@lists.postgresql.org; Wed, 06 Aug 2025 08:47:45 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujZoD-0010bd-2G for pgsql-performance@lists.postgresql.org; Wed, 06 Aug 2025 08:47:43 +0000 Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-605b9488c28so11626781a12.2 for ; Wed, 06 Aug 2025 01:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754470061; x=1755074861; darn=lists.postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=og6v2aBfth1ocfVPSi07aVGMeL+rBHMx0HX8faIChx8=; b=Scsp8VnIJIl6ZpJksf/d+8BY4EuRLSiZVr6tyo+fAo3PndtX5s/6C+jOEc5aGtCroa eLymEhHIqwKmGyWDJBJ5X20EpFRC4NFUSlImWydahr+Pr9nFEHP0brlLYlGlTRDEJhNq 0Y6OtM19vpF8nsupzfkwl8+VrZyCOfwQQ/UKv1ztFqWzWiJDVt/2//59f1x2lSR5ErD8 +NIP6PLiH/2Curcu0zybyN11SY8QPT0CJWjrmRN8VxqCD7UuiF0WFjBs02Sf3pcDbI13 DRlzBWz7VQV0yJrqQpTZsdkVkpL/36xM3J4LZygSBbwbqKTg0y+HoIPeAZyOZjxUJ+3v 3zyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754470061; x=1755074861; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=og6v2aBfth1ocfVPSi07aVGMeL+rBHMx0HX8faIChx8=; b=LD+YvEGPSVsNgzWd/91nG1z7BkczbfddFj9hHkF+OT+ryyYj8O/HTzYADDtr0QtIQW pwnjAT8cVM9GZvaoRPDeWMiWm+pCjnXD++d1/4NGCn6+0nNphQt13UDoTVykzn/GC7fd s7wV49AvZOtuzPNS6saFufUUmusrbvldL1184vz9/ICEPNYly/ZaJvY7AsI+VmlctWa+ RbZncmZAcHVLFzF18uJnUD9fB/8csXCiptFS8+0wkOy5eD646RMqmQz2JJzYgqv3Amyb atFlqS7AXaII0OF4b/J6viHbY2A5YcYQF/CBXc2ed6wDJKZ94f8m3FNANKqoPqRythTz j3bg== X-Forwarded-Encrypted: i=1; AJvYcCVrRxEcINemeIg0FVIGF5WM/EHNWDffwwMLsJEYd1JOLcems9vETUK/Dx5HlrgIPhm0NVG6nOyJS2cziiaWWNz9hg==@lists.postgresql.org X-Gm-Message-State: AOJu0YxaWCrbTfvfpq//QHv8I23Mn852bm2wTvcZ6d6ldWeZ+YQb3wee r4OgEEYQfHmcebmjl6CfEDLhqUg3OjnR3XFpLlN5A5dBjKRd7H7GXyJT X-Gm-Gg: ASbGncsNpawjrJ3wKw2wWF4QNHGWd0PjQUrTWZWQgX8iQeMMvPpRCa6A1CcMpZLJyaL i9w4ofxxHJS/BB4aJ9BaSguworYz5q3z0H9bHxgFKOSBbe43VVzPU14zMbqlLjTJBHRFmbyRwge 4qdGiAXitWo9LsfpqZeoi+URbMV7GXgu9GC/YApqXdrh1w+jgO2x8CtOVT1ABT1Mqh2PGHJyrsJ IUGbDngT5TttuqhHptO+0i6jCjlTp6EqH0l63LJZKGBU9ZLf6K9pAupVcl8MiS8r3YPYUvuDxic AN+kqGI/N9lgfptVlFDFjq5qa2DEHd6yDg5BSt6T3XIl0gU4I3Mt67lNzEtTMf7WDKuCys6vpfK 6eQVsGIZOfxPyJyZO6yb/JlDtobE0ZjmeEr7aBMRe8MULwD7YJ4UITIA5nMB3XM1Bd3uq9dE0MF wxi39TydiVFDyqLQ7XpfLM5A/ndxJwBQLvdxVBA2+FrBKABHPiUOp1o+xfGQ== X-Google-Smtp-Source: AGHT+IGFmWZC/5CTsLGaDtMcAxn3FiN2VDkvlTRaySiCR9EM0Rdhu//ocWkLWSSIszNukmnGfz44zg== X-Received: by 2002:a17:907:3f8e:b0:ae9:c494:1ade with SMTP id a640c23a62f3a-af99048f104mr188710766b.53.1754470060855; Wed, 06 Aug 2025 01:47:40 -0700 (PDT) Received: from smtpclient.apple (2001-1c04-0681-7700-b99e-53e5-eebe-dfe2.cable.dynamic.v6.ziggo.nl. [2001:1c04:681:7700:b99e:53e5:eebe:dfe2]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-af96bc4fb0dsm476797066b.112.2025.08.06.01.47.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Aug 2025 01:47:40 -0700 (PDT) From: Frits Hoogland Message-Id: <5520EFB7-EDD1-42C1-8141-63EED777E026@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_A6A8FF39-3F27-42A6-B08A-D8D10F983AB7" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: Safe vm.overcommit_ratio for Large Multi-Instance PostgreSQL Fleet Date: Wed, 6 Aug 2025 10:47:29 +0200 In-Reply-To: <3633df47-5693-4871-9e12-bfbb68dd3313@joeconway.com> Cc: Priya V , pgsql-performance@lists.postgresql.org To: Joe Conway References: <3633df47-5693-4871-9e12-bfbb68dd3313@joeconway.com> X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_A6A8FF39-3F27-42A6-B08A-D8D10F983AB7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Joe,=20 Can you name any technical reason why not having swap for a database is = an actual bad idea? Memory always is limited. Swap was invented to overcome a situation = where the (incidental) memory usage of paged in memory was could = (regularly) get higher than physical memory would allow, and thus have = the (clear) workaround of having swap to 'cushion' the memory shortage = issue by allowing a "second level" memory storage on disk. Still, this does not making memory unlimited. Swap extends the physical = memory available with the amount of swap. There still is a situation = where you can run out of memory when swap is added, simply by paging in = more memory than physical memory and swap. Today, most systems are not memory constrained anymore, or: it is = possible to get a server with enough physical memory to hold your common = needed total memory need.=20 And given the latency sensitive nature of databases in general, which = includes postgres, for any serious deployment you should get a server = with enough memory to host your workload, and configure postgres not to = overload the memory. If you do oversubscribe on (physical) memory, you will get pain = somewhere, there is no way around that. The article in defense of swap in essence is saying that if you happen = to oversubscribe on memory, sharing the pain between anonymous and file = is better. I would say you are already in a bad place if that happens, which is = especially bad for databases, and databases should allow you to make = memory usage predictable. However, what I found is that with 4+ kernels (4.18 to be precise; rhel = 8), the kernel can try to favour file pages in certain situations making = anonymous memory getting paged out even if swappiness is set to 1 or 0, = and if there is a wealth of inactive file memory. It seems to have to do = with workingset protection(?) mechanisms, but given the lack of clear = statistics I can't be sure about that. What it does lead to in my = situations is a constant rate of swapping in and out in certain = situations, whilst there is no technical reason for linux to do so = because there is enough available memory. My point of view has been that vm.overcommit_memory set to 2 was the way = to go, because that allows linux to limit based on a set limit on = allocation time, which guarantees way to make the database never run out = of memory. it does guarantees linux to never run out of memory, absolutely. However, this limit is hard, and is applied for the process at both = usermode and system mode (kernel level), and thus can enforce not = providing memory at times where it's not safe to do so, and thus corrupt = execution. I have to be honest, I have not seen this myself, but = trustworthy sources have reported this repeatedly, which I am inclined = to believe. This means postgres execution can corrupt/terminate in = unlucky situations, which is impacts availability. =20 Frits Hoogland > On 5 Aug 2025, at 20:52, Joe Conway wrote: >=20 > On 8/5/25 13:01, Priya V wrote: >> *Environment:* >> *PostgreSQL Versions:* Mix of 13.13 and 15.12 (upgrades in = progress >> to be at 15.12 currently both are actively in use) >=20 > PostgreSQL 13 end of life after November 13, 2025 >=20 >> *OS / Kernel:* RHEL 7 & RHEL 8 variants, kernels in the = 4.14=E2=80=934.18 range >=20 > RHEL 7 has been EOL for quite a while now. Note that you have to watch = out for collation issues/corrupted indexes after OS upgrades due to = collations changing with newer glibc versions. >=20 >> *Swap:* Currently none >=20 > bad idea >=20 >> *Workload:* Highly mixed =E2=80=94 OLTP-style internal apps with >> unpredictable query patterns and connection counts >> *Goal:* Uniform, safe memory settings across the fleet to avoid >> kernel or database instability >=20 >> We=E2=80=99re considering: >> *|vm.overcommit_memory =3D 2|* for strict accounting >=20 > yes >=20 >> Increasing |vm.overcommit_ratio| from 50 =E2=86=92 80 or 90 to = better >> reflect actual PostgreSQL usage (e.g., |work_mem| reservations = that >> aren=E2=80=99t fully used) >=20 > work_mem does not reserve memory -- it is a maximum that might be used = in memory for a particular operation >=20 >> *Our questions for those running large PostgreSQL fleets:* >> 1. >> What |overcommit_ratio| do you find safe for PostgreSQL without >> causing kernel memory crunches? >=20 > Read this: > = https://www.cybertec-postgresql.com/en/what-you-should-know-about-linux-me= mory-overcommit-in-postgresql/ >=20 >> 2. >> Do you prefer |overcommit_memory =3D 1| or |=3D 2| for production = stability? >=20 > Use overcommit_memory =3D 2 for production stability >=20 >> 3. >> How much swap (if any) do you keep in large-memory servers where >> PostgreSQL is the primary workload? Is having swap configured a = good >> idea or not ? >=20 > You don't necessary need a large amount of swap, but you definitely = should not disable it. >=20 > Some background on that: > https://chrisdown.name/2018/01/02/in-defence-of-swap.html >=20 >> 4. >> Any real-world cases where kernel accounting was too strict or too >> loose for PostgreSQL? >=20 > In my experience the biggest issues are when postgres is running in a = memory constrained cgroup. If you want to constrain memory with cgroups, = use cgroup v2 (not 1) and use memory.high to constrain it, not = memory.max. >=20 >> 5. What settings to go with if we are not planning on using swap ? >=20 > IMHO do not disable swap on Linux, at least not on production, ever. >=20 >> We=E2=80=99d like to avoid both extremes: >> Too low a ratio =E2=86=92 PostgreSQL backends failing allocations = even with >> free RAM >=20 > Have you actually seen this or are you theorizing? >=20 >> Too high a ratio =E2=86=92 OOM killer terminating PostgreSQL under = load spikes >=20 > If overcommit_memory =3D 2, overcommit_ratio is reasonable (less than = 100, maybe 80 or so as you suggested), and swap is not disabled, and you = are not running in a memory constrained cgroup, I would be very = surprised if you will ever get hit by the OOM killer. And if you do, = things are so bad the database was probably dying anyway. >=20 > HTH, >=20 > --=20 > Joe Conway > PostgreSQL Contributors Team > Amazon Web Services: https://aws.amazon.com >=20 >=20 --Apple-Mail=_A6A8FF39-3F27-42A6-B08A-D8D10F983AB7 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Joe, 

Can you name any = technical reason why not having swap for a database is an actual bad = idea?

Memory always is limited. Swap was = invented to overcome a situation where the (incidental) memory usage of = paged in memory was could (regularly) get higher than physical memory = would allow, and thus have the (clear) workaround of having swap to = 'cushion' the memory shortage issue by allowing a "second level" memory = storage on disk.
Still, this does not making memory unlimited. = Swap extends the physical memory available with the amount of swap. = There still is a situation where you can run out of memory when swap is = added, simply by paging in more memory than physical memory and = swap.

Today, most systems are not memory = constrained anymore, or: it is possible to get a server with enough = physical memory to hold your common needed total memory = need. 
And given the latency sensitive nature of = databases in general, which includes postgres, for any serious = deployment you should get a server with enough memory to host your = workload, and configure postgres not to overload the = memory.

If you do oversubscribe on (physical) = memory, you will get pain somewhere, there is no way around = that.
The article in defense of swap in essence is saying that = if you happen to oversubscribe on memory, sharing the pain between = anonymous and file is better.
I would say you are already in a = bad place if that happens, which is especially bad for databases, and = databases should allow you to make memory usage = predictable.

However, what I found is that with = 4+ kernels (4.18 to be precise; rhel 8), the kernel can try to favour = file pages in certain situations making anonymous memory getting paged = out even if swappiness is set to 1 or 0, and if there is a wealth of = inactive file memory. It seems to have to do with workingset = protection(?) mechanisms, but given the lack of clear statistics I can't = be sure about that. What it does lead to in my situations is a constant = rate of swapping in and out in certain situations, whilst there is no = technical reason for linux to do so because there is enough available = memory.

My point of view has been that = vm.overcommit_memory set to 2 was the way to go, because that allows = linux to limit based on a set limit on allocation time, which guarantees = way to make the database never run out of memory.
it does = guarantees linux to never run out of memory, = absolutely.
However, this limit is hard, and is applied for = the process at both usermode and system mode (kernel level), and thus = can enforce not providing memory at times where it's not safe to do so, = and thus corrupt execution. I have to be honest, I have not seen this = myself, but trustworthy sources have reported this repeatedly, which I = am inclined to believe. This means postgres execution can = corrupt/terminate in unlucky situations, which is impacts = availability.

 
Frits = Hoogland




On 5 Aug 2025, at 20:52, Joe = Conway <mail@joeconway.com> wrote:

On 8/5/25 13:01, Priya V = wrote:
*Environment:*
=    *PostgreSQL Versions:* Mix of 13.13 and 15.12 = (upgrades in progress
   to be at 15.12 currently = both are actively in use)

PostgreSQL 13 end of life = after November 13, 2025

=    *OS / Kernel:* RHEL 7 & RHEL 8 variants, kernels = in the 4.14=E2=80=934.18 range

RHEL 7 has been EOL = for quite a while now. Note that you have to watch out for collation = issues/corrupted indexes after OS upgrades due to collations changing = with newer glibc versions.

=    *Swap:* Currently none

bad = idea

   *Workload:* = Highly mixed =E2=80=94 OLTP-style internal apps with
=    unpredictable query patterns and connection counts
=    *Goal:* Uniform, safe memory settings across the fleet = to avoid
   kernel or database = instability

We=E2=80=99re = considering:
   *|vm.overcommit_memory =3D 2|* for = strict accounting

yes

   Increasing |vm.overcommit_ratio| from = 50 =E2=86=92 80 or 90 to better
   reflect actual = PostgreSQL usage (e.g., |work_mem| reservations that
=    aren=E2=80=99t fully = used)

work_mem does not reserve memory -- it is a = maximum that might be used in memory for a particular = operation

*Our questions for those = running large PostgreSQL fleets:*
1.
   What = |overcommit_ratio| do you find safe for PostgreSQL without
=    causing kernel memory = crunches?

Read = this:
https://www.cybertec-postgresql.com/en/what-you-should-know-about= -linux-memory-overcommit-in-postgresql/

= 2.
   Do you prefer |overcommit_memory =3D 1| or |=3D = 2| for production stability?

Use overcommit_memory =3D= 2 for production stability

3.
=    How much swap (if any) do you keep in large-memory = servers where
   PostgreSQL is the primary workload? = Is having swap configured a good
   idea or not = ?

You don't necessary need a large amount of swap, = but you definitely should not disable it.

Some background on = that:
https://chrisdown.name/2018/01/02/in-defence-of-swap.html

=
4.
   Any real-world cases = where kernel accounting was too strict or too
=    loose for PostgreSQL?

In my = experience the biggest issues are when postgres is running in a memory = constrained cgroup. If you want to constrain memory with cgroups, use = cgroup v2 (not 1) and use memory.high to constrain it, not = memory.max.

5. What settings to go = with if we are not planning on using swap ?

IMHO do = not disable swap on Linux, at least not on production, = ever.

We=E2=80=99d like to avoid both = extremes:
   Too low a ratio =E2=86=92 PostgreSQL = backends failing allocations even with
   free = RAM

Have you actually seen this or are you = theorizing?

   Too high = a ratio =E2=86=92 OOM killer terminating PostgreSQL under load = spikes

If overcommit_memory =3D 2, overcommit_ratio = is reasonable (less than 100, maybe 80 or so as you suggested), and swap = is not disabled, and you are not running in a memory constrained cgroup, = I would be very surprised if you will ever get hit by the OOM killer. = And if you do, things are so bad the database was probably dying = anyway.

HTH,

--
Joe Conway
PostgreSQL Contributors = Team
Amazon Web Services: = https://aws.amazon.com



= --Apple-Mail=_A6A8FF39-3F27-42A6-B08A-D8D10F983AB7--