Blob Blame History Raw
Back-patch upstream's 9.2 patch to add support for setting oom_score_adj.


diff -Naur postgresql-9.1.4.orig/src/backend/postmaster/fork_process.c postgresql-9.1.4/src/backend/postmaster/fork_process.c
--- postgresql-9.1.4.orig/src/backend/postmaster/fork_process.c	2012-05-31 19:07:09.000000000 -0400
+++ postgresql-9.1.4/src/backend/postmaster/fork_process.c	2012-07-14 17:55:42.911859485 -0400
@@ -68,12 +68,40 @@
 		 * 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
 		{