mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
Support Linux's oom_score_adj API as well as the older oom_adj API.
The simplest way to handle this is just to copy-and-paste the relevant code block in fork_process.c, so that's what I did. (It's possible that something more complicated would be useful to packagers who want to work with either the old or the new API; but at this point the number of such people is rapidly approaching zero, so let's just get the minimal thing done.) Update relevant documentation as well.
This commit is contained in:
@ -68,12 +68,40 @@ fork_process(void)
|
||||
* process sizes *including shared memory*. (This is unbelievably
|
||||
* stupid, but the kernel hackers seem uninterested in improving it.)
|
||||
* Therefore it's often a good idea to protect the postmaster by
|
||||
* setting its oom_adj value negative (which has to be done in a
|
||||
* root-owned startup script). If you just do that much, all child
|
||||
* setting its oom_score_adj value negative (which has to be done in a
|
||||
* root-owned startup script). If you just do that much, all child
|
||||
* processes will also be protected against OOM kill, which might not
|
||||
* be desirable. You can then choose to build with LINUX_OOM_ADJ
|
||||
* #defined to 0, or some other value that you want child processes to
|
||||
* adopt here.
|
||||
* be desirable. You can then choose to build with
|
||||
* LINUX_OOM_SCORE_ADJ #defined to 0, or to some other value that you
|
||||
* want child processes to adopt here.
|
||||
*/
|
||||
#ifdef LINUX_OOM_SCORE_ADJ
|
||||
{
|
||||
/*
|
||||
* Use open() not stdio, to ensure we control the open flags. Some
|
||||
* Linux security environments reject anything but O_WRONLY.
|
||||
*/
|
||||
int fd = open("/proc/self/oom_score_adj", O_WRONLY, 0);
|
||||
|
||||
/* We ignore all errors */
|
||||
if (fd >= 0)
|
||||
{
|
||||
char buf[16];
|
||||
int rc;
|
||||
|
||||
snprintf(buf, sizeof(buf), "%d\n", LINUX_OOM_SCORE_ADJ);
|
||||
rc = write(fd, buf, strlen(buf));
|
||||
(void) rc;
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
#endif /* LINUX_OOM_SCORE_ADJ */
|
||||
|
||||
/*
|
||||
* Older Linux kernels have oom_adj not oom_score_adj. This works
|
||||
* similarly except with a different scale of adjustment values.
|
||||
* If it's necessary to build Postgres to work with either API,
|
||||
* you can define both LINUX_OOM_SCORE_ADJ and LINUX_OOM_ADJ.
|
||||
*/
|
||||
#ifdef LINUX_OOM_ADJ
|
||||
{
|
||||
|
Reference in New Issue
Block a user