diff --git a/src/backend/storage/ipc/shm_mq.c b/src/backend/storage/ipc/shm_mq.c
index 126cb07..39ea673 100644
--- a/src/backend/storage/ipc/shm_mq.c
+++ b/src/backend/storage/ipc/shm_mq.c
@@ -803,6 +808,22 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, const void *data,
 					return SHM_MQ_DETACHED;
 				}
 				mqh->mqh_counterparty_attached = true;
+
+				/*
+				 * Re-check if the queue is still full.
+				 *
+				 * While we were using our procLatch to detect receiver's
+				 * connection, the receiver could have connected and started
+				 * reading from it - that includes concurrent manipulation of
+				 * the latch, in order to report on reading activity. Thus we
+				 * could miss the information that some data has already been
+				 * consumed, and cause a deadlock by calling SetLatch() below.
+				 *
+				 * (If the receiver starts waiting on its latch soon enough,
+				 * our call of shm_mq_notify_receiver() will have no effect.)
+				 */
+				if (!nowait)
+					continue;
 			}
 
 			/* Let the receiver know that we need them to read some data. */
