From 4d6158f7d9829dd5a909ee44748d7670e82b9983 Mon Sep 17 00:00:00 2001
From: Pavan Deolasee <pavan.deolasee@gmail.com>
Date: Thu, 17 Mar 2022 12:37:11 +0530
Subject: [PATCH 1/1] Flush the queue even if receiver has not attached.

The new API introduced in PG15 delays flushing shared memoey queue
unless the caller has requested a `force_flush`. But if the
receiver has not yet attached to the queue, we fail to enforce
`force_flush` and return with success without actually flushing the
queue. This commit fixes the bug.
---
 src/backend/storage/ipc/shm_mq.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/backend/storage/ipc/shm_mq.c b/src/backend/storage/ipc/shm_mq.c
index 603cf9b0fa7..b0450f68e66 100644
--- a/src/backend/storage/ipc/shm_mq.c
+++ b/src/backend/storage/ipc/shm_mq.c
@@ -528,8 +528,6 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait,
 		SpinLockAcquire(&mq->mq_mutex);
 		receiver = mq->mq_receiver;
 		SpinLockRelease(&mq->mq_mutex);
-		if (receiver == NULL)
-			return SHM_MQ_SUCCESS;
 		mqh->mqh_counterparty_attached = true;
 	}
 
@@ -541,7 +539,9 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait,
 	if (force_flush || mqh->mqh_send_pending > (mq->mq_ring_size >> 2))
 	{
 		shm_mq_inc_bytes_written(mq, mqh->mqh_send_pending);
-		SetLatch(&receiver->procLatch);
+		/* If the receiver is attached, signal it. */
+		if (receiver != NULL)
+			SetLatch(&receiver->procLatch);
 		mqh->mqh_send_pending = 0;
 	}
 
-- 
2.30.1 (Apple Git-130)

