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

Commitb460f5d

Browse files
committed
Add max_parallel_workers GUC.
Increase the default value of the existing max_worker_processes GUCfrom 8 to 16, and add a new max_parallel_workers GUC with a maximumof 8. This way, even if the maximum amount of parallel query ishappening, there is still room for background workers that do otherthings, as originally envisioned when max_worker_processes was added.Julien Rouhaud, reviewed by Amit Kapila and by revised by me.
1 parent5714931 commitb460f5d

File tree

9 files changed

+93
-10
lines changed

9 files changed

+93
-10
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ include_dir 'conf.d'
19821982
<para>
19831983
Sets the maximum number of background processes that the system
19841984
can support. This parameter can only be set at server start. The
1985-
default is8.
1985+
default is16.
19861986
</para>
19871987

19881988
<para>
@@ -2004,8 +2004,9 @@ include_dir 'conf.d'
20042004
Sets the maximum number of workers that can be started by a single
20052005
<literal>Gather</literal> node. Parallel workers are taken from the
20062006
pool of processes established by
2007-
<xref linkend="guc-max-worker-processes">. Note that the requested
2008-
number of workers may not actually be available at run time. If this
2007+
<xref linkend="guc-max-worker-processes">, limited by
2008+
<xref linkend="guc-max-parallel-workers">. Note that the requested
2009+
number of workers may not actually be available at runtime. If this
20092010
occurs, the plan will run with fewer workers than expected, which may
20102011
be inefficient. The default value is 2. Setting this value to 0
20112012
disables parallel query execution.
@@ -2034,6 +2035,22 @@ include_dir 'conf.d'
20342035
</listitem>
20352036
</varlistentry>
20362037

2038+
<varlistentry id="guc-max-parallel-workers" xreflabel="max_parallel_workers">
2039+
<term><varname>max_parallel_workers</varname> (<type>integer</type>)
2040+
<indexterm>
2041+
<primary><varname>max_parallel_workers</> configuration parameter</primary>
2042+
</indexterm>
2043+
</term>
2044+
<listitem>
2045+
<para>
2046+
Sets the maximum number of workers that the system can support for
2047+
parallel queries. The default value is 8. When increasing or
2048+
decreasing this value, consider also adjusting
2049+
<xref linkend="guc-max-parallel-workers-per-gather">.
2050+
</para>
2051+
</listitem>
2052+
</varlistentry>
2053+
20372054
<varlistentry id="guc-backend-flush-after" xreflabel="backend_flush_after">
20382055
<term><varname>backend_flush_after</varname> (<type>integer</type>)
20392056
<indexterm>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ LaunchParallelWorkers(ParallelContext *pcxt)
454454
snprintf(worker.bgw_name,BGW_MAXLEN,"parallel worker for PID %d",
455455
MyProcPid);
456456
worker.bgw_flags=
457-
BGWORKER_SHMEM_ACCESS |BGWORKER_BACKEND_DATABASE_CONNECTION;
457+
BGWORKER_SHMEM_ACCESS |BGWORKER_BACKEND_DATABASE_CONNECTION
458+
|BGWORKER_CLASS_PARALLEL;
458459
worker.bgw_start_time=BgWorkerStart_ConsistentState;
459460
worker.bgw_restart_time=BGW_NEVER_RESTART;
460461
worker.bgw_main=ParallelWorkerMain;

‎src/backend/postmaster/bgworker.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,22 @@ typedef struct BackgroundWorkerSlot
8080
BackgroundWorkerworker;
8181
}BackgroundWorkerSlot;
8282

83+
/*
84+
* In order to limit the total number of parallel workers (according to
85+
* max_parallel_workers GUC), we maintain the number of active parallel
86+
* workers. Since the postmaster cannot take locks, two variables are used for
87+
* this purpose: the number of registered parallel workers (modified by the
88+
* backends, protected by BackgroundWorkerLock) and the number of terminated
89+
* parallel workers (modified only by the postmaster, lockless). The active
90+
* number of parallel workers is the number of registered workers minus the
91+
* terminated ones. These counters can of course overflow, but it's not
92+
* important here since the subtraction will still give the right number.
93+
*/
8394
typedefstructBackgroundWorkerArray
8495
{
8596
inttotal_slots;
97+
uint32parallel_register_count;
98+
uint32parallel_terminate_count;
8699
BackgroundWorkerSlotslot[FLEXIBLE_ARRAY_MEMBER];
87100
}BackgroundWorkerArray;
88101

@@ -127,6 +140,8 @@ BackgroundWorkerShmemInit(void)
127140
intslotno=0;
128141

129142
BackgroundWorkerData->total_slots=max_worker_processes;
143+
BackgroundWorkerData->parallel_register_count=0;
144+
BackgroundWorkerData->parallel_terminate_count=0;
130145

131146
/*
132147
* Copy contents of worker list into shared memory. Record the shared
@@ -267,9 +282,12 @@ BackgroundWorkerStateChange(void)
267282

268283
/*
269284
* We need a memory barrier here to make sure that the load of
270-
* bgw_notify_pid completes before the store to in_use.
285+
* bgw_notify_pid and the update of parallel_terminate_count
286+
* complete before the store to in_use.
271287
*/
272288
notify_pid=slot->worker.bgw_notify_pid;
289+
if ((slot->worker.bgw_flags&BGWORKER_CLASS_PARALLEL)!=0)
290+
BackgroundWorkerData->parallel_terminate_count++;
273291
pg_memory_barrier();
274292
slot->pid=0;
275293
slot->in_use= false;
@@ -370,6 +388,9 @@ ForgetBackgroundWorker(slist_mutable_iter *cur)
370388

371389
Assert(rw->rw_shmem_slot<max_worker_processes);
372390
slot=&BackgroundWorkerData->slot[rw->rw_shmem_slot];
391+
if ((rw->rw_worker.bgw_flags&BGWORKER_CLASS_PARALLEL)!=0)
392+
BackgroundWorkerData->parallel_terminate_count++;
393+
373394
slot->in_use= false;
374395

375396
ereport(DEBUG1,
@@ -824,6 +845,7 @@ RegisterDynamicBackgroundWorker(BackgroundWorker *worker,
824845
{
825846
intslotno;
826847
boolsuccess= false;
848+
boolparallel;
827849
uint64generation=0;
828850

829851
/*
@@ -840,8 +862,27 @@ RegisterDynamicBackgroundWorker(BackgroundWorker *worker,
840862
if (!SanityCheckBackgroundWorker(worker,ERROR))
841863
return false;
842864

865+
parallel= (worker->bgw_flags&BGWORKER_CLASS_PARALLEL)!=0;
866+
843867
LWLockAcquire(BackgroundWorkerLock,LW_EXCLUSIVE);
844868

869+
/*
870+
* If this is a parallel worker, check whether there are already too many
871+
* parallel workers; if so, don't register another one. Our view of
872+
* parallel_terminate_count may be slightly stale, but that doesn't really
873+
* matter: we would have gotten the same result if we'd arrived here
874+
* slightly earlier anyway. There's no help for it, either, since the
875+
* postmaster must not take locks; a memory barrier wouldn't guarantee
876+
* anything useful.
877+
*/
878+
if (parallel&& (BackgroundWorkerData->parallel_register_count-
879+
BackgroundWorkerData->parallel_terminate_count) >=
880+
max_parallel_workers)
881+
{
882+
LWLockRelease(BackgroundWorkerLock);
883+
return false;
884+
}
885+
845886
/*
846887
* Look for an unused slot. If we find one, grab it.
847888
*/
@@ -856,6 +897,8 @@ RegisterDynamicBackgroundWorker(BackgroundWorker *worker,
856897
slot->generation++;
857898
slot->terminate= false;
858899
generation=slot->generation;
900+
if (parallel)
901+
BackgroundWorkerData->parallel_register_count++;
859902

860903
/*
861904
* Make sure postmaster doesn't see the slot as in use before it

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ intreplacement_sort_tuples = 150000;
121121
*/
122122
intNBuffers=1000;
123123
intMaxConnections=90;
124-
intmax_worker_processes=8;
124+
intmax_worker_processes=16;
125+
intmax_parallel_workers=8;
125126
intMaxBackends=0;
126127

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

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,7 @@ static struct config_int ConfigureNamesInt[] =
24772477
NULL,
24782478
},
24792479
&max_worker_processes,
2480-
8,0,MAX_BACKENDS,
2480+
16,0,MAX_BACKENDS,
24812481
check_max_worker_processes,NULL,NULL
24822482
},
24832483

@@ -2664,6 +2664,16 @@ static struct config_int ConfigureNamesInt[] =
26642664
NULL,NULL,NULL
26652665
},
26662666

2667+
{
2668+
{"max_parallel_workers",PGC_USERSET,RESOURCES_ASYNCHRONOUS,
2669+
gettext_noop("Sets the maximum number of parallel workers than can be active at one time."),
2670+
NULL
2671+
},
2672+
&max_parallel_workers,
2673+
8,0,1024,
2674+
NULL,NULL,NULL
2675+
},
2676+
26672677
{
26682678
{"autovacuum_work_mem",PGC_SIGHUP,RESOURCES_MEM,
26692679
gettext_noop("Sets the maximum memory to be used by each autovacuum worker process."),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@
161161
# - Asynchronous Behavior -
162162

163163
#effective_io_concurrency = 1# 1-1000; 0 disables prefetching
164-
#max_worker_processes =8# (change requires restart)
164+
#max_worker_processes =16# (change requires restart)
165165
#max_parallel_workers_per_gather = 2# taken from max_worker_processes
166+
#max_parallel_workers = 8 # total maximum number of worker_processes
166167
#old_snapshot_threshold = -1# 1min-60d; -1 disables; 0 is immediate
167168
# (change requires restart)
168169
#backend_flush_after = 0# measured in pages, 0 disables

‎src/bin/pg_resetxlog/pg_resetxlog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ GuessControlValues(void)
584584
ControlFile.wal_log_hints= false;
585585
ControlFile.track_commit_timestamp= false;
586586
ControlFile.MaxConnections=100;
587-
ControlFile.max_worker_processes=8;
587+
ControlFile.max_worker_processes=16;
588588
ControlFile.max_prepared_xacts=0;
589589
ControlFile.max_locks_per_xact=64;
590590

@@ -800,7 +800,7 @@ RewriteControlFile(void)
800800
ControlFile.wal_log_hints= false;
801801
ControlFile.track_commit_timestamp= false;
802802
ControlFile.MaxConnections=100;
803-
ControlFile.max_worker_processes=8;
803+
ControlFile.max_worker_processes=16;
804804
ControlFile.max_prepared_xacts=0;
805805
ControlFile.max_locks_per_xact=64;
806806

‎src/include/miscadmin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ extern PGDLLIMPORT int NBuffers;
157157
externintMaxBackends;
158158
externintMaxConnections;
159159
externintmax_worker_processes;
160+
externintmax_parallel_workers;
160161

161162
externPGDLLIMPORTintMyProcPid;
162163
externPGDLLIMPORTpg_time_tMyStartTime;

‎src/include/postmaster/bgworker.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
*/
5959
#defineBGWORKER_BACKEND_DATABASE_CONNECTION0x0002
6060

61+
/*
62+
* This class is used internally for parallel queries, to keep track of the
63+
* number of active parallel workers and make sure we never launch more than
64+
* max_parallel_workers parallel workers at the same time. Third party
65+
* background workers should not use this class.
66+
*/
67+
#defineBGWORKER_CLASS_PARALLEL0x0010
68+
/* add additional bgworker classes here */
69+
6170

6271
typedefvoid (*bgworker_main_type) (Datummain_arg);
6372

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp