From: Julien Rouhaud <julien.rouhaud@dalibo.com>
To: Amit Kapila <amit.kapila16@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Josh Berkus <josh@agliodbs.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: Peter Geoghegan <pg@heroku.com>
Cc: Noah Misch <noah@leadboat.com>
Cc: David Rowley <david.rowley@2ndquadrant.com>
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
Cc: Magnus Hagander <magnus@hagander.net>
Cc: Bruce Momjian <bruce@momjian.us>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: Rename max_parallel_degree?
Date: Mon, 27 Jun 2016 19:05:11 +0200
Message-ID: <f3421024-c236-a728-4cb4-38404b69bc9f@dalibo.com> (raw)
In-Reply-To: <CAA4eK1+Q_DdJ28qXYSHZiBKNf2MY1QFrv5XAOAw4ZXHw4TPMxA@mail.gmail.com>
References: <20160426152217.GA548975@alvherre.pgsql>
<dabc5b37-b51e-169a-15f1-8441d5bc4c0b@dalibo.com>
<5856af3e-3c20-89d1-3a9c-af3c2cd3a92b@dalibo.com>
<CA+TgmobUZZV6sMj-ZXP71vV5zaHXNtUu5uL+oaeEw54_SySE2Q@mail.gmail.com>
<9b599d4c-cb80-4f1f-48ad-169469579efe@dalibo.com>
<CA+TgmoZWASaz5_wwF8RifWh95AmzzJUcLqgW5aVZJdRMO=m0Rw@mail.gmail.com>
<f5aadd54-6ea0-c8a2-a414-612acdbdab46@dalibo.com>
<CA+TgmobpC00y0tbTZ+nioH3teKBYQUis0DjG9sQaiN7i0g6VWA@mail.gmail.com>
<2b70e5a6-28fe-6601-d294-cd568930265f@dalibo.com>
<79f47dd2-819a-940a-5912-5ff643e87b54@dalibo.com>
<CAA4eK1+MR0+ZkF5NadZdddMs=KqqCLbEDGR=GX7bYcWu9K+s5g@mail.gmail.com>
<98adbca7-3bec-7ee4-92b0-893a0ace5cca@dalibo.com>
<CAA4eK1+SPLt3M0znpNK1qGbKtiC3XmvoOoSP=Bf7bgmQ2P_pkA@mail.gmail.com>
<a1bcad71-db24-2d47-843d-f9fa9677da11@dalibo.com>
<CAA4eK1J3ZnTBSQv7-YxH9g=9h9L0cEiNU3pqeOZC9=pa_9=Rsw@mail.gmail.com>
<CAA4eK1+Q_DdJ28qXYSHZiBKNf2MY1QFrv5XAOAw4ZXHw4TPMxA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>
On 27/06/2016 07:18, Amit Kapila wrote:
> On Mon, Jun 27, 2016 at 10:21 AM, Amit Kapila <amit.kapila16@gmail.com> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: julien.rouhaud@dalibo.com, amit.kapila16@gmail.com, robertmhaas@gmail.com, tgl@sss.pgh.pa.us, josh@agliodbs.com, david.g.johnston@gmail.com, pg@heroku.com, noah@leadboat.com, david.rowley@2ndquadrant.com, alvherre@2ndquadrant.com, magnus@hagander.net, bruce@momjian.us
Subject: Re: Rename max_parallel_degree?
In-Reply-To: <f3421024-c236-a728-4cb4-38404b69bc9f@dalibo.com>
References: <20160426152217.GA548975@alvherre.pgsql> <dabc5b37-b51e-169a-15f1-8441d5bc4c0b@dalibo.com> <5856af3e-3c20-89d1-3a9c-af3c2cd3a92b@dalibo.com> <CA+TgmobUZZV6sMj-ZXP71vV5zaHXNtUu5uL+oaeEw54_SySE2Q@mail.gmail.com> <9b599d4c-cb80-4f1f-48ad-169469579efe@dalibo.com> <CA+TgmoZWASaz5_wwF8RifWh95AmzzJUcLqgW5aVZJdRMO=m0Rw@mail.gmail.com> <f5aadd54-6ea0-c8a2-a414-612acdbdab46@dalibo.com> <CA+TgmobpC00y0tbTZ+nioH3teKBYQUis0DjG9sQaiN7i0g6VWA@mail.gmail.com> <2b70e5a6-28fe-6601-d294-cd568930265f@dalibo.com> <79f47dd2-819a-940a-5912-5ff643e87b54@dalibo.com> <CAA4eK1+MR0+ZkF5NadZdddMs=KqqCLbEDGR=GX7bYcWu9K+s5g@mail.gmail.com> <98adbca7-3bec-7ee4-92b0-893a0ace5cca@dalibo.com> <CAA4eK1+SPLt3M0znpNK1qGbKtiC3XmvoOoSP=Bf7bgmQ2P_pkA@mail.gmail.com> <a1bcad71-db24-2d47-843d-f9fa9677da11@dalibo.com> <CAA4eK1J3ZnTBSQv7-YxH9g=9h9L0cEiNU3pqeOZC9=pa_9=Rsw@mail.gmail.com> <CAA4eK1+Q_DdJ28qXYSHZiBKNf2MY1QFrv5XAOAw4ZXHw4TPMxA@mail.gmail.com> <f3421024-c236-a728-4cb4-38404b69bc9f@dalibo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox