Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XypFd-0003Wm-U9 for pgsql-bugs@arkaria.postgresql.org; Wed, 10 Dec 2014 21:56:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XypFd-00025G-7c for pgsql-bugs@arkaria.postgresql.org; Wed, 10 Dec 2014 21:56:53 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XypFZ-0001yp-Ul for pgsql-bugs@postgresql.org; Wed, 10 Dec 2014 21:56:50 +0000 Received: from mail.catalyst.net.nz ([2404:130:0:10::40:0]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XypFV-0002Dp-Gv for pgsql-bugs@postgresql.org; Wed, 10 Dec 2014 21:56:47 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.catalyst.net.nz (Postfix) with ESMTP id E3767321B1; Thu, 11 Dec 2014 10:56:41 +1300 (NZDT) X-Virus-Scanned: Debian amavisd-new at catalyst.net.nz Received: from mail.catalyst.net.nz ([127.0.0.1]) by localhost (bertrand.catalyst.net.nz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mC3XXslRdDtX; Thu, 11 Dec 2014 10:56:39 +1300 (NZDT) Received: from [IPv6:2404:130:0:1000:d04f:401b:daa9:e9d] (unknown [IPv6:2404:130:0:1000:d04f:401b:daa9:e9d]) (Authenticated sender: mark.kirkwood) by mail.catalyst.net.nz (Postfix) with ESMTPSA id 5048E320D7; Thu, 11 Dec 2014 10:56:39 +1300 (NZDT) Message-ID: <5488C197.3060900@catalyst.net.nz> Date: Thu, 11 Dec 2014 10:56:39 +1300 From: Mark Kirkwood User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Alvaro Herrera CC: Andrew Sackville-West , pgsql-bugs@postgresql.org, Paulo Tanimoto Subject: Re: regression, deadlock in high frequency single-row UPDATE References: <20140731233051.GN17765@andrew-ThinkPad-X230> <20140801005950.GF5475@eldon.alvh.no-ip.org> <20140804161205.GA30380@andrew-ThinkPad-X230> <20140804163027.GI5475@eldon.alvh.no-ip.org> <20141208235552.GZ6930@andrew-ThinkPad-X230> <54868FA2.80003@catalyst.net.nz> <5487B8F2.6050806@catalyst.net.nz> <20141210193314.GZ1768@alvh.no-ip.org> In-Reply-To: <20141210193314.GZ1768@alvh.no-ip.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 11/12/14 08:33, Alvaro Herrera wrote: > Mark Kirkwood wrote: > >> Not so much advantages - just seeing if I could still reproduce the issue >> with a simpler test case i.e ensuring you were not doing anything 'odd' - >> and you are not :-) >> >> The next step would be trying to figure out what commit introduced this >> behaviour - but depesz has already beaten me to that (nice work)! > > So I traced through the problem using the simplified test case I posted. > It goes like this: > > * tuple on the referenced table has been updated a bunch of times > already, and keeps being updated, so there's a large update chain to > follow. > > * two or more transactions (T1 and T2, say) have snapshots in which > some version of the tuple (not the last one) is visible. > > * transaction T3 has a ForKeyShare lock on the latest version of the > tuple, which is grabbed because of the INSERT in the referencing > table. Note that these locks are propagated upwards in an update > chain, so even if T3 locked an old version, all future versions are > also locked by T3. > > * each of T1 and T2 tries to update the version they have visible; this > fails because it's not the last version, so they need to go through > EvalPlanQual, which walks through the update chain. > > At this point, T1 passes through heap_lock_tuple, gets the HW tuple > lock, marks itself as locker in the tuple's Xmax. T1 is scheduled out; > time for T2 to run. T2 goes through heap_lock_tuple, grabs HW lock; > examines Xmax, sees that T1 is the locker, goes to sleep until T1 > awakes. T1 is scheduled to run again, runs heap_update. The first > thing it needs is to do LockTuple ... but T2 is holding that one, so T1 > goes to sleep until T2 awakes -- now they are both sleeping on each > other. > > Kaboom. > > I'm not seeing a solution here. There is something wrong in having to > release the HW lock then grab it again, in that EvalPlanQual dance. It > sounds like we need to hold on to the tuple's HW lock until after > heap_update has run, but it's not at all clear how this would work -- > the interface through EvalPlanQual is messy enough as it is without > entertaining the idea that a "please keep HW lock until later" flag > needs to be passed up and down all over the place. > > One simple idea that occurs to me is have some global state in heapam.c; > when the EPQ code is invoked from ExecUpdate, we tell heapam to keep the > HW lock, and it's only released once heap_update is finished. That > would keep T2 away from the tuple. We'd need a PG_TRY block in > ExecUpdate to reset the global state once the update is done. (It's not > entirely clear that we need all this for deletes too. The main > difference is that after a delete completes the tuple is gone, which is > obviously not the case in an update. Also, deletes don't create update > chains.) > > One thing I haven't thought too much about is why doesn't this happen in > 9.2. > Hmmm - interesting, yeah I think the why-it-doesn't-in-9.2 part is the next thing to understand :-) Cheers Mark -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs