Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vlikh-0007kq-DP for pgsql-bugs@arkaria.postgresql.org; Wed, 27 Nov 2013 17:18:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vlikg-0001pI-S2 for pgsql-bugs@arkaria.postgresql.org; Wed, 27 Nov 2013 17:18:14 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vlikf-0001nX-6w for pgsql-bugs@postgresql.org; Wed, 27 Nov 2013 17:18:13 +0000 Received: from smtprelay0239.b.hostedemail.com ([64.98.42.239] helo=smtprelay.b.hostedemail.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VlikN-0007D2-EB for pgsql-bugs@postgresql.org; Wed, 27 Nov 2013 17:18:12 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay06.b.hostedemail.com (Postfix) with ESMTP id 8C1308D025; Wed, 27 Nov 2013 17:17:54 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 50, 0, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::, RULES_HIT:1:41:355:379:599:800:871:967:973:988:989:1260:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1541:1575:1594:1595:1596:1711:1730:1747:1764:1777:1792:2393:2525:2561:2564:2682:2685:2693:2859:2895:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3657:3865:3866:3867:3868:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:4605:4659:5007:6120:6261:6748:7281:7398:7901:7903:8599:8957:8985:9025:9121:9388:10004:10394:10400:10848:11026:11256:11257:11604:11658:11914:12043:12296:12438:12517:12519:12533:12555:12679:12682:12698:12737:13095:13149:13161:13163:13180:13229:13230:13894:13895, 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:0:0 X-HE-Tag: lunch85_61e43e72ff509 X-Filterd-Recvd-Size: 11156 Received: from eldon.alvh.no-ip.org (pc-223-216-160-190.cm.vtr.net [190.160.216.223]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf11.b.hostedemail.com (Postfix) with ESMTPA; Wed, 27 Nov 2013 17:17:52 +0000 (UTC) Received: by eldon.alvh.no-ip.org (Postfix, from userid 1000) id A8F831FC8; Wed, 27 Nov 2013 13:42:27 -0300 (CLST) Date: Wed, 27 Nov 2013 13:42:27 -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: <20131127164226.GA14522@eldon.alvh.no-ip.org> References: <20131004172217.GW5235@eldon.alvh.no-ip.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="/9DWx/yDrRhgMJTb" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20131004172217.GW5235@eldon.alvh.no-ip.org> 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 --/9DWx/yDrRhgMJTb Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit I think I have figured this out. This code was being too simplistic in that it was only checking whether there was a key-update or not; but that, it seems, is not sufficiently fine-grained. If we instead think in terms of an already-acquired MultiXactStatus, and a new MultiXactStatus corresponding to the lock we're trying to acquire, we can decide granularly whether each future version of the tuple can be locked by us or not. And if not, we can decide whether we need to wait on the transaction holding the lock, or fail if it already committed. (There is a funny trick here which is that we represent the held lock with a MultiXactStatus, even if the lock is only a plain Xid and not a multi. This doesn't seem a problem to me.) So I propose the attached patch. This doesn't change the behavior codified in the existing isolation tests, and it fixes my reduction of your original test case. (I haven't run your original test case yet, but I soon will.) Note: I don't quite like that this patch duplicates some code in compute_new_xmax_infomask() which determines the MultiXactStatus from the infomask bits. Not sure what a good refactoring is, though, because that code just issues a WARNING when the LOCK_ONLY bit is set and no other lockmode is set; whereas the new code issues an ERROR. It seems hard to justify doing otherwise in either place, though; and doing something more complicated doesn't seem warranted for such a corner case. Note: this patch doesn't apply to master in isolation. You will need the patch I mention in http://www.postgresql.org/message-id/20131125201039.GF6597@eldon.alvh.no-ip.org even though I haven't posted it yet. Will do so shortly. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --/9DWx/yDrRhgMJTb Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="granular-chain-following.patch" *** a/src/backend/access/heap/heapam.c --- b/src/backend/access/heap/heapam.c *************** *** 4799,4804 **** l5: --- 4799,4881 ---- *result_xmax = new_xmax; } + /* + * Subroutine for heap_lock_updated_tuple_rec. + * + * Given an hypothetical multixact status held by the transaction identified + * with the given xid, does the current transaction need to wait, fail, or can + * it continue if it wanted to acquire a lock of the given mode? "needwait" + * is set to true if waiting is necessary; if it can continue, then + * HeapTupleMayBeUpdated is returned. In case of a conflict, a different + * HeapTupleSatisfiesUpdate return code is returned. + * + * The held status is said to be hypothetical because it might correspond to a + * lock held by a single Xid, i.e. not a real MultiXactId; we express it this + * way for simplicity of API. + */ + static HTSU_Result + test_lockmode_for_conflict(MultiXactStatus status, TransactionId xid, + LockTupleMode mode, bool *needwait) + { + MultiXactStatus wantedstatus; + + *needwait = false; + wantedstatus = get_mxact_status_for_lock(mode, false); + + /* + * Note: we *must* check TransactionIdIsInProgress before + * TransactionIdDidAbort/Commit; see comment at top of tqual.c for an + * explanation. + */ + if (TransactionIdIsCurrentTransactionId(xid)) + { + /* + * Updated by our own transaction? Just return failure. This shouldn't + * normally happen. + */ + return HeapTupleSelfUpdated; + } + else if (TransactionIdIsInProgress(xid)) + { + /* + * If the locking transaction is running, what we do depends on whether + * the lock modes conflict: if they do, then we must wait for it to + * finish; otherwise we can fall through to lock this tuple version + * without waiting. + */ + if (DoLockModesConflict(LOCKMODE_from_mxstatus(status), + LOCKMODE_from_mxstatus(wantedstatus))) + { + *needwait = true; + } + + /* + * If we set needwait above, then this value doesn't matter; otherwise, + * this value signals to caller that it's okay to proceed. + */ + return HeapTupleMayBeUpdated; + } + else if (TransactionIdDidAbort(xid)) + return HeapTupleMayBeUpdated; + else if (TransactionIdDidCommit(xid)) + { + /* + * If the updating transaction committed, what we do depends on whether + * the lock modes conflict: if they do, then we must report error to + * caller. But if they don't, we can fall through to lock it. + */ + if (DoLockModesConflict(LOCKMODE_from_mxstatus(status), + LOCKMODE_from_mxstatus(wantedstatus))) + /* bummer */ + return HeapTupleUpdated; + + return HeapTupleMayBeUpdated; + } + + /* Not in progress, not aborted, not committed -- must have crashed */ + return HeapTupleMayBeUpdated; + } + /* * Recursive part of heap_lock_updated_tuple *************** *** 4816,4822 **** heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid, Buffer buf; uint16 new_infomask, new_infomask2, ! old_infomask; TransactionId xmax, new_xmax; TransactionId priorXmax = InvalidTransactionId; --- 4893,4900 ---- Buffer buf; uint16 new_infomask, new_infomask2, ! old_infomask, ! old_infomask2; TransactionId xmax, new_xmax; TransactionId priorXmax = InvalidTransactionId; *************** *** 4858,4902 **** l4: } old_infomask = mytup.t_data->t_infomask; 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 (!(old_infomask & HEAP_XMAX_INVALID) && ! (mytup.t_data->t_infomask2 & HEAP_KEYS_UPDATED)) { ! TransactionId update_xid; ! /* ! * Note: we *must* check TransactionIdIsInProgress before ! * TransactionIdDidAbort/Commit; see comment at top of tqual.c for ! * an explanation. ! */ ! update_xid = HeapTupleHeaderGetUpdateXid(mytup.t_data); ! if (TransactionIdIsCurrentTransactionId(update_xid)) { ! UnlockReleaseBuffer(buf); ! return HeapTupleSelfUpdated; ! } ! else if (TransactionIdIsInProgress(update_xid)) ! { ! LockBuffer(buf, BUFFER_LOCK_UNLOCK); ! /* No LockTupleTuplock here -- see heap_lock_updated_tuple */ ! XactLockTableWait(update_xid); ! goto l4; } ! else if (TransactionIdDidAbort(update_xid)) ! ; /* okay to proceed */ ! else if (TransactionIdDidCommit(update_xid)) { ! UnlockReleaseBuffer(buf); ! return HeapTupleUpdated; } } --- 4936,5042 ---- } old_infomask = mytup.t_data->t_infomask; + old_infomask2 = mytup.t_data->t_infomask2; xmax = HeapTupleHeaderGetRawXmax(mytup.t_data); /* ! * If this tuple version has been updated or locked by some concurrent ! * transaction(s), what we do depends on whether our lock mode ! * conflicts with what those other transactions hold, and also on the ! * status of them. */ ! if (!(old_infomask & HEAP_XMAX_INVALID)) { ! TransactionId rawxmax; ! bool needwait; ! rawxmax = HeapTupleHeaderGetRawXmax(mytup.t_data); ! if (old_infomask & HEAP_XMAX_IS_MULTI) { ! int nmembers; ! int i; ! MultiXactMember *members; ! ! nmembers = GetMultiXactIdMembers(rawxmax, &members, false, false); ! for (i = 0; i < nmembers; i++) ! { ! HTSU_Result res; ! ! res = test_lockmode_for_conflict(members[i].status, ! members[i].xid, ! mode, &needwait); ! ! if (needwait) ! { ! LockBuffer(buf, BUFFER_LOCK_UNLOCK); ! XactLockTableWait(members[i].xid); ! pfree(members); ! goto l4; ! } ! if (res != HeapTupleMayBeUpdated) ! { ! UnlockReleaseBuffer(buf); ! pfree(members); ! return res; ! } ! } ! if (members) ! pfree(members); } ! else { ! HTSU_Result res; ! MultiXactStatus status; ! ! /* ! * For a non-multi Xmax, we first need to compute the ! * corresponding MultiXactStatus by using the infomask bits. ! */ ! if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)) ! { ! if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask)) ! status = MultiXactStatusForKeyShare; ! else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask)) ! status = MultiXactStatusForShare; ! else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) ! { ! if (old_infomask2 & HEAP_KEYS_UPDATED) ! status = MultiXactStatusForUpdate; ! else ! status = MultiXactStatusForNoKeyUpdate; ! } ! else ! { ! /* ! * LOCK_ONLY present alone (a pg_upgraded tuple ! * marked as share-locked in the old cluster) shouldn't ! * be seen in the middle of an update chain. ! */ ! elog(ERROR, "invalid lock status in tuple"); ! } ! } ! else ! { ! /* it's an update, but which kind? */ ! if (old_infomask2 & HEAP_KEYS_UPDATED) ! status = MultiXactStatusUpdate; ! else ! status = MultiXactStatusNoKeyUpdate; ! } ! ! res = test_lockmode_for_conflict(status, rawxmax, mode, ! &needwait); ! if (needwait) ! { ! LockBuffer(buf, BUFFER_LOCK_UNLOCK); ! XactLockTableWait(rawxmax); ! goto l4; ! } ! if (res != HeapTupleMayBeUpdated) ! { ! UnlockReleaseBuffer(buf); ! return res; ! } } } --/9DWx/yDrRhgMJTb Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --/9DWx/yDrRhgMJTb--