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

Commit eed1ce7

Browse files
committed
Allow background workers to bypass datallowconn
THis adds a "flags" field to the BackgroundWorkerInitializeConnection() and BackgroundWorkerInitializeConnectionByOid(). For now only one flag, BGWORKER_BYPASS_ALLOWCONN, is defined, which allows the worker to ignore datallowconn.
1 parent 1664ae1 commit eed1ce7

File tree

11 files changed

+25
-20
lines changed

11 files changed

+25
-20
lines changed

contrib/pg_prewarm/autoprewarm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ autoprewarm_database_main(Datum main_arg)
445445
ereport(ERROR,
446446
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
447447
errmsg("could not map dynamic shared memory segment")));
448-
BackgroundWorkerInitializeConnectionByOid(apw_state->database, InvalidOid);
448+
BackgroundWorkerInitializeConnectionByOid(apw_state->database, InvalidOid, 0);
449449
block_info = (BlockInfoRecord *) dsm_segment_address(seg);
450450
pos = apw_state->prewarm_start_idx;
451451

src/backend/access/transam/parallel.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,8 @@ ParallelWorkerMain(Datum main_arg)
13241324

13251325
/* Restore database connection. */
13261326
BackgroundWorkerInitializeConnectionByOid(fps->database_id,
1327-
fps->authenticated_user_id);
1327+
fps->authenticated_user_id,
1328+
0);
13281329

13291330
/*
13301331
* Set the client encoding to the database encoding, since that is what

src/backend/bootstrap/bootstrap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ BootstrapModeMain(void)
498498
*/
499499
InitProcess();
500500

501-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL);
501+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
502502

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

src/backend/postmaster/autovacuum.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ AutoVacLauncherMain(int argc, char *argv[])
477477
InitProcess();
478478
#endif
479479

480-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL);
480+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
481481

482482
SetProcessingMode(NormalProcessing);
483483

@@ -1693,7 +1693,7 @@ AutoVacWorkerMain(int argc, char *argv[])
16931693
* Note: if we have selected a just-deleted database (due to using
16941694
* stale stats info), we'll fail and exit here.
16951695
*/
1696-
InitPostgres(NULL, dbid, NULL, InvalidOid, dbname);
1696+
InitPostgres(NULL, dbid, NULL, InvalidOid, dbname, false);
16971697
SetProcessingMode(NormalProcessing);
16981698
set_ps_display(dbname, false);
16991699
ereport(DEBUG1,

src/backend/postmaster/postmaster.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -5582,7 +5582,7 @@ MaxLivePostmasterChildren(void)
55825582
* Connect background worker to a database.
55835583
*/
55845584
void
5585-
BackgroundWorkerInitializeConnection(const char *dbname, const char *username)
5585+
BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
55865586
{
55875587
BackgroundWorker *worker = MyBgworkerEntry;
55885588

@@ -5592,7 +5592,7 @@ BackgroundWorkerInitializeConnection(const char *dbname, const char *username)
55925592
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
55935593
errmsg("database connection requirement not indicated during registration")));
55945594

5595-
InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
5595+
InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
55965596

55975597
/* it had better not gotten out of "init" mode yet */
55985598
if (!IsInitProcessingMode())
@@ -5605,7 +5605,7 @@ BackgroundWorkerInitializeConnection(const char *dbname, const char *username)
56055605
* Connect background worker to a database using OIDs.
56065606
*/
56075607
void
5608-
BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
5608+
BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
56095609
{
56105610
BackgroundWorker *worker = MyBgworkerEntry;
56115611

@@ -5615,7 +5615,7 @@ BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
56155615
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
56165616
errmsg("database connection requirement not indicated during registration")));
56175617

5618-
InitPostgres(NULL, dboid, NULL, useroid, NULL);
5618+
InitPostgres(NULL, dboid, NULL, useroid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
56195619

56205620
/* it had better not gotten out of "init" mode yet */
56215621
if (!IsInitProcessingMode())

src/backend/replication/logical/launcher.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ ApplyLauncherMain(Datum main_arg)
901901
* Establish connection to nailed catalogs (we only ever access
902902
* pg_subscription).
903903
*/
904-
BackgroundWorkerInitializeConnection(NULL, NULL);
904+
BackgroundWorkerInitializeConnection(NULL, NULL, 0);
905905

906906
/* Enter main loop */
907907
for (;;)

src/backend/replication/logical/worker.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,8 @@ ApplyWorkerMain(Datum main_arg)
15441544

15451545
/* Connect to our database. */
15461546
BackgroundWorkerInitializeConnectionByOid(MyLogicalRepWorker->dbid,
1547-
MyLogicalRepWorker->userid);
1547+
MyLogicalRepWorker->userid,
1548+
0);
15481549

15491550
/* Load the subscription into persistent memory context. */
15501551
ApplyContext = AllocSetContextCreate(TopMemoryContext,

src/backend/tcop/postgres.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3775,7 +3775,7 @@ PostgresMain(int argc, char *argv[],
37753775
* it inside InitPostgres() instead. In particular, anything that
37763776
* involves database access should be there, not here.
37773777
*/
3778-
InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
3778+
InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL, false);
37793779

37803780
/*
37813781
* If the PostmasterContext is still around, recycle the space; we don't

src/backend/utils/init/postinit.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
static HeapTuple GetDatabaseTuple(const char *dbname);
6767
static HeapTuple GetDatabaseTupleByOid(Oid dboid);
6868
static void PerformAuthentication(Port *port);
69-
static void CheckMyDatabase(const char *name, bool am_superuser);
69+
static void CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections);
7070
static void InitCommunication(void);
7171
static void ShutdownPostgres(int code, Datum arg);
7272
static void StatementTimeoutHandler(void);
@@ -290,7 +290,7 @@ PerformAuthentication(Port *port)
290290
* CheckMyDatabase -- fetch information from the pg_database entry for our DB
291291
*/
292292
static void
293-
CheckMyDatabase(const char *name, bool am_superuser)
293+
CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections)
294294
{
295295
HeapTuple tup;
296296
Form_pg_database dbform;
@@ -326,7 +326,7 @@ CheckMyDatabase(const char *name, bool am_superuser)
326326
/*
327327
* Check that the database is currently allowing connections.
328328
*/
329-
if (!dbform->datallowconn)
329+
if (!dbform->datallowconn && !override_allow_connections)
330330
ereport(FATAL,
331331
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
332332
errmsg("database \"%s\" is not currently accepting connections",
@@ -563,7 +563,7 @@ BaseInit(void)
563563
*/
564564
void
565565
InitPostgres(const char *in_dbname, Oid dboid, const char *username,
566-
Oid useroid, char *out_dbname)
566+
Oid useroid, char *out_dbname, bool override_allow_connections)
567567
{
568568
bool bootstrap = IsBootstrapProcessingMode();
569569
bool am_superuser;
@@ -1006,7 +1006,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
10061006
* user is a superuser, so the above stuff has to happen first.)
10071007
*/
10081008
if (!bootstrap)
1009-
CheckMyDatabase(dbname, am_superuser);
1009+
CheckMyDatabase(dbname, am_superuser, override_allow_connections);
10101010

10111011
/*
10121012
* Now process any command-line switches and any additional GUC variable

src/include/miscadmin.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ extern AuxProcType MyAuxProcType;
421421
extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
422422
extern void InitializeMaxBackends(void);
423423
extern void InitPostgres(const char *in_dbname, Oid dboid, const char *username,
424-
Oid useroid, char *out_dbname);
424+
Oid useroid, char *out_dbname, bool override_allow_connections);
425425
extern void BaseInit(void);
426426

427427
/* in utils/init/miscinit.c */

src/include/postmaster/bgworker.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,13 @@ extern PGDLLIMPORT BackgroundWorker *MyBgworkerEntry;
140140
* If dbname is NULL, connection is made to no specific database;
141141
* only shared catalogs can be accessed.
142142
*/
143-
extern void BackgroundWorkerInitializeConnection(const char *dbname, const char *username);
143+
extern void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags);
144144

145145
/* Just like the above, but specifying database and user by OID. */
146-
extern void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid);
146+
extern void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags);
147+
148+
/* Flags to BackgroundWorkerInitializeConnection et al */
149+
#define BGWORKER_BYPASS_ALLOWCONN 1
147150

148151
/* Block/unblock signals in a background worker process */
149152
extern void BackgroundWorkerBlockSignals(void);

0 commit comments

Comments
 (0)