From 569bfe5bd2493d8873d8763c1767ccf34fa15505 Mon Sep 17 00:00:00 2001 From: Melanie Plageman Date: Tue, 14 Dec 2021 12:26:56 -0500 Subject: [PATCH v20 2/8] Move backend pgstat initialization earlier Initialize the pgstats subsystem earlier during process initialization so that more process types have a backend activity state (PgBackendStatus). Conditionally initializing backend activity state in some types of processes and not in others necessitates surprising special cases in the code. This particular commit was motivated by single user mode missing a backend activity state. This commit also adds a new BackendType for standalone backends, B_STANDALONE_BACKEND (and alphabetizes the BackendTypes). Both the bootstrap backend and single user mode backends will have BackendType B_STANDALONE_BACKEND. Author: Melanie Plageman Discussion: https://www.postgresql.org/message-id/CAAKRu_aaq33UnG4TXq3S-OSXGWj1QGf0sU%2BECH4tNwGFNERkZA%40mail.gmail.com --- src/backend/utils/init/miscinit.c | 23 ++++++++++++++--------- src/backend/utils/init/postinit.c | 7 +++---- src/include/miscadmin.h | 7 ++++--- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 88801374b5..41d0b023cd 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -176,6 +176,8 @@ InitStandaloneProcess(const char *argv0) { Assert(!IsPostmasterEnvironment); + MyBackendType = B_STANDALONE_BACKEND; + /* * Start our win32 signal implementation */ @@ -255,6 +257,9 @@ GetBackendTypeDesc(BackendType backendType) case B_INVALID: backendDesc = "not initialized"; break; + case B_ARCHIVER: + backendDesc = "archiver"; + break; case B_AUTOVAC_LAUNCHER: backendDesc = "autovacuum launcher"; break; @@ -273,9 +278,18 @@ GetBackendTypeDesc(BackendType backendType) case B_CHECKPOINTER: backendDesc = "checkpointer"; break; + case B_LOGGER: + backendDesc = "logger"; + break; + case B_STANDALONE_BACKEND: + backendDesc = "standalone backend"; + break; case B_STARTUP: backendDesc = "startup"; break; + case B_STATS_COLLECTOR: + backendDesc = "stats collector"; + break; case B_WAL_RECEIVER: backendDesc = "walreceiver"; break; @@ -285,15 +299,6 @@ GetBackendTypeDesc(BackendType backendType) case B_WAL_WRITER: backendDesc = "walwriter"; break; - case B_ARCHIVER: - backendDesc = "archiver"; - break; - case B_STATS_COLLECTOR: - backendDesc = "stats collector"; - break; - case B_LOGGER: - backendDesc = "logger"; - break; } return backendDesc; diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 7292e51f7d..11f1fec17e 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -623,6 +623,8 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, RegisterTimeout(CLIENT_CONNECTION_CHECK_TIMEOUT, ClientCheckTimeoutHandler); } + pgstat_beinit(); + /* * If this is either a bootstrap process nor a standalone backend, start * up the XLOG machinery, and register to have it closed down at exit. @@ -638,6 +640,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, */ CreateAuxProcessResourceOwner(); + pgstat_bestart(); StartupXLOG(); /* Release (and warn about) any buffer pins leaked in StartupXLOG */ ReleaseAuxProcessResources(true); @@ -665,7 +668,6 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, EnablePortalManager(); /* Initialize status reporting */ - pgstat_beinit(); /* * Load relcache entries for the shared system catalogs. This must create @@ -903,10 +905,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, * transaction we started before returning. */ if (!bootstrap) - { - pgstat_bestart(); CommitTransactionCommand(); - } return; } diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index 90a3016065..1d688f9e51 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -323,19 +323,20 @@ extern void SwitchBackToLocalLatch(void); typedef enum BackendType { B_INVALID = 0, + B_ARCHIVER, B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, + B_LOGGER, + B_STANDALONE_BACKEND, B_STARTUP, + B_STATS_COLLECTOR, B_WAL_RECEIVER, B_WAL_SENDER, B_WAL_WRITER, - B_ARCHIVER, - B_STATS_COLLECTOR, - B_LOGGER, } BackendType; extern BackendType MyBackendType; -- 2.30.2