From 9702054c37aa80cb60a16b2d80a475e9b27b087a Mon Sep 17 00:00:00 2001
From: Soumyadeep Chakraborty <soumyadeep2007@gmail.com>
Date: Wed, 7 Feb 2024 18:04:43 -0800
Subject: [PATCH v1 1/1] Use memory barrier in DisownLatch

Failing to do so may cause another process undergoing startup to see our
pid associated with the latch during OwnLatch's sanity check.
---
 src/backend/storage/ipc/latch.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index d8a69990b3a..332c5e0b2cf 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -493,6 +493,12 @@ DisownLatch(Latch *latch)
 	Assert(latch->owner_pid == MyProcPid);
 
 	latch->owner_pid = 0;
+
+	/*
+	 * Ensure that another backend reusing this PGPROC during startup sees that
+	 * owner_pid = 0 and doesn't blow up in OwnLatch().
+	 */
+	pg_memory_barrier();
 }
 
 /*
-- 
2.34.1

