6

So, my friend says swap is an old concept and shouldn't be needed on new systems as long as you have enough RAM. But then I heard suggestions that swap might actually be required when using Postgres on Linux or even Unix because malloc/free will not return memory to the system. I'm told this is a feature, not a bug, and I should use swap because system will use swap for the freed memory... Is this correct or am I missing something? Can someone confirm this and maybe shed more light on it?

My setup for context:

  • PGSQL config: shared_buffers + (temp_buffers + work_mem) * max_connections = 20 GB;
  • PGSQL is version 16, the server in on Ubuntu 22.
  • The server has 50 GB of RAM and 0 swap. ZFS ARC is maxed out at 8 GB.
  • I'm actually seeing 20 GB surpassed after a few hours.
  • I see bumps in RAM usage each day. 50 GB is reached within 5-7 days (drops to 0 on PGSQL restart).

That's the context. Essentially, the question is: do I need to, or should I, have swap for a VM with Postgres server or not?

9
  • Check /proc/pressure/memory and /proc/meminfo for Inactive(anon). If you have significant memory pressure and a lot of inactive memory then enabling swap could be useful. Commented Oct 25, 2024 at 13:09
  • At the moment this zero or low, but postgres was just restarted. cat /proc/pressure/memory some avg10=0.00 avg60=0.00 avg300=0.00 total=1909323617 full avg10=0.00 avg60=0.00 avg300=0.00 total=1894853199 grep "Inactive(anon)" /proc/meminfo Inactive(anon): 17904 kB Commented Oct 25, 2024 at 13:46
  • 1
    Just how reliable do you need your system to be? Read about the out-of-fuel killer. I'll take systems built on old concepts like "reliability" and "availability" over "we'll LIE to processes about how much memory they can use, and if we run out, we'll just kill 'em until we have enough memory". Put enough memory on the system, add a proper amount of swap for the intended usage, then disable the OOM killer and all memory overcommit. That "old concept" means your database server will be reliable. Have higher standards than "we hope this'll work". Commented Oct 26, 2024 at 11:49
  • 1
    Always use swap. Even 2GiB of swap are good, always use swap partition if possible. As you can see, when the swap is being used, you either have not enough physical memory, or some program has a memory leak, which should be fixed. It also acts as some emergency buffer, especially when using as swap partition. Should your filesystem ever run full, you could at least delete the swap partition and use the free space to enlage your full filesystem. Commented Oct 28, 2024 at 10:24
  • 1
    Disabling memory overcommit is only a good way to go if your software is well written and not garbage. Let there be some buggy library that decides it always needs to allocate 10GiB, just to make sure it has enough memory, but uses only 123KiB... As far as I can see, the garbage becomes bigger and bigger these days... Commented Oct 28, 2024 at 10:32

1 Answer 1

7

It’s actually safer to have some swap space so that when the system runs out of physical memory, it will experience a slowdown instead of immediately killing processes. In short, in modern systems, swap space acts as insurance you’ll probably never need.

A similar discussion with many arguments can be found here:

https://community.nethserver.org/t/linux-swap-needed-or-not/23655

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.