From 748d8fca4c396b6e96d500ecdf4a3048bce97d60 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Herrera?= <alvherre@alvh.no-ip.org>
Date: Thu, 20 Mar 2025 15:46:14 +0100
Subject: [PATCH v09 2/9] Move progress-related fields from PgBackendStatus to
 PgBackendProgress

---
 src/backend/access/heap/vacuumlazy.c          |  4 +--
 src/backend/commands/analyze.c                |  2 +-
 src/backend/utils/activity/backend_progress.c | 33 ++++++++++---------
 src/backend/utils/activity/backend_status.c   |  9 ++---
 src/backend/utils/adt/pgstatfuncs.c           |  6 ++--
 src/include/utils/backend_progress.h          | 15 ++++++++-
 src/include/utils/backend_status.h            | 14 ++------
 7 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 2cbcf5e5db2..76c8ec15dde 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -1107,10 +1107,10 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
 				 * We bypass the changecount mechanism because this value is
 				 * only updated by the calling process.  We also rely on the
 				 * above call to pgstat_progress_end_command() to not clear
-				 * the st_progress_param array.
+				 * the st_progress.p_param array.
 				 */
 				appendStringInfo(&buf, _("delay time: %.3f ms\n"),
-								 (double) MyBEEntry->st_progress_param[PROGRESS_VACUUM_DELAY_TIME] / 1000000.0);
+								 (double) MyBEEntry->st_progress.p_param[PROGRESS_VACUUM_DELAY_TIME] / 1000000.0);
 			}
 			if (track_io_timing)
 			{
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 2b5fbdcbd82..8d88b665f18 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -815,7 +815,7 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
 				 * only updated by the calling process.
 				 */
 				appendStringInfo(&buf, _("delay time: %.3f ms\n"),
-								 (double) MyBEEntry->st_progress_param[PROGRESS_ANALYZE_DELAY_TIME] / 1000000.0);
+								 (double) MyBEEntry->st_progress.p_param[PROGRESS_ANALYZE_DELAY_TIME] / 1000000.0);
 			}
 			if (track_io_timing)
 			{
diff --git a/src/backend/utils/activity/backend_progress.c b/src/backend/utils/activity/backend_progress.c
index 99a8c73bf04..17b5d87446b 100644
--- a/src/backend/utils/activity/backend_progress.c
+++ b/src/backend/utils/activity/backend_progress.c
@@ -19,8 +19,8 @@
 /*-----------
  * pgstat_progress_start_command() -
  *
- * Set st_progress_command (and st_progress_command_target) in own backend
- * entry.  Also, zero-initialize st_progress_param array.
+ * Set st_progress.p_command (and st_progress.p_command_target) in own backend
+ * entry.  Also, zero-initialize st_progress.p_param array.
  *-----------
  */
 void
@@ -32,16 +32,17 @@ pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
 		return;
 
 	PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
-	beentry->st_progress_command = cmdtype;
-	beentry->st_progress_command_target = relid;
-	MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
+	beentry->st_progress.p_command = cmdtype;
+	beentry->st_progress.p_command_target = relid;
+	MemSet(&beentry->st_progress.p_param, 0,
+		   sizeof(beentry->st_progress.p_param));
 	PGSTAT_END_WRITE_ACTIVITY(beentry);
 }
 
 /*-----------
  * pgstat_progress_update_param() -
  *
- * Update index'th member in st_progress_param[] of own backend entry.
+ * Update index'th member in st_progress.p_param[] of own backend entry.
  *-----------
  */
 void
@@ -55,14 +56,14 @@ pgstat_progress_update_param(int index, int64 val)
 		return;
 
 	PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
-	beentry->st_progress_param[index] = val;
+	beentry->st_progress.p_param[index] = val;
 	PGSTAT_END_WRITE_ACTIVITY(beentry);
 }
 
 /*-----------
  * pgstat_progress_incr_param() -
  *
- * Increment index'th member in st_progress_param[] of own backend entry.
+ * Increment index'th member in st_progress.p_param[] of own backend entry.
  *-----------
  */
 void
@@ -76,7 +77,7 @@ pgstat_progress_incr_param(int index, int64 incr)
 		return;
 
 	PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
-	beentry->st_progress_param[index] += incr;
+	beentry->st_progress.p_param[index] += incr;
 	PGSTAT_END_WRITE_ACTIVITY(beentry);
 }
 
@@ -113,7 +114,7 @@ pgstat_progress_parallel_incr_param(int index, int64 incr)
 /*-----------
  * pgstat_progress_update_multi_param() -
  *
- * Update multiple members in st_progress_param[] of own backend entry.
+ * Update multiple members in st_progress.p_param[] of own backend entry.
  * This is atomic; readers won't see intermediate states.
  *-----------
  */
@@ -133,7 +134,7 @@ pgstat_progress_update_multi_param(int nparam, const int *index,
 	{
 		Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
 
-		beentry->st_progress_param[index[i]] = val[i];
+		beentry->st_progress.p_param[index[i]] = val[i];
 	}
 
 	PGSTAT_END_WRITE_ACTIVITY(beentry);
@@ -142,8 +143,8 @@ pgstat_progress_update_multi_param(int nparam, const int *index,
 /*-----------
  * pgstat_progress_end_command() -
  *
- * Reset st_progress_command (and st_progress_command_target) in own backend
- * entry.  This signals the end of the command.
+ * Reset st_progress.p_command (and st_progress.p_command_target) in own
+ * backend entry.  This signals the end of the command.
  *-----------
  */
 void
@@ -154,11 +155,11 @@ pgstat_progress_end_command(void)
 	if (!beentry || !pgstat_track_activities)
 		return;
 
-	if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID)
+	if (beentry->st_progress.p_command == PROGRESS_COMMAND_INVALID)
 		return;
 
 	PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
-	beentry->st_progress_command = PROGRESS_COMMAND_INVALID;
-	beentry->st_progress_command_target = InvalidOid;
+	beentry->st_progress.p_command = PROGRESS_COMMAND_INVALID;
+	beentry->st_progress.p_command_target = InvalidOid;
 	PGSTAT_END_WRITE_ACTIVITY(beentry);
 }
diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c
index 7681b4ba5a9..db2b4391969 100644
--- a/src/backend/utils/activity/backend_status.c
+++ b/src/backend/utils/activity/backend_status.c
@@ -318,13 +318,14 @@ pgstat_bestart_initial(void)
 	lbeentry.st_gss = false;
 
 	lbeentry.st_state = STATE_STARTING;
-	lbeentry.st_progress_command = PROGRESS_COMMAND_INVALID;
-	lbeentry.st_progress_command_target = InvalidOid;
 	lbeentry.st_query_id = UINT64CONST(0);
 
+	lbeentry.st_progress.p_command = PROGRESS_COMMAND_INVALID;
+	lbeentry.st_progress.p_command_target = InvalidOid;
+
 	/*
-	 * we don't zero st_progress_param here to save cycles; nobody should
-	 * examine it until st_progress_command has been set to something other
+	 * we don't zero st_progress.p_param here to save cycles; nobody should
+	 * examine it until st_progress.p_command has been set to something other
 	 * than PROGRESS_COMMAND_INVALID
 	 */
 
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index ed24efc1a65..0f27780abae 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -299,7 +299,7 @@ pg_stat_get_progress_info(PG_FUNCTION_ARGS)
 		 * Report values for only those backends which are running the given
 		 * command.
 		 */
-		if (beentry->st_progress_command != cmdtype)
+		if (beentry->st_progress.p_command != cmdtype)
 			continue;
 
 		/* Value available to all callers */
@@ -309,9 +309,9 @@ pg_stat_get_progress_info(PG_FUNCTION_ARGS)
 		/* show rest of the values including relid only to role members */
 		if (HAS_PGSTAT_PERMISSIONS(beentry->st_userid))
 		{
-			values[2] = ObjectIdGetDatum(beentry->st_progress_command_target);
+			values[2] = ObjectIdGetDatum(beentry->st_progress.p_command_target);
 			for (i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
-				values[i + 3] = Int64GetDatum(beentry->st_progress_param[i]);
+				values[i + 3] = Int64GetDatum(beentry->st_progress.p_param[i]);
 		}
 		else
 		{
diff --git a/src/include/utils/backend_progress.h b/src/include/utils/backend_progress.h
index da3d14bb97b..10aaec9b15c 100644
--- a/src/include/utils/backend_progress.h
+++ b/src/include/utils/backend_progress.h
@@ -31,8 +31,21 @@ typedef enum ProgressCommandType
 	PROGRESS_COMMAND_COPY,
 } ProgressCommandType;
 
-#define PGSTAT_NUM_PROGRESS_PARAM	20
 
+/*
+ * Any command which wishes can advertise that it is running by setting
+ * command, command_target, and param[].  command_target should be the OID of
+ * the relation which the command targets (we assume there's just one, as this
+ * is meant for utility commands), but the meaning of each element in the
+ * param array is command-specific.
+ */
+#define PGSTAT_NUM_PROGRESS_PARAM	20
+typedef struct PgBackendProgress
+{
+	ProgressCommandType p_command;
+	Oid			p_command_target;
+	int64		p_param[PGSTAT_NUM_PROGRESS_PARAM];
+} PgBackendProgress;
 
 extern void pgstat_progress_start_command(ProgressCommandType cmdtype,
 										  Oid relid);
diff --git a/src/include/utils/backend_status.h b/src/include/utils/backend_status.h
index 1c9b4fe14d0..8e024274d76 100644
--- a/src/include/utils/backend_status.h
+++ b/src/include/utils/backend_status.h
@@ -156,18 +156,8 @@ typedef struct PgBackendStatus
 	 */
 	char	   *st_activity_raw;
 
-	/*
-	 * Command progress reporting.  Any command which wishes can advertise
-	 * that it is running by setting st_progress_command,
-	 * st_progress_command_target, and st_progress_param[].
-	 * st_progress_command_target should be the OID of the relation which the
-	 * command targets (we assume there's just one, as this is meant for
-	 * utility commands), but the meaning of each element in the
-	 * st_progress_param array is command-specific.
-	 */
-	ProgressCommandType st_progress_command;
-	Oid			st_progress_command_target;
-	int64		st_progress_param[PGSTAT_NUM_PROGRESS_PARAM];
+	/* Command progress reporting. */
+	PgBackendProgress	st_progress;
 
 	/* query identifier, optionally computed using post_parse_analyze_hook */
 	uint64		st_query_id;
-- 
2.39.5

