Move bgworker specific logic to bgworker.c
authorHeikki Linnakangas <[email protected]>
Tue, 2 Jul 2024 17:12:05 +0000 (20:12 +0300)
committerHeikki Linnakangas <[email protected]>
Tue, 2 Jul 2024 17:12:05 +0000 (20:12 +0300)
For clarity, we've been slowly moving functions that are not called
from the postmaster process out of postmaster.c.

Author: Xing Guo <[email protected]>
Discussion: https://www.postgresql.org/message-id/CACpMh%2BDBHVT4xPGimzvex%3DwMdMLQEu9PYhT%2BkwwD2x2nu9dU_Q%40mail.gmail.com

src/backend/postmaster/bgworker.c
src/backend/postmaster/postmaster.c

index b37ccb85ad6fc0ad5b1672d374694a2db1845095..13dc2cf064e10323c155b2b273de579ad9408da2 100644 (file)
@@ -851,6 +851,89 @@ BackgroundWorkerMain(char *startup_data, size_t startup_data_len)
    proc_exit(0);
 }
 
+/*
+ * Connect background worker to a database.
+ */
+void
+BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
+{
+   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;
+   /* ignore rolcanlogin? */
+   if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
+       init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
+
+   /* XXX is this the right errcode? */
+   if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
+       ereport(FATAL,
+               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                errmsg("database connection requirement not indicated during registration")));
+
+   InitPostgres(dbname, InvalidOid,    /* database to connect to */
+                username, InvalidOid,  /* role to connect as */
+                init_flags,
+                NULL);         /* no out_dbname */
+
+   /* it had better not gotten out of "init" mode yet */
+   if (!IsInitProcessingMode())
+       ereport(ERROR,
+               (errmsg("invalid processing mode in background worker")));
+   SetProcessingMode(NormalProcessing);
+}
+
+/*
+ * Connect background worker to a database using OIDs.
+ */
+void
+BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
+{
+   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;
+   /* ignore rolcanlogin? */
+   if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
+       init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
+
+   /* XXX is this the right errcode? */
+   if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
+       ereport(FATAL,
+               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                errmsg("database connection requirement not indicated during registration")));
+
+   InitPostgres(NULL, dboid,   /* database to connect to */
+                NULL, useroid, /* role to connect as */
+                init_flags,
+                NULL);         /* no out_dbname */
+
+   /* it had better not gotten out of "init" mode yet */
+   if (!IsInitProcessingMode())
+       ereport(ERROR,
+               (errmsg("invalid processing mode in background worker")));
+   SetProcessingMode(NormalProcessing);
+}
+
+/*
+ * Block/unblock signals in a background worker
+ */
+void
+BackgroundWorkerBlockSignals(void)
+{
+   sigprocmask(SIG_SETMASK, &BlockSig, NULL);
+}
+
+void
+BackgroundWorkerUnblockSignals(void)
+{
+   sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
+}
+
 /*
  * Register a new static background worker.
  *
index 97c8332c84d0339472dce11b3ba6b9470431a1d5..6f974a8d21a8a438f6b11c06fd0defc9431c40b0 100644 (file)
@@ -4148,89 +4148,6 @@ MaxLivePostmasterChildren(void)
                max_wal_senders + max_worker_processes);
 }
 
-/*
- * Connect background worker to a database.
- */
-void
-BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
-{
-   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;
-   /* ignore rolcanlogin? */
-   if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
-       init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
-
-   /* XXX is this the right errcode? */
-   if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
-       ereport(FATAL,
-               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                errmsg("database connection requirement not indicated during registration")));
-
-   InitPostgres(dbname, InvalidOid,    /* database to connect to */
-                username, InvalidOid,  /* role to connect as */
-                init_flags,
-                NULL);         /* no out_dbname */
-
-   /* it had better not gotten out of "init" mode yet */
-   if (!IsInitProcessingMode())
-       ereport(ERROR,
-               (errmsg("invalid processing mode in background worker")));
-   SetProcessingMode(NormalProcessing);
-}
-
-/*
- * Connect background worker to a database using OIDs.
- */
-void
-BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
-{
-   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;
-   /* ignore rolcanlogin? */
-   if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
-       init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
-
-   /* XXX is this the right errcode? */
-   if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
-       ereport(FATAL,
-               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                errmsg("database connection requirement not indicated during registration")));
-
-   InitPostgres(NULL, dboid,   /* database to connect to */
-                NULL, useroid, /* role to connect as */
-                init_flags,
-                NULL);         /* no out_dbname */
-
-   /* it had better not gotten out of "init" mode yet */
-   if (!IsInitProcessingMode())
-       ereport(ERROR,
-               (errmsg("invalid processing mode in background worker")));
-   SetProcessingMode(NormalProcessing);
-}
-
-/*
- * Block/unblock signals in a background worker
- */
-void
-BackgroundWorkerBlockSignals(void)
-{
-   sigprocmask(SIG_SETMASK, &BlockSig, NULL);
-}
-
-void
-BackgroundWorkerUnblockSignals(void)
-{
-   sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
-}
-
 /*
  * Start a new bgworker.
  * Starting time conditions must have been checked already.