From a499a683c6f4a93bdc58ea05cecb531d2b917cf7 Mon Sep 17 00:00:00 2001
From: Martijn van Oosterhout <oosterhout@fox-it.com>
Date: Fri, 2 Aug 2019 14:22:46 +0200
Subject: [PATCH 3/3] Debugging for NOTIFY

---
 src/backend/commands/async.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 20bed5ca71..597e568c85 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -880,12 +880,20 @@ PreCommit_Notify(void)
 						(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 						 errmsg("too many notifications in the NOTIFY queue")));
 			nextNotify = asyncQueueAddEntries(nextNotify);
+			if (Trace_notify)
+				elog(DEBUG1, "NOTIFY QUEUE = (%d,%d)...(%d,%d)",
+					QUEUE_POS_PAGE(QUEUE_TAIL), QUEUE_POS_OFFSET(QUEUE_TAIL),
+					QUEUE_POS_PAGE(QUEUE_HEAD), QUEUE_POS_OFFSET(QUEUE_HEAD));
 
 			/* If we are advancing to a new page, remember this so after the
 			 * transaction commits we can attempt to advance the tail
 			 * pointer, see ProcessCompletedNotifies() */
 			if (QUEUE_POS_OFFSET(QUEUE_HEAD) == 0)
+			{
+				if (Trace_notify)
+					elog(DEBUG1, "backendTryAdvanceTail -> true");
 				backendTryAdvanceTail = true;
+			}
 			LWLockRelease(AsyncQueueLock);
 		}
 	}
@@ -1194,6 +1202,8 @@ ProcessCompletedNotifies(void)
 		 * queue, so we try to advance the tail ourselves, possibly waking
 		 * up another backend if it is running behind */
 		backendTryAdvanceTail = false;
+		if (Trace_notify)
+			elog(DEBUG1, "backendTryAdvanceTail -> false");
 		asyncQueueAdvanceTail();
 	}
 
@@ -2066,6 +2076,8 @@ asyncQueueAdvanceTail(void)
 	int			slowbackendid = InvalidBackendId;
 	int			slowbackendpid;
 
+	if (Trace_notify)
+		elog(DEBUG1, "asyncQueueAdvanceTail");
 	/* Advance the tail as far as possible, noting if there is a slow
 	 * backend we could kick */
 	LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
@@ -2096,6 +2108,8 @@ asyncQueueAdvanceTail(void)
 		 * AsyncQueueLock; which is unlikely but certainly possible. So we
 		 * just log a low-level debug message if it happens.
 		 */
+		if (Trace_notify)
+			elog(DEBUG1, "waking backend %d (pid %d)", slowbackendid, slowbackendpid);
 		if (SendProcSignal(slowbackendpid, PROCSIG_NOTIFY_INTERRUPT, slowbackendid) < 0)
 			elog(DEBUG3, "could not signal backend with PID %d: %m", slowbackendpid);
 	}
-- 
2.11.0

