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 1ujgQd-00AiKF-C6 for pgsql-performance@arkaria.postgresql.org; Wed, 06 Aug 2025 15:51: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 1ujgQb-00Gib9-Vq for pgsql-performance@arkaria.postgresql.org; Wed, 06 Aug 2025 15:51:46 +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 1ujgQb-00Gib1-Gs for pgsql-performance@lists.postgresql.org; Wed, 06 Aug 2025 15:51:45 +0000 Received: from mail-yw1-x1134.google.com ([2607:f8b0:4864:20::1134]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujgQY-0015ZG-2S for pgsql-performance@lists.postgresql.org; Wed, 06 Aug 2025 15:51:45 +0000 Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-719f5a141a3so7480987b3.1 for ; Wed, 06 Aug 2025 08:51:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754495501; x=1755100301; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gf2jKGidP7VF4qBO8ifNYv+TSpxH9CXbobH6M1WSA8k=; b=AB0uCRSZCMiJuDA5m1ZUiHY8b3MRhj53jfsBJPrgSFzs5KZ6xgDYyT5OoDJac5oU92 FAvTRINAWuuvwiy+sFCxKW0MMWP9vULVFCDTeTgDGNNbX6/bvbGMlCCbJS412zypCgrI lFOILzIC0dmXvH+0ylnUHH36oL9GA5YOn8xPrX9gV37aFBcX2tGRb7AMkGKMNnM5Z7Ib wWw5jK0xwE/MtO5xkxJSdulSKJdppRSvsbG8gnL8U+YPP7WMAVfNG+y3QYEi5F0LcJZ3 Xk8rwZCMR7qI8trqD0/GX9b/tB8hxBUIqPzCaYxsR49e2K3BQsl3paPaYqD9seorxWoe JrSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754495501; x=1755100301; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gf2jKGidP7VF4qBO8ifNYv+TSpxH9CXbobH6M1WSA8k=; b=reXd2PjmaE2NxLfflY7QhPSD+j71Ahr7NHAWS+FR0dtPFyfIZTiV+4Uq0L6VCm0oaY wdxhU92R/6tm8vquorRGn5rv2MS5aFiORQd4hsp4L/AbU13OTx0gt9qJ357PTL6CQIUC nj8vIaTr6xI/xJ/ZAgiUI1uKtmIC9OKyrO18ArvF+x+IQ9schVQz1/3rLcsmXBr0Ahh/ nV0sH3fs9Y3kMGprj8EbvtB6zhEq8oG6Couf7RpGx1RHJ6tQk8ovwygBWkoFrZGZOaH2 0Z1zwfIRqUV6cFBawKsAF9NVatDbWt2Chiiv6Zx66Kg/7LPS4aXB2OQg//jPEsmIWagZ OozA== X-Forwarded-Encrypted: i=1; AJvYcCUeKF/UpAY1z0qe24NNdzk2gwNTMB6xnCqjQSsi8vb+vP8Mj9Obx6zw1tRnl4t2aVrj/6l+qZkGGJnSvbNa7HM0Yw==@lists.postgresql.org X-Gm-Message-State: AOJu0YzeeRJHGTLKCAxlFezStfzVx+NcmK9huAyJyvJvZ4LsFJTMLpF7 t0zHRg37YgSCEa7QrPXkvBbNPFpRYcfIc/11lSaM8YJ6mCTmqPWCoWIcCWKBzO0kMi458opD+gh RD13c8n9ZSp3zwQ7MgTQiafdv/iw94ik= X-Gm-Gg: ASbGncuTln4U/BtNd+mldxGPPWwlKmAcDrqEp27dP5qGubgd3js4/d2XtY8gR8qrJCd QP+iSO4e7zzVKRBEWr2Ug1wHKSLvX8RRwGyxXbEWDCWhczxnFHxrAXsTtpFKFzwqTlxBWDz4/xj OP8pyPFaFclzUadXuM2ZhkiINE2UidEDs6sHlW/jMKAM1pJul/iA+FYrEOae2c4eKQXPZGLpVxc vDweArRtUJkAA== X-Google-Smtp-Source: AGHT+IGPfWeae3UTS0X9eCz1BZN8M3NX0s0SeEAXTo2Jis/z3ADLvUKck4gZOloQkmL8i6LqnICirA6+WM3kwODlI10= X-Received: by 2002:a05:690c:4d47:b0:712:c5f7:1f11 with SMTP id 00721157ae682-71bdaf5c829mr960457b3.10.1754495500370; Wed, 06 Aug 2025 08:51:40 -0700 (PDT) MIME-Version: 1.0 References: <3633df47-5693-4871-9e12-bfbb68dd3313@joeconway.com> <5520EFB7-EDD1-42C1-8141-63EED777E026@gmail.com> In-Reply-To: <5520EFB7-EDD1-42C1-8141-63EED777E026@gmail.com> From: Priya V Date: Wed, 6 Aug 2025 10:51:29 -0500 X-Gm-Features: Ac12FXwoipED4FpG3ECqoYKivF1yBDmCswvgaIUFAIowy3hs6uv5TLbtxoSisS4 Message-ID: Subject: Re: Safe vm.overcommit_ratio for Large Multi-Instance PostgreSQL Fleet To: Frits Hoogland Cc: Joe Conway , pgsql-performance@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000000f477d063bb45136" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000f477d063bb45136 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Frits, Joe, Thank you both for you insights *Current situation:* *cat /proc/sys/vm/overcommit_memory* 0 *cat /proc/sys/vm/overcommit_ratio* 50 $ *cat /proc/sys/vm/swappiness* 60 *Workload*: Multi-tenant PostgreSQL *uname -r* 4.18.0-477.83.1.el8_8.x86_64 *free -h* total used free shared buff/cache available Mem: 249Gi 4.3Gi 1.7Gi 22Gi 243Gi 221Gi Swap: 0B 0B 0B if we set overcommit_memory =3D 2, what should we set the overcommit_ration value to ? Can you pls suggest ? Is there a rule of thumb to go with ? *Our goal is to not run into OOM issues, no memory wastage and also not starve kernel ? * Thanks! On Wed, Aug 6, 2025 at 3:47=E2=80=AFAM Frits Hoogland wrote: > Joe, > > Can you name any technical reason why not having swap for a database is a= n > 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) ge= t > 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 wher= e > 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 possibl= e > 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 wit= h > enough memory to host your workload, and configure postgres not to overlo= ad > 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 memo= ry > 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, a= nd > 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 statisti= cs > 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 providi= ng > 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 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 ou= t > for collation issues/corrupted indexes after OS upgrades due to collation= s > 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-m= emory-overcommit-in-postgresql/ > > 2. > Do you prefer |overcommit_memory =3D 1| or |=3D 2| for production stab= ility? > > > 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 shoul= d > 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 loa= d 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 n= ot > 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 > > > > --0000000000000f477d063bb45136 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Frits, Joe,=C2=A0

Thank y= ou both for you insights=C2=A0

Current situatio= n:

cat /proc/sys/vm/overcommit_memory0

cat /proc/sys/vm/overcommit_ratio
50

$ cat /= proc/sys/vm/swappiness
60

Workload<= /strong>: Multi-tenant PostgreSQL

uname -r<= br>4.18.0-477.83.1.el8_8.x86_64

free -h=
total used free shared buff/cache available
Mem: 249Gi 4.3Gi 1.7Gi = 22Gi 243Gi 221Gi
Swap: 0B 0B 0B

if we set overc= ommit_memory =3D 2, what should we set the overcommit_ration value to ? Can= you pls suggest ?=C2=A0
Is there a rule of thumb to go with ?

Our goal is to not run into OOM issues, no memory= wastage and also not starve kernel ?=C2=A0

Th= anks!=C2=A0





On Wed, Aug 6, 2025 at 3:47=E2=80=AFAM Frits Hoogla= nd <frits.hoogland@gmail.com= > wrote:
=
Joe,=C2=A0

Can you name any technical reason why no= t 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) workaro= und of having swap to 'cushion' the memory shortage issue by allowi= ng a "second level" memory storage on disk.
Still, this= does not making memory unlimited. Swap extends the physical memory availab= le with the amount of swap. There still is a situation where you can run ou= t of memory when swap is added, simply by paging in more memory than physic= al memory and swap.

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

If you do oversubscribe on (physical) mem= ory, you will get pain somewhere, there is no way around that.
Th= e article in defense of swap in essence is saying that if you happen to ove= rsubscribe on memory, sharing the pain between anonymous and file is better= .
I would say you are already in a bad place if that happens, whi= ch 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 f= avour file pages in certain situations making anonymous memory getting page= d out even if swappiness is set to 1 or 0, and if there is a wealth of inac= tive file memory. It seems to have to do with workingset protection(?) mech= anisms, but given the lack of clear statistics I can't be sure about th= at. 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 lin= ux to do so because there is enough available memory.

<= div>My point of view has been that vm.overcommit_memory set to 2 was the wa= y to go, because that allows linux to limit based on a set limit on allocat= ion 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 prov= iding memory at times where it's not safe to do so, and thus corrupt ex= ecution. 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, whic= h is impacts availability.

=C2=A0
Frits Hoogland

<= /div>


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

On 8/5/25 13:01, Priya V wrote:
<= blockquote type=3D"cite">*Environment:*
=C2=A0=C2=A0=C2=A0*PostgreSQL V= ersions:* Mix of 13.13 and 15.12 (upgrades in progress
=C2=A0=C2=A0=C2= =A0to be at 15.12 currently both are actively in use)

P= ostgreSQL 13 end of life after November 13, 2025

=C2=A0=C2=A0=C2=A0*OS / Kernel:* RHEL 7 & RHEL 8 variants, kern= els 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.

=C2=A0=C2=A0=C2=A0*Swap:*= Currently none

bad idea

=C2=A0=C2=A0=C2=A0*Workload:* Highly mixed =E2=80=94 OLTP-style interna= l apps with
=C2=A0=C2=A0=C2=A0unpredictable query patterns and connecti= on counts
=C2=A0=C2=A0=C2=A0*Goal:* Uniform, safe memory settings acros= s the fleet to avoid
=C2=A0=C2=A0=C2=A0kernel or database instability

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

yes

=C2=A0=C2=A0=C2=A0In= creasing |vm.overcommit_ratio| from 50 =E2=86=92 80 or 90 to better
=C2= =A0=C2=A0=C2=A0reflect actual PostgreSQL usage (e.g., |work_mem| reservatio= ns that
=C2=A0=C2=A0=C2=A0aren=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 qu= estions for those running large PostgreSQL fleets:*
1.
=C2=A0=C2=A0= =C2=A0What |overcommit_ratio| do you find safe for PostgreSQL without
= =C2=A0=C2=A0=C2=A0causing kernel memory crunches?

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

2.
=C2=A0= =C2=A0=C2=A0Do you prefer |overcommit_memory =3D 1| or |=3D 2| for producti= on stability?

Use overcommit_memory =3D 2 for productio= n stability

3.
=C2=A0=C2=A0=C2=A0How = much swap (if any) do you keep in large-memory servers where
=C2=A0=C2= =A0=C2=A0PostgreSQL is the primary workload? Is having swap configured a go= od
=C2=A0=C2=A0=C2=A0idea or not ?

You don't ne= cessary 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.=
=C2=A0=C2=A0=C2=A0Any real-world cases where kernel accounting was too= strict or too
=C2=A0=C2=A0=C2=A0loose for PostgreSQL?
=
In my experience the biggest issues are when postgres is running in a m= emory 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 pl= anning on using swap ?

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

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

Have you actually seen this or are yo= u theorizing?

=C2=A0=C2=A0=C2=A0Too 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 sur= prised if you will ever get hit by the OOM killer. And if you do, things ar= e so bad the database was probably dying anyway.

HTH,

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


--0000000000000f477d063bb45136--