From a78b5d691cb7abb7819c39ca1a58c34e85295fd7 Mon Sep 17 00:00:00 2001
From: Manuel Reyes Bravo <manuelreyesbravo@gmail.com>
Date: Tue, 17 Nov 2026 16:35:43 -0300
Subject: [PATCH v2] Fix IO tracking of temporary relations in single-user mode

pgstat_tracks_io_object() lists B_STANDALONE_BACKEND among the backend
types that never do IO on temporary relations, since 28e626bde00 (v16).
A backend in single-user mode executes queries like a client backend,
and can create and use temporary tables and sequences.  With assertions
enabled, an INSERT into a temporary table, CREATE TEMP TABLE AS or
nextval() on a temporary sequence fails the assertion in
pgstat_count_io_op():

  $ echo "CREATE TEMP TABLE t AS SELECT 1;" | postgres --single postgres
  TRAP: failed Assert("pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op)")

Without assertions, this IO is not counted: pg_stat_io has no
"temp relation" row for "standalone backend".

Remove B_STANDALONE_BACKEND from this list, and fix the comment above
it.

Discussion:
---
 src/backend/utils/activity/pgstat_io.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/activity/pgstat_io.c b/src/backend/utils/activity/pgstat_io.c
index 9d6e067382..8293949874 100644
--- a/src/backend/utils/activity/pgstat_io.c
+++ b/src/backend/utils/activity/pgstat_io.c
@@ -376,16 +376,16 @@ pgstat_tracks_io_object(BackendType bktype, IOObject io_object,
 		return false;
 
 	/*
-	 * In core Postgres, only regular backends and WAL Sender processes
-	 * executing queries will use local buffers and operate on temporary
-	 * relations. Parallel workers will not use local buffers (see
-	 * InitLocalBuffers()); however, extensions leveraging background workers
-	 * have no such limitation, so track IO on IOOBJECT_TEMP_RELATION for
-	 * BackendType B_BG_WORKER.
+	 * In core Postgres, only regular backends, WAL Sender processes executing
+	 * queries and standalone backends (single-user mode) will use local
+	 * buffers and operate on temporary relations. Parallel workers will not
+	 * use local buffers (see InitLocalBuffers()); however, extensions
+	 * leveraging background workers have no such limitation, so track IO on
+	 * IOOBJECT_TEMP_RELATION for BackendType B_BG_WORKER.
 	 */
 	no_temp_rel = bktype == B_AUTOVAC_LAUNCHER || bktype == B_BG_WRITER ||
 		bktype == B_CHECKPOINTER || bktype == B_AUTOVAC_WORKER ||
-		bktype == B_STANDALONE_BACKEND || bktype == B_STARTUP;
+		bktype == B_STARTUP;
 
 	if (no_temp_rel && io_context == IOCONTEXT_NORMAL &&
 		io_object == IOOBJECT_TEMP_RELATION)
-- 
2.56.1

