When memory runs out, the kernel’s OOM killer picks a process to kill, usually the biggest one. That might be your database instead of a runaway script. You can nudge its choice.
choom -p 1234
This shows the current OOM score of process 1234 and its adjust value. The score is what the kernel uses to choose: the highest score goes first. The adjust value runs from -1000 to 1000 and is added to the score. To make a process the preferred victim:
choom -p 1234 -n 500
You can also start a command with a value, which suits batch jobs and test scripts:
choom -n 800 -- ./big-import.sh
A value of -1000 means never kill this one. Going below the current value needs root, or the CAP_SYS_RESOURCE capability, so a normal user can only make their own processes more likely to be killed, not less. That is how I would use it most of the time: raise the score of the things you can afford to lose, instead of shielding the important ones.
For a systemd service, set it in the unit with OOMScoreAdjust=, so it survives restarts. A value set with choom lasts only as long as that process, though children inherit it.
Be careful with -1000 on a big process. If the kernel may not kill it, it has to kill everything else first, and the machine can end up worse off than with a clean kill of that one program.
Further reading: choom(1) manual page, proc_pid_oom_score_adj(5) manual page