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

Commit6bc8ef0

Browse files
committed
Add new GUC, max_worker_processes, limiting number of bgworkers.
In 9.3, there's no particular limit on the number of bgworkers;instead, we just count up the number that are actually registered,and use that to set MaxBackends. However, that approach causesproblems for Hot Standby, which needs both MaxBackends and thesize of the lock table to be the same on the standby as on themaster, yet it may not be desirable to run the same bgworkers inboth places. 9.3 handles that by failing to notice the problem,which will probably work fine in nearly all cases anyway, but isnot theoretically sound.A further problem with simply counting the number of registeredworkers is that new workers can't be registered without apostmaster restart. This is inconvenient for administrators,since bouncing the postmaster causes an interruption of service.Moreover, there are a number of applications for backgroundprocesses where, by necessity, the background process must bestarted on the fly (e.g. parallel query). While this patchdoesn't actually make it possible to register new backgroundworkers after startup time, it's a necessary prerequisite.Patch by me. Review by Michael Paquier.
1 parent5cbe935 commit6bc8ef0

File tree

15 files changed

+77
-59
lines changed

15 files changed

+77
-59
lines changed

‎doc/src/sgml/bgworker.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,9 @@ typedef struct BackgroundWorker
146146
The <filename>worker_spi</> contrib module contains a working example,
147147
which demonstrates some useful techniques.
148148
</para>
149+
150+
<para>
151+
The maximum number of registered background workers is limited by
152+
<xref linkend="guc-max-worker-processes">.
153+
</para>
149154
</chapter>

‎doc/src/sgml/config.sgml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,25 @@ include 'filename'
15951595
</para>
15961596
</listitem>
15971597
</varlistentry>
1598+
1599+
<varlistentry id="guc-max-worker-processes" xreflabel="max_worrker_processes">
1600+
<term><varname>max_worker_processes</varname> (<type>integer</type>)</term>
1601+
<indexterm>
1602+
<primary><varname>max_worker_processes</> configuration parameter</primary>
1603+
</indexterm>
1604+
<listitem>
1605+
<para>
1606+
Sets the maximum number of background processes that the system
1607+
can support. This parameter can only be set at server start.
1608+
</para>
1609+
1610+
<para>
1611+
When running a standby server, you must set this parameter to the
1612+
same or higher value than on the master server. Otherwise, queries
1613+
will not be allowed in the standby server.
1614+
</para>
1615+
</listitem>
1616+
</varlistentry>
15981617
</variablelist>
15991618
</sect2>
16001619
</sect1>

‎src/backend/access/rmgrdesc/xlogdesc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ xlog_desc(StringInfo buf, uint8 xl_info, char *rec)
117117
}
118118
}
119119

120-
appendStringInfo(buf,"parameter change: max_connections=%d max_prepared_xacts=%d max_locks_per_xact=%d wal_level=%s",
120+
appendStringInfo(buf,"parameter change: max_connections=%dmax_worker_processes=%dmax_prepared_xacts=%d max_locks_per_xact=%d wal_level=%s",
121121
xlrec.MaxConnections,
122+
xlrec.max_worker_processes,
122123
xlrec.max_prepared_xacts,
123124
xlrec.max_locks_per_xact,
124125
wal_level_str);

‎src/backend/access/transam/xlog.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,7 @@ BootStrapXLOG(void)
41344134

41354135
/* Set important parameter values for use when replaying WAL */
41364136
ControlFile->MaxConnections=MaxConnections;
4137+
ControlFile->max_worker_processes=max_worker_processes;
41374138
ControlFile->max_prepared_xacts=max_prepared_xacts;
41384139
ControlFile->max_locks_per_xact=max_locks_per_xact;
41394140
ControlFile->wal_level=wal_level;
@@ -4841,6 +4842,9 @@ CheckRequiredParameterValues(void)
48414842
RecoveryRequiresIntParameter("max_connections",
48424843
MaxConnections,
48434844
ControlFile->MaxConnections);
4845+
RecoveryRequiresIntParameter("max_worker_processes",
4846+
max_worker_processes,
4847+
ControlFile->max_worker_processes);
48444848
RecoveryRequiresIntParameter("max_prepared_transactions",
48454849
max_prepared_xacts,
48464850
ControlFile->max_prepared_xacts);
@@ -7770,6 +7774,7 @@ XLogReportParameters(void)
77707774
{
77717775
if (wal_level!=ControlFile->wal_level||
77727776
MaxConnections!=ControlFile->MaxConnections||
7777+
max_worker_processes!=ControlFile->max_worker_processes||
77737778
max_prepared_xacts!=ControlFile->max_prepared_xacts||
77747779
max_locks_per_xact!=ControlFile->max_locks_per_xact)
77757780
{
@@ -7786,6 +7791,7 @@ XLogReportParameters(void)
77867791
xl_parameter_changexlrec;
77877792

77887793
xlrec.MaxConnections=MaxConnections;
7794+
xlrec.max_worker_processes=max_worker_processes;
77897795
xlrec.max_prepared_xacts=max_prepared_xacts;
77907796
xlrec.max_locks_per_xact=max_locks_per_xact;
77917797
xlrec.wal_level=wal_level;
@@ -7799,6 +7805,7 @@ XLogReportParameters(void)
77997805
}
78007806

78017807
ControlFile->MaxConnections=MaxConnections;
7808+
ControlFile->max_worker_processes=max_worker_processes;
78027809
ControlFile->max_prepared_xacts=max_prepared_xacts;
78037810
ControlFile->max_locks_per_xact=max_locks_per_xact;
78047811
ControlFile->wal_level=wal_level;
@@ -8184,6 +8191,7 @@ xlog_redo(XLogRecPtr lsn, XLogRecord *record)
81848191

81858192
LWLockAcquire(ControlFileLock,LW_EXCLUSIVE);
81868193
ControlFile->MaxConnections=xlrec.MaxConnections;
8194+
ControlFile->max_worker_processes=xlrec.max_worker_processes;
81878195
ControlFile->max_prepared_xacts=xlrec.max_prepared_xacts;
81888196
ControlFile->max_locks_per_xact=xlrec.max_locks_per_xact;
81898197
ControlFile->wal_level=xlrec.wal_level;

‎src/backend/postmaster/postmaster.c

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ static void reaper(SIGNAL_ARGS);
402402
staticvoidsigusr1_handler(SIGNAL_ARGS);
403403
staticvoidstartup_die(SIGNAL_ARGS);
404404
staticvoiddummy_handler(SIGNAL_ARGS);
405-
staticintGetNumRegisteredBackgroundWorkers(intflags);
406405
staticvoidStartupPacketTimeoutHandler(void);
407406
staticvoidCleanupBackend(intpid,intexitstatus);
408407
staticboolCleanupBackgroundWorker(intpid,intexitstatus);
@@ -5212,7 +5211,7 @@ int
52125211
MaxLivePostmasterChildren(void)
52135212
{
52145213
return2* (MaxConnections+autovacuum_max_workers+1+
5215-
GetNumRegisteredBackgroundWorkers(0));
5214+
max_worker_processes);
52165215
}
52175216

52185217
/*
@@ -5226,7 +5225,6 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
52265225
{
52275226
RegisteredBgWorker*rw;
52285227
intnamelen=strlen(worker->bgw_name);
5229-
staticintmaxworkers;
52305228
staticintnumworkers=0;
52315229

52325230
#ifdefEXEC_BACKEND
@@ -5238,11 +5236,6 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
52385236
staticintBackgroundWorkerCookie=1;
52395237
#endif
52405238

5241-
/* initialize upper limit on first call */
5242-
if (numworkers==0)
5243-
maxworkers=MAX_BACKENDS-
5244-
(MaxConnections+autovacuum_max_workers+1);
5245-
52465239
if (!IsUnderPostmaster)
52475240
ereport(LOG,
52485241
(errmsg("registering background worker: %s",worker->bgw_name)));
@@ -5298,17 +5291,17 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
52985291
/*
52995292
* Enforce maximum number of workers. Note this is overly restrictive: we
53005293
* could allow more non-shmem-connected workers, because these don't count
5301-
* towards the MAX_BACKENDS limit elsewhere. This doesn't really matter
5302-
* for practical purposes; several million processes would need to run on
5303-
* a single server.
5294+
* towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
5295+
* important to relax this restriction.
53045296
*/
5305-
if (++numworkers>maxworkers)
5297+
if (++numworkers>max_worker_processes)
53065298
{
53075299
ereport(LOG,
53085300
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
53095301
errmsg("too many background workers"),
53105302
errdetail("Up to %d background workers can be registered with the current settings.",
5311-
maxworkers)));
5303+
max_worker_processes),
5304+
errhint("Consider increasing the configuration parameter \"max_worker_processes\".")));
53125305
return;
53135306
}
53145307

@@ -5589,41 +5582,6 @@ do_start_bgworker(void)
55895582
proc_exit(0);
55905583
}
55915584

5592-
/*
5593-
* Return the number of background workers registered that have at least
5594-
* one of the passed flag bits set.
5595-
*/
5596-
staticint
5597-
GetNumRegisteredBackgroundWorkers(intflags)
5598-
{
5599-
slist_iteriter;
5600-
intcount=0;
5601-
5602-
slist_foreach(iter,&BackgroundWorkerList)
5603-
{
5604-
RegisteredBgWorker*rw;
5605-
5606-
rw=slist_container(RegisteredBgWorker,rw_lnode,iter.cur);
5607-
5608-
if (flags!=0&&
5609-
!(rw->rw_worker.bgw_flags&flags))
5610-
continue;
5611-
5612-
count++;
5613-
}
5614-
5615-
returncount;
5616-
}
5617-
5618-
/*
5619-
* Return the number of bgworkers that need to have PGPROC entries.
5620-
*/
5621-
int
5622-
GetNumShmemAttachedBgworkers(void)
5623-
{
5624-
returnGetNumRegisteredBackgroundWorkers(BGWORKER_SHMEM_ACCESS);
5625-
}
5626-
56275585
#ifdefEXEC_BACKEND
56285586
staticpid_t
56295587
bgworker_forkexec(intcookie)

‎src/backend/storage/lmgr/proc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ ProcGlobalSemas(void)
140140
* running out when trying to start another backend is a common failure.
141141
* So, now we grab enough semaphores to support the desired max number
142142
* of backends immediately at initialization --- if the sysadmin has set
143-
* MaxConnections or autovacuum_max_workers higher than his kernel will
144-
* support, he'll find out sooner rather than later. (The number of
145-
* background worker processes registered by loadable modules is also taken
146-
* into consideration.)
143+
* MaxConnections, max_worker_processes, or autovacuum_max_workers higher
144+
* than his kernel will support, he'll find out sooner rather than later.
147145
*
148146
* Another reason for creating semaphores here is that the semaphore
149147
* implementation typically requires us to create semaphores in the

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ intmaintenance_work_mem = 16384;
109109
*/
110110
intNBuffers=1000;
111111
intMaxConnections=90;
112+
intmax_worker_processes=8;
112113
intMaxBackends=0;
113114

114115
intVacuumCostPageHit=1;/* GUC parameters for vacuum */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ InitializeMaxBackends(void)
436436

437437
/* the extra unit accounts for the autovacuum launcher */
438438
MaxBackends=MaxConnections+autovacuum_max_workers+1+
439-
GetNumShmemAttachedBgworkers();
439+
+max_worker_processes;
440440

441441
/* internal error because the values were all checked previously */
442442
if (MaxBackends>MAX_BACKENDS)

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ static const char *show_tcp_keepalives_idle(void);
190190
staticconstchar*show_tcp_keepalives_interval(void);
191191
staticconstchar*show_tcp_keepalives_count(void);
192192
staticboolcheck_maxconnections(int*newval,void**extra,GucSourcesource);
193+
staticboolcheck_max_worker_processes(int*newval,void**extra,GucSourcesource);
193194
staticboolcheck_autovacuum_max_workers(int*newval,void**extra,GucSourcesource);
194195
staticboolcheck_effective_io_concurrency(int*newval,void**extra,GucSourcesource);
195196
staticvoidassign_effective_io_concurrency(intnewval,void*extra);
@@ -2158,6 +2159,18 @@ static struct config_int ConfigureNamesInt[] =
21582159
check_effective_io_concurrency,assign_effective_io_concurrency,NULL
21592160
},
21602161

2162+
{
2163+
{"max_worker_processes",
2164+
PGC_POSTMASTER,
2165+
RESOURCES_ASYNCHRONOUS,
2166+
gettext_noop("Maximum number of concurrent worker processes."),
2167+
NULL,
2168+
},
2169+
&max_worker_processes,
2170+
8,1,MAX_BACKENDS,
2171+
check_max_worker_processes,NULL,NULL
2172+
},
2173+
21612174
{
21622175
{"log_rotation_age",PGC_SIGHUP,LOGGING_WHERE,
21632176
gettext_noop("Automatic log file rotation will occur after N minutes."),
@@ -8667,17 +8680,24 @@ show_tcp_keepalives_count(void)
86678680
staticbool
86688681
check_maxconnections(int*newval,void**extra,GucSourcesource)
86698682
{
8670-
if (*newval+GetNumShmemAttachedBgworkers()+autovacuum_max_workers+1>
8671-
MAX_BACKENDS)
8683+
if (*newval+autovacuum_max_workers+1+
8684+
max_worker_processes>MAX_BACKENDS)
86728685
return false;
86738686
return true;
86748687
}
86758688

86768689
staticbool
86778690
check_autovacuum_max_workers(int*newval,void**extra,GucSourcesource)
86788691
{
8679-
if (MaxConnections+*newval+1+GetNumShmemAttachedBgworkers()>
8680-
MAX_BACKENDS)
8692+
if (MaxConnections+*newval+1+max_worker_processes>MAX_BACKENDS)
8693+
return false;
8694+
return true;
8695+
}
8696+
8697+
staticbool
8698+
check_max_worker_processes(int*newval,void**extra,GucSourcesource)
8699+
{
8700+
if (MaxConnections+autovacuum_max_workers+1+*newval>MAX_BACKENDS)
86818701
return false;
86828702
return true;
86838703
}

‎src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
# - Asynchronous Behavior -
153153

154154
#effective_io_concurrency = 1# 1-1000; 0 disables prefetching
155+
#max_worker_processes = 8
155156

156157

157158
#------------------------------------------------------------------------------

‎src/bin/pg_controldata/pg_controldata.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ main(int argc, char *argv[])
260260
wal_level_str(ControlFile.wal_level));
261261
printf(_("Current max_connections setting: %d\n"),
262262
ControlFile.MaxConnections);
263+
printf(_("Current max_worker_processes setting: %d\n"),
264+
ControlFile.max_worker_processes);
263265
printf(_("Current max_prepared_xacts setting: %d\n"),
264266
ControlFile.max_prepared_xacts);
265267
printf(_("Current max_locks_per_xact setting: %d\n"),

‎src/bin/pg_resetxlog/pg_resetxlog.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ GuessControlValues(void)
518518

519519
ControlFile.wal_level=WAL_LEVEL_MINIMAL;
520520
ControlFile.MaxConnections=100;
521+
ControlFile.max_worker_processes=8;
521522
ControlFile.max_prepared_xacts=0;
522523
ControlFile.max_locks_per_xact=64;
523524

@@ -664,6 +665,7 @@ RewriteControlFile(void)
664665
*/
665666
ControlFile.wal_level=WAL_LEVEL_MINIMAL;
666667
ControlFile.MaxConnections=100;
668+
ControlFile.max_worker_processes=8;
667669
ControlFile.max_prepared_xacts=0;
668670
ControlFile.max_locks_per_xact=64;
669671

‎src/include/access/xlog_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef struct BkpBlock
5555
/*
5656
* Each page of XLOG file has a header like this:
5757
*/
58-
#defineXLOG_PAGE_MAGIC0xD075/* can be used as WAL version indicator */
58+
#defineXLOG_PAGE_MAGIC0xD076/* can be used as WAL version indicator */
5959

6060
typedefstructXLogPageHeaderData
6161
{
@@ -205,6 +205,7 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
205205
typedefstructxl_parameter_change
206206
{
207207
intMaxConnections;
208+
intmax_worker_processes;
208209
intmax_prepared_xacts;
209210
intmax_locks_per_xact;
210211
intwal_level;

‎src/include/catalog/pg_control.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ typedef struct ControlFileData
172172
*/
173173
intwal_level;
174174
intMaxConnections;
175+
intmax_worker_processes;
175176
intmax_prepared_xacts;
176177
intmax_locks_per_xact;
177178

‎src/include/miscadmin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ extern PGDLLIMPORT char *DataDir;
141141
externPGDLLIMPORTintNBuffers;
142142
externintMaxBackends;
143143
externintMaxConnections;
144+
externintmax_worker_processes;
144145

145146
externPGDLLIMPORTintMyProcPid;
146147
externPGDLLIMPORTpg_time_tMyStartTime;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp