Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93f4d7f

Browse files
committed
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 relevantcode block in fork_process.c, so that's what I did. (It's possible thatsomething more complicated would be useful to packagers who want to workwith either the old or the new API; but at this point the number of suchpeople is rapidly approaching zero, so let's just get the minimal thingdone.) Update relevant documentation as well.
1 parentb9212e3 commit93f4d7f

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

‎contrib/start-scripts/linux

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ PGLOG="$PGDATA/serverlog"
4242

4343
# It's often a good idea to protect the postmaster from being killed by the
4444
# OOM killer (which will tend to preferentially kill the postmaster because
45-
# of the way it accounts for shared memory). Setting the OOM_ADJ value to
46-
# -17 will disable OOM kill altogether. If you enable this, you probably want
47-
# to compile PostgreSQL with "-DLINUX_OOM_ADJ=0", so that individual backends
48-
# can still be killed by the OOM killer.
45+
# of the way it accounts for shared memory). Setting the OOM_SCORE_ADJ value
46+
# to -1000 will disable OOM kill altogether. If you enable this, you probably
47+
# want to compile PostgreSQL with "-DLINUX_OOM_SCORE_ADJ=0", so that
48+
# individual backends can still be killed by the OOM killer.
49+
#OOM_SCORE_ADJ=-1000
50+
# Older Linux kernels may not have /proc/self/oom_score_adj, but instead
51+
# /proc/self/oom_adj, which works similarly except the disable value is -17.
52+
# For such a system, enable this and compile with "-DLINUX_OOM_ADJ=0".
4953
#OOM_ADJ=-17
5054

5155
## STOP EDITING HERE
@@ -78,6 +82,7 @@ test -x $DAEMON ||
7882
case$1in
7983
start)
8084
echo -n"Starting PostgreSQL:"
85+
test x"$OOM_SCORE_ADJ"!= x&&echo"$OOM_SCORE_ADJ"> /proc/self/oom_score_adj
8186
test x"$OOM_ADJ"!= x&&echo"$OOM_ADJ"> /proc/self/oom_adj
8287
su -$PGUSER -c"$DAEMON -D '$PGDATA' &">>$PGLOG2>&1
8388
echo"ok"
@@ -90,6 +95,7 @@ case $1 in
9095
restart)
9196
echo -n"Restarting PostgreSQL:"
9297
su -$PGUSER -c"$PGCTL stop -D '$PGDATA' -s -m fast -w"
98+
test x"$OOM_SCORE_ADJ"!= x&&echo"$OOM_SCORE_ADJ"> /proc/self/oom_score_adj
9399
test x"$OOM_ADJ"!= x&&echo"$OOM_ADJ"> /proc/self/oom_adj
94100
su -$PGUSER -c"$DAEMON -D '$PGDATA' &">>$PGLOG2>&1
95101
echo"ok"

‎doc/src/sgml/runtime.sgml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ default:\
12681268
In Linux 2.4 and later, the default virtual memory behavior is not
12691269
optimal for <productname>PostgreSQL</productname>. Because of the
12701270
way that the kernel implements memory overcommit, the kernel might
1271-
terminate the <productname>PostgreSQL</productname>server (the
1271+
terminate the <productname>PostgreSQL</productname>postmaster (the
12721272
master server process) if the memory demands of
12731273
another process cause the system to run out of virtual memory.
12741274
</para>
@@ -1317,22 +1317,31 @@ sysctl -w vm.overcommit_memory=2
13171317
<para>
13181318
Another approach, which can be used with or without altering
13191319
<varname>vm.overcommit_memory</>, is to set the process-specific
1320-
<varname>oom_adj</> value for the postmaster process to <literal>-17</>,
1321-
thereby guaranteeing it will not be targeted by the OOM killer. The
1322-
simplest way to do this is to execute
1320+
<varname>oom_score_adj</> value for the postmaster process to
1321+
<literal>-1000</>,thereby guaranteeing it will not be targeted by the OOM
1322+
killer. Thesimplest way to do this is to execute
13231323
<programlisting>
1324-
echo -17 > /proc/self/oom_adj
1324+
echo -1000 > /proc/self/oom_score_adj
13251325
</programlisting>
13261326
in the postmaster's startup script just before invoking the postmaster.
13271327
Note that this action must be done as root, or it will have no effect;
13281328
so a root-owned startup script is the easiest place to do it. If you
13291329
do this, you may also wish to build <productname>PostgreSQL</>
1330-
with <literal>-DLINUX_OOM_ADJ=0</> added to <varname>CPPFLAGS</>.
1330+
with <literal>-DLINUX_OOM_SCORE_ADJ=0</> added to <varname>CPPFLAGS</>.
13311331
That will cause postmaster child processes to run with the normal
1332-
<varname>oom_adj</> value of zero, so that the OOM killer can still
1332+
<varname>oom_score_adj</> value of zero, so that the OOM killer can still
13331333
target them at need.
13341334
</para>
13351335

1336+
<para>
1337+
Older Linux kernels do not offer <filename>/proc/self/oom_score_adj</>,
1338+
but may have a previous version of the same functionality called
1339+
<filename>/proc/self/oom_adj</>. This works the same except the disable
1340+
value is <literal>-17</> not <literal>-1000</>. The corresponding
1341+
build flag for <productname>PostgreSQL</> is
1342+
<literal>-DLINUX_OOM_ADJ=0</>.
1343+
</para>
1344+
13361345
<note>
13371346
<para>
13381347
Some vendors' Linux 2.4 kernels are reported to have early versions

‎src/backend/postmaster/fork_process.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,40 @@ fork_process(void)
6868
* process sizes *including shared memory*. (This is unbelievably
6969
* stupid, but the kernel hackers seem uninterested in improving it.)
7070
* Therefore it's often a good idea to protect the postmaster by
71-
* setting itsoom_adj value negative (which has to be done in a
72-
* root-owned startup script).If you just do that much, all child
71+
* setting itsoom_score_adj value negative (which has to be done in a
72+
* root-owned startup script).If you just do that much, all child
7373
* processes will also be protected against OOM kill, which might not
74-
* be desirable. You can then choose to build with LINUX_OOM_ADJ
75-
* #defined to 0, or some other value that you want child processes to
76-
* adopt here.
74+
* be desirable. You can then choose to build with
75+
* LINUX_OOM_SCORE_ADJ #defined to 0, or to some other value that you
76+
* want child processes to adopt here.
77+
*/
78+
#ifdefLINUX_OOM_SCORE_ADJ
79+
{
80+
/*
81+
* Use open() not stdio, to ensure we control the open flags. Some
82+
* Linux security environments reject anything but O_WRONLY.
83+
*/
84+
intfd=open("/proc/self/oom_score_adj",O_WRONLY,0);
85+
86+
/* We ignore all errors */
87+
if (fd >=0)
88+
{
89+
charbuf[16];
90+
intrc;
91+
92+
snprintf(buf,sizeof(buf),"%d\n",LINUX_OOM_SCORE_ADJ);
93+
rc=write(fd,buf,strlen(buf));
94+
(void)rc;
95+
close(fd);
96+
}
97+
}
98+
#endif/* LINUX_OOM_SCORE_ADJ */
99+
100+
/*
101+
* Older Linux kernels have oom_adj not oom_score_adj. This works
102+
* similarly except with a different scale of adjustment values.
103+
* If it's necessary to build Postgres to work with either API,
104+
* you can define both LINUX_OOM_SCORE_ADJ and LINUX_OOM_ADJ.
77105
*/
78106
#ifdefLINUX_OOM_ADJ
79107
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp