Exposing the cgroup memory limit to SQL?

Started by Joao Detomini21 days ago4 messageshackers
Jump to latest
#1Joao Detomini
joao.detomini@enterprisedb.com

Hi,

When Postgres runs in a container with a memory limit, the server knows
nothing about it. I recently posted a docs patch
<https://commitfest.postgresql.org/patch/7381/&gt; about how the cgroup
OOM killer behaves , and while testing that I tried the obvious
case: postgres:17 (17.11) with -m 512m and no swap, and six sessions
each sorting 3M rows with work_mem = 300MB. One backend got SIGKILL,
the postmaster restarted everything, and all six sessions lost their
connection. Nobody got an out-of-memory error, since the allocations
themselves succeeded. The server had started with the usual
shared_buffers = 128MB and max_connections = 100, so nothing in it
reflected the 512MB limit.

I couldn't find any code in the tree that looks at cgroups, nor an
earlier discussion in the archives, though I may have missed one.

What I have in mind is small: a SQL function that returns the memory
limit of the cgroup the server runs in (NULL if there is none, or not
on Linux), so tools and operators don't have to parse /sys/fs/cgroup
themselves. It would not change any defaults or behavior.

Does that seem worth having in core, or would you rather see it in
contrib or left to external tools? I'd only look at cgroup v2 at
first. If the idea sounds reasonable, I'm happy to write a patch.

Thanks,
João Marcelo

#2Umar Hayat
postgresql.wizard@gmail.com
In reply to: Joao Detomini (#1)
Re: Exposing the cgroup memory limit to SQL?

Hi,
There is the PostgreSQL wiki page "Cgroups"[1]https://wiki.postgresql.org/wiki/Cgroups explaining the problem
statement and also potential issues for implementation.
Seems like nobody proposed any implementation for cgroups yet

Regards
Umar Hayat

[1]: https://wiki.postgresql.org/wiki/Cgroups

#3Joe Conway
mail@joeconway.com
In reply to: Umar Hayat (#2)
Re: Exposing the cgroup memory limit to SQL?

On 10/1/26 23:05, Umar Hayat wrote:

Hi,
There is the PostgreSQL wiki page "Cgroups"[1] explaining the problem
statement and also potential issues for implementation.
Seems like nobody proposed any implementation for cgroups yet

Regards
Umar Hayat

[1] https://wiki.postgresql.org/wiki/Cgroups

This is not in the postgres tree, but I think it does what you are
looking for (plus a lot more): https://github.com/pgnodemx/pgnodemx

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

#4Joao Detomini
joao.detomini@enterprisedb.com
In reply to: Joe Conway (#3)
Re: Exposing the cgroup memory limit to SQL?

Hi,
Thanks, I hadn't come across pgnodemx. cgroup_scalar_bigint('memory.max')
covers what I had in mind, so I'll drop the idea of a function in core.

Em dom., 4 de out. de 2026 às 15:58, Joe Conway <mail@joeconway.com>
escreveu:

Show quoted text

On 10/1/26 23:05, Umar Hayat wrote:

Hi,
There is the PostgreSQL wiki page "Cgroups"[1] explaining the problem
statement and also potential issues for implementation.
Seems like nobody proposed any implementation for cgroups yet

Regards
Umar Hayat

[1] https://wiki.postgresql.org/wiki/Cgroups

This is not in the postgres tree, but I think it does what you are
looking for (plus a lot more): https://github.com/pgnodemx/pgnodemx

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