1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Minor refactoring of assign_backendlist_entry()

Make assign_backendlist_entry() responsible just for allocating the
Backend struct. Linking it to the RegisteredBgWorker is the caller's
responsibility now. Seems more clear that way.

Discussion: https://www.postgresql.org/message-id/835232c0-a5f7-4f20-b95b-5b56ba57d741@iki.fi
This commit is contained in:
Heikki Linnakangas
2024-08-01 23:23:55 +03:00
parent ef4c35b416
commit 63bef4df97

View File

@ -416,7 +416,7 @@ static void TerminateChildren(int signal);
#define SignalChildren(sig) SignalSomeChildren(sig, BACKEND_TYPE_ALL) #define SignalChildren(sig) SignalSomeChildren(sig, BACKEND_TYPE_ALL)
static int CountChildren(int target); static int CountChildren(int target);
static bool assign_backendlist_entry(RegisteredBgWorker *rw); static Backend *assign_backendlist_entry(void);
static void maybe_start_bgworkers(void); static void maybe_start_bgworkers(void);
static bool CreateOptsFile(int argc, char *argv[], char *fullprogname); static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
static pid_t StartChildProcess(BackendType type); static pid_t StartChildProcess(BackendType type);
@ -4028,6 +4028,7 @@ MaxLivePostmasterChildren(void)
static bool static bool
do_start_bgworker(RegisteredBgWorker *rw) do_start_bgworker(RegisteredBgWorker *rw)
{ {
Backend *bn;
pid_t worker_pid; pid_t worker_pid;
Assert(rw->rw_pid == 0); Assert(rw->rw_pid == 0);
@ -4042,11 +4043,14 @@ do_start_bgworker(RegisteredBgWorker *rw)
* tried again right away, most likely we'd find ourselves hitting the * tried again right away, most likely we'd find ourselves hitting the
* same resource-exhaustion condition. * same resource-exhaustion condition.
*/ */
if (!assign_backendlist_entry(rw)) bn = assign_backendlist_entry();
if (bn == NULL)
{ {
rw->rw_crashed_at = GetCurrentTimestamp(); rw->rw_crashed_at = GetCurrentTimestamp();
return false; return false;
} }
rw->rw_backend = bn;
rw->rw_child_slot = bn->child_slot;
ereport(DEBUG1, ereport(DEBUG1,
(errmsg_internal("starting background worker process \"%s\"", (errmsg_internal("starting background worker process \"%s\"",
@ -4119,12 +4123,10 @@ bgworker_should_start_now(BgWorkerStartTime start_time)
* Allocate the Backend struct for a connected background worker, but don't * Allocate the Backend struct for a connected background worker, but don't
* add it to the list of backends just yet. * add it to the list of backends just yet.
* *
* On failure, return false without changing any worker state. * On failure, return NULL.
*
* Some info from the Backend is copied into the passed rw.
*/ */
static bool static Backend *
assign_backendlist_entry(RegisteredBgWorker *rw) assign_backendlist_entry(void)
{ {
Backend *bn; Backend *bn;
@ -4138,7 +4140,7 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
ereport(LOG, ereport(LOG,
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
errmsg("no slot available for new background worker process"))); errmsg("no slot available for new background worker process")));
return false; return NULL;
} }
bn = palloc_extended(sizeof(Backend), MCXT_ALLOC_NO_OOM); bn = palloc_extended(sizeof(Backend), MCXT_ALLOC_NO_OOM);
@ -4147,7 +4149,7 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
ereport(LOG, ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"))); errmsg("out of memory")));
return false; return NULL;
} }
bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot(); bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
@ -4155,10 +4157,7 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
bn->dead_end = false; bn->dead_end = false;
bn->bgworker_notify = false; bn->bgworker_notify = false;
rw->rw_backend = bn; return bn;
rw->rw_child_slot = bn->child_slot;
return true;
} }
/* /*