Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7e8d0eb

Browse files
committed
Check for too many postmaster children before spawning a bgworker.
The postmaster's code path for spawning a bgworker neglected to checkwhether we already have the max number of live child processes. That'sa bit hard to hit, since it would necessarily be a transient condition;but if we do, AssignPostmasterChildSlot() fails causing a postmastercrash, as seen in a report from Bhargav Kamineni.To fix, invoke canAcceptConnections() in the bgworker code path, as wedo in the other code paths that spawn children. Since we don't wantthe same pmState tests in this case, add a child-process-type parameterto canAcceptConnections() so that it can know what to do.Back-patch to 9.5. In principle the same hazard exists in 9.4, but thecode is enough different that this patch wouldn't quite fix it there.Given the tiny usage of bgworkers in that branch it doesn't seem worthcreating a variant patch for it.Discussion:https://postgr.es/m/18733.1570382257@sss.pgh.pa.us
1 parente4d050e commit7e8d0eb

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

‎src/backend/postmaster/postmaster.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
409409
staticvoidprocessCancelRequest(Port*port,void*pkt);
410410
staticintinitMasks(fd_set*rmask);
411411
staticvoidreport_fork_failure_to_client(Port*port,interrnum);
412-
staticCAC_statecanAcceptConnections(void);
412+
staticCAC_statecanAcceptConnections(intbackend_type);
413413
staticboolRandomCancelKey(int32*cancel_key);
414414
staticvoidsignal_child(pid_tpid,intsignal);
415415
staticboolSignalSomeChildren(intsignal,inttargets);
@@ -2384,24 +2384,30 @@ processCancelRequest(Port *port, void *pkt)
23842384
}
23852385

23862386
/*
2387-
* canAcceptConnections --- check to see if database state allows connections.
2387+
* canAcceptConnections --- check to see if database state allows connections
2388+
* of the specified type. backend_type can be BACKEND_TYPE_NORMAL,
2389+
* BACKEND_TYPE_AUTOVAC, or BACKEND_TYPE_BGWORKER. (Note that we don't yet
2390+
* know whether a NORMAL connection might turn into a walsender.)
23882391
*/
23892392
staticCAC_state
2390-
canAcceptConnections(void)
2393+
canAcceptConnections(intbackend_type)
23912394
{
23922395
CAC_stateresult=CAC_OK;
23932396

23942397
/*
23952398
* Can't start backends when in startup/shutdown/inconsistent recovery
2396-
* state.
2399+
* state. We treat autovac workers the same as user backends for this
2400+
* purpose. However, bgworkers are excluded from this test; we expect
2401+
* bgworker_should_start_now() decided whether the DB state allows them.
23972402
*
23982403
* In state PM_WAIT_BACKUP only superusers can connect (this must be
23992404
* allowed so that a superuser can end online backup mode); we return
24002405
* CAC_WAITBACKUP code to indicate that this must be checked later. Note
24012406
* that neither CAC_OK nor CAC_WAITBACKUP can safely be returned until we
24022407
* have checked for too many children.
24032408
*/
2404-
if (pmState!=PM_RUN)
2409+
if (pmState!=PM_RUN&&
2410+
backend_type!=BACKEND_TYPE_BGWORKER)
24052411
{
24062412
if (pmState==PM_WAIT_BACKUP)
24072413
result=CAC_WAITBACKUP;/* allow superusers only */
@@ -2421,9 +2427,9 @@ canAcceptConnections(void)
24212427
/*
24222428
* Don't start too many children.
24232429
*
2424-
* We allow more connections than we can have backends here because some
2430+
* We allow more connectionsherethan we can have backends because some
24252431
* might still be authenticating; they might fail auth, or some existing
2426-
* backend might exit before the auth cycle is completed. The exact
2432+
* backend might exit before the auth cycle is completed.The exact
24272433
* MaxBackends limit is enforced when a new backend tries to join the
24282434
* shared-inval backend array.
24292435
*
@@ -4086,7 +4092,7 @@ BackendStartup(Port *port)
40864092
bn->cancel_key=MyCancelKey;
40874093

40884094
/* Pass down canAcceptConnections state */
4089-
port->canAcceptConnections=canAcceptConnections();
4095+
port->canAcceptConnections=canAcceptConnections(BACKEND_TYPE_NORMAL);
40904096
bn->dead_end= (port->canAcceptConnections!=CAC_OK&&
40914097
port->canAcceptConnections!=CAC_WAITBACKUP);
40924098

@@ -5458,7 +5464,7 @@ StartAutovacuumWorker(void)
54585464
* we have to check to avoid race-condition problems during DB state
54595465
* changes.
54605466
*/
5461-
if (canAcceptConnections()==CAC_OK)
5467+
if (canAcceptConnections(BACKEND_TYPE_AUTOVAC)==CAC_OK)
54625468
{
54635469
/*
54645470
* Compute the cancel key that will be assigned to this session. We
@@ -5703,12 +5709,13 @@ do_start_bgworker(RegisteredBgWorker *rw)
57035709

57045710
/*
57055711
* Allocate and assign the Backend element. Note we must do this before
5706-
* forking, so that we can handle out of memory properly.
5712+
* forking, so that we can handle failures (out of memory or child-process
5713+
* slots) cleanly.
57075714
*
57085715
* Treat failure as though the worker had crashed. That way, the
5709-
* postmaster will wait a bit before attempting to start it again; ifit
5710-
* tried again right away, most likelyit'd finditself repeating the
5711-
*out-of-memory or fork failure condition.
5716+
* postmaster will wait a bit before attempting to start it again; ifwe
5717+
* tried again right away, most likelywe'd findourselves hitting the
5718+
*same resource-exhaustion condition.
57125719
*/
57135720
if (!assign_backendlist_entry(rw))
57145721
{
@@ -5834,6 +5841,19 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
58345841
{
58355842
Backend*bn;
58365843

5844+
/*
5845+
* Check that database state allows another connection. Currently the
5846+
* only possible failure is CAC_TOOMANY, so we just log an error message
5847+
* based on that rather than checking the error code precisely.
5848+
*/
5849+
if (canAcceptConnections(BACKEND_TYPE_BGWORKER)!=CAC_OK)
5850+
{
5851+
ereport(LOG,
5852+
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
5853+
errmsg("no slot available for new worker process")));
5854+
return false;
5855+
}
5856+
58375857
/*
58385858
* Compute the cancel key that will be assigned to this session. We
58395859
* probably don't need cancel keys for background workers, but we'd better

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp