>From 9bc05ac103c77fcd2276754a3ccbf49e66dd00b7 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 6 May 2014 19:08:07 +0200
Subject: [PATCH] Don't bail in dsm_attach() if any any other segment is about
 to be destroyed.

The previous coding accidentally checked for refcnt == 1, which
signals moribund segments, before checking whether the segment we're
looking at is the one we're trying to attach to.
---
 src/backend/storage/ipc/dsm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index d86b0c7..bde7026 100644
--- a/src/backend/storage/ipc/dsm.c
+++ b/src/backend/storage/ipc/dsm.c
@@ -570,6 +570,10 @@ dsm_attach(dsm_handle h)
 		if (dsm_control->item[i].refcnt == 0)
 			continue;
 
+		/* Not the segment you're looking for. */
+		if (dsm_control->item[i].handle != seg->handle)
+			continue;
+
 		/*
 		 * If the reference count is 1, the slot is still in use, but the
 		 * segment is in the process of going away.  Treat that as if we
@@ -578,13 +582,10 @@ dsm_attach(dsm_handle h)
 		if (dsm_control->item[i].refcnt == 1)
 			break;
 
-		/* Otherwise, if the descriptor matches, we've found a match. */
-		if (dsm_control->item[i].handle == seg->handle)
-		{
-			dsm_control->item[i].refcnt++;
-			seg->control_slot = i;
-			break;
-		}
+		/* Otherwise we've found a match. */
+		dsm_control->item[i].refcnt++;
+		seg->control_slot = i;
+		break;
 	}
 	LWLockRelease(DynamicSharedMemoryControlLock);
 
-- 
1.8.5.rc2.dirty

