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

Commit1b5c2dd

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 parent1b6b348 commit1b5c2dd

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
@@ -418,7 +418,7 @@ static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
418418
staticvoidprocessCancelRequest(Port*port,void*pkt);
419419
staticintinitMasks(fd_set*rmask);
420420
staticvoidreport_fork_failure_to_client(Port*port,interrnum);
421-
staticCAC_statecanAcceptConnections(void);
421+
staticCAC_statecanAcceptConnections(intbackend_type);
422422
staticboolRandomCancelKey(int32*cancel_key);
423423
staticvoidsignal_child(pid_tpid,intsignal);
424424
staticboolSignalSomeChildren(intsignal,inttargets);
@@ -2370,24 +2370,30 @@ processCancelRequest(Port *port, void *pkt)
23702370
}
23712371

23722372
/*
2373-
* canAcceptConnections --- check to see if database state allows connections.
2373+
* canAcceptConnections --- check to see if database state allows connections
2374+
* of the specified type. backend_type can be BACKEND_TYPE_NORMAL,
2375+
* BACKEND_TYPE_AUTOVAC, or BACKEND_TYPE_BGWORKER. (Note that we don't yet
2376+
* know whether a NORMAL connection might turn into a walsender.)
23742377
*/
23752378
staticCAC_state
2376-
canAcceptConnections(void)
2379+
canAcceptConnections(intbackend_type)
23772380
{
23782381
CAC_stateresult=CAC_OK;
23792382

23802383
/*
23812384
* Can't start backends when in startup/shutdown/inconsistent recovery
2382-
* state.
2385+
* state. We treat autovac workers the same as user backends for this
2386+
* purpose. However, bgworkers are excluded from this test; we expect
2387+
* bgworker_should_start_now() decided whether the DB state allows them.
23832388
*
23842389
* In state PM_WAIT_BACKUP only superusers can connect (this must be
23852390
* allowed so that a superuser can end online backup mode); we return
23862391
* CAC_WAITBACKUP code to indicate that this must be checked later. Note
23872392
* that neither CAC_OK nor CAC_WAITBACKUP can safely be returned until we
23882393
* have checked for too many children.
23892394
*/
2390-
if (pmState!=PM_RUN)
2395+
if (pmState!=PM_RUN&&
2396+
backend_type!=BACKEND_TYPE_BGWORKER)
23912397
{
23922398
if (pmState==PM_WAIT_BACKUP)
23932399
result=CAC_WAITBACKUP;/* allow superusers only */
@@ -2407,9 +2413,9 @@ canAcceptConnections(void)
24072413
/*
24082414
* Don't start too many children.
24092415
*
2410-
* We allow more connections than we can have backends here because some
2416+
* We allow more connectionsherethan we can have backends because some
24112417
* might still be authenticating; they might fail auth, or some existing
2412-
* backend might exit before the auth cycle is completed. The exact
2418+
* backend might exit before the auth cycle is completed.The exact
24132419
* MaxBackends limit is enforced when a new backend tries to join the
24142420
* shared-inval backend array.
24152421
*
@@ -4038,7 +4044,7 @@ BackendStartup(Port *port)
40384044
bn->cancel_key=MyCancelKey;
40394045

40404046
/* Pass down canAcceptConnections state */
4041-
port->canAcceptConnections=canAcceptConnections();
4047+
port->canAcceptConnections=canAcceptConnections(BACKEND_TYPE_NORMAL);
40424048
bn->dead_end= (port->canAcceptConnections!=CAC_OK&&
40434049
port->canAcceptConnections!=CAC_WAITBACKUP);
40444050

@@ -5439,7 +5445,7 @@ StartAutovacuumWorker(void)
54395445
* we have to check to avoid race-condition problems during DB state
54405446
* changes.
54415447
*/
5442-
if (canAcceptConnections()==CAC_OK)
5448+
if (canAcceptConnections(BACKEND_TYPE_AUTOVAC)==CAC_OK)
54435449
{
54445450
/*
54455451
* Compute the cancel key that will be assigned to this session. We
@@ -5684,12 +5690,13 @@ do_start_bgworker(RegisteredBgWorker *rw)
56845690

56855691
/*
56865692
* Allocate and assign the Backend element. Note we must do this before
5687-
* forking, so that we can handle out of memory properly.
5693+
* forking, so that we can handle failures (out of memory or child-process
5694+
* slots) cleanly.
56885695
*
56895696
* Treat failure as though the worker had crashed. That way, the
5690-
* postmaster will wait a bit before attempting to start it again; ifit
5691-
* tried again right away, most likelyit'd finditself repeating the
5692-
*out-of-memory or fork failure condition.
5697+
* postmaster will wait a bit before attempting to start it again; ifwe
5698+
* tried again right away, most likelywe'd findourselves hitting the
5699+
*same resource-exhaustion condition.
56935700
*/
56945701
if (!assign_backendlist_entry(rw))
56955702
{
@@ -5815,6 +5822,19 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
58155822
{
58165823
Backend*bn;
58175824

5825+
/*
5826+
* Check that database state allows another connection. Currently the
5827+
* only possible failure is CAC_TOOMANY, so we just log an error message
5828+
* based on that rather than checking the error code precisely.
5829+
*/
5830+
if (canAcceptConnections(BACKEND_TYPE_BGWORKER)!=CAC_OK)
5831+
{
5832+
ereport(LOG,
5833+
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
5834+
errmsg("no slot available for new worker process")));
5835+
return false;
5836+
}
5837+
58185838
/*
58195839
* Compute the cancel key that will be assigned to this session. We
58205840
* probably don't need cancel keys for background workers, but we'd better

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp