From b6108d834dec272440f4bc69ad8a5f170e9054b1 Mon Sep 17 00:00:00 2001
From: Mike Palmiotto <mike.palmiotto@crunchydata.com>
Date: Tue, 17 Mar 2020 18:39:30 +0000
Subject: [PATCH v2 11/12] Get rid of st_beckendType and use MySubprocess

---
 src/backend/postmaster/pgstat.c     |  7 +++----
 src/backend/postmaster/postmaster.c |  1 +
 src/backend/utils/adt/pgstatfuncs.c | 10 ++++++----
 src/include/pgstat.h                |  3 ---
 4 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 6d4f2ace56..6bb063420f 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -2820,7 +2820,6 @@ pgstat_bestart(void)
 	 * out-of-line data.  Those have to be handled separately, below.
 	 */
 	lbeentry.st_procpid = MyProcPid;
-	lbeentry.st_backendType = MyBackendType;
 	lbeentry.st_proc_start_timestamp = MyStartTimestamp;
 	lbeentry.st_activity_start_timestamp = 0;
 	lbeentry.st_state_start_timestamp = 0;
@@ -2828,9 +2827,9 @@ pgstat_bestart(void)
 	lbeentry.st_databaseid = MyDatabaseId;
 
 	/* We have userid for client-backends, wal-sender and bgworker processes */
-	if (lbeentry.st_backendType == B_BACKEND
-		|| lbeentry.st_backendType == B_WAL_SENDER
-		|| lbeentry.st_backendType == B_BG_WORKER)
+	if (MySubprocessType == ClientBackendType
+		|| MySubprocessType == WalSenderType
+		|| MySubprocessType == BgWorkerType)
 		lbeentry.st_userid = GetSessionUserId();
 	else
 		lbeentry.st_userid = InvalidOid;
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index f979d505df..65305a18ee 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2085,6 +2085,7 @@ retry1:
 				 */
 				if (strcmp(valptr, "database") == 0)
 				{
+					InitMySubprocess(WalSenderType);
 					am_walsender = true;
 					am_db_walsender = true;
 				}
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index cea01534a5..599e9219ec 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -24,6 +24,7 @@
 #include "pgstat.h"
 #include "postmaster/bgworker_internals.h"
 #include "postmaster/postmaster.h"
+#include "postmaster/subprocess.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
 #include "utils/acl.h"
@@ -693,7 +694,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 
 			proc = BackendPidGetProc(beentry->st_procpid);
 
-			if (proc == NULL && (beentry->st_backendType != B_BACKEND))
+			if (proc == NULL && (MySubprocessType != ClientBackendType))
 			{
 				/*
 				 * For an auxiliary process, retrieve process info from
@@ -741,7 +742,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 			 * date.
 			 */
 			if (beentry->st_xact_start_timestamp != 0 &&
-				beentry->st_backendType != B_WAL_SENDER)
+				MySubprocessType != WalSenderType)
 				values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
 			else
 				nulls[8] = true;
@@ -829,7 +830,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 				}
 			}
 			/* Add backend type */
-			if (beentry->st_backendType == B_BG_WORKER)
+			if (MySubprocessType == BgWorkerType)
 			{
 				const char *bgw_type;
 
@@ -841,7 +842,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 			}
 			else
 				values[17] =
-					CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType));
+					// CStringGetTextDatum(GetBackendTypeDesc(beentry->st_backendType));
+					CStringGetTextDatum(MySubprocess->desc);
 
 			/* SSL information */
 			if (beentry->st_ssl)
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 92943f5cff..405a3224cd 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -1031,9 +1031,6 @@ typedef struct PgBackendStatus
 	/* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
 	int			st_procpid;
 
-	/* Type of backends */
-	BackendType st_backendType;
-
 	/* Times when current backend, transaction, and activity started */
 	TimestampTz st_proc_start_timestamp;
 	TimestampTz st_xact_start_timestamp;
-- 
2.17.0

