From ca798b7828c655198f1c39030ce901a4437e5979 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Tue, 17 Nov 2026 00:18:18 +0200
Subject: [PATCH 3/3] Add a test for losing a sinval catchup signal

Add an injection point in ReceiveSharedInvalidMessages() after it has
caught up, and a TAP test that stops an idle backend there, makes it
fall behind again so that it's sent a second catchup signal, and checks
that the backend processes the second request after it continues, and
that it's sent further catchup signals after that.

With the extra sleep in the test, this also covers the assertion failure
---
 src/backend/storage/ipc/sinval.c              |  12 ++
 src/test/modules/test_misc/meson.build        |   1 +
 .../modules/test_misc/t/016_sinval_catchup.pl | 131 ++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 src/test/modules/test_misc/t/016_sinval_catchup.pl

diff --git a/src/backend/storage/ipc/sinval.c b/src/backend/storage/ipc/sinval.c
index 756776c8732..6c5af2951b4 100644
--- a/src/backend/storage/ipc/sinval.c
+++ b/src/backend/storage/ipc/sinval.c
@@ -18,6 +18,7 @@
 #include "miscadmin.h"
 #include "storage/latch.h"
 #include "storage/sinvaladt.h"
+#include "utils/injection_point.h"
 #include "utils/inval.h"
 
 
@@ -78,6 +79,7 @@ ReceiveSharedInvalidMessages(void (*invalFunction) (SharedInvalidationMessage *m
 	 */
 	static volatile int nextmsg = 0;
 	static volatile int nummsgs = 0;
+	bool		read_any = false;
 
 	/* Deal with any messages still pending from an outer recursion */
 	while (nextmsg < nummsgs)
@@ -96,6 +98,8 @@ ReceiveSharedInvalidMessages(void (*invalFunction) (SharedInvalidationMessage *m
 
 		/* Try to get some more messages */
 		getResult = SIGetDataEntries(messages, MAXINVALMSGS);
+		if (getResult != 0)
+			read_any = true;
 
 		if (getResult < 0)
 		{
@@ -124,6 +128,14 @@ ReceiveSharedInvalidMessages(void (*invalFunction) (SharedInvalidationMessage *m
 		 */
 	} while (nummsgs == MAXINVALMSGS);
 
+	/*
+	 * Allow tests to pause here, after we have caught up, but before we pass
+	 * on any catchup request.  Only if we read any messages or a reset, so
+	 * that this is not reached on every call.
+	 */
+	if (read_any)
+		INJECTION_POINT("sinval-caught-up", NULL);
+
 	/*
 	 * We are now caught up.  If we received a catchup signal, call
 	 * SICleanupQueue().  This is not so much because we need to flush dead
diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build
index 5d81f5b13be..0e97a511909 100644
--- a/src/test/modules/test_misc/meson.build
+++ b/src/test/modules/test_misc/meson.build
@@ -24,6 +24,7 @@ tests += {
       't/013_temp_obj_multisession.pl',
       't/014_log_statement_max_length.pl',
       't/015_temp_schema_exit_deferrable.pl',
+      't/016_sinval_catchup.pl',
     ],
     # The injection points are cluster-wide, so disable installcheck
     'runningcheck': false,
diff --git a/src/test/modules/test_misc/t/016_sinval_catchup.pl b/src/test/modules/test_misc/t/016_sinval_catchup.pl
new file mode 100644
index 00000000000..5850286de22
--- /dev/null
+++ b/src/test/modules/test_misc/t/016_sinval_catchup.pl
@@ -0,0 +1,131 @@
+
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test that a shared invalidation catchup request is not lost, if it arrives
+# while the backend is still processing a previous catchup request.
+#
+# When a backend falls far behind in the shared invalidation queue,
+# SICleanupQueue() sends it a catchup signal and sets its "signaled" flag.
+# The backend clears the flag when it has caught up, and as long as it is
+# set, no further catchup signals are sent to it.  If a new signal arrives
+# after the backend has caught up and cleared the flag, but before it has
+# finished processing the previous request, the new request must not be
+# lost.  Otherwise the backend is left with the flag set but no pending
+# request, and won't be sent any more catchup signals while it stays idle.
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+	plan skip_all => 'Injection points not supported by this build';
+}
+
+my $node = PostgreSQL::Test::Cluster->new('node');
+$node->init();
+
+# The idle session below should be the backend furthest behind in the
+# invalidation queue, so that it's the one that gets sent catchup requests.
+# Keep autovacuum out of the way.
+$node->append_conf('postgresql.conf', 'autovacuum = off');
+$node->append_conf('postgresql.conf', 'log_min_messages = debug4');
+$node->start;
+
+if (!$node->check_extension('injection_points'))
+{
+	plan skip_all => 'Extension injection_points not installed';
+}
+
+$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
+
+# Start the session that we'll observe.  It logs at DEBUG4 so that we can see
+# when it processes catchup requests.  Attach the injection point locally,
+# so that only this session waits on it.  After the setup, the session sits
+# idle, waiting for a command from the client, which is when it processes
+# catchup requests.
+my $idle = $node->background_psql('postgres');
+my $idle_pid = $idle->query_safe('SELECT pg_backend_pid();');
+$idle->query_safe(
+	q{
+	SELECT injection_points_set_local();
+	SELECT injection_points_attach('sinval-caught-up', 'wait');
+});
+
+my $sent_re = qr/sending sinval catchup signal to PID $idle_pid\b/;
+my $processed_re = qr/\[$idle_pid\] .*DEBUG:  ProcessCatchupEvent/;
+
+# Count the catchup requests sent to the idle session so far.
+sub requests_sent
+{
+	my $log = slurp_file($node->logfile);
+	my $count = () = $log =~ /$sent_re/g;
+	return $count;
+}
+
+# Generate invalidation messages, in batches, until the idle session has been
+# sent at least $target catchup requests.  The sending backend logs at DEBUG4
+# too, so that we can see the requests in the log.
+sub generate_invals_until_sent
+{
+	my ($target) = @_;
+
+	for (my $i = 0; $i < 1000; $i++)
+	{
+		return 1 if requests_sent() >= $target;
+		$node->safe_psql(
+			'postgres', q{
+			DO $$
+			BEGIN
+				FOR i IN 1..10 LOOP
+					EXECUTE 'CREATE TABLE sinval_t (a int)';
+					EXECUTE 'DROP TABLE sinval_t';
+				END LOOP;
+			END $$;
+		});
+	}
+	return requests_sent() >= $target;
+}
+
+# Make the idle session fall behind, so that it gets a catchup request.  It
+# catches up, and then stops at the injection point, before it has finished
+# processing the request.
+ok(generate_invals_until_sent(1),
+	'idle session was sent a catchup request');
+$node->wait_for_event('client backend', 'sinval-caught-up');
+
+# While it's stopped there, make it fall behind again, so that it gets a
+# second catchup request.  It has already caught up, so it's eligible for a
+# new request.
+ok(generate_invals_until_sent(2),
+	'idle session was sent a second catchup request while processing the first'
+);
+
+# This sleep makes the test hit the assertion failure
+sleep(10);
+
+# Let it continue.  Detach first, so that it doesn't stop again.  It should
+# now process the second request too.
+my $log_offset = -s $node->logfile;
+$node->safe_psql(
+	'postgres', q{
+	SELECT injection_points_detach('sinval-caught-up');
+	SELECT injection_points_wakeup('sinval-caught-up');
+});
+$node->wait_for_log($processed_re, $log_offset);
+pass('idle session processed the second catchup request');
+
+# Once it has caught up again, it should get further catchup requests as
+# usual.  If the second request had been lost, its "signaled" flag would
+# still be set, and it would never be sent another one.
+ok(generate_invals_until_sent(3),
+	'idle session was sent a catchup request again');
+
+# The session is still usable.
+is($idle->query_safe('SELECT 1;'), '1', 'idle session still works');
+$idle->quit;
+
+done_testing();
-- 
2.47.3

