From dccc076e43fb502fbc515b43ff1ca7e9ace14a1d Mon Sep 17 00:00:00 2001
From: Antonin Houska <ah@cybertec.at>
Date: Sat, 21 Mar 2026 16:32:07 +0100
Subject: [PATCH] Teach isolation tester about injection points in background workers.

When session gets blocked, the isolation tester checks if the corresponding
backend is waiting on a lock or on an injection point. If the backend launched
a background worker, the isolation tester is able to detect when the worker is
waiting on a lock - this is due to the concept of "lock groups". However, it
does not recoginze that the background worker is waiting on an injection
point.

This patch tries to fix the problem by calling
pg_isolation_test_session_is_blocked() not only for the backend, but also for
its background worker(s). The assumption is that if the backend is blocked,
and at the same time its background worker is waiting on an injection point,
then that waiting is probably the reason for the backend to be blocked.
---
 src/test/isolation/isolationtester.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index 440c875b8ac..8f17ee412c9 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -216,15 +216,22 @@ main(int argc, char **argv)
 	 * exactly expect concurrent use of test tables.  However, autovacuum will
 	 * occasionally take AccessExclusiveLock to truncate a table, and we must
 	 * ignore that transient wait.
+	 *
+	 * If the session's backend is blocked, and if its background worker is
+	 * waiting on an injection point, we assume that the injection point is
+	 * the reason for the backend to be blocked. That's what we check in the
+	 * second query of the UNION. XXX Should we use a separate query for that?
 	 */
 	initPQExpBuffer(&wait_query);
 	appendPQExpBufferStr(&wait_query,
+						 "WITH blocking(res) AS ("
 						 "SELECT pg_catalog.pg_isolation_test_session_is_blocked($1, '{");
 	/* The spec syntax requires at least one session; assume that here. */
 	appendPQExpBufferStr(&wait_query, conns[1].backend_pid_str);
 	for (i = 2; i < nconns; i++)
 		appendPQExpBuffer(&wait_query, ",%s", conns[i].backend_pid_str);
-	appendPQExpBufferStr(&wait_query, "}')");
+	appendPQExpBufferStr(&wait_query, "}') UNION "
+						 "SELECT pg_catalog.pg_isolation_test_session_is_blocked(pid, '{}') FROM pg_stat_activity WHERE leader_pid=$1) SELECT bool_or(res) FROM blocking");
 
 	res = PQprepare(conns[0].conn, PREP_WAITING, wait_query.data, 0, NULL);
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
-- 
2.47.3

