start-listen-from-min.patch.txt

text/plain

Filename: start-listen-from-min.patch.txt
Type: text/plain
Part: 0
Message: Re: LISTEN/NOTIFY bug: VACUUM sets frozenxid past a xid in async queue
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 4bd37d5beb5..586d5609c51 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -1041,6 +1041,8 @@ static void
 Exec_ListenPreCommit(void)
 {
 	QueuePosition head;
+	QueuePosition startPos;
+	QueuePosition min;
 	QueuePosition max;
 	ProcNumber	prevListener;
 
@@ -1085,18 +1087,20 @@ Exec_ListenPreCommit(void)
 	 * and manipulate the list links.
 	 */
 	LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);
-	head = QUEUE_HEAD;
+	head = min = QUEUE_HEAD;
 	max = QUEUE_TAIL;
 	prevListener = INVALID_PROC_NUMBER;
 	for (ProcNumber i = QUEUE_FIRST_LISTENER; i != INVALID_PROC_NUMBER; i = QUEUE_NEXT_LISTENER(i))
 	{
+		min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
 		if (QUEUE_BACKEND_DBOID(i) == MyDatabaseId)
 			max = QUEUE_POS_MAX(max, QUEUE_BACKEND_POS(i));
 		/* Also find last listening backend before this one */
 		if (i < MyProcNumber)
 			prevListener = i;
 	}
-	QUEUE_BACKEND_POS(MyProcNumber) = max;
+	startPos = QUEUE_POS_MAX(min, max);
+	QUEUE_BACKEND_POS(MyProcNumber) = startPos;
 	QUEUE_BACKEND_PID(MyProcNumber) = MyProcPid;
 	QUEUE_BACKEND_DBOID(MyProcNumber) = MyDatabaseId;
 	/* Insert backend into list of listeners at correct position */
@@ -1123,7 +1127,7 @@ Exec_ListenPreCommit(void)
 	 * our transaction might have executed NOTIFY, those message(s) aren't
 	 * queued yet so we won't skip them here.
 	 */
-	if (!QUEUE_POS_EQUAL(max, head))
+	if (!QUEUE_POS_EQUAL(startPos, head))
 		asyncQueueReadAllNotifications();
 }