mirror of
https://github.com/postgres/postgres.git
synced 2025-04-27 22:56:53 +03:00
Refactor InitPostgres() to use bitwise option flags
InitPostgres() has been using a set of boolean arguments to control its behavior, and a patch under discussion was aiming at expanding it with a third one. In preparation for expanding this area, this commit switches all the current boolean arguments of this routine to a single bits32 argument instead. Two values are currently supported for the flags: - INIT_PG_LOAD_SESSION_LIBS to load [session|local]_preload_libraries at startup. - INIT_PG_OVERRIDE_ALLOW_CONNS to allow connection to a database even if it has !datallowconn. This is used by bgworkers. Reviewed-by: Bertrand Drouvot Discussion: https://postgr.es/m/ZSTn66_BXRZCeaqS@paquier.xyz
This commit is contained in:
parent
28139037c0
commit
4800a5dfb4
@ -345,7 +345,7 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
|
|||||||
if (pg_link_canary_is_frontend())
|
if (pg_link_canary_is_frontend())
|
||||||
elog(ERROR, "backend is incorrectly linked to frontend functions");
|
elog(ERROR, "backend is incorrectly linked to frontend functions");
|
||||||
|
|
||||||
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL);
|
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
|
||||||
|
|
||||||
/* Initialize stuff for bootstrap-file processing */
|
/* Initialize stuff for bootstrap-file processing */
|
||||||
for (i = 0; i < MAXATTR; i++)
|
for (i = 0; i < MAXATTR; i++)
|
||||||
|
@ -488,7 +488,7 @@ AutoVacLauncherMain(int argc, char *argv[])
|
|||||||
/* Early initialization */
|
/* Early initialization */
|
||||||
BaseInit();
|
BaseInit();
|
||||||
|
|
||||||
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL);
|
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
|
||||||
|
|
||||||
SetProcessingMode(NormalProcessing);
|
SetProcessingMode(NormalProcessing);
|
||||||
|
|
||||||
@ -1706,8 +1706,7 @@ AutoVacWorkerMain(int argc, char *argv[])
|
|||||||
* Note: if we have selected a just-deleted database (due to using
|
* Note: if we have selected a just-deleted database (due to using
|
||||||
* stale stats info), we'll fail and exit here.
|
* stale stats info), we'll fail and exit here.
|
||||||
*/
|
*/
|
||||||
InitPostgres(NULL, dbid, NULL, InvalidOid, false, false,
|
InitPostgres(NULL, dbid, NULL, InvalidOid, 0, dbname);
|
||||||
dbname);
|
|
||||||
SetProcessingMode(NormalProcessing);
|
SetProcessingMode(NormalProcessing);
|
||||||
set_ps_display(dbname);
|
set_ps_display(dbname);
|
||||||
ereport(DEBUG1,
|
ereport(DEBUG1,
|
||||||
|
@ -5562,6 +5562,11 @@ void
|
|||||||
BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
|
BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
|
||||||
{
|
{
|
||||||
BackgroundWorker *worker = MyBgworkerEntry;
|
BackgroundWorker *worker = MyBgworkerEntry;
|
||||||
|
bits32 init_flags = 0; /* never honor session_preload_libraries */
|
||||||
|
|
||||||
|
/* ignore datallowconn? */
|
||||||
|
if (flags & BGWORKER_BYPASS_ALLOWCONN)
|
||||||
|
init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
|
||||||
|
|
||||||
/* XXX is this the right errcode? */
|
/* XXX is this the right errcode? */
|
||||||
if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
|
if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
|
||||||
@ -5571,8 +5576,7 @@ BackgroundWorkerInitializeConnection(const char *dbname, const char *username, u
|
|||||||
|
|
||||||
InitPostgres(dbname, InvalidOid, /* database to connect to */
|
InitPostgres(dbname, InvalidOid, /* database to connect to */
|
||||||
username, InvalidOid, /* role to connect as */
|
username, InvalidOid, /* role to connect as */
|
||||||
false, /* never honor session_preload_libraries */
|
init_flags,
|
||||||
(flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
|
|
||||||
NULL); /* no out_dbname */
|
NULL); /* no out_dbname */
|
||||||
|
|
||||||
/* it had better not gotten out of "init" mode yet */
|
/* it had better not gotten out of "init" mode yet */
|
||||||
@ -5589,6 +5593,11 @@ void
|
|||||||
BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
|
BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
|
||||||
{
|
{
|
||||||
BackgroundWorker *worker = MyBgworkerEntry;
|
BackgroundWorker *worker = MyBgworkerEntry;
|
||||||
|
bits32 init_flags = 0; /* never honor session_preload_libraries */
|
||||||
|
|
||||||
|
/* ignore datallowconn? */
|
||||||
|
if (flags & BGWORKER_BYPASS_ALLOWCONN)
|
||||||
|
init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
|
||||||
|
|
||||||
/* XXX is this the right errcode? */
|
/* XXX is this the right errcode? */
|
||||||
if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
|
if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
|
||||||
@ -5598,8 +5607,7 @@ BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
|
|||||||
|
|
||||||
InitPostgres(NULL, dboid, /* database to connect to */
|
InitPostgres(NULL, dboid, /* database to connect to */
|
||||||
NULL, useroid, /* role to connect as */
|
NULL, useroid, /* role to connect as */
|
||||||
false, /* never honor session_preload_libraries */
|
init_flags,
|
||||||
(flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
|
|
||||||
NULL); /* no out_dbname */
|
NULL); /* no out_dbname */
|
||||||
|
|
||||||
/* it had better not gotten out of "init" mode yet */
|
/* it had better not gotten out of "init" mode yet */
|
||||||
|
@ -4206,11 +4206,12 @@ PostgresMain(const char *dbname, const char *username)
|
|||||||
* NOTE: if you are tempted to add code in this vicinity, consider putting
|
* NOTE: if you are tempted to add code in this vicinity, consider putting
|
||||||
* it inside InitPostgres() instead. In particular, anything that
|
* it inside InitPostgres() instead. In particular, anything that
|
||||||
* involves database access should be there, not here.
|
* involves database access should be there, not here.
|
||||||
|
*
|
||||||
|
* Honor session_preload_libraries if not dealing with a WAL sender.
|
||||||
*/
|
*/
|
||||||
InitPostgres(dbname, InvalidOid, /* database to connect to */
|
InitPostgres(dbname, InvalidOid, /* database to connect to */
|
||||||
username, InvalidOid, /* role to connect as */
|
username, InvalidOid, /* role to connect as */
|
||||||
!am_walsender, /* honor session_preload_libraries? */
|
(!am_walsender) ? INIT_PG_LOAD_SESSION_LIBS : 0,
|
||||||
false, /* don't ignore datallowconn */
|
|
||||||
NULL); /* no out_dbname */
|
NULL); /* no out_dbname */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -681,8 +681,9 @@ BaseInit(void)
|
|||||||
* Parameters:
|
* Parameters:
|
||||||
* in_dbname, dboid: specify database to connect to, as described below
|
* in_dbname, dboid: specify database to connect to, as described below
|
||||||
* username, useroid: specify role to connect as, as described below
|
* username, useroid: specify role to connect as, as described below
|
||||||
* load_session_libraries: TRUE to honor [session|local]_preload_libraries
|
* flags:
|
||||||
* override_allow_connections: TRUE to connect despite !datallowconn
|
* - INIT_PG_LOAD_SESSION_LIBS to honor [session|local]_preload_libraries.
|
||||||
|
* - INIT_PG_OVERRIDE_ALLOW_CONNS to connect despite !datallowconn.
|
||||||
* out_dbname: optional output parameter, see below; pass NULL if not used
|
* out_dbname: optional output parameter, see below; pass NULL if not used
|
||||||
*
|
*
|
||||||
* The database can be specified by name, using the in_dbname parameter, or by
|
* The database can be specified by name, using the in_dbname parameter, or by
|
||||||
@ -701,8 +702,8 @@ BaseInit(void)
|
|||||||
* database but not a username; conversely, a physical walsender specifies
|
* database but not a username; conversely, a physical walsender specifies
|
||||||
* username but not database.
|
* username but not database.
|
||||||
*
|
*
|
||||||
* By convention, load_session_libraries should be passed as true in
|
* By convention, INIT_PG_LOAD_SESSION_LIBS should be passed in "flags" in
|
||||||
* "interactive" sessions (including standalone backends), but false in
|
* "interactive" sessions (including standalone backends), but not in
|
||||||
* background processes such as autovacuum. Note in particular that it
|
* background processes such as autovacuum. Note in particular that it
|
||||||
* shouldn't be true in parallel worker processes; those have another
|
* shouldn't be true in parallel worker processes; those have another
|
||||||
* mechanism for replicating their leader's set of loaded libraries.
|
* mechanism for replicating their leader's set of loaded libraries.
|
||||||
@ -717,8 +718,7 @@ BaseInit(void)
|
|||||||
void
|
void
|
||||||
InitPostgres(const char *in_dbname, Oid dboid,
|
InitPostgres(const char *in_dbname, Oid dboid,
|
||||||
const char *username, Oid useroid,
|
const char *username, Oid useroid,
|
||||||
bool load_session_libraries,
|
bits32 flags,
|
||||||
bool override_allow_connections,
|
|
||||||
char *out_dbname)
|
char *out_dbname)
|
||||||
{
|
{
|
||||||
bool bootstrap = IsBootstrapProcessingMode();
|
bool bootstrap = IsBootstrapProcessingMode();
|
||||||
@ -1189,7 +1189,8 @@ InitPostgres(const char *in_dbname, Oid dboid,
|
|||||||
* user is a superuser, so the above stuff has to happen first.)
|
* user is a superuser, so the above stuff has to happen first.)
|
||||||
*/
|
*/
|
||||||
if (!bootstrap)
|
if (!bootstrap)
|
||||||
CheckMyDatabase(dbname, am_superuser, override_allow_connections);
|
CheckMyDatabase(dbname, am_superuser,
|
||||||
|
(flags & INIT_PG_OVERRIDE_ALLOW_CONNS) != 0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Now process any command-line switches and any additional GUC variable
|
* Now process any command-line switches and any additional GUC variable
|
||||||
@ -1227,7 +1228,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
|
|||||||
* during the initial transaction in case anything that requires database
|
* during the initial transaction in case anything that requires database
|
||||||
* access needs to be done.
|
* access needs to be done.
|
||||||
*/
|
*/
|
||||||
if (load_session_libraries)
|
if ((flags & INIT_PG_LOAD_SESSION_LIBS) != 0)
|
||||||
process_session_preload_libraries();
|
process_session_preload_libraries();
|
||||||
|
|
||||||
/* report this backend in the PgBackendStatus array */
|
/* report this backend in the PgBackendStatus array */
|
||||||
|
@ -463,12 +463,14 @@ extern PGDLLIMPORT AuxProcType MyAuxProcType;
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
/* in utils/init/postinit.c */
|
/* in utils/init/postinit.c */
|
||||||
|
/* flags for InitPostgres() */
|
||||||
|
#define INIT_PG_LOAD_SESSION_LIBS 0x0001
|
||||||
|
#define INIT_PG_OVERRIDE_ALLOW_CONNS 0x0002
|
||||||
extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
|
extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
|
||||||
extern void InitializeMaxBackends(void);
|
extern void InitializeMaxBackends(void);
|
||||||
extern void InitPostgres(const char *in_dbname, Oid dboid,
|
extern void InitPostgres(const char *in_dbname, Oid dboid,
|
||||||
const char *username, Oid useroid,
|
const char *username, Oid useroid,
|
||||||
bool load_session_libraries,
|
bits32 flags,
|
||||||
bool override_allow_connections,
|
|
||||||
char *out_dbname);
|
char *out_dbname);
|
||||||
extern void BaseInit(void);
|
extern void BaseInit(void);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user