Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vlzfv-0002Wt-Vh for pgsql-bugs@arkaria.postgresql.org; Thu, 28 Nov 2013 11:22:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vlzfv-0006XO-FX for pgsql-bugs@arkaria.postgresql.org; Thu, 28 Nov 2013 11:22:27 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vlzft-0006XI-W8 for pgsql-bugs@postgresql.org; Thu, 28 Nov 2013 11:22:26 +0000 Received: from smtprelay0024.b.hostedemail.com ([64.98.42.24] helo=smtprelay.b.hostedemail.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vlzfo-0000hc-IW for pgsql-bugs@postgresql.org; Thu, 28 Nov 2013 11:22:25 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay03.b.hostedemail.com (Postfix) with ESMTP id CE3A11A732A; Thu, 28 Nov 2013 11:22:19 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 40, 2.5, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::, RULES_HIT:41:327:355:379:599:871:967:973:988:989:1260:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1539:1575:1594:1595:1596:1711:1714:1730:1747:1764:1777:1792:2393:2525:2560:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3351:3865:3866:3871:3872:3873:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4605:4659:5007:6120:6261:6748:7281:7398:7901:8957:9025:9121:10011:10400:10848:11232:11256:11257:11604:11658:11914:12043:12438:12517:12519:12533: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:2:0 X-HE-Tag: fang37_6b8888fb76a2b X-Filterd-Recvd-Size: 27001 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 omf08.b.hostedemail.com (Postfix) with ESMTPA; Thu, 28 Nov 2013 11:22:18 +0000 (UTC) Received: by eldon.alvh.no-ip.org (Postfix, from userid 1000) id 897381FC8; Thu, 28 Nov 2013 08:22:17 -0300 (CLST) Date: Thu, 28 Nov 2013 08:22:17 -0300 From: Alvaro Herrera To: Tomonari Katsumata Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8434: Why does dead lock occur many times ? Message-ID: <20131128112217.GE5513@eldon.alvh.no-ip.org> References: <20131004172217.GW5235@eldon.alvh.no-ip.org> <20131127164226.GA14522@eldon.alvh.no-ip.org> <5296BFE1.2090906@po.ntts.co.jp> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="3siQDZowHQqNOShm" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <5296BFE1.2090906@po.ntts.co.jp> 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 --3siQDZowHQqNOShm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Tomonari Katsumata wrote: > Hi Alvaro > > I applied all patches you told me against 93_STABLE. > But I could not build the source with the patches. > > It seems your patch use GetMultiXactIdMembers with four arguments, > but the function requires three arguments. > ---- > nmembers = GetMultiXactIdMembers(rawxmax, &members, false, false); > ---- > > do you have another patch? Sorry, yes. This is the complete series of patches, although I have not tried them against 9.3 yet. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --3siQDZowHQqNOShm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Skip-looking-up-very-old-LOCK_ONLY-MultiXactIds.patch" From ef026a8110f40941741585e07671398beea28f11 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 27 Nov 2013 17:19:41 -0300 Subject: [PATCH 1/5] Skip looking up very old LOCK_ONLY MultiXactIds Commit 0ac5ad5134f2 removed an optimization in multixact.c that skipped fetching members of MultiXactId that were older than our OldestVisibleMXactId value. The reason this was removed is that it is possible for multis that contain updates to be older than that value. However, if the caller is certain that the multi does not contain an update (because the infomask bits say so), it can pass this info down to GetMultiXactIdMembers, enabling it to use the old optimization. Pointed out by Andres Freund in 20131121200517.GM7240@alap2.anarazel.de --- contrib/pgrowlocks/pgrowlocks.c | 3 ++- src/backend/access/heap/heapam.c | 20 +++++++++++++------- src/backend/access/transam/multixact.c | 29 ++++++++++++++++++++++++----- src/backend/utils/time/tqual.c | 11 ++++++----- src/include/access/multixact.h | 4 ++-- 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c index 636ff05..8d4961c 100644 --- a/contrib/pgrowlocks/pgrowlocks.c +++ b/contrib/pgrowlocks/pgrowlocks.c @@ -168,7 +168,8 @@ pgrowlocks(PG_FUNCTION_ARGS) allow_old = !(infomask & HEAP_LOCK_MASK) && (infomask & HEAP_XMAX_LOCK_ONLY); - nmembers = GetMultiXactIdMembers(xmax, &members, allow_old); + nmembers = GetMultiXactIdMembers(xmax, &members, allow_old, + false); if (nmembers == -1) { values[Atnum_xids] = "{0}"; diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index a21f31b..32ef453 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -3987,7 +3987,9 @@ l3: * the case, HeapTupleSatisfiesUpdate would have returned * MayBeUpdated and we wouldn't be here. */ - nmembers = GetMultiXactIdMembers(xwait, &members, false); + nmembers = + GetMultiXactIdMembers(xwait, &members, false, + HEAP_XMAX_IS_LOCKED_ONLY(infomask)); for (i = 0; i < nmembers; i++) { @@ -4008,7 +4010,8 @@ l3: } } - pfree(members); + if (members) + pfree(members); } /* @@ -4157,7 +4160,9 @@ l3: * been the case, HeapTupleSatisfiesUpdate would have returned * MayBeUpdated and we wouldn't be here. */ - nmembers = GetMultiXactIdMembers(xwait, &members, false); + nmembers = + GetMultiXactIdMembers(xwait, &members, false, + HEAP_XMAX_IS_LOCKED_ONLY(infomask)); if (nmembers <= 0) { @@ -4626,7 +4631,7 @@ l5: * MultiXactIdExpand if we weren't to do this, so this check is not * incurring extra work anyhow. */ - if (!MultiXactIdIsRunning(xmax)) + if (!MultiXactIdIsRunning(xmax, HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))) { if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) || TransactionIdDidAbort(MultiXactIdGetUpdateXid(xmax, @@ -5217,7 +5222,7 @@ GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask, * We only use this in multis we just created, so they cannot be values * pre-pg_upgrade. */ - nmembers = GetMultiXactIdMembers(multi, &members, false); + nmembers = GetMultiXactIdMembers(multi, &members, false, false); for (i = 0; i < nmembers; i++) { @@ -5293,7 +5298,7 @@ MultiXactIdGetUpdateXid(TransactionId xmax, uint16 t_infomask) * Since we know the LOCK_ONLY bit is not set, this cannot be a multi from * pre-pg_upgrade. */ - nmembers = GetMultiXactIdMembers(xmax, &members, false); + nmembers = GetMultiXactIdMembers(xmax, &members, false, false); if (nmembers > 0) { @@ -5376,7 +5381,8 @@ Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status, int remain = 0; allow_old = !(infomask & HEAP_LOCK_MASK) && HEAP_XMAX_IS_LOCKED_ONLY(infomask); - nmembers = GetMultiXactIdMembers(multi, &members, allow_old); + nmembers = GetMultiXactIdMembers(multi, &members, allow_old, + HEAP_XMAX_IS_LOCKED_ONLY(infomask)); if (nmembers >= 0) { diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c index e3f5cbc..20b0ea2 100644 --- a/src/backend/access/transam/multixact.c +++ b/src/backend/access/transam/multixact.c @@ -391,7 +391,7 @@ MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status) * caller of this function does a check that the multixact is no longer * running. */ - nmembers = GetMultiXactIdMembers(multi, &members, false); + nmembers = GetMultiXactIdMembers(multi, &members, false, false); if (nmembers < 0) { @@ -477,7 +477,7 @@ MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status) * a pg_upgraded share-locked tuple. */ bool -MultiXactIdIsRunning(MultiXactId multi) +MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly) { MultiXactMember *members; int nmembers; @@ -489,7 +489,7 @@ MultiXactIdIsRunning(MultiXactId multi) * "false" here means we assume our callers have checked that the given * multi cannot possibly come from a pg_upgraded database. */ - nmembers = GetMultiXactIdMembers(multi, &members, false); + nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly); if (nmembers < 0) { @@ -1029,10 +1029,15 @@ GetNewMultiXactId(int nmembers, MultiXactOffset *offset) * the value currently known as the next to assign, raise an error. Previously * these also returned -1, but since this can lead to the wrong visibility * results, it is dangerous to do that. + * + * onlyLock must be set to true if caller is certain that the given multi + * is used only to lock tuples; can be false without loss of correctness, + * but passing a true means we can return quickly without checking for + * old updates. */ int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, - bool allow_old) + bool allow_old, bool onlyLock) { int pageno; int prev_pageno; @@ -1066,6 +1071,19 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, MultiXactIdSetOldestVisible(); /* + * If we know the multi is used only for locking and not for updates, + * then we can skip checking if the value is older than our oldest + * visible multi. It cannot possibly still be running. + */ + if (onlyLock && + MultiXactIdPrecedes(multi, OldestVisibleMXactId[MyBackendId])) + { + debug_elog2(DEBUG2, "GetMembers: a locker-only multi is too old"); + *members = NULL; + return -1; + } + + /* * We check known limits on MultiXact before resorting to the SLRU area. * * An ID older than MultiXactState->oldestMultiXactId cannot possibly be @@ -2507,7 +2525,8 @@ pg_get_multixact_members(PG_FUNCTION_ARGS) multi = palloc(sizeof(mxact)); /* no need to allow for old values here */ - multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false); + multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false, + false); multi->iter = 0; tupdesc = CreateTemplateTupleDesc(2, false); diff --git a/src/backend/utils/time/tqual.c b/src/backend/utils/time/tqual.c index ed66c49..e0d627b 100644 --- a/src/backend/utils/time/tqual.c +++ b/src/backend/utils/time/tqual.c @@ -565,7 +565,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, */ if ((tuple->t_infomask & (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)) && - MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple))) + MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), true)) return HeapTupleBeingUpdated; SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId); @@ -575,7 +575,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, xmax = HeapTupleGetUpdateXid(tuple); if (!TransactionIdIsValid(xmax)) { - if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple))) + if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), false)) return HeapTupleBeingUpdated; SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId); @@ -590,7 +590,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, return HeapTupleInvisible; /* updated before scan started */ } - if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple))) + if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), false)) return HeapTupleBeingUpdated; if (TransactionIdDidCommit(xmax)) @@ -1158,7 +1158,8 @@ HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, */ if ((tuple->t_infomask & (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)) && - MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple))) + MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), + true)) return HEAPTUPLE_LIVE; SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId); @@ -1185,7 +1186,7 @@ HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, { TransactionId xmax; - if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple))) + if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), false)) { /* already checked above */ Assert(!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask)); diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h index 8a9edde..3572992 100644 --- a/src/include/access/multixact.h +++ b/src/include/access/multixact.h @@ -82,10 +82,10 @@ extern MultiXactId MultiXactIdCreate(TransactionId xid1, extern MultiXactId MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status); extern MultiXactId ReadNextMultiXactId(void); -extern bool MultiXactIdIsRunning(MultiXactId multi); +extern bool MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly); extern void MultiXactIdSetOldestMember(void); extern int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **xids, - bool allow_old); + bool allow_old, bool isLockOnly); extern bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2); extern void AtEOXact_MultiXact(void); -- 1.7.10.4 --3siQDZowHQqNOShm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-Cope-with-heap_fetch-failure-while-locking-an-update.patch" From 2d227750252da8e66849525f7ca5879cd2e0cb7d Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 27 Nov 2013 17:45:25 -0300 Subject: [PATCH 2/5] Cope with heap_fetch failure while locking an update chain The reason for the fetch failure is that the tuple was removed because it was dead; so the failure is innocuous and can be ignored. Moreover, there's no need for further work and we can return success to the caller immediately. EvalPlanQualFetch is doing something very similar to this already. Report and test case from Andres Freund in 20131124000203.GA4403@alap2.anarazel.de --- src/backend/access/heap/heapam.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 32ef453..6d7cd0c 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4829,7 +4829,16 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid, ItemPointerCopy(&tupid, &(mytup.t_self)); if (!heap_fetch(rel, SnapshotAny, &mytup, &buf, false, NULL)) - elog(ERROR, "unable to fetch updated version of tuple"); + { + /* + * if we fail to find the updated version of the tuple, it's + * because it was vacuumed/pruned away after its creator + * transaction aborted. So behave as if we got to the end of the + * chain, and there's no further tuple to lock: return success to + * caller. + */ + return HeapTupleMayBeUpdated; + } l4: CHECK_FOR_INTERRUPTS(); -- 1.7.10.4 --3siQDZowHQqNOShm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0003-Don-t-try-to-set-InvalidXid-as-page-pruning-hint.patch" From 8ce864afd98bc669db856d7c75f0177981a46fd6 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 27 Nov 2013 17:47:16 -0300 Subject: [PATCH 3/5] Don't try to set InvalidXid as page pruning hint If a transaction updates/deletes a tuple just before aborting, and a concurrent transaction tries to prune the page concurrently, the pruner may see HeapTupleSatisfiesVacuum return HEAPTUPLE_DELETE_IN_PROGRESS, but a later call to HeapTupleGetUpdateXid() return InvalidXid. This would cause an assertion failure in development builds, but would be otherwise Mostly Harmless. Fix by checking whether the updater Xid is valid before trying to apply it as page prune point. Reported by Andres in 20131124000203.GA4403@alap2.anarazel.de --- src/backend/access/heap/pruneheap.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c index 3ec10a0..fdfa37c 100644 --- a/src/backend/access/heap/pruneheap.c +++ b/src/backend/access/heap/pruneheap.c @@ -479,13 +479,22 @@ heap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum, break; case HEAPTUPLE_DELETE_IN_PROGRESS: + { + TransactionId xmax; + + /* + * This tuple may soon become DEAD. Update the hint field + * so that the page is reconsidered for pruning in future. + * If there was a MultiXactId updater, and it aborted after + * HTSV checked, then we will get an invalid Xid here. + * There is no need for future pruning of the page in that + * case, so skip it. + */ + xmax = HeapTupleHeaderGetUpdateXid(htup); + if (TransactionIdIsValid(xmax)) + heap_prune_record_prunable(prstate, xmax); + } - /* - * This tuple may soon become DEAD. Update the hint field so - * that the page is reconsidered for pruning in future. - */ - heap_prune_record_prunable(prstate, - HeapTupleHeaderGetUpdateXid(htup)); break; case HEAPTUPLE_LIVE: -- 1.7.10.4 --3siQDZowHQqNOShm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0004-Compare-Xmin-to-previous-Xmax-when-locking-an-update.patch" From 793296739bd38e6166510311b9134e0da7db6e74 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 27 Nov 2013 17:49:12 -0300 Subject: [PATCH 4/5] Compare Xmin to previous Xmax when locking an update chain Not doing so causes us to traverse an update chain that has been broken by concurrent page pruning. All other code that traverses update chains uses this check as one of the cases in which to stop iterating, so replicate it here too. Failure to do so leads to erroneous CLOG, subtrans or multixact lookups. Per bug report by J Smith in CADFUPgc5bmtv-yg9znxV-vcfkb+JPRqs7m2OesQXaM_4Z1JpdQ@mail.gmail.com diagnosed by Andres Freund. --- src/backend/access/heap/heapam.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 6d7cd0c..f70e1f7 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4819,6 +4819,7 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid, old_infomask; TransactionId xmax, new_xmax; + TransactionId priorXmax = InvalidTransactionId; ItemPointerCopy(tid, &tupid); @@ -4844,6 +4845,18 @@ l4: CHECK_FOR_INTERRUPTS(); LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); + /* + * Check the tuple XMIN against prior XMAX, if any. If we reached + * the end of the chain, we're done, so return success. + */ + if (TransactionIdIsValid(priorXmax) && + !TransactionIdEquals(HeapTupleHeaderGetXmin(mytup.t_data), + priorXmax)) + { + UnlockReleaseBuffer(buf); + return HeapTupleMayBeUpdated; + } + old_infomask = mytup.t_data->t_infomask; xmax = HeapTupleHeaderGetRawXmax(mytup.t_data); @@ -4944,6 +4957,7 @@ l4: } /* tail recursion */ + priorXmax = HeapTupleHeaderGetUpdateXid(mytup.t_data); ItemPointerCopy(&(mytup.t_data->t_ctid), &tupid); UnlockReleaseBuffer(buf); } -- 1.7.10.4 --3siQDZowHQqNOShm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0005-Use-a-more-granular-approach-to-follow-update-chains.patch" From 447b5325503fca11a413240c15dc1d837d9d8151 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 27 Nov 2013 17:50:33 -0300 Subject: [PATCH 5/5] Use a more granular approach to follow update chains Instead of simply checking the KEYS_UPDATED bit, we need to check whether each lock held on the future version of the tuple conflicts with the lock we're trying to acquire. Per bug report #8434 by Tomonari Katsumata --- src/backend/access/heap/heapam.c | 202 ++++++++++++++++++++++++++++++++------ 1 file changed, 171 insertions(+), 31 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index f70e1f7..6802cbb 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4799,6 +4799,83 @@ l5: *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,7 +4893,8 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid, Buffer buf; uint16 new_infomask, new_infomask2, - old_infomask; + old_infomask, + old_infomask2; TransactionId xmax, new_xmax; TransactionId priorXmax = InvalidTransactionId; @@ -4858,45 +4936,107 @@ l4: } old_infomask = mytup.t_data->t_infomask; + old_infomask2 = mytup.t_data->t_infomask2; 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 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) && - (mytup.t_data->t_infomask2 & HEAP_KEYS_UPDATED)) + if (!(old_infomask & HEAP_XMAX_INVALID)) { - TransactionId update_xid; + TransactionId rawxmax; + bool needwait; - /* - * 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)) + rawxmax = HeapTupleHeaderGetRawXmax(mytup.t_data); + if (old_infomask & HEAP_XMAX_IS_MULTI) { - 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; + 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 if (TransactionIdDidAbort(update_xid)) - ; /* okay to proceed */ - else if (TransactionIdDidCommit(update_xid)) + else { - UnlockReleaseBuffer(buf); - return HeapTupleUpdated; + 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; + } } } -- 1.7.10.4 --3siQDZowHQqNOShm 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 --3siQDZowHQqNOShm--