From 09f678653820b62d0823ea6c951adb3ff2f470ea Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 2 Aug 2021 17:42:25 +0000 Subject: [PATCH v1 1/1] Calculate MaxBackends earlier in PostmasterMain(). Presently, InitializeMaxBackends() is called after processing shared_preload_libraries because it used to tally up the number of registered background workers requested by the libraries. Since 6bc8ef0b, InitializeMaxBackends() has simply used the max_worker_processes GUC instead, so all the comments about needing to register background workers before initializing MaxBackends are no longer correct. In addition to revising the comments, this patch reorders InitializeMaxBackends() to before shared_preload_libraries is processed so that modules can make use of MaxBackends in their _PG_init() functions. --- src/backend/postmaster/postmaster.c | 19 +++++++++---------- src/backend/utils/init/postinit.c | 4 +--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 00d051d520..5eff4610fd 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -990,10 +990,15 @@ PostmasterMain(int argc, char *argv[]) LocalProcessControlFile(false); /* - * Register the apply launcher. Since it registers a background worker, - * it needs to be called before InitializeMaxBackends(), and it's probably - * a good idea to call it before any modules had chance to take the - * background worker slots. + * Calculate MaxBackends. This is done before processing + * shared_preload_libraries so that such libraries can make use of it in + * _PG_init(). + */ + InitializeMaxBackends(); + + /* + * Register the apply launcher. It's probably a good idea to call it before + * any modules had chance to take the background worker slots. */ ApplyLauncherRegister(); @@ -1013,12 +1018,6 @@ PostmasterMain(int argc, char *argv[]) } #endif - /* - * Now that loadable modules have had their chance to register background - * workers, calculate MaxBackends. - */ - InitializeMaxBackends(); - /* * Set up shared memory and semaphores. */ diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 51d1bbef30..f8136dfc6f 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -502,9 +502,7 @@ pg_split_opts(char **argv, int *argcp, const char *optstr) /* * Initialize MaxBackends value from config options. * - * This must be called after modules have had the chance to register background - * workers in shared_preload_libraries, and before shared memory size is - * determined. + * This must be called before shared memory size is determined. * * Note that in EXEC_BACKEND environment, the value is passed down from * postmaster to subprocesses via BackendParameters in SubPostmasterMain; only -- 2.16.6