Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VS958-0000KB-7F for pgsql-bugs@arkaria.postgresql.org; Fri, 04 Oct 2013 17:22:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VS957-0008Sb-FE for pgsql-bugs@arkaria.postgresql.org; Fri, 04 Oct 2013 17:22:25 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VS955-0008Qx-Tc for pgsql-bugs@postgresql.org; Fri, 04 Oct 2013 17:22:24 +0000 Received: from smtprelay0123.b.hostedemail.com ([64.98.42.123] helo=smtprelay.b.hostedemail.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VS952-0006WG-Om for pgsql-bugs@postgresql.org; Fri, 04 Oct 2013 17:22:23 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay02.b.hostedemail.com (Postfix) with ESMTP id EC3E7D39A3; Fri, 4 Oct 2013 17:22:19 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 30, 2, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::, RULES_HIT:41:355:379:599:967:968:973:988:989:1260:1277:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1534:1543:1593:1594:1595:1596:1605:1711:1730:1747:1777:1792:2198:2199:2393:2525:2540:2560:2563:2682:2685:2693:2828:2859:2895:2898:2901:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4006:4011:4250:4362:4605:4659:5007:6120:6261:7809:7875:7901:7903:7904:8957:9025:9121:10010:10400:10450:10455:10848:11026:11232:11256:11257:11473:11657:11658:11914:12043:12050:12296:12438:12517:12519:12533:13149:13161:13229:13230:13894:13895:19904:19999, 0, RBL:none, CacheIP:none, Bayesian:0.5, 0.5, 0.5, Netcheck:none, DomainCache:0, MSF:not bulk, SPF:fn, MSBL:0, DNSBL:none, Custom_rules:0:1:0 X-HE-Tag: tent06_316217180493a X-Filterd-Recvd-Size: 4225 Received: from eldon.alvh.no-ip.org (unknown [201.186.21.5]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf05.b.hostedemail.com (Postfix) with ESMTPA; Fri, 4 Oct 2013 17:22:19 +0000 (UTC) Received: by eldon.alvh.no-ip.org (Postfix, from userid 1000) id A01791FC8; Fri, 4 Oct 2013 14:22:17 -0300 (CLST) Date: Fri, 4 Oct 2013 14:22:17 -0300 From: Alvaro Herrera To: katsumata.tomonari@po.ntts.co.jp Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8434: Why does dead lock occur many times ? Message-ID: <20131004172217.GW5235@eldon.alvh.no-ip.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) 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 I spent a lot of time looking at this issue. Your problem scenario can be reduced to the following test case: CREATE TABLE t (a INT); INSERT INTO t VALUES (1); The three sessions need to execute the following commands, in this sequence: -- Session 1 /* 1 */ BEGIN; SELECT * FROM t FOR UPDATE; -- Session 2 /* 2 */ BEGIN; UPDATE t SET a = 2; -- blocks waiting on S1 -- Session 3 /* 3 */ BEGIN; UPDATE t SET a = 3; -- blocks -- this is waiting on S1, but since S2 is also waiting on S1 and will acquire a -- conflicting lock first, this session will only be released after S2 releases -- its own lock. Session 1 /* 4 */ UPDATE t SET a = 4; COMMIT; -- releases session 2 BEGIN; SELECT * FROM t FOR UPDATE; -- blocks waiting on S2 (*) Session 2: /* 5 */ COMMIT; -- releases session 1 BEGIN; UPDATE t SET a = 5; -- blocks waiting on S3 Session 1: /* 6 */ UPDATE t SET a = 6; -- blocks waiting on S3 At this point, all sessions are blocked on their UPDATE commands, and deadlock is reported. (For debugging, it's useful to set a very large deadlock_timeout and examine pg_locks, etc.) The point at which things started to go wrong was where the session 1's SELECT FOR UPDATE with the (*) was allowed to continue after session 2 commits. In 9.2 that session remains blocked, and instead we release session 3. The problem is the handling of following of the update chain during a lock command; the question is should sessions be blocked waiting for locks on future versions of the row? They currently don't block, which is what leads the SELECT FOR UPDATE to continue; but if we make them block, other things break. I initially toyed with the following patch, which removes the deadlock the above report and also your original test case: --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4832,15 +4832,12 @@ l4: xmax = HeapTupleHeaderGetRawXmax(mytup.t_data); /* - * If this tuple is updated and the key has been modified (or - * deleted), what we do depends on the status of the updating - * transaction: if it's live, we sleep until it finishes; if it has - * committed, we have to fail (i.e. return HeapTupleUpdated); if it - * aborted, we ignore it. For updates that didn't touch the key, we - * can just plough ahead. + * If this tuple is updated, what we do depends on the status of the + * updating transaction: if it's live, we sleep until it finishes; if + * it has committed, we have to fail (i.e. return HeapTupleUpdated); if + * it aborted, we ignore it. */ - if (!(old_infomask & HEAP_XMAX_INVALID) && - (mytup.t_data->t_infomask2 & HEAP_KEYS_UPDATED)) + if (!(old_infomask & HEAP_XMAX_INVALID)) { TransactionId update_xid; However, the differences in behavior this causes (visible by running the isolation tests) don't look good to me. It's quite possible that there are other bugs elsewhere. Sadly, I have to look at other things now and I won't have time to research this further until after next week's minor releases; so whatever we decide to do, if anything, will not be in 9.3.1. Thanks for the report. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs