From 191f6daebabcccb50a363801cb93edae71c12504 Mon Sep 17 00:00:00 2001
From: Kuntal Ghosh <kuntal.ghosh@enterprisedb.com>
Date: Wed, 15 Mar 2017 12:27:00 +0530
Subject: [PATCH 2/3] Expose stats for all backends

All backends include auxiliary procs, autovacuum launcher and
bgworkers.
---
 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/postmaster.c   | 6 ++++++
 src/backend/postmaster/startup.c      | 4 ++++
 src/backend/postmaster/walwriter.c    | 3 +++
 src/backend/replication/walreceiver.c | 4 ++++
 src/backend/replication/walsender.c   | 5 ++++-
 src/backend/utils/init/postinit.c     | 5 ++++-
 11 files changed, 37 insertions(+), 3 deletions(-)

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 fa8de13..550a731 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 5b7804a..1b01b53 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -2689,7 +2689,6 @@ pgstat_initialize(void)
  * pgstat_bestart() -
  *
  *	Initialize this backend's entry in the PgBackendStatus array.
- *	Called from InitPostgres.
  *
  *	Apart from auxiliary processes, MyDatabaseId, session userid,
  *	and application_name must be set for a backend (hence, this
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 6831342..7d78bff 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -5461,6 +5461,9 @@ BackgroundWorkerInitializeConnection(char *dbname, char *username)
 
 	InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
 
+	/* report the background worker process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/* it had better not gotten out of "init" mode yet */
 	if (!IsInitProcessingMode())
 		ereport(ERROR,
@@ -5484,6 +5487,9 @@ BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
 
 	InitPostgres(NULL, dboid, NULL, useroid, NULL);
 
+	/* report the background worker process in the PgBackendStatus array */
+	pgstat_bestart();
+
 	/* it had better not gotten out of "init" mode yet */
 	if (!IsInitProcessingMode())
 		ereport(ERROR,
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/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 127efec..9b80284 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();
 }
 
 /*
@@ -1806,7 +1809,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..cbe9b49 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -665,7 +665,9 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 
 	/* The autovacuum launcher is done here */
 	if (IsAutoVacuumLauncherProcess())
+	{
 		return;
+	}
 
 	/*
 	 * Start a new transaction here before first access to db, and get a
@@ -808,7 +810,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 +877,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		 */
 		if (!bootstrap)
 			CommitTransactionCommand();
+
 		return;
 	}
 
-- 
1.8.3.1

