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

Commitd5e0029

Browse files
committed
Add some simple support and documentation for using process-specific oom_adj
settings to prevent the postmaster from being OOM-killed on Linux systems.Alex Hunsaker and Tom Lane
1 parent292176a commitd5e0029

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

‎contrib/start-scripts/linux

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# Original author: Ryan Kirkpatrick <pgsql@rkirkpat.net>
2626

27-
# $PostgreSQL: pgsql/contrib/start-scripts/linux,v 1.9 2009/08/27 16:59:38 tgl Exp $
27+
# $PostgreSQL: pgsql/contrib/start-scripts/linux,v 1.10 2010/01/11 18:39:32 tgl Exp $
2828

2929
## EDIT FROM HERE
3030

@@ -40,6 +40,14 @@ PGUSER=postgres
4040
# Where to keep a log file
4141
PGLOG="$PGDATA/serverlog"
4242

43+
# It's often a good idea to protect the postmaster from being killed by the
44+
# 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.
49+
#OOM_ADJ=-17
50+
4351
## STOP EDITING HERE
4452

4553
# The path that is to be used for the script
@@ -62,6 +70,7 @@ test -x $DAEMON || exit 0
6270
case$1in
6371
start)
6472
echo -n"Starting PostgreSQL:"
73+
test x"$OOM_ADJ"!= x&&echo"$OOM_ADJ"> /proc/self/oom_adj
6574
su -$PGUSER -c"$DAEMON -D '$PGDATA' &">>$PGLOG2>&1
6675
echo"ok"
6776
;;
@@ -73,6 +82,7 @@ case $1 in
7382
restart)
7483
echo -n"Restarting PostgreSQL:"
7584
su -$PGUSER -c"$PGCTL stop -D '$PGDATA' -s -m fast -w"
85+
test x"$OOM_ADJ"!= x&&echo"$OOM_ADJ"> /proc/self/oom_adj
7686
su -$PGUSER -c"$DAEMON -D '$PGDATA' &">>$PGLOG2>&1
7787
echo"ok"
7888
;;

‎doc/src/sgml/runtime.sgml

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.429 2009/12/10 06:32:28 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.430 2010/01/11 18:39:32 tgl Exp $ -->
22

33
<chapter Id="runtime">
44
<title>Server Setup and Operation</title>
@@ -1244,7 +1244,7 @@ default:\
12441244
this (consult your system documentation and configuration on where
12451245
to look for such a message):
12461246
<programlisting>
1247-
Out of Memory: Killed process 12345 (postgres).
1247+
Out of Memory: Killed process 12345 (postgres).
12481248
</programlisting>
12491249
This indicates that the <filename>postgres</filename> process
12501250
has been terminated due to memory pressure.
@@ -1258,13 +1258,13 @@ Out of Memory: Killed process 12345 (postgres).
12581258
<productname>PostgreSQL</productname> on a machine where you can
12591259
be sure that other processes will not run the machine out of
12601260
memory. If memory is tight, increasing the swap space of the
1261-
operating system can helpavoiding the problem, because the
1262-
out-of-memory (OOM) killer is invokedwhenever physical memory and
1261+
operating system can helpavoid the problem, because the
1262+
out-of-memory (OOM) killer is invokedonly when physical memory and
12631263
swap space are exhausted.
12641264
</para>
12651265

12661266
<para>
1267-
On Linux 2.6 and later,an additional measure is to modify the
1267+
On Linux 2.6 and later,it is possible to modify the
12681268
kernel's behavior so that it will not <quote>overcommit</> memory.
12691269
Although this setting will not prevent the <ulink
12701270
url="http://lwn.net/Articles/104179/">OOM killer</> from being invoked
@@ -1275,11 +1275,31 @@ Out of Memory: Killed process 12345 (postgres).
12751275
sysctl -w vm.overcommit_memory=2
12761276
</programlisting>
12771277
or placing an equivalent entry in <filename>/etc/sysctl.conf</>.
1278-
You might also wish to modify the related setting
1279-
<literal>vm.overcommit_ratio</>. For details see the kernel documentation
1278+
You might also wish to modify the related setting
1279+
<varname>vm.overcommit_ratio</>. For details see the kernel documentation
12801280
file <filename>Documentation/vm/overcommit-accounting</>.
12811281
</para>
12821282

1283+
<para>
1284+
Another approach, which can be used with or without altering
1285+
<varname>vm.overcommit_memory</>, is to set the process-specific
1286+
<varname>oom_adj</> value for the postmaster process to <literal>-17</>,
1287+
thereby guaranteeing it will not be targeted by the OOM killer. The
1288+
simplest way to do this is to execute
1289+
<programlisting>
1290+
echo -17 > /proc/self/oom_adj
1291+
</programlisting>
1292+
in the postmaster's startup script just before invoking the postmaster.
1293+
Note that this action must be done as root, or it will have no effect;
1294+
so a root-owned startup script is the easiest place to do it. If you
1295+
do this, you may also wish to build <productname>PostgreSQL</>
1296+
with <literal>-DLINUX_OOM_ADJ=0</> added to <varname>CFLAGS</>.
1297+
That will cause postmaster child processes to run with the normal
1298+
<varname>oom_adj</> value of zero, so that the OOM killer can still
1299+
target them at need.
1300+
</para>
1301+
1302+
<note>
12831303
<para>
12841304
Some vendors' Linux 2.4 kernels are reported to have early versions
12851305
of the 2.6 overcommit <command>sysctl</command> parameter. However, setting
@@ -1294,6 +1314,7 @@ sysctl -w vm.overcommit_memory=2
12941314
feature is there. If in any doubt, consult a kernel expert or your
12951315
kernel vendor.
12961316
</para>
1317+
</note>
12971318
</sect2>
12981319
</sect1>
12991320

‎src/backend/postmaster/fork_process.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
* Copyright (c) 1996-2010, PostgreSQL Global Development Group
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/postmaster/fork_process.c,v 1.10 2010/01/02 16:57:50 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/postmaster/fork_process.c,v 1.11 2010/01/11 18:39:32 tgl Exp $
1111
*/
1212
#include"postgres.h"
1313
#include"postmaster/fork_process.h"
1414

15+
#include<fcntl.h>
1516
#include<time.h>
17+
#include<sys/stat.h>
1618
#include<sys/time.h>
1719
#include<unistd.h>
1820

@@ -60,6 +62,38 @@ fork_process(void)
6062
setitimer(ITIMER_PROF,&prof_itimer,NULL);
6163
#endif
6264

65+
/*
66+
* By default, Linux tends to kill the postmaster in out-of-memory
67+
* situations, because it blames the postmaster for the sum of child
68+
* process sizes *including shared memory*. (This is unbelievably
69+
* stupid, but the kernel hackers seem uninterested in improving it.)
70+
* Therefore it's often a good idea to protect the postmaster by
71+
* setting its oom_adj value negative (which has to be done in a
72+
* root-owned startup script). If you just do that much, all child
73+
* 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
76+
* to adopt here.
77+
*/
78+
#ifdefLINUX_OOM_ADJ
79+
{
80+
/*
81+
* Use open() not stdio, to ensure we control the open flags.
82+
* Some Linux security environments reject anything but O_WRONLY.
83+
*/
84+
intfd=open("/proc/self/oom_adj",O_WRONLY,0);
85+
86+
/* We ignore all errors */
87+
if (fd >=0)
88+
{
89+
charbuf[16];
90+
91+
snprintf(buf,sizeof(buf),"%d\n",LINUX_OOM_ADJ);
92+
(void)write(fd,buf,strlen(buf));
93+
close(fd);
94+
}
95+
}
96+
#endif/* LINUX_OOM_ADJ */
6397
}
6498

6599
returnresult;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp