From 13b82c63aae4f0a0529abca65cd1f570ebcdfd12 Mon Sep 17 00:00:00 2001 From: Kuntal Ghosh 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/bgwriter.c | 3 +++ src/backend/postmaster/checkpointer.c | 3 +++ src/backend/postmaster/pgstat.c | 3 ++- 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 | 16 +++++++++++++++- 9 files changed, 41 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/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 34cca73..215df08 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -2677,7 +2677,8 @@ pgstat_initialize(void) * pgstat_bestart() - * * Initialize this backend's entry in the PgBackendStatus array. - * Called from InitPostgres. + * Called from InitPostgres and other main entry points for + * auxiliary processes. * * Apart from auxiliary processes, MyDatabaseId, session userid, * and application_name must be set for a backend (hence, this 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..6f88b0f 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_bestart(); 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_bestart(); /* 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_bestart(); return; } -- 1.8.3.1