Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brQWe-000461-1p for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Oct 2016 14:16:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1brQWd-0000be-I7 for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Oct 2016 14:16:55 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1brQWc-0000bR-Ei for pgsql-hackers@postgresql.org; Tue, 04 Oct 2016 14:16:54 +0000 Received: from mimolette.dalibo.net ([212.85.157.144] helo=mail.dalibo.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1brQWY-0007e7-Cl for pgsql-hackers@postgresql.org; Tue, 04 Oct 2016 14:16:53 +0000 Received: from [192.168.0.2] (aur75-1-78-192-238-147.fbxo.proxad.net [78.192.238.147]) by mail.dalibo.com (Postfix) with ESMTPSA id 0F8912C0205; Tue, 4 Oct 2016 16:16:47 +0200 (CEST) Subject: Re: Rename max_parallel_degree? To: Robert Haas References: <20160426152217.GA548975@alvherre.pgsql> <9f84548d-8b59-b5c8-510d-991f8ee3ab20@dalibo.com> <20312f33-c14e-0d3f-a79f-95212f64a38c@dalibo.com> <9a3ca4ad-c3ea-2c02-499a-029e053fedda@dalibo.com> <7ef3a740-8c64-764d-9f02-b27132d89602@2ndquadrant.com> <699e9156-fb30-7a6d-0f5f-b78b97ddce14@dalibo.com> Cc: Peter Eisentraut , Amit Kapila , Tom Lane , Josh Berkus , "David G. Johnston" , Peter Geoghegan , Noah Misch , David Rowley , Alvaro Herrera , Magnus Hagander , Bruce Momjian , PostgreSQL-development From: Julien Rouhaud Message-ID: Date: Tue, 4 Oct 2016 16:16:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------B40DB0A8AD3B073532E729F7" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------B40DB0A8AD3B073532E729F7 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 03/10/2016 21:27, Robert Haas wrote: > On Fri, Sep 30, 2016 at 12:23 PM, Julien Rouhaud > wrote: >> I've already fixed every other issues mentioned upthread, but I'm facing >> a problem for this one. Assuming that the bgworker classes are supposed >> to be mutually exclusive, I don't see a simple and clean way to add such >> a check in SanityCheckBackgroundWorker(). Am I missing something >> obvious, or can someone give me some advice for this? > > My advice is "don't worry about it". If somebody thinks of something > that can be usefully added there, it will take very little time to add > it and test that it works. Don't get hung up on that for now. > Ok, thanks! Please find attached v9 of the patch, adding the parallel worker class and changing max_worker_processes default to 16 and max_parallel_workers to 8. I also added Amit's explanation for the need of a write barrier in ForgetBackgroundWorker(). I'll add this patch to the next commitfest. -- Julien Rouhaud http://dalibo.com - http://dalibo.org --------------B40DB0A8AD3B073532E729F7 Content-Type: text/x-diff; name="global_max_parallel_workers-v9.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="global_max_parallel_workers-v9.diff" diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index e826c19..61c5a7c 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1984,7 +1984,7 @@ include_dir 'conf.d' Sets the maximum number of background processes that the system can support. This parameter can only be set at server start. The - default is 8. + default is 16. =20 @@ -2006,8 +2006,9 @@ include_dir 'conf.d' Sets the maximum number of workers that can be started by a single Gather node. Parallel workers are taken from = the pool of processes established by - . Note that the reque= sted - number of workers may not actually be available at run time. If = this + , limited by + . Note that the reque= sted + number of workers may not actually be available at runtime. If t= his occurs, the plan will run with fewer workers than expected, which= may be inefficient. The default value is 2. Setting this value to 0 disables parallel query execution. @@ -2036,6 +2037,21 @@ include_dir 'conf.d' =20 + + max_parallel_workers (integer) + + max_parallel_workers configuration parameter<= /primary> + + + + + Sets the maximum number of workers that the system can support for + parallel queries. The default value is 8. Setting this value to= 0 + disables parallel query execution. + + + + backend_flush_after (integer) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/tra= nsam/parallel.c index cde0ed3..0177401 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -453,7 +453,8 @@ LaunchParallelWorkers(ParallelContext *pcxt) snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d", MyProcPid); worker.bgw_flags =3D - BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION; + BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION + | BGWORKER_CLASS_PARALLEL; worker.bgw_start_time =3D BgWorkerStart_ConsistentState; worker.bgw_restart_time =3D BGW_NEVER_RESTART; worker.bgw_main =3D ParallelWorkerMain; diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/= path/allpaths.c index e42ef98..6ad8fd0 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -718,9 +718,11 @@ create_plain_partial_paths(PlannerInfo *root, RelOptIn= fo *rel) } =20 /* - * In no case use more than max_parallel_workers_per_gather workers. + * In no case use more than max_parallel_workers or + * max_parallel_workers_per_gather workers. */ - parallel_workers =3D Min(parallel_workers, max_parallel_workers_per_gathe= r); + parallel_workers =3D Min(max_parallel_workers, Min(parallel_workers, + max_parallel_workers_per_gather)); =20 /* If any limit was set to zero, the user doesn't want a parallel scan. */ if (parallel_workers <=3D 0) diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/= path/costsize.c index 2a49639..09dc077 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -113,6 +113,7 @@ int effective_cache_size =3D DEFAULT_EFFECTIVE_CACHE_= SIZE; =20 Cost disable_cost =3D 1.0e10; =20 +int max_parallel_workers =3D 8; int max_parallel_workers_per_gather =3D 2; =20 bool enable_seqscan =3D true; diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/p= lan/planner.c index f657ffc..aa8670b 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -249,6 +249,7 @@ standard_planner(Query *parse, int cursorOptions, Param= ListInfo boundParams) parse->utilityStmt =3D=3D NULL && !parse->hasModifyingCTE && max_parallel_workers_per_gather > 0 && + max_parallel_workers > 0 && !IsParallelWorker() && !IsolationIsSerializable()) { diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgw= orker.c index 699c934..665c2f4 100644 --- a/src/backend/postmaster/bgworker.c +++ b/src/backend/postmaster/bgworker.c @@ -16,6 +16,7 @@ =20 #include "miscadmin.h" #include "libpq/pqsignal.h" +#include "optimizer/cost.h" #include "postmaster/bgworker_internals.h" #include "postmaster/postmaster.h" #include "storage/barrier.h" @@ -76,12 +77,26 @@ typedef struct BackgroundWorkerSlot bool terminate; pid_t pid; /* InvalidPid =3D not started yet; 0 =3D dead */ uint64 generation; /* incremented when slot is recycled */ + int worker_class; BackgroundWorker worker; } BackgroundWorkerSlot; =20 +/* + * In order to limit the total number of parallel workers (according to + * max_parallel_workers GUC), we maintain the number of active parallel + * workers. Since the postmaster cannot take locks, two variables are use= d for + * this purpose: the number of registered parallel workers (modified by the + * backends, protected by BackgroundWorkerLock) and the number of terminat= ed + * parallel workers (modified only by the postmaster, lockless). The acti= ve + * number of parallel workers is the number of registered workers minus the + * terminated ones. These counters can of course overflow, but it's not + * important here since the subtraction will still give the right number. + */ typedef struct BackgroundWorkerArray { int total_slots; + uint32 parallel_register_count; + uint32 parallel_terminate_count; BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]; } BackgroundWorkerArray; =20 @@ -126,6 +141,8 @@ BackgroundWorkerShmemInit(void) int slotno =3D 0; =20 BackgroundWorkerData->total_slots =3D max_worker_processes; + BackgroundWorkerData->parallel_register_count =3D 0; + BackgroundWorkerData->parallel_terminate_count =3D 0; =20 /* * Copy contents of worker list into shared memory. Record the shared @@ -144,6 +161,7 @@ BackgroundWorkerShmemInit(void) slot->terminate =3D false; slot->pid =3D InvalidPid; slot->generation =3D 0; + slot->worker_class =3D 0; rw->rw_shmem_slot =3D slotno; rw->rw_worker.bgw_notify_pid =3D 0; /* might be reinit after crash */ memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker)); @@ -266,9 +284,12 @@ BackgroundWorkerStateChange(void) =20 /* * We need a memory barrier here to make sure that the load of - * bgw_notify_pid completes before the store to in_use. + * bgw_notify_pid and the update of parallel_terminate_count + * complete before the store to in_use. */ notify_pid =3D slot->worker.bgw_notify_pid; + if (slot->worker_class & BGWORKER_CLASS_PARALLEL) + BackgroundWorkerData->parallel_terminate_count++; pg_memory_barrier(); slot->pid =3D 0; slot->in_use =3D false; @@ -369,6 +390,16 @@ ForgetBackgroundWorker(slist_mutable_iter *cur) =20 Assert(rw->rw_shmem_slot < max_worker_processes); slot =3D &BackgroundWorkerData->slot[rw->rw_shmem_slot]; + if (slot->worker_class & BGWORKER_CLASS_PARALLEL) + BackgroundWorkerData->parallel_terminate_count++; + + /* + * We need a write barrier to make sure the update of + * parallel_terminate_count is done before the store to in_use. Otherwise, + * it's possible that concurrent registration of worker can rewrite the + * worker_class before we check it here. + */ + pg_write_barrier(); slot->in_use =3D false; =20 ereport(DEBUG1, @@ -498,6 +529,28 @@ SanityCheckBackgroundWorker(BackgroundWorker *worker, = int elevel) /* XXX other checks? */ } =20 + /* sanity checks for parallel workers */ + if (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) + { + if (max_parallel_workers =3D=3D 0) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("background worker \"%s\": cannot request parallel worker if = no parallel worker is allowed", + worker->bgw_name))); + return false; + } + if ((worker->bgw_flags & BGWORKER_SHMEM_ACCESS) =3D=3D 0 || + (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION) =3D=3D 0) + { + ereport(elevel, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("background worker \"%s\": cannot request parallel worker wit= hout shared memory access and database connection", + worker->bgw_name))); + return false; + } + } + if ((worker->bgw_restart_time < 0 && worker->bgw_restart_time !=3D BGW_NEVER_RESTART) || (worker->bgw_restart_time > USECS_PER_DAY / 1000)) @@ -824,6 +877,7 @@ RegisterDynamicBackgroundWorker(BackgroundWorker *worke= r, { int slotno; bool success =3D false; + bool parallel; uint64 generation =3D 0; =20 /* @@ -840,8 +894,24 @@ RegisterDynamicBackgroundWorker(BackgroundWorker *work= er, if (!SanityCheckBackgroundWorker(worker, ERROR)) return false; =20 + parallel =3D (worker->bgw_flags & BGWORKER_CLASS_PARALLEL); + LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE); =20 + if (parallel && (BackgroundWorkerData->parallel_register_count - + BackgroundWorkerData->parallel_terminate_count) >=3D + max_parallel_workers) + { + LWLockRelease(BackgroundWorkerLock); + return false; + } + + /* + * We need a read barrier here to make sure the above test doesn't get + * reordered + */ + pg_read_barrier(); + /* * Look for an unused slot. If we find one, grab it. */ @@ -855,7 +925,10 @@ RegisterDynamicBackgroundWorker(BackgroundWorker *work= er, slot->pid =3D InvalidPid; /* indicates not started yet */ slot->generation++; slot->terminate =3D false; + slot->worker_class =3D (worker->bgw_flags & BGWORKER_CLASS_MASK); generation =3D slot->generation; + if (parallel) + BackgroundWorkerData->parallel_register_count++; =20 /* * Make sure postmaster doesn't see the slot as in use before it diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/glob= als.c index f232083..8277d84 100644 --- a/src/backend/utils/init/globals.c +++ b/src/backend/utils/init/globals.c @@ -121,7 +121,7 @@ int replacement_sort_tuples =3D 150000; */ int NBuffers =3D 1000; int MaxConnections =3D 90; -int max_worker_processes =3D 8; +int max_worker_processes =3D 16; int MaxBackends =3D 0; =20 int VacuumCostPageHit =3D 1; /* GUC parameters for vacuum */ diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 622279b..3c12b3d 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -2479,7 +2479,7 @@ static struct config_int ConfigureNamesInt[] =3D NULL, }, &max_worker_processes, - 8, 0, MAX_BACKENDS, + 16, 0, MAX_BACKENDS, check_max_worker_processes, NULL, NULL }, =20 @@ -2667,6 +2667,16 @@ static struct config_int ConfigureNamesInt[] =3D }, =20 { + {"max_parallel_workers", PGC_USERSET, RESOURCES_ASYNCHRONOUS, + gettext_noop("Sets the maximum number of parallel workers than can be a= ctive at one time."), + NULL + }, + &max_parallel_workers, + 8, 0, 1024, + NULL, NULL, NULL + }, + + { {"autovacuum_work_mem", PGC_SIGHUP, RESOURCES_MEM, gettext_noop("Sets the maximum memory to be used by each autovacuum wor= ker process."), NULL, diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/ut= ils/misc/postgresql.conf.sample index 05b1373..3fb5a60 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -162,8 +162,9 @@ # - Asynchronous Behavior - =20 #effective_io_concurrency =3D 1 # 1-1000; 0 disables prefetching -#max_worker_processes =3D 8 # (change requires restart) +#max_worker_processes =3D 16 # (change requires restart) #max_parallel_workers_per_gather =3D 2 # taken from max_worker_processes +#max_parallel_workers =3D 8 # total maximum number of worker_processes #old_snapshot_threshold =3D -1 # 1min-60d; -1 disables; 0 is immediate # (change requires restart) #backend_flush_after =3D 0 # 0 disables, default is 0 diff --git a/src/bin/pg_resetxlog/pg_resetxlog.c b/src/bin/pg_resetxlog/pg_= resetxlog.c index 525b82ba..5e2c55f 100644 --- a/src/bin/pg_resetxlog/pg_resetxlog.c +++ b/src/bin/pg_resetxlog/pg_resetxlog.c @@ -584,7 +584,7 @@ GuessControlValues(void) ControlFile.wal_log_hints =3D false; ControlFile.track_commit_timestamp =3D false; ControlFile.MaxConnections =3D 100; - ControlFile.max_worker_processes =3D 8; + ControlFile.max_worker_processes =3D 16; ControlFile.max_prepared_xacts =3D 0; ControlFile.max_locks_per_xact =3D 64; =20 @@ -800,7 +800,7 @@ RewriteControlFile(void) ControlFile.wal_log_hints =3D false; ControlFile.track_commit_timestamp =3D false; ControlFile.MaxConnections =3D 100; - ControlFile.max_worker_processes =3D 8; + ControlFile.max_worker_processes =3D 16; ControlFile.max_prepared_xacts =3D 0; ControlFile.max_locks_per_xact =3D 64; =20 diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index 2a4df2f..190f33b 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -55,6 +55,7 @@ extern PGDLLIMPORT double parallel_setup_cost; extern PGDLLIMPORT int effective_cache_size; extern Cost disable_cost; extern int max_parallel_workers_per_gather; +extern int max_parallel_workers; extern bool enable_seqscan; extern bool enable_indexscan; extern bool enable_indexonlyscan; diff --git a/src/include/postmaster/bgworker.h b/src/include/postmaster/bgw= orker.h index b6889a3..5272c91 100644 --- a/src/include/postmaster/bgworker.h +++ b/src/include/postmaster/bgworker.h @@ -58,6 +58,18 @@ */ #define BGWORKER_BACKEND_DATABASE_CONNECTION 0x0002 =20 +/* bitmask to define background worker classes */ +#define BGWORKER_CLASS_MASK 0x00f0 + +/* + * This class is used internally for parallel queries, to keep track of the + * number of active parallel workers and make sure we never launch more th= an + * max_parallel_workers parallel workers at the same time. Third party + * background workers should not use this class. + */ +#define BGWORKER_CLASS_PARALLEL 0x0010 +/* add additional bgworker classes here */ + =20 typedef void (*bgworker_main_type) (Datum main_arg); =20 --------------B40DB0A8AD3B073532E729F7 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------B40DB0A8AD3B073532E729F7--