pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Bruce Momjian <bruce@momjian.us>
Cc: Oskari Saarenmaa <os@ohmu.fi>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression
Date: Fri, 10 Apr 2015 13:17:04 -0300
Message-ID: <20150410161704.GH4369@alvh.no-ip.org> (raw)
In-Reply-To: <20150402005616.GB29404@momjian.us>
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>
	<20150105172318.GM1457@alvh.no-ip.org>
	<20150402005616.GB29404@momjian.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Bruce Momjian wrote:
> On Mon, Jan  5, 2015 at 02:23:18PM -0300, Alvaro Herrera wrote:
> > 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.
> 
> Where are we on this?

That's indeed the question.  I gave this a look yesterday and came up
with two patches that "fix" the issue.  (I had to fix one additional bug
in the formulation of the complex patch that I posted in January).  I
leave the details of the bug as exercise for the interested readers
(hint: run the isolation tests).  First some timings.  Ran the script
against unpatched master and each of the patches five times.  Results:

unpatched:
real    0m8.192s
real    0m8.230s
real    0m8.233s
real    0m8.187s
real    0m8.212s

simple patch:
real    0m0.741s
real    0m0.728s
real    0m0.729s
real    0m0.738s
real    0m0.731s

complex patch:
real    0m0.732s
real    0m0.723s
real    0m0.730s
real    0m0.725s
real    0m0.726s

In 9.2 the time is about 0.150s, so the regression is not completely
resolved, but it's a huge improvement.

The main catch of the "simple" formulation of the patch is that we do
the new GetMultiXactIdMembers call with the buffer lock held, which is a
horrible idea from a concurrency point of view; it will make many cases
where the optimization doesn't apply a lot slower.  I think with some
extra contortions we could fix that problem, but it's already quite ugly
that we have a duplicate check for the are-we-already-a-multixact-locker
so I reject the idea that this seemingly simple patch is any good.  I
much prefer the complex formulation, which is what I had to start with,
and makes thing a bit less unclear(*).

There was some traction to the idea of backpatching this, but I'm no
longer on board with that.  If somebody wants to, I would like some
commitment to a huge testing effort.


(*) In a scale 1 to 10 with 10 being most unclear, the original code is
about 12-unclear, and with the patch is 11-unclear.  So it's an
improvement anyhow.

PS: Apologies for unified diff.  This is one case where filterdiff
dropped some hunks from the patch produced by git diff.

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, 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

Attachments: download all (2 files, .tar.gz)

8470-simple.patch (text/x-diff, 2.5K) download *** a/src/backend/access/heap/heapam.c --- b/src/backend/access/heap/heapam.c *************** *** 4539,4547 **** failed: /* * 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 --- 4539,4546 ---- /* * 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. First check ! * for cases where xmax 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 *************** *** 4568,4573 **** failed: --- 4567,4618 ---- } /* + * Now check the multixact case. It was already checked above, but it's + * possible to get MayBeUpdated from HTSU and so we wouldn't have run + * through it. + * + * XXX this is run with buffer lock held -- probably not a good idea. + */ + if (old_infomask & HEAP_XMAX_IS_MULTI) + { + 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(xmax, &members, false, + HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); + + for (i = 0; i < nmembers; i++) + { + if (TransactionIdIsCurrentTransactionId(members[i].xid)) + { + LockTupleMode membermode; + + membermode = TUPLOCK_from_mxstatus(members[i].status); + + if (membermode >= mode) + { + LockBuffer(*buffer, BUFFER_LOCK_UNLOCK); + if (have_tuple_lock) + UnlockTupleTuplock(relation, tid, mode); + + pfree(members); + return HeapTupleMayBeUpdated; + } + } + } + + if (members) + pfree(members); + } + + /* * 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
8470-complex.patch (text/x-diff, 24.9K) download 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 cb6f8a3..457cd70 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_self), 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_self), 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_self), XLTW_Delete); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -3204,8 +3215,6 @@ l2: uint16 infomask; bool can_continue = false; - checked_lockers = true; - /* * XXX note that we don't consider the "no wait" case here. This * isn't a problem currently because no caller uses that case, but it @@ -3223,8 +3232,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 +3259,34 @@ 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_self, XLTW_Update, - &remain); - LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); + /* wait for multixact */ + MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, + relation, &oldtup.t_self, XLTW_Update, + &remain); + checked_lockers = true; + locker_remains = remain != 0; + 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 @@ -3294,9 +3307,10 @@ l2: * before this one, which are important to keep in case this * subxact aborts. */ - update_xact = InvalidTransactionId; if (!HEAP_XMAX_IS_LOCKED_ONLY(oldtup.t_data->t_infomask)) update_xact = HeapTupleGetUpdateXid(oldtup.t_data); + else + update_xact = InvalidTransactionId; /* * There was no UPDATE in the MultiXact; or it aborted. No @@ -3306,61 +3320,56 @@ l2: if (!TransactionIdIsValid(update_xact) || TransactionIdDidAbort(update_xact)) can_continue = true; - - 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. + */ + checked_lockers = true; + locker_remains = true; + can_continue = 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. + */ + checked_lockers = true; + locker_remains = true; + can_continue = 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_self, + XLTW_Update); + checked_lockers = true; + 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_self, - 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 +4106,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 +4146,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 +4363,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 +4396,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 +4455,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 +4491,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 +4574,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 +4835,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 f9ca028..3dbf6b9 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 777f55c..a4a478d 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 45dc32a..640b198 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);

view thread (26+ messages)  latest in thread

Message-ID: <20150410161704.GH4369@alvh.no-ip.org>
Permalink:  ../20150410161704.GH4369@alvh.no-ip.org/
Also on:    postgresql.org/message-id/20150410161704.GH4369@alvh.no-ip.org

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: alvherre@2ndquadrant.com, bruce@momjian.us, os@ohmu.fi
  Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression
  In-Reply-To: <20150410161704.GH4369@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> <20150105172318.GM1457@alvh.no-ip.org> <20150402005616.GB29404@momjian.us> <20150410161704.GH4369@alvh.no-ip.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox