Sobes.tech
Senior

How to manage the OOM Killer in Linux so that the database is never killed? Which parameter is responsible for this?

sobes.tech AI

Answer from AI

In Linux, the management of the OOM Killer (Out-Of-Memory Killer) behavior for a specific process, such as a database, is controlled through the oom_score_adj parameter. This parameter accepts values from -1000 to 1000, where -1000 means the process is completely excluded from the list of candidates for termination when memory is low.

To ensure the database is never killed by the OOM Killer, set its process oom_score_adj to -1000. This can be done, for example, as follows:

# Find the PID of the database process
pid=$(pidof postgres)
# Set oom_score_adj
echo -1000 | sudo tee /proc/$pid/oom_score_adj

If the database is started via systemd, you can add the following to the unit file:

OOMScoreAdjust=-1000

This guarantees that when memory is low, the OOM Killer will not kill the database process.

It is also advisable to monitor overall memory consumption and configure swap to minimize the triggering of the OOM Killer.