From f31e5efb33a82cd2fc646f9717d46682ec4c5471 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20St=C3=A4hlin?= <me@packi.ch>
Date: Thu, 24 Jul 2025 15:16:00 +0200
Subject: [PATCH] Do not modify wait events we never added

Commit 84e5b2f07a5e8ba983ff0f6e71b063b27f45f346 added a new wait event
if we're running under postmaster but it seems like we always reference
 LatchWaitSetPostmasterDeathPos in WaitLatch even if we didn't add the
WaitEvent.

This caused postgres in --single mode to crash when waiting in
WaitForProcSignalBarrier after dropping a database.

Co-authored-by: Ronan Dunklau <ronan.dunklau@aiven.io>
---
 src/backend/storage/ipc/latch.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index c6aefd2f688..f4e17ab526b 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -187,9 +187,10 @@ WaitLatch(Latch *latch, int wakeEvents, long timeout,
 	if (!(wakeEvents & WL_LATCH_SET))
 		latch = NULL;
 	ModifyWaitEvent(LatchWaitSet, LatchWaitSetLatchPos, WL_LATCH_SET, latch);
-	ModifyWaitEvent(LatchWaitSet, LatchWaitSetPostmasterDeathPos,
-					(wakeEvents & (WL_EXIT_ON_PM_DEATH | WL_POSTMASTER_DEATH)),
-					NULL);
+	if (IsUnderPostmaster)
+		ModifyWaitEvent(LatchWaitSet, LatchWaitSetPostmasterDeathPos,
+						(wakeEvents & (WL_EXIT_ON_PM_DEATH | WL_POSTMASTER_DEATH)),
+						NULL);
 
 	if (WaitEventSetWait(LatchWaitSet,
 						 (wakeEvents & WL_TIMEOUT) ? timeout : -1,
-- 
2.48.1

