From 53d7d2f4d388e6d4df68d6fef60b25fbd7cc6be8 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Tue, 17 Nov 2026 00:18:14 +0200
Subject: [PATCH 1/3] Fix losing a sinval catchup signal that arrives during
 catchup

If a new catchup signal arrives while we're still processing a
previous catchup signal, the new signal might be lost:

When a backend falls far behind in the shared invalidation queue,
SICleanupQueue() sets its "signaled" flag, and sends it
PROCSIG_CATCHUP_INTERRUPT, which sets the catchupInterruptPending flag
in the receiving backend.  catchupInterruptPending is checked in the
receiving backend when it's idle, to process the catchup request.  No
further catchup signals are sent to the backend while the "signaled"
flag is set.  SIGetDataEntries() clears "signaled" when the backend
has caught up, after which it can be signaled again.

There was a race condition in how ReceiveSharedInvalidMessages() reset
catchupInterruptPending and "signaled".  It cleared "signaled" first,
after consuming all the messages, and catchupInterruptPending only
after that.  If a new catchup signal arrived after the "signaled" flag
was cleared in SIGetDataEntries(), but before clearing
catchupInterruptPending, the backend could be left with its "signaled"
flag set but catchupInterruptPending not set.  As a result, other
backends would not send it any more catchup signals, but it would not
wake up to consume the messages until AcceptInvalidationMessages() was
called for some other reason, typically when it next ran a query.

To fix, reset catchupInterruptPending only if "signaled" was not
concurrently re-armed.  Move the responsibility for resetting it into
SICleanupQueue() itself, because it already holds the lock needed to
read "signaled".
---
 src/backend/storage/ipc/sinval.c    | 16 +++++++++-------
 src/backend/storage/ipc/sinvaladt.c | 16 ++++++++++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/backend/storage/ipc/sinval.c b/src/backend/storage/ipc/sinval.c
index 1540c7e0696..756776c8732 100644
--- a/src/backend/storage/ipc/sinval.c
+++ b/src/backend/storage/ipc/sinval.c
@@ -125,16 +125,18 @@ ReceiveSharedInvalidMessages(void (*invalFunction) (SharedInvalidationMessage *m
 	} while (nummsgs == MAXINVALMSGS);
 
 	/*
-	 * We are now caught up.  If we received a catchup signal, reset that
-	 * flag, and call SICleanupQueue().  This is not so much because we need
-	 * to flush dead messages right now, as that we want to pass on the
-	 * catchup signal to the next slowest backend.  "Daisy chaining" the
-	 * catchup signal this way avoids creating spikes in system load for what
-	 * should be just a background maintenance activity.
+	 * We are now caught up.  If we received a catchup signal, call
+	 * SICleanupQueue().  This is not so much because we need to flush dead
+	 * messages right now, as that we want to pass on the catchup signal to
+	 * the next slowest backend.  "Daisy chaining" the catchup signal this way
+	 * avoids creating spikes in system load for what should be just a
+	 * background maintenance activity.
+	 *
+	 * SICleanupQueue() resets catchupInterruptPending, unless we got signaled
+	 * again in the meanwhile.
 	 */
 	if (catchupInterruptPending)
 	{
-		catchupInterruptPending = false;
 		elog(DEBUG4, "sinval catchup complete, cleaning queue");
 		SICleanupQueue(false, 0);
 	}
diff --git a/src/backend/storage/ipc/sinvaladt.c b/src/backend/storage/ipc/sinvaladt.c
index 16880e7cc4a..4cc270ac629 100644
--- a/src/backend/storage/ipc/sinvaladt.c
+++ b/src/backend/storage/ipc/sinvaladt.c
@@ -556,6 +556,8 @@ SIGetDataEntries(SharedInvalidationMessage *data, int datasize)
  * backends as "reset" in the array, and sending PROCSIG_CATCHUP_INTERRUPT
  * to some backend that seems to be getting too far behind.  We signal at
  * most one backend at a time, for reasons explained at the top of the file.
+ * We also clear catchupInterruptPending, unless this backend has been
+ * signaled again since we last caught up.
  *
  * Caution: because we transiently release write lock when we have to signal
  * some other backend, it is NOT guaranteed that there are still minFree
@@ -655,6 +657,20 @@ SICleanupQueue(bool callerHasWriteLock, int minFree)
 	else
 		segP->nextThreshold = (numMsgs / CLEANUP_QUANTUM + 1) * CLEANUP_QUANTUM;
 
+	/*
+	 * Clear our local catchupInterruptPending flag, now that we have
+	 * processed the catchup request.
+	 *
+	 * "signaled" is expected to be false if we were called in response to a
+	 * catchup signal, because the caller consumes all the messages for this
+	 * backend before performing cleanup.  But it's possible that more
+	 * messages arrived after that, causing us to be signaled again.  Don't
+	 * clear catchupInterruptPending in that case, or we'd miss the new
+	 * catchup request.
+	 */
+	if (!segP->procState[MyProcNumber].signaled)
+		catchupInterruptPending = false;
+
 	/*
 	 * Lastly, signal anyone who needs a catchup interrupt.  Since
 	 * SendProcSignal() might not be fast, we don't want to hold locks while
-- 
2.47.3

