From 3ef887301927fe1c4215c8950f0b706b9454c5f0 Mon Sep 17 00:00:00 2001
From: Joel Jacobson <joel@compiler.org>
Date: Tue, 25 Nov 2025 10:10:51 +0100
Subject: [PATCH 2/3] Move asyncQueueAdvanceTail from AtCommit to PreCommit

---
 src/backend/commands/async.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 6d753079553..d5676d43a30 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -981,6 +981,21 @@ PreCommit_Notify(void)
 			LWLockRelease(NotifyQueueLock);
 		}
 
+		/*
+		 * If it's time to try to advance the global tail pointer, do that.
+		 *
+		 * (It might seem odd to do this in the sender, when more than likely the
+		 * listeners won't yet have read the messages we just sent.  However,
+		 * there's less contention if only the sender does it, and there is little
+		 * need for urgency in advancing the global tail.  So this typically will
+		 * be clearing out messages that were sent some time ago.)
+		 */
+		if (tryAdvanceTail)
+		{
+			tryAdvanceTail = false;
+			asyncQueueAdvanceTail();
+		}
+
 		/* Note that we don't clear pendingNotifies; AtCommit_Notify will. */
 	}
 }
@@ -1046,21 +1061,6 @@ AtCommit_Notify(void)
 	if (pendingNotifies != NULL)
 		SignalBackends();
 
-	/*
-	 * If it's time to try to advance the global tail pointer, do that.
-	 *
-	 * (It might seem odd to do this in the sender, when more than likely the
-	 * listeners won't yet have read the messages we just sent.  However,
-	 * there's less contention if only the sender does it, and there is little
-	 * need for urgency in advancing the global tail.  So this typically will
-	 * be clearing out messages that were sent some time ago.)
-	 */
-	if (tryAdvanceTail)
-	{
-		tryAdvanceTail = false;
-		asyncQueueAdvanceTail();
-	}
-
 	/* And clean up */
 	ClearPendingActionsAndNotifies();
 }
@@ -2135,9 +2135,6 @@ asyncQueueProcessPageEntries(QueuePosition *current,
 /*
  * Advance the shared queue tail variable to the minimum of all the
  * per-backend tail pointers.  Truncate pg_notify space if possible.
- *
- * This is (usually) called during CommitTransaction(), so it's important for
- * it to have very low probability of failure.
  */
 static void
 asyncQueueAdvanceTail(void)
-- 
2.50.1

