aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/init/postinit.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2025-03-04 14:09:44 +0900
committerMichael Paquier <michael@paquier.xyz>2025-03-04 14:09:44 +0900
commitc76db55c9085d0b7984ea337576e45a7d1268b97 (patch)
tree29393607928688ec1276d7989dd154453a3fbcf8 /src/backend/utils/init/postinit.c
parent40d3f8274499cb1dd349f60f2e5915f907acce6e (diff)
downloadpostgresql-c76db55c9085d0b7984ea337576e45a7d1268b97.tar.gz
postgresql-c76db55c9085d0b7984ea337576e45a7d1268b97.zip
Split pgstat_bestart() into three different routines
pgstat_bestart(), used post-authentication to set up a backend entry in the PgBackendStatus array, so as its data becomes visible in pg_stat_activity and related catalogs, has its logic divided into three routines with this commit, called in order at different steps of the backend initialization: * pgstat_bestart_initial() sets up the backend entry with a minimal amount of information, reporting it with a new BackendState called STATE_STARTING while waiting for backend initialization and client authentication to complete. The main benefit that this offers is observability, so as it is possible to monitor the backend activity during authentication. This step happens earlier than in the logic prior to this commit. pgstat_beinit() happens earlier as well, before authentication. * pgstat_bestart_security() reports the SSL/GSS status of the connection, once authentication completes. Auxiliary processes, for example, do not need to call this step, hence it is optional. This step is called after performing authentication, same as previously. * pgstat_bestart_final() reports the user and database IDs, takes the entry out of STATE_STARTING, and reports its application_name. This is called as the last step of the three, once authentication completes. An injection point is added, with a test checking that the "starting" phase of a backend entry is visible in pg_stat_activity. Some follow-up patches are planned to take advantage of this refactoring with more information provided in backend entries during authentication (LDAP hanging was a problem for the author, initially). Author: Jacob Champion <jacob.champion@enterprisedb.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CAOYmi+=60deN20WDyCoHCiecgivJxr=98s7s7-C8SkXwrCfHXg@mail.gmail.com
Diffstat (limited to 'src/backend/utils/init/postinit.c')
-rw-r--r--src/backend/utils/init/postinit.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 318600d6d02..b428a59bdd2 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -59,6 +59,7 @@
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc_hooks.h"
+#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/portal.h"
@@ -718,6 +719,20 @@ InitPostgres(const char *in_dbname, Oid dboid,
*/
InitProcessPhase2();
+ /* Initialize status reporting */
+ pgstat_beinit();
+
+ /*
+ * And initialize an entry in the PgBackendStatus array. That way, if
+ * LWLocks or third-party authentication should happen to hang, it is
+ * possible to retrieve some information about what is going on.
+ */
+ if (!bootstrap)
+ {
+ pgstat_bestart_initial();
+ INJECTION_POINT("init-pre-auth");
+ }
+
/*
* Initialize my entry in the shared-invalidation manager's array of
* per-backend data.
@@ -786,9 +801,6 @@ InitPostgres(const char *in_dbname, Oid dboid,
/* Initialize portal manager */
EnablePortalManager();
- /* Initialize status reporting */
- pgstat_beinit();
-
/*
* Load relcache entries for the shared system catalogs. This must create
* at least entries for pg_database and catalogs used for authentication.
@@ -809,8 +821,8 @@ InitPostgres(const char *in_dbname, Oid dboid,
/* The autovacuum launcher is done here */
if (AmAutoVacuumLauncherProcess())
{
- /* report this backend in the PgBackendStatus array */
- pgstat_bestart();
+ /* fill in the remainder of this entry in the PgBackendStatus array */
+ pgstat_bestart_final();
return;
}
@@ -884,6 +896,14 @@ InitPostgres(const char *in_dbname, Oid dboid,
am_superuser = superuser();
}
+ /* Report any SSL/GSS details for the session. */
+ if (MyProcPort != NULL)
+ {
+ Assert(!bootstrap);
+
+ pgstat_bestart_security();
+ }
+
/*
* Binary upgrades only allowed super-user connections
*/
@@ -953,8 +973,8 @@ InitPostgres(const char *in_dbname, Oid dboid,
/* initialize client encoding */
InitializeClientEncoding();
- /* report this backend in the PgBackendStatus array */
- pgstat_bestart();
+ /* fill in the remainder of this entry in the PgBackendStatus array */
+ pgstat_bestart_final();
/* close the transaction we started above */
CommitTransactionCommand();
@@ -997,7 +1017,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
*/
if (!bootstrap)
{
- pgstat_bestart();
+ pgstat_bestart_final();
CommitTransactionCommand();
}
return;
@@ -1197,9 +1217,9 @@ InitPostgres(const char *in_dbname, Oid dboid,
if ((flags & INIT_PG_LOAD_SESSION_LIBS) != 0)
process_session_preload_libraries();
- /* report this backend in the PgBackendStatus array */
+ /* fill in the remainder of this entry in the PgBackendStatus array */
if (!bootstrap)
- pgstat_bestart();
+ pgstat_bestart_final();
/* close the transaction we started above */
if (!bootstrap)