From 39715af23ae459684963c350dd69eafa2d502e7e Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Thu, 28 Jun 2012 12:57:22 -0400
Subject: [PATCH] Fix broken mmap failure-detection code, and improve error
 message.

Per an observation by Thom Brown that my previous commit made an
overly large shmem allocation crash the server, on Linux.
---
 src/backend/port/sysv_shmem.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 4bbd0ec6495..20f31ed218d 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -419,10 +419,17 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
 		 */
 		AnonymousShmem = mmap(NULL, size, PROT_READ|PROT_WRITE, PG_MMAP_FLAGS,
 							  -1, 0);
-		if (AnonymousShmem == NULL)
+		if (AnonymousShmem == MAP_FAILED)
 			ereport(FATAL,
-			 (errmsg("could not map %lu bytes of anonymous shared memory: %m",
-				(unsigned long) AnonymousShmemSize)));
+			 (errmsg("could not map anonymous shared memory: %m"),
+			  (errno == ENOMEM) ?
+			   errhint("This error usually means that PostgreSQL's request "
+					   "for a shared memory segment exceeded available memory "
+					   "or swap space. To reduce the request size (currently "
+					   "%lu bytes), reduce PostgreSQL's shared memory usage, "
+					   "perhaps by reducing shared_buffers or "
+					   "max_connections.",
+					(unsigned long) AnonymousShmemSize) : 0));
 
 		/* Now we can allocate a minimal SHM block. */
 		allocsize = sizeof(PGShmemHeader);
-- 
GitLab