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

Commitcdbc0ca

Browse files
committed
Fix background workers for EXEC_BACKEND
Commitda07a1e was broken for EXEC_BACKEND because I failed to realizethat the MaxBackends recomputation needed to be duplicated bysubprocesses in SubPostmasterMain. However, instead of having the valuebe recomputed at all, it's better to assign the correct value atpostmaster initialization time, and have it be propagated to exec'edbackends via BackendParameters.MaxBackends stays as zero until after modules inshared_preload_libraries have had a chance to register bgworkers, sincethe value is going to be untrustworthy till that's finished.Heikki Linnakangas and Álvaro Herrera
1 parentd194d7a commitcdbc0ca

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

‎src/backend/postmaster/postmaster.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ typedef struct
499499
boolredirection_done;
500500
boolIsBinaryUpgrade;
501501
intmax_safe_fds;
502+
intMaxBackends;
502503
#ifdefWIN32
503504
HANDLEPostmasterHandle;
504505
HANDLEinitial_signal_pipe;
@@ -897,15 +898,14 @@ PostmasterMain(int argc, char *argv[])
897898
process_shared_preload_libraries();
898899

899900
/*
900-
* If loadable modules have added background workers, MaxBackends needs to
901-
* be updated.Do so now by forcing a no-op update of max_connections.
902-
* XXX This is a pretty ugly way to do it, but it doesn't seem worth
903-
* introducing a new entry point in guc.c to do it in a cleaner fashion.
901+
* Now that loadable modules have had their chance to register background
902+
* workers, calculate MaxBackends. Add one for the autovacuum launcher.
904903
*/
905-
if (GetNumShmemAttachedBgworkers()>0)
906-
SetConfigOption("max_connections",
907-
GetConfigOption("max_connections", false, false),
908-
PGC_POSTMASTER,PGC_S_OVERRIDE);
904+
MaxBackends=MaxConnections+autovacuum_max_workers+1+
905+
GetNumShmemAttachedBgworkers();
906+
/* internal error because the values were all checked previously */
907+
if (MaxBackends>MAX_BACKENDS)
908+
elog(ERROR,"too many backends configured");
909909

910910
/*
911911
* Establish input sockets.
@@ -5152,6 +5152,8 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
51525152
{
51535153
RegisteredBgWorker*rw;
51545154
intnamelen=strlen(worker->bgw_name);
5155+
staticintmaxworkers;
5156+
staticintnumworkers=0;
51555157

51565158
#ifdefEXEC_BACKEND
51575159

@@ -5162,6 +5164,11 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
51625164
staticintBackgroundWorkerCookie=1;
51635165
#endif
51645166

5167+
/* initialize upper limit on first call */
5168+
if (numworkers==0)
5169+
maxworkers=MAX_BACKENDS-
5170+
(MaxConnections+autovacuum_max_workers+1);
5171+
51655172
if (!IsUnderPostmaster)
51665173
ereport(LOG,
51675174
(errmsg("registering background worker: %s",worker->bgw_name)));
@@ -5214,6 +5221,23 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
52145221
return;
52155222
}
52165223

5224+
/*
5225+
* Enforce maximum number of workers. Note this is overly restrictive:
5226+
* we could allow more non-shmem-connected workers, because these don't
5227+
* count towards the MAX_BACKENDS limit elsewhere. This doesn't really
5228+
* matter for practical purposes; several million processes would need to
5229+
* run on a single server.
5230+
*/
5231+
if (++numworkers>maxworkers)
5232+
{
5233+
ereport(LOG,
5234+
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
5235+
errmsg("too many background workers"),
5236+
errdetail("Up to %d background workers can be registered with the current settings.",
5237+
maxworkers)));
5238+
return;
5239+
}
5240+
52175241
/*
52185242
* Copy the registration data into the registered workers list.
52195243
*/
@@ -5836,6 +5860,8 @@ save_backend_variables(BackendParameters *param, Port *port,
58365860
param->IsBinaryUpgrade=IsBinaryUpgrade;
58375861
param->max_safe_fds=max_safe_fds;
58385862

5863+
param->MaxBackends=MaxBackends;
5864+
58395865
#ifdefWIN32
58405866
param->PostmasterHandle=PostmasterHandle;
58415867
if (!write_duplicated_handle(&param->initial_signal_pipe,
@@ -6061,6 +6087,8 @@ restore_backend_variables(BackendParameters *param, Port *port)
60616087
IsBinaryUpgrade=param->IsBinaryUpgrade;
60626088
max_safe_fds=param->max_safe_fds;
60636089

6090+
MaxBackends=param->MaxBackends;
6091+
60646092
#ifdefWIN32
60656093
PostmasterHandle=param->PostmasterHandle;
60666094
pgwin32_initial_signal_pipe=param->initial_signal_pipe;

‎src/backend/utils/init/globals.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ intwork_mem = 1024;
103103
intmaintenance_work_mem=16384;
104104

105105
/*
106-
* Primary determinants of sizes of shared-memory structures. MaxBackends is
107-
* MaxConnections + autovacuum_max_workers + 1 (it is computed by the GUC
108-
* assign hooks for those variables):
106+
* Primary determinants of sizes of shared-memory structures.
107+
*
108+
* MaxBackends is computed by PostmasterMain after modules have had a chance to
109+
* register background workers.
109110
*/
110111
intNBuffers=1000;
111-
intMaxBackends=100;
112112
intMaxConnections=90;
113+
intMaxBackends=0;
113114

114115
intVacuumCostPageHit=1;/* GUC parameters for vacuum */
115116
intVacuumCostPageMiss=10;

‎src/backend/utils/misc/guc.c

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,6 @@
103103
#defineMAX_KILOBYTES(INT_MAX / 1024)
104104
#endif
105105

106-
/*
107-
* Note: MAX_BACKENDS is limited to 2^23-1 because inval.c stores the
108-
* backend ID as a 3-byte signed integer. Even if that limitation were
109-
* removed, we still could not exceed INT_MAX/4 because some places compute
110-
* 4*MaxBackends without any overflow check. This is rechecked in
111-
* check_maxconnections, since MaxBackends is computed as MaxConnections
112-
* plus the number of bgworkers plus autovacuum_max_workers plus one (for the
113-
* autovacuum launcher).
114-
*/
115-
#defineMAX_BACKENDS0x7fffff
116-
117106
#defineKB_PER_MB (1024)
118107
#defineKB_PER_GB (1024*1024)
119108

@@ -199,9 +188,7 @@ static const char *show_tcp_keepalives_idle(void);
199188
staticconstchar*show_tcp_keepalives_interval(void);
200189
staticconstchar*show_tcp_keepalives_count(void);
201190
staticboolcheck_maxconnections(int*newval,void**extra,GucSourcesource);
202-
staticvoidassign_maxconnections(intnewval,void*extra);
203191
staticboolcheck_autovacuum_max_workers(int*newval,void**extra,GucSourcesource);
204-
staticvoidassign_autovacuum_max_workers(intnewval,void*extra);
205192
staticboolcheck_effective_io_concurrency(int*newval,void**extra,GucSourcesource);
206193
staticvoidassign_effective_io_concurrency(intnewval,void*extra);
207194
staticvoidassign_pgstat_temp_directory(constchar*newval,void*extra);
@@ -1615,7 +1602,7 @@ static struct config_int ConfigureNamesInt[] =
16151602
},
16161603
&MaxConnections,
16171604
100,1,MAX_BACKENDS,
1618-
check_maxconnections,assign_maxconnections,NULL
1605+
check_maxconnections,NULL,NULL
16191606
},
16201607

16211608
{
@@ -2290,7 +2277,7 @@ static struct config_int ConfigureNamesInt[] =
22902277
},
22912278
&autovacuum_max_workers,
22922279
3,1,MAX_BACKENDS,
2293-
check_autovacuum_max_workers,assign_autovacuum_max_workers,NULL
2280+
check_autovacuum_max_workers,NULL,NULL
22942281
},
22952282

22962283
{
@@ -8636,13 +8623,6 @@ check_maxconnections(int *newval, void **extra, GucSource source)
86368623
return true;
86378624
}
86388625

8639-
staticvoid
8640-
assign_maxconnections(intnewval,void*extra)
8641-
{
8642-
MaxBackends=newval+autovacuum_max_workers+1+
8643-
GetNumShmemAttachedBgworkers();
8644-
}
8645-
86468626
staticbool
86478627
check_autovacuum_max_workers(int*newval,void**extra,GucSourcesource)
86488628
{
@@ -8652,12 +8632,6 @@ check_autovacuum_max_workers(int *newval, void **extra, GucSource source)
86528632
return true;
86538633
}
86548634

8655-
staticvoid
8656-
assign_autovacuum_max_workers(intnewval,void*extra)
8657-
{
8658-
MaxBackends=MaxConnections+newval+1+GetNumShmemAttachedBgworkers();
8659-
}
8660-
86618635
staticbool
86628636
check_effective_io_concurrency(int*newval,void**extra,GucSourcesource)
86638637
{

‎src/include/postmaster/postmaster.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,13 @@ extern Size ShmemBackendArraySize(void);
6161
externvoidShmemBackendArrayAllocation(void);
6262
#endif
6363

64+
/*
65+
* Note: MAX_BACKENDS is limited to 2^23-1 because inval.c stores the
66+
* backend ID as a 3-byte signed integer. Even if that limitation were
67+
* removed, we still could not exceed INT_MAX/4 because some places compute
68+
* 4*MaxBackends without any overflow check. This is rechecked in the relevant
69+
* GUC check hooks and in RegisterBackgroundWorker().
70+
*/
71+
#defineMAX_BACKENDS0x7fffff
72+
6473
#endif/* _POSTMASTER_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp