From 1b96d656fae4c038e7fdac9a252b3b86bbea94ee Mon Sep 17 00:00:00 2001
From: Kuntal Ghosh <kuntal.ghosh@enterprisedb.com>
Date: Wed, 15 Feb 2017 17:18:32 +0530
Subject: [PATCH] Expose stats for autovacuum launcher and bgworker

---
 src/backend/postmaster/autovacuum.c        |  4 ++++
 src/backend/replication/logical/launcher.c |  4 ++++
 src/backend/utils/init/postinit.c          | 16 +++++++++++++++-
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 0c5ffa0..f6132bc 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -597,6 +597,8 @@ AutoVacLauncherMain(int argc, char *argv[])
 		 * Wait until naptime expires or we get some type of signal (all the
 		 * signal handlers will wake us by calling SetLatch).
 		 */
+		pgstat_report_activity(STATE_IDLE, NULL);
+
 		rc = WaitLatch(MyLatch,
 					   WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
 					   (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
@@ -614,6 +616,8 @@ AutoVacLauncherMain(int argc, char *argv[])
 		if (rc & WL_POSTMASTER_DEATH)
 			proc_exit(1);
 
+		pgstat_report_activity(STATE_RUNNING, NULL);
+
 		/* the normal shutdown case */
 		if (got_SIGTERM)
 			break;
diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 39530f96..2f927c8 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -590,6 +590,8 @@ ApplyLauncherMain(Datum main_arg)
 
 		now = GetCurrentTimestamp();
 
+		pgstat_report_activity(STATE_RUNNING, NULL);
+
 		/* Limit the start retry to once a wal_retrieve_retry_interval */
 		if (TimestampDifferenceExceeds(last_start_time, now,
 									   wal_retrieve_retry_interval))
@@ -647,6 +649,8 @@ ApplyLauncherMain(Datum main_arg)
 		}
 
 		/* Wait for more work. */
+		pgstat_report_activity(STATE_IDLE, NULL);
+
 		rc = WaitLatch(&MyProc->procLatch,
 					   WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
 					   wait_time,
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index f8c36c0..b831cf9 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -665,7 +665,14 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 
 	/* The autovacuum launcher is done here */
 	if (IsAutoVacuumLauncherProcess())
+	{
+		/*
+		 * Before returning, report autovacuum launcher process in the
+		 * PgBackendStatus array.
+		 */
+		pgstat_procstart();
 		return;
+	}
 
 	/*
 	 * Start a new transaction here before first access to db, and get a
@@ -808,7 +815,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_procstart();
 
 		/* close the transaction we started above */
@@ -875,6 +882,13 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		 */
 		if (!bootstrap)
 			CommitTransactionCommand();
+
+		/*
+		 * Before returning, report the background worker process in the
+		 * PgBackendStatus array.
+		 */
+		if (!bootstrap)
+			pgstat_procstart();
 		return;
 	}
 
-- 
1.8.3.1

