From 4035675111869c637883848a23b60cc1c64b473b Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 2 Feb 2026 16:12:06 +0200
Subject: [PATCH 1/1] Fix pg_stat_get_backend_wait_event() for aux processes

The pg_stat_activity view shows the information for aux processes, but
the pg_stat_get_backend_wait_event() and
pg_stat_get_backend_wait_event_type() functions did not.

The straightforward fix would've been to call AuxiliaryPidGetProc(pid)
if BackendPidGetProc(pid) returns NULL, like we do in
pg_stat_get_activity(). It's a little inefficient though: we already
have the ProcNumber of the process, so we're really only using
BackendPidGetProc(pid) to check if the process is still alive. Make
that check a little cheaper while we're at it.
---
 src/backend/utils/adt/pgstatfuncs.c | 30 +++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 73ca0bb0b7f..2a573b603bd 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -817,15 +817,24 @@ pg_stat_get_backend_wait_event_type(PG_FUNCTION_ARGS)
 {
 	int32		procNumber = PG_GETARG_INT32(0);
 	PgBackendStatus *beentry;
-	PGPROC	   *proc;
 	const char *wait_event_type = NULL;
 
 	if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
 		wait_event_type = "<backend information not available>";
 	else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
 		wait_event_type = "<insufficient privilege>";
-	else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
-		wait_event_type = pgstat_get_wait_event_type(proc->wait_event_info);
+	else
+	{
+		PGPROC	   *proc = GetPGProcByNumber(procNumber);
+		uint32		raw_wait_event;
+
+		raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
+
+		/* check if the process is still running */
+		pg_read_barrier();
+		if (proc->pid == beentry->st_procpid)
+			wait_event_type = pgstat_get_wait_event_type(raw_wait_event);
+	}
 
 	if (!wait_event_type)
 		PG_RETURN_NULL();
@@ -838,15 +847,24 @@ pg_stat_get_backend_wait_event(PG_FUNCTION_ARGS)
 {
 	int32		procNumber = PG_GETARG_INT32(0);
 	PgBackendStatus *beentry;
-	PGPROC	   *proc;
 	const char *wait_event = NULL;
 
 	if ((beentry = pgstat_get_beentry_by_proc_number(procNumber)) == NULL)
 		wait_event = "<backend information not available>";
 	else if (!HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
 		wait_event = "<insufficient privilege>";
-	else if ((proc = BackendPidGetProc(beentry->st_procpid)) != NULL)
-		wait_event = pgstat_get_wait_event(proc->wait_event_info);
+	else
+	{
+		PGPROC	   *proc = GetPGProcByNumber(procNumber);
+		uint32		raw_wait_event;
+
+		raw_wait_event = UINT32_ACCESS_ONCE(proc->wait_event_info);
+
+		/* check if the process is still running */
+		pg_read_barrier();
+		if (proc->pid == beentry->st_procpid)
+			wait_event = pgstat_get_wait_event(raw_wait_event);
+	}
 
 	if (!wait_event)
 		PG_RETURN_NULL();
-- 
2.47.3

