Work around unintelligent kernel OOM-kill algorithm.

This commit is contained in:
Tom Lane 2010-01-11 18:11:25 +00:00
parent 2a1cdc60c9
commit 8c0b311010
3 changed files with 70 additions and 2 deletions

57
postgresql-oom-adj.patch Normal file
View File

@ -0,0 +1,57 @@
Back-ported patch from Postgres devel head: reset oom_adj to zero in any
postmaster child process. This allows us to disable OOM kill on the postmaster
(see the init script) without affecting OOM behavior for child processes.
diff -Naur postgresql-8.4.2.orig/src/backend/postmaster/fork_process.c postgresql-8.4.2/src/backend/postmaster/fork_process.c
--- postgresql-8.4.2.orig/src/backend/postmaster/fork_process.c 2009-01-01 12:23:46.000000000 -0500
+++ postgresql-8.4.2/src/backend/postmaster/fork_process.c 2010-01-11 12:28:17.000000000 -0500
@@ -12,7 +12,9 @@
#include "postgres.h"
#include "postmaster/fork_process.h"
+#include <fcntl.h>
#include <time.h>
+#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
@@ -60,6 +62,38 @@
setitimer(ITIMER_PROF, &prof_itimer, NULL);
#endif
+ /*
+ * By default, Linux tends to kill the postmaster in out-of-memory
+ * situations, because it blames the postmaster for the sum of child
+ * 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
+ * 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.
+ */
+#ifdef LINUX_OOM_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_adj", O_WRONLY, 0);
+
+ /* We ignore all errors */
+ if (fd >= 0)
+ {
+ char buf[16];
+
+ snprintf(buf, sizeof(buf), "%d\n", LINUX_OOM_ADJ);
+ (void) write(fd, buf, strlen(buf));
+ close(fd);
+ }
+ }
+#endif /* LINUX_OOM_ADJ */
}
return result;

View File

@ -108,6 +108,8 @@ PGENGINE=/usr/bin
PGPORT=5432
PGDATA=/var/lib/pgsql/data
PGLOG=/var/lib/pgsql/pgstartup.log
# Value to set as postmaster process's oom_adj
PG_OOM_ADJ=-17
# Override defaults from /etc/sysconfig/pgsql if file is present
[ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}
@ -177,6 +179,7 @@ start(){
fi
echo -n "$PSQL_START"
test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_adj
$SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
sleep 2
pid=`pidof -s "$PGENGINE/postmaster"`

View File

@ -53,7 +53,7 @@ Summary: PostgreSQL client programs
Name: postgresql
%global majorversion 8.4
Version: 8.4.2
Release: 3%{?dist}
Release: 4%{?dist}
# PostgreSQL calls their license simplified BSD, but the requirements are
# more similar to other MIT licenses.
License: MIT
@ -74,6 +74,7 @@ Source17: http://www.postgresql.org/docs/manuals/postgresql-8.4.2-US.pdf
Patch1: rpm-pgsql.patch
Patch2: postgresql-ac-version.patch
Patch3: postgresql-logging.patch
Patch4: postgresql-oom-adj.patch
Patch6: postgresql-perl-rpath.patch
BuildRequires: perl(ExtUtils::MakeMaker) glibc-devel bison flex autoconf gawk
@ -272,6 +273,7 @@ system, including regression tests and benchmarks.
%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch6 -p1
autoconf
@ -281,10 +283,11 @@ cp -p %{SOURCE17} .
%build
CFLAGS="${CFLAGS:-%optflags}" ; export CFLAGS
CXXFLAGS="${CXXFLAGS:-%optflags}" ; export CXXFLAGS
# Strip out -ffast-math from CFLAGS....
CFLAGS=`echo $CFLAGS|xargs -n 1|grep -v ffast-math|xargs -n 100`
# Add LINUX_OOM_ADJ=0 to ensure child processes reset postmaster's oom_adj
CFLAGS="$CFLAGS -DLINUX_OOM_ADJ=0"
# let's try removing this kluge, it may just be a workaround for bz#520916
# # use -O1 on sparc64 and alpha
# %ifarch sparc64 alpha
@ -693,6 +696,11 @@ rm -rf $RPM_BUILD_ROOT
%endif
%changelog
* Mon Jan 11 2010 Tom Lane <tgl@redhat.com> 8.4.2-4
- Arrange for the postmaster, but not any of its child processes, to be run
with oom_adj -17. This compensates for the OOM killer not being smart about
accounting for shared memory usage.
* Sat Jan 9 2010 Tom Lane <tgl@redhat.com> 8.4.2-3
- Remove the postgresql-python and postgresql-tcl subpackages. These files
are now broken out as their own packages (PyGreSQL and tcl-pgtcl,