From d9328739f9fab8ca4d9919085af45f711bfce90a Mon Sep 17 00:00:00 2001
From: Kuntal Ghosh <kuntal.ghosh@enterprisedb.com>
Date: Tue, 21 Mar 2017 18:18:22 +0530
Subject: [PATCH 2/3] Expose stats for all backends

All backends include auxiliary procs, autovacuum launcher and
bgworkers having valid BackendIds.
---
 src/backend/access/transam/parallel.c      | 3 +++
 src/backend/bootstrap/bootstrap.c          | 3 +++
 src/backend/postmaster/autovacuum.c        | 3 +++
 src/backend/postmaster/bgwriter.c          | 3 +++
 src/backend/postmaster/checkpointer.c      | 3 +++
 src/backend/postmaster/pgstat.c            | 1 -
 src/backend/postmaster/startup.c           | 4 ++++
 src/backend/postmaster/walwriter.c         | 3 +++
 src/backend/replication/logical/launcher.c | 3 +++
 src/backend/replication/logical/worker.c   | 3 +++
 src/backend/replication/walreceiver.c      | 4 ++++
 src/backend/replication/walsender.c        | 5 ++++-
 src/backend/utils/init/postinit.c          | 3 ++-
 src/test/modules/worker_spi/worker_spi.c   | 3 +++
 14 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c
index 3e0ee87..ed2ac47 100644
--- a/src/backend/access/transam/parallel.c
+++ b/src/backend/access/transam/parallel.c
@@ -1044,6 +1044,9 @@ ParallelWorkerMain(Datum main_arg)
 	BackgroundWorkerInitializeConnectionByOid(fps->database_id,
 											  fps->authenticated_user_id);
 
+	/* report the parallel worker process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/*
 	 * Set the client encoding to the database encoding, since that is what
 	 * the leader will expect.
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 6511c60..f56e229 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -387,6 +387,9 @@ AuxiliaryProcessMain(int argc, char *argv[])
 		/* finish setting up bufmgr.c */
 		InitBufferPoolBackend();
 
+		/* Initialize stats collection */
+		pgstat_initialize();
+
 		/* register a before-shutdown callback for LWLock cleanup */
 		before_shmem_exit(ShutdownAuxiliaryProcess, 0);
 	}
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 33ca749..0dd8ccc 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -453,6 +453,9 @@ AutoVacLauncherMain(int argc, char *argv[])
 
 	InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL);
 
+	/* report autovacuum launcher process in the  PgBackendStatus array */
+	pgstat_bestart();
+
 	SetProcessingMode(NormalProcessing);
 
 	/*
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index dcb4cf2..6ba2e2e 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -248,6 +248,9 @@ BackgroundWriterMain(void)
 	 */
 	prev_hibernate = false;
 
+	/* report bgwriter process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/*
 	 * Loop forever
 	 */
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index fe9041f..8eb0a2a 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -345,6 +345,9 @@ CheckpointerMain(void)
 	 */
 	ProcGlobal->checkpointerLatch = &MyProc->procLatch;
 
+	/* report checkpointer process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/*
 	 * Loop forever
 	 */
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index a2b9bbf..ff565a3 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -2690,7 +2690,6 @@ pgstat_initialize(void)
  * pgstat_bestart() -
  *
  *	Initialize this backend's entry in the PgBackendStatus array.
- *	Called from InitPostgres.
  *
  *	Apart from auxiliary processes, MyBackendId, MyDatabaseId,
  *	session userid, and application_name must be set for a
diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c
index b172b5e..56ff042 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -25,6 +25,7 @@
 #include "access/xlog.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
+#include "pgstat.h"
 #include "postmaster/startup.h"
 #include "storage/ipc.h"
 #include "storage/latch.h"
@@ -210,6 +211,9 @@ StartupProcessMain(void)
 	 */
 	PG_SETMASK(&UnBlockSig);
 
+	/* report startup process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/*
 	 * Do what we came for.
 	 */
diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c
index a575d8f..9056652 100644
--- a/src/backend/postmaster/walwriter.c
+++ b/src/backend/postmaster/walwriter.c
@@ -231,6 +231,9 @@ WalWriterMain(void)
 	 */
 	ProcGlobal->walwriterLatch = &MyProc->procLatch;
 
+	/* report walwriter process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/*
 	 * Loop forever
 	 */
diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 20b4362..221adb0 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -571,6 +571,9 @@ ApplyLauncherMain(Datum main_arg)
 	 */
 	BackgroundWorkerInitializeConnection(NULL, NULL);
 
+	/* report the apply launcher process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/* Enter main loop */
 	while (!got_SIGTERM)
 	{
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index c3e54af..db09540 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -1376,6 +1376,9 @@ ApplyWorkerMain(Datum main_arg)
 	BackgroundWorkerInitializeConnectionByOid(MyLogicalRepWorker->dbid,
 											  MyLogicalRepWorker->userid);
 
+	/* report the apply worker process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/* Load the subscription into persistent memory context. */
 	CreateCacheMemoryContext();
 	ApplyCacheContext = AllocSetContextCreate(CacheMemoryContext,
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 18d9d7e..3265d03 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -316,6 +316,10 @@ WalReceiverMain(void)
 	SpinLockRelease(&walrcv->mutex);
 
 	first_stream = true;
+
+	/* report walreceiver process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	for (;;)
 	{
 		char	   *primary_sysid;
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 0f6b828..de1edc6 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -245,6 +245,9 @@ InitWalSender(void)
 	 */
 	MarkPostmasterChildWalSender();
 	SendPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE);
+
+	/* report walsender process in the PgBackendStatus array */
+	pgstat_bestart();
 }
 
 /*
@@ -1808,7 +1811,7 @@ WalSndLoop(WalSndSendDataCallback send_data)
 	waiting_for_ping_response = false;
 
 	/* Report to pgstat that this process is a WAL sender */
-	pgstat_report_activity(STATE_RUNNING, "walsender");
+	pgstat_report_activity(STATE_RUNNING, NULL);
 
 	/*
 	 * Loop until we reach the end of this timeline or the client requests to
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 9f938f2..cc4a639 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -808,7 +808,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		/* initialize client encoding */
 		InitializeClientEncoding();
 
-		/* report this backend in the PgBackendStatus array */
+		/* report walsender process in the PgBackendStatus array */
 		pgstat_bestart();
 
 		/* close the transaction we started above */
@@ -875,6 +875,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		 */
 		if (!bootstrap)
 			CommitTransactionCommand();
+
 		return;
 	}
 
diff --git a/src/test/modules/worker_spi/worker_spi.c b/src/test/modules/worker_spi/worker_spi.c
index 72ab846..bea6aa63 100644
--- a/src/test/modules/worker_spi/worker_spi.c
+++ b/src/test/modules/worker_spi/worker_spi.c
@@ -181,6 +181,9 @@ worker_spi_main(Datum main_arg)
 	/* Connect to our database */
 	BackgroundWorkerInitializeConnection("postgres", NULL);
 
+	/* report the worker spi process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	elog(LOG, "%s initialized with %s.%s",
 		 MyBgworkerEntry->bgw_name, table->schema, table->name);
 	initialize_worker_spi(table);
-- 
1.8.3.1

