Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bHZyC-0003ul-Vb for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Jun 2016 17:05:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bHZyC-0001qB-F8 for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Jun 2016 17:05:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bHZyB-0001p7-P2 for pgsql-hackers@postgresql.org; Mon, 27 Jun 2016 17:05:11 +0000 Received: from mimolette.dalibo.net ([212.85.157.144] helo=mail.dalibo.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bHZy3-0003mX-N8 for pgsql-hackers@postgresql.org; Mon, 27 Jun 2016 17:05:10 +0000 Received: from [172.16.1.87] (2-229-27-106.ip194.fastwebnet.it [2.229.27.106]) by mail.dalibo.com (Postfix) with ESMTPSA id 1EE562C2C1B; Mon, 27 Jun 2016 19:05:01 +0200 (CEST) Subject: Re: Rename max_parallel_degree? To: Amit Kapila References: <20160426152217.GA548975@alvherre.pgsql> <5856af3e-3c20-89d1-3a9c-af3c2cd3a92b@dalibo.com> <9b599d4c-cb80-4f1f-48ad-169469579efe@dalibo.com> <2b70e5a6-28fe-6601-d294-cd568930265f@dalibo.com> <79f47dd2-819a-940a-5912-5ff643e87b54@dalibo.com> <98adbca7-3bec-7ee4-92b0-893a0ace5cca@dalibo.com> Cc: Robert Haas , 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: Mon, 27 Jun 2016 19:05:11 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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 On 27/06/2016 07:18, Amit Kapila wrote: > On Mon, Jun 27, 2016 at 10:21 AM, Amit Kapila wrote: >> >> I think as the parallel_terminate_count is only modified by postmaster >> and read by other processes, such an operation will be considered >> atomic. We don't need to specifically make it atomic unless multiple >> processes are allowed to modify such a counter. Although, I think we >> need to use some barriers in code to make it safe. >> >> 1. >> @@ -272,6 +279,8 @@ BackgroundWorkerStateChange(void) >> pg_memory_barrier(); >> >> slot->pid = 0; >> slot->in_use = false; >> + if (slot->parallel) >> + >> BackgroundWorkerData->parallel_terminate_count++; >> >> I think the check of slot->parallel and increment to >> parallel_terminate_count should be done before marking slot->in_use to >> false. Consider the case if slot->in_use is marked as false and then >> before next line execution, one of the backends registers dynamic >> worker (non-parallel worker), so that >> backend can see this slot as free and use it. It will also mark the >> parallel flag of slot as false. Now when postmaster will check the >> slot->parallel flag, it will find it false and then skip the increment >> to parallel_terminate_count. >> > > If you agree with above theory, then you need to use write barrier as > well after incrementing the parallel_terminate_count to avoid > re-ordering with slot->in_use flag. > I totally agree, I didn't thought about this corner case. There's already a pg_memory_barrier() call in BackgroundWorkerStateChange(), to avoid reordering the notify_pid load. Couldn't we use it to also make sure the parallel_terminate_count increment happens before the slot->in_use store? I guess that a write barrier will be needed in ForgetBacgroundWorker(). >> 2. >> + if (parallel && (BackgroundWorkerData->parallel_register_count - >> + >> BackgroundWorkerData->parallel_terminate_count) >= >> + >> max_parallel_workers) >> + { >> + LWLockRelease(BackgroundWorkerLock); >> + return >> false; >> + } >>+ >> >> I think we need a read barrier here, so that this check doesn't get >> reordered with the for loop below it. You mean between the end of this block and the for loop? (Sorry, I'm not at all familiar with the pg_barrier mechanism). >> Also, see if you find the code >> more readable by moving the after && part of check to next line. I think I'll just pgindent the file. -- Julien Rouhaud http://dalibo.com - http://dalibo.org -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers