Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4800a5d

Browse files
committed
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
1 parent 2813903 commit 4800a5d

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

src/backend/bootstrap/bootstrap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
345345
if (pg_link_canary_is_frontend())
346346
elog(ERROR, "backend is incorrectly linked to frontend functions");
347347

348-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL);
348+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
349349

350350
/* Initialize stuff for bootstrap-file processing */
351351
for (i = 0; i < MAXATTR; i++)

src/backend/postmaster/autovacuum.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ AutoVacLauncherMain(int argc, char *argv[])
488488
/* Early initialization */
489489
BaseInit();
490490

491-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL);
491+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
492492

493493
SetProcessingMode(NormalProcessing);
494494

@@ -1706,8 +1706,7 @@ AutoVacWorkerMain(int argc, char *argv[])
17061706
* Note: if we have selected a just-deleted database (due to using
17071707
* stale stats info), we'll fail and exit here.
17081708
*/
1709-
InitPostgres(NULL, dbid, NULL, InvalidOid, false, false,
1710-
dbname);
1709+
InitPostgres(NULL, dbid, NULL, InvalidOid, 0, dbname);
17111710
SetProcessingMode(NormalProcessing);
17121711
set_ps_display(dbname);
17131712
ereport(DEBUG1,

src/backend/postmaster/postmaster.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -5562,6 +5562,11 @@ void
55625562
BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
55635563
{
55645564
BackgroundWorker *worker = MyBgworkerEntry;
5565+
bits32 init_flags = 0; /* never honor session_preload_libraries */
5566+
5567+
/* ignore datallowconn? */
5568+
if (flags & BGWORKER_BYPASS_ALLOWCONN)
5569+
init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
55655570

55665571
/* XXX is this the right errcode? */
55675572
if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
@@ -5571,8 +5576,7 @@ BackgroundWorkerInitializeConnection(const char *dbname, const char *username, u
55715576

55725577
InitPostgres(dbname, InvalidOid, /* database to connect to */
55735578
username, InvalidOid, /* role to connect as */
5574-
false, /* never honor session_preload_libraries */
5575-
(flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
5579+
init_flags,
55765580
NULL); /* no out_dbname */
55775581

55785582
/* it had better not gotten out of "init" mode yet */
@@ -5589,6 +5593,11 @@ void
55895593
BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
55905594
{
55915595
BackgroundWorker *worker = MyBgworkerEntry;
5596+
bits32 init_flags = 0; /* never honor session_preload_libraries */
5597+
5598+
/* ignore datallowconn? */
5599+
if (flags & BGWORKER_BYPASS_ALLOWCONN)
5600+
init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
55925601

55935602
/* XXX is this the right errcode? */
55945603
if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
@@ -5598,8 +5607,7 @@ BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
55985607

55995608
InitPostgres(NULL, dboid, /* database to connect to */
56005609
NULL, useroid, /* role to connect as */
5601-
false, /* never honor session_preload_libraries */
5602-
(flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
5610+
init_flags,
56035611
NULL); /* no out_dbname */
56045612

56055613
/* it had better not gotten out of "init" mode yet */

src/backend/tcop/postgres.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -4206,11 +4206,12 @@ PostgresMain(const char *dbname, const char *username)
42064206
* NOTE: if you are tempted to add code in this vicinity, consider putting
42074207
* it inside InitPostgres() instead. In particular, anything that
42084208
* involves database access should be there, not here.
4209+
*
4210+
* Honor session_preload_libraries if not dealing with a WAL sender.
42094211
*/
42104212
InitPostgres(dbname, InvalidOid, /* database to connect to */
42114213
username, InvalidOid, /* role to connect as */
4212-
!am_walsender, /* honor session_preload_libraries? */
4213-
false, /* don't ignore datallowconn */
4214+
(!am_walsender) ? INIT_PG_LOAD_SESSION_LIBS : 0,
42144215
NULL); /* no out_dbname */
42154216

42164217
/*

src/backend/utils/init/postinit.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,9 @@ BaseInit(void)
681681
* Parameters:
682682
* in_dbname, dboid: specify database to connect to, as described below
683683
* username, useroid: specify role to connect as, as described below
684-
* load_session_libraries: TRUE to honor [session|local]_preload_libraries
685-
* override_allow_connections: TRUE to connect despite !datallowconn
684+
* flags:
685+
* - INIT_PG_LOAD_SESSION_LIBS to honor [session|local]_preload_libraries.
686+
* - INIT_PG_OVERRIDE_ALLOW_CONNS to connect despite !datallowconn.
686687
* out_dbname: optional output parameter, see below; pass NULL if not used
687688
*
688689
* The database can be specified by name, using the in_dbname parameter, or by
@@ -701,8 +702,8 @@ BaseInit(void)
701702
* database but not a username; conversely, a physical walsender specifies
702703
* username but not database.
703704
*
704-
* By convention, load_session_libraries should be passed as true in
705-
* "interactive" sessions (including standalone backends), but false in
705+
* By convention, INIT_PG_LOAD_SESSION_LIBS should be passed in "flags" in
706+
* "interactive" sessions (including standalone backends), but not in
706707
* background processes such as autovacuum. Note in particular that it
707708
* shouldn't be true in parallel worker processes; those have another
708709
* mechanism for replicating their leader's set of loaded libraries.
@@ -717,8 +718,7 @@ BaseInit(void)
717718
void
718719
InitPostgres(const char *in_dbname, Oid dboid,
719720
const char *username, Oid useroid,
720-
bool load_session_libraries,
721-
bool override_allow_connections,
721+
bits32 flags,
722722
char *out_dbname)
723723
{
724724
bool bootstrap = IsBootstrapProcessingMode();
@@ -1189,7 +1189,8 @@ InitPostgres(const char *in_dbname, Oid dboid,
11891189
* user is a superuser, so the above stuff has to happen first.)
11901190
*/
11911191
if (!bootstrap)
1192-
CheckMyDatabase(dbname, am_superuser, override_allow_connections);
1192+
CheckMyDatabase(dbname, am_superuser,
1193+
(flags & INIT_PG_OVERRIDE_ALLOW_CONNS) != 0);
11931194

11941195
/*
11951196
* Now process any command-line switches and any additional GUC variable
@@ -1227,7 +1228,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
12271228
* during the initial transaction in case anything that requires database
12281229
* access needs to be done.
12291230
*/
1230-
if (load_session_libraries)
1231+
if ((flags & INIT_PG_LOAD_SESSION_LIBS) != 0)
12311232
process_session_preload_libraries();
12321233

12331234
/* report this backend in the PgBackendStatus array */

src/include/miscadmin.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,14 @@ extern PGDLLIMPORT AuxProcType MyAuxProcType;
463463
*****************************************************************************/
464464

465465
/* in utils/init/postinit.c */
466+
/* flags for InitPostgres() */
467+
#define INIT_PG_LOAD_SESSION_LIBS 0x0001
468+
#define INIT_PG_OVERRIDE_ALLOW_CONNS 0x0002
466469
extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
467470
extern void InitializeMaxBackends(void);
468471
extern void InitPostgres(const char *in_dbname, Oid dboid,
469472
const char *username, Oid useroid,
470-
bool load_session_libraries,
471-
bool override_allow_connections,
473+
bits32 flags,
472474
char *out_dbname);
473475
extern void BaseInit(void);
474476

0 commit comments

Comments
 (0)