Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y8BNW-0000aQ-Ee for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Jan 2015 17:23:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y8BNV-0002FF-Pe for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Jan 2015 17:23:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y8BNU-0002F8-EQ for pgsql-bugs@postgresql.org; Mon, 05 Jan 2015 17:23:40 +0000 Received: from outmail148108.authsmtp.net ([62.13.148.108]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y8BNJ-0004FR-Oh for pgsql-bugs@postgresql.org; Mon, 05 Jan 2015 17:23:39 +0000 Received: from mail-c235.authsmtp.com (mail-c235.authsmtp.com [62.13.128.235]) by punt15.authsmtp.com (8.14.2/8.14.2/) with ESMTP id t05HNJ0J029442; Mon, 5 Jan 2015 17:23:19 GMT Received: from alvin.alvh.no-ip.org (client-186-37-113-166.imovil.entelpcs.cl [186.37.113.166] (may be forged)) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id t05HN5gZ053830; Mon, 5 Jan 2015 17:23:12 GMT Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id B9BF412F4; Mon, 5 Jan 2015 14:23:18 -0300 (CLST) Date: Mon, 5 Jan 2015 14:23:18 -0300 From: Alvaro Herrera To: Oskari Saarenmaa Cc: Andres Freund , pgsql-bugs@postgresql.org Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression Message-ID: <20150105172318.GM1457@alvh.no-ip.org> References: <20131220204753.GG22570@eldon.alvh.no-ip.org> <20131223155603.GI22570@eldon.alvh.no-ip.org> <20131223162903.GJ22570@eldon.alvh.no-ip.org> <20131223195300.GK22570@eldon.alvh.no-ip.org> <20131230181151.GG12910@alap2.anarazel.de> <20131231163418.GW22570@eldon.alvh.no-ip.org> <20131231202753.GY22570@eldon.alvh.no-ip.org> <20140114124305.GA28072@saarenmaa.fi> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="5mCyUwZo2JvN/JJP" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20140114124305.GA28072@saarenmaa.fi> User-Agent: Mutt/1.5.23 (2014-03-12) X-Server-Quench: 896b5399-94ff-11e4-b396-002590a15da7 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZcSBoa BzkeAQdJTRslYBtL RB4bawxAL0wMSABN M15eIxoIcUtEGBZ7 UjgUWVRVVU1wXml3 aABQaktcakxQXAJ0 UUlKXFNUEhptCRse BlBaUhwKCQAREn51 Zk5hXj5TXER9cwh1 S0gGEG4PbHppbDZJ TUFZIgdJIgBDeAJF alJ6XXNcYGBTN30u JQJhdwMbABpkHWx/ fy5FZgJOHh5ZVm96 HFgGGj4sHEsKAD4u JQA8I1gHFU0NM0I0 eUMsQ1wfKRobDAZT fQlXDSBDKkIaRi0t ZQAA X-Authentic-SMTP: 61633235383639.1023:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 186.37.113.166/587 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Pg-Spam-Score: -2.6 (--) 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 --5mCyUwZo2JvN/JJP Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit I just got around to merging this patch to 9.5 sources. I'm glad I did it because in the course of doing so I noticed a bug in a recent patch, which led to commit d5e3d1e969d2f65009f718d3100d6565f47f9112 (back-patched to 9.3). I'm now more confident in this code and will probably push this a few days, but only to 9.5 at least for now. It probably won't apply cleanly to 9.3 due to other changes in the area, such as 05315498012530d44cd89a2 and df630b0dd5ea2de52972d456f5978a012436115e and others. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --5mCyUwZo2JvN/JJP Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="optimize-locking-locked-multi-5.patch" diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c index 36974f7..88f7137 100644 --- a/contrib/pgrowlocks/pgrowlocks.c +++ b/contrib/pgrowlocks/pgrowlocks.c @@ -136,14 +136,9 @@ pgrowlocks(PG_FUNCTION_ARGS) infomask = tuple->t_data->t_infomask; /* - * a tuple is locked if HTSU returns BeingUpdated, and if it returns - * MayBeUpdated but the Xmax is valid and pointing at us. + * A tuple is locked if HTSU returns BeingUpdated. */ - if (htsu == HeapTupleBeingUpdated || - (htsu == HeapTupleMayBeUpdated && - !(infomask & HEAP_XMAX_INVALID) && - !(infomask & HEAP_XMAX_IS_MULTI) && - (xmax == GetCurrentTransactionIdIfAny()))) + if (htsu == HeapTupleBeingUpdated) { char **values; diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 24e300c..e351a96 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -2694,42 +2694,47 @@ l1: xwait = HeapTupleHeaderGetRawXmax(tp.t_data); infomask = tp.t_data->t_infomask; - LockBuffer(buffer, BUFFER_LOCK_UNLOCK); - /* - * Acquire tuple lock to establish our priority for the tuple (see - * heap_lock_tuple). LockTuple will release us when we are - * next-in-line for the tuple. + * Sleep until concurrent transaction ends -- except when there's a single + * locker and it's our own transaction. Note we don't care + * which lock mode the locker has, because we need the strongest one. + * + * Before sleeping, we need to acquire tuple lock to establish our + * priority for the tuple (see heap_lock_tuple). LockTuple will + * release us when we are next-in-line for the tuple. * * If we are forced to "start over" below, we keep the tuple lock; * this arranges that we stay at the head of the line while rechecking * tuple state. */ - heap_acquire_tuplock(relation, &(tp.t_self), LockTupleExclusive, - LockWaitBlock, &have_tuple_lock); - - /* - * Sleep until concurrent transaction ends. Note that we don't care - * which lock mode the locker has, because we need the strongest one. - */ - if (infomask & HEAP_XMAX_IS_MULTI) { /* wait for multixact */ - MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate, infomask, - relation, &tp.t_data->t_ctid, XLTW_Delete, - NULL); - LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); + if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, + LockTupleExclusive)) + { + LockBuffer(buffer, BUFFER_LOCK_UNLOCK); - /* - * If xwait had just locked the tuple then some other xact could - * update this tuple before we get to this point. Check for xmax - * change, and start over if so. - */ - if (xmax_infomask_changed(tp.t_data->t_infomask, infomask) || - !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data), - xwait)) - goto l1; + /* acquire tuple lock, if necessary */ + heap_acquire_tuplock(relation, &(tp.t_self), LockTupleExclusive, + LockWaitBlock, &have_tuple_lock); + + /* wait for multixact */ + MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate, infomask, + relation, &tp.t_data->t_ctid, XLTW_Delete, + NULL); + LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); + + /* + * If xwait had just locked the tuple then some other xact + * could update this tuple before we get to this point. Check + * for xmax change, and start over if so. + */ + if (xmax_infomask_changed(tp.t_data->t_infomask, infomask) || + !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data), + xwait)) + goto l1; + } /* * You might think the multixact is necessarily done here, but not @@ -2741,9 +2746,15 @@ l1: * since we are about to overwrite the xmax altogether. */ } - else + else if (!TransactionIdIsCurrentTransactionId(xwait)) { - /* wait for regular transaction to end */ + /* + * Wait for regular transaction to end; but first, acquire + * tuple lock. + */ + LockBuffer(buffer, BUFFER_LOCK_UNLOCK); + heap_acquire_tuplock(relation, &(tp.t_self), LockTupleExclusive, + LockWaitBlock, &have_tuple_lock); XactLockTableWait(xwait, relation, &tp.t_data->t_ctid, XLTW_Delete); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -3223,8 +3234,6 @@ l2: xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data); infomask = oldtup.t_data->t_infomask; - LockBuffer(buffer, BUFFER_LOCK_UNLOCK); - /* * Now we have to do something about the existing locker. If it's a * multi, sleep on it; we might be awakened before it is completely @@ -3252,28 +3261,32 @@ l2: TransactionId update_xact; int remain; - /* acquire tuple lock, if necessary */ - if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, *lockmode)) + if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, + *lockmode)) { + LockBuffer(buffer, BUFFER_LOCK_UNLOCK); + + /* acquire tuple lock, if necessary */ heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, LockWaitBlock, &have_tuple_lock); - } - /* wait for multixact */ - MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, - relation, &oldtup.t_data->t_ctid, XLTW_Update, - &remain); - LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); + /* wait for multixact */ + MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, + relation, &oldtup.t_data->t_ctid, XLTW_Update, + &remain); + LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); - /* - * If xwait had just locked the tuple then some other xact could - * update this tuple before we get to this point. Check for xmax - * change, and start over if so. - */ - if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) || - !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data), - xwait)) - goto l2; + /* + * If xwait had just locked the tuple then some other xact + * could update this tuple before we get to this point. Check + * for xmax change, and start over if so. + */ + if (xmax_infomask_changed(oldtup.t_data->t_infomask, + infomask) || + !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data), + xwait)) + goto l2; + } /* * Note that the multixact may not be done by now. It could have @@ -3309,58 +3322,52 @@ l2: locker_remains = remain != 0; } + else if (TransactionIdIsCurrentTransactionId(xwait)) + { + /* + * The only locker is ourselves; we can avoid grabbing the tuple + * lock here, but must preserve our locking information. + */ + can_continue = true; + locker_remains = true; + } + else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) && key_intact) + { + /* + * If it's just a key-share locker, and we're not changing the + * key columns, we don't need to wait for it to end; but we + * need to preserve it as locker. + */ + can_continue = true; + locker_remains = true; + } else { /* - * If it's just a key-share locker, and we're not changing the key - * columns, we don't need to wait for it to end; but we need to - * preserve it as locker. + * Wait for regular transaction to end; but first, acquire + * tuple lock. */ - if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) && key_intact) - { - LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); + LockBuffer(buffer, BUFFER_LOCK_UNLOCK); + heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, + LockWaitBlock, &have_tuple_lock); + XactLockTableWait(xwait, relation, &oldtup.t_data->t_ctid, + XLTW_Update); + LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); - /* - * recheck the locker; if someone else changed the tuple while - * we weren't looking, start over. - */ - if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) || - !TransactionIdEquals( - HeapTupleHeaderGetRawXmax(oldtup.t_data), - xwait)) - goto l2; + /* + * xwait is done, but if xwait had just locked the tuple then some + * other xact could update this tuple before we get to this point. + * Check for xmax change, and start over if so. + */ + if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) || + !TransactionIdEquals(xwait, + HeapTupleHeaderGetRawXmax(oldtup.t_data))) + goto l2; + /* Otherwise check if it committed or aborted */ + UpdateXmaxHintBits(oldtup.t_data, buffer, xwait); + if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) can_continue = true; - locker_remains = true; - } - else - { - /* - * Wait for regular transaction to end; but first, acquire - * tuple lock. - */ - heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, - LockWaitBlock, &have_tuple_lock); - XactLockTableWait(xwait, relation, &oldtup.t_data->t_ctid, - XLTW_Update); - LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); - - /* - * xwait is done, but if xwait had just locked the tuple then - * some other xact could update this tuple before we get to - * this point. Check for xmax change, and start over if so. - */ - if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) || - !TransactionIdEquals( - HeapTupleHeaderGetRawXmax(oldtup.t_data), - xwait)) - goto l2; - - /* Otherwise check if it committed or aborted */ - UpdateXmaxHintBits(oldtup.t_data, buffer, xwait); - if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) - can_continue = true; - } } result = can_continue ? HeapTupleMayBeUpdated : HeapTupleUpdated; @@ -4097,6 +4104,7 @@ heap_lock_tuple(Relation relation, HeapTuple tuple, uint16 old_infomask, new_infomask, new_infomask2; + bool first_time = true; bool have_tuple_lock = false; *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid)); @@ -4136,47 +4144,77 @@ l3: /* * If any subtransaction of the current top transaction already holds - * a lock as strong or stronger than what we're requesting, we + * a lock as strong as or stronger than what we're requesting, we * effectively hold the desired lock already. We *must* succeed * without trying to take the tuple lock, else we will deadlock * against anyone wanting to acquire a stronger lock. + * + * Note we only do this the first time we loop on the HTSU result; + * there is no point in testing in subsequent passes, because + * evidently our own transaction cannot have acquired a new lock after + * the first time we checked. */ - if (infomask & HEAP_XMAX_IS_MULTI) + if (first_time) { - int i; - int nmembers; - MultiXactMember *members; - - /* - * We don't need to allow old multixacts here; if that had been - * the case, HeapTupleSatisfiesUpdate would have returned - * MayBeUpdated and we wouldn't be here. - */ - nmembers = - GetMultiXactIdMembers(xwait, &members, false, - HEAP_XMAX_IS_LOCKED_ONLY(infomask)); + first_time = false; - for (i = 0; i < nmembers; i++) + if (infomask & HEAP_XMAX_IS_MULTI) { - if (TransactionIdIsCurrentTransactionId(members[i].xid)) - { - LockTupleMode membermode; + int i; + int nmembers; + MultiXactMember *members; - membermode = TUPLOCK_from_mxstatus(members[i].status); + /* + * We don't need to allow old multixacts here; if that had + * been the case, HeapTupleSatisfiesUpdate would have returned + * MayBeUpdated and we wouldn't be here. + */ + nmembers = + GetMultiXactIdMembers(xwait, &members, false, + HEAP_XMAX_IS_LOCKED_ONLY(infomask)); - if (membermode >= mode) - { - if (have_tuple_lock) - UnlockTupleTuplock(relation, tid, mode); + for (i = 0; i < nmembers; i++) + { + /* only consider members of our own transaction */ + if (!TransactionIdIsCurrentTransactionId(members[i].xid)) + continue; + if (TUPLOCK_from_mxstatus(members[i].status) >= mode) + { pfree(members); return HeapTupleMayBeUpdated; } } - } - if (members) - pfree(members); + if (members) + pfree(members); + } + else if (TransactionIdIsCurrentTransactionId(xwait)) + { + switch (mode) + { + case LockTupleKeyShare: + Assert(HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) || + HEAP_XMAX_IS_SHR_LOCKED(infomask) || + HEAP_XMAX_IS_EXCL_LOCKED(infomask)); + return HeapTupleMayBeUpdated; + break; + case LockTupleShare: + if (HEAP_XMAX_IS_SHR_LOCKED(infomask) || + HEAP_XMAX_IS_EXCL_LOCKED(infomask)) + return HeapTupleMayBeUpdated; + break; + case LockTupleNoKeyExclusive: + if (HEAP_XMAX_IS_EXCL_LOCKED(infomask)) + return HeapTupleMayBeUpdated; + break; + case LockTupleExclusive: + if (HEAP_XMAX_IS_EXCL_LOCKED(infomask) && + infomask2 & HEAP_KEYS_UPDATED) + return HeapTupleMayBeUpdated; + break; + } + } } /* @@ -4323,6 +4361,31 @@ l3: } /* + * As a check independent from those above, we can also avoid sleeping + * if the current transaction is the sole locker of the tuple. Note + * that the strength of the lock already held is irrelevant; this is + * not about recording the lock in Xmax (which will be done regardless + * of this optimization, below). Also, note that the cases where we + * hold a lock stronger than we are requesting are already handled + * above by not doing anything. + * + * Note we only deal with the non-multixact case here; MultiXactIdWait + * is well equipped to deal with this situation on its own. + */ + if (require_sleep && !(infomask & HEAP_XMAX_IS_MULTI) && + TransactionIdIsCurrentTransactionId(xwait)) + { + /* ... but if the xmax changed in the meantime, start over */ + LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); + if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || + !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), + xwait)) + goto l3; + Assert(HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask)); + require_sleep = false; + } + + /* * By here, we either have already acquired the buffer exclusive lock, * or we must wait for the locking transaction or multixact; so below * we ensure that we grab buffer lock after the sleep. @@ -4331,9 +4394,9 @@ l3: if (require_sleep) { /* - * Acquire tuple lock to establish our priority for the tuple. - * LockTuple will release us when we are next-in-line for the tuple. - * We must do this even if we are share-locking. + * Acquire tuple lock to establish our priority for the tuple, or + * die trying. LockTuple will release us when we are next-in-line + * for the tuple. We must do this even if we are share-locking. * * If we are forced to "start over" below, we keep the tuple lock; * this arranges that we stay at the head of the line while rechecking @@ -4390,37 +4453,6 @@ l3: break; } - /* if there are updates, follow the update chain */ - if (follow_updates && - !HEAP_XMAX_IS_LOCKED_ONLY(infomask)) - { - HTSU_Result res; - - res = heap_lock_updated_tuple(relation, tuple, &t_ctid, - GetCurrentTransactionId(), - mode); - if (res != HeapTupleMayBeUpdated) - { - result = res; - /* recovery code expects to have buffer lock held */ - LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); - goto failed; - } - } - - LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); - - /* - * If xwait had just locked the tuple then some other xact - * could update this tuple before we get to this point. Check - * for xmax change, and start over if so. - */ - if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || - !TransactionIdEquals( - HeapTupleHeaderGetRawXmax(tuple->t_data), - xwait)) - goto l3; - /* * Of course, the multixact might not be done here: if we're * requesting a light lock mode, other transactions with light @@ -4457,43 +4489,46 @@ l3: RelationGetRelationName(relation)))); break; } + } - /* if there are updates, follow the update chain */ - if (follow_updates && - !HEAP_XMAX_IS_LOCKED_ONLY(infomask)) - { - HTSU_Result res; + /* if there are updates, follow the update chain */ + if (follow_updates && !HEAP_XMAX_IS_LOCKED_ONLY(infomask)) + { + HTSU_Result res; - res = heap_lock_updated_tuple(relation, tuple, &t_ctid, - GetCurrentTransactionId(), - mode); - if (res != HeapTupleMayBeUpdated) - { - result = res; - /* recovery code expects to have buffer lock held */ - LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); - goto failed; - } + res = heap_lock_updated_tuple(relation, tuple, &t_ctid, + GetCurrentTransactionId(), + mode); + if (res != HeapTupleMayBeUpdated) + { + result = res; + /* recovery code expects to have buffer lock held */ + LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); + goto failed; } + } - LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); + LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); - /* - * xwait is done, but if xwait had just locked the tuple then - * some other xact could update this tuple before we get to - * this point. Check for xmax change, and start over if so. - */ - if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || - !TransactionIdEquals( - HeapTupleHeaderGetRawXmax(tuple->t_data), - xwait)) - goto l3; + /* + * xwait is done, but if xwait had just locked the tuple then + * some other xact could update this tuple before we get to + * this point. Check for xmax change, and start over if so. + */ + if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || + !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), + xwait)) + goto l3; + if (!(infomask & HEAP_XMAX_IS_MULTI)) + { /* * Otherwise check if it committed or aborted. Note we cannot * be here if the tuple was only locked by somebody who didn't - * conflict with us; that should have been handled above. So - * that transaction must necessarily be gone by now. + * conflict with us; that would have been handled above. So + * that transaction must necessarily be gone by now. But don't + * check for this in the multixact case, because some locker + * transactions might still be running. */ UpdateXmaxHintBits(tuple->t_data, *buffer, xwait); } @@ -4537,37 +4572,6 @@ failed: old_infomask = tuple->t_data->t_infomask; /* - * We might already hold the desired lock (or stronger), possibly under a - * different subtransaction of the current top transaction. If so, there - * is no need to change state or issue a WAL record. We already handled - * the case where this is true for xmax being a MultiXactId, so now check - * for cases where it is a plain TransactionId. - * - * Note in particular that this covers the case where we already hold - * exclusive lock on the tuple and the caller only wants key share or - * share lock. It would certainly not do to give up the exclusive lock. - */ - if (!(old_infomask & (HEAP_XMAX_INVALID | - HEAP_XMAX_COMMITTED | - HEAP_XMAX_IS_MULTI)) && - (mode == LockTupleKeyShare ? - (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask) || - HEAP_XMAX_IS_SHR_LOCKED(old_infomask) || - HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) : - mode == LockTupleShare ? - (HEAP_XMAX_IS_SHR_LOCKED(old_infomask) || - HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) : - (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))) && - TransactionIdIsCurrentTransactionId(xmax)) - { - LockBuffer(*buffer, BUFFER_LOCK_UNLOCK); - /* Probably can't hold tuple lock here, but may as well check */ - if (have_tuple_lock) - UnlockTupleTuplock(relation, tid, mode); - return HeapTupleMayBeUpdated; - } - - /* * If this is the first possibly-multixact-able operation in the current * transaction, set my per-backend OldestMemberMXactId setting. We can be * certain that the transaction will never become a member of any older @@ -4829,7 +4833,7 @@ l5: if (!MultiXactIdIsRunning(xmax, HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))) { if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) || - TransactionIdDidAbort(MultiXactIdGetUpdateXid(xmax, + !TransactionIdDidCommit(MultiXactIdGetUpdateXid(xmax, old_infomask))) { /* diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c index fff9f83..f8af11c 100644 --- a/src/backend/access/transam/multixact.c +++ b/src/backend/access/transam/multixact.c @@ -533,7 +533,7 @@ MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly) */ nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly); - if (nmembers < 0) + if (nmembers <= 0) { debug_elog2(DEBUG2, "IsRunning: no members"); return false; @@ -1338,39 +1338,6 @@ retry: } /* - * MultiXactHasRunningRemoteMembers - * Does the given multixact have still-live members from - * transactions other than our own? - */ -bool -MultiXactHasRunningRemoteMembers(MultiXactId multi) -{ - MultiXactMember *members; - int nmembers; - int i; - - nmembers = GetMultiXactIdMembers(multi, &members, true, false); - if (nmembers <= 0) - return false; - - for (i = 0; i < nmembers; i++) - { - /* not interested in our own members */ - if (TransactionIdIsCurrentTransactionId(members[i].xid)) - continue; - - if (TransactionIdIsInProgress(members[i].xid)) - { - pfree(members); - return true; - } - } - - pfree(members); - return false; -} - -/* * mxactMemberComparator * qsort comparison function for MultiXactMember * diff --git a/src/backend/utils/time/tqual.c b/src/backend/utils/time/tqual.c index a2c4989..c8b65cc 100644 --- a/src/backend/utils/time/tqual.c +++ b/src/backend/utils/time/tqual.c @@ -514,20 +514,20 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, if (tuple->t_infomask & HEAP_XMAX_IS_MULTI) { - if (MultiXactHasRunningRemoteMembers(xmax)) + if (MultiXactIdIsRunning(xmax, true)) return HeapTupleBeingUpdated; else return HeapTupleMayBeUpdated; } - /* if locker is gone, all's well */ + /* + * If the locker is gone, then there is nothing of interest + * left in this Xmax; otherwise, report the tuple as + * locked/updated. + */ if (!TransactionIdIsInProgress(xmax)) return HeapTupleMayBeUpdated; - - if (!TransactionIdIsCurrentTransactionId(xmax)) - return HeapTupleBeingUpdated; - else - return HeapTupleMayBeUpdated; + return HeapTupleBeingUpdated; } if (tuple->t_infomask & HEAP_XMAX_IS_MULTI) @@ -539,10 +539,11 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, /* not LOCKED_ONLY, so it has to have an xmax */ Assert(TransactionIdIsValid(xmax)); - /* updating subtransaction must have aborted */ + /* deleting subtransaction must have aborted */ if (!TransactionIdIsCurrentTransactionId(xmax)) { - if (MultiXactHasRunningRemoteMembers(HeapTupleHeaderGetRawXmax(tuple))) + if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple), + false)) return HeapTupleBeingUpdated; return HeapTupleMayBeUpdated; } @@ -664,7 +665,7 @@ HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetRawXmax(tuple))) { if (HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask)) - return HeapTupleMayBeUpdated; + return HeapTupleBeingUpdated; if (HeapTupleHeaderGetCmax(tuple) >= curcid) return HeapTupleSelfUpdated; /* updated after scan started */ else diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h index ac58a37..ec265c8 100644 --- a/src/include/access/multixact.h +++ b/src/include/access/multixact.h @@ -96,7 +96,6 @@ extern bool MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly); extern void MultiXactIdSetOldestMember(void); extern int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **xids, bool allow_old, bool isLockOnly); -extern bool MultiXactHasRunningRemoteMembers(MultiXactId multi); extern bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2); extern bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2); --5mCyUwZo2JvN/JJP 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 --5mCyUwZo2JvN/JJP--