Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vv7tP-0000VJ-Mq for pgsql-bugs@arkaria.postgresql.org; Mon, 23 Dec 2013 15:58:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vv7tP-0004zR-64 for pgsql-bugs@arkaria.postgresql.org; Mon, 23 Dec 2013 15:58:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vv7tO-0004zL-B3 for pgsql-bugs@postgresql.org; Mon, 23 Dec 2013 15:58:06 +0000 Received: from smtprelay0141.b.hostedemail.com ([64.98.42.141] helo=smtprelay.b.hostedemail.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vv7tB-0005BR-RI for pgsql-bugs@postgresql.org; Mon, 23 Dec 2013 15:58:05 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay06.b.hostedemail.com (Postfix) with ESMTP id 0E2288D237; Mon, 23 Dec 2013 15:57:51 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 57, 3.5, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::, RULES_HIT:4:41:355:379:599:871:967:973:988:989:1260:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1542:1575:1594:1595:1596:1711:1730:1747:1764:1777:1792:2393:2525:2560:2563:2682:2685:2693:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4605:4659:5007:6261:6748:7281:7398:8660:8957:9007:9025:9121:10013:10400:10482:10848:11256:11257:11604:11658:11914:12296:12438:12517:12519:12555:12663:12679:13148:13161:13180:13229:13230:13870:13894:13895:19901:19997, 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:3:0 X-HE-Tag: duck18_26eb3fd23305c X-Filterd-Recvd-Size: 15952 Received: from eldon.alvh.no-ip.org (unknown [186.37.105.156]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf07.b.hostedemail.com (Postfix) with ESMTPA; Mon, 23 Dec 2013 15:57:47 +0000 (UTC) Received: by eldon.alvh.no-ip.org (Postfix, from userid 1000) id 0A55B1FC9; Mon, 23 Dec 2013 12:56:04 -0300 (CLST) Date: Mon, 23 Dec 2013 12:56:04 -0300 From: Alvaro Herrera To: os@ohmu.fi Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression Message-ID: <20131223155603.GI22570@eldon.alvh.no-ip.org> References: <20131220204753.GG22570@eldon.alvh.no-ip.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20131220204753.GG22570@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 --bCsyhTFzCvuiizWE Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit After looking at this some more, I am more convinced that we need to have HeapTupleSatisfiesUpdate return BeingUpdated when a row is locked by the same xact. This is more in line with what we do for MultiXacts, and allows us to treat some cases in a saner fashion: in particular, if a transaction grabs a lock on a tuple and a subxact grabs a stronger lock, we now create a multi recording both locks instead of silently ignoring the second request. The fallout from this change wasn't as bad as I had feared; I had to add some coding in heap_update and heap_delete to prevent them from trying to wait on their own Xid, but it looks pretty innocuous otherwise. In ran your test case inflating the loop counters a bit, so that it shows more exaggerated timings. The results of running this twice on 9.2 are: Timing: 6140,506 ms Timing: 6054,553 ms I only ran it once in an unpatched 9.3: Timing: 221204,352 This is patched 9.3: Timing: 13757,925 ms Note patched 9.3 is 2x slower than 9.2, which is understandable because it does more work ... yet I think it returns performance to a much more acceptable level, comparable to 9.2. I have more confidence in this patch than in the previous version, despite it being more intrusive. The fact that it allows us to get rid of some very strange coding in pgrowlocks is comforting: we now know that anytime a tuple is locked we will get BeingUpdated from HeapTupleSatisfiesUpdate, regardless of *who* locked it, and all the callers are able to identify this case and act in consequence. This seems saner in concept that sometimes returning MayBeUpdated, because that turns each such return into a condition that must be double-checked by callers. This means cleaner code in pgrowlocks around a HTSU call, for instance. Also, we have discussed and seriously considered this idea in several occasions previously in connection with other things. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --bCsyhTFzCvuiizWE Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="optimize-locking-locked-multi.patch" diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c index 075d781..9ef90cd 100644 *** a/contrib/pgrowlocks/pgrowlocks.c --- b/contrib/pgrowlocks/pgrowlocks.c *************** *** 138,151 **** 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. */ ! if (htsu == HeapTupleBeingUpdated || ! (htsu == HeapTupleMayBeUpdated && ! !(infomask & HEAP_XMAX_INVALID) && ! !(infomask & HEAP_XMAX_IS_MULTI) && ! (xmax == GetCurrentTransactionIdIfAny()))) { char **values; --- 138,146 ---- infomask = tuple->t_data->t_infomask; /* ! * A tuple is locked if HTSU returns BeingUpdated. */ ! if (htsu == HeapTupleBeingUpdated) { char **values; diff --git a/src/backend/access/heap/hindex b5977da..6eb1de2 100644 *** a/src/backend/access/heap/heapam.c --- b/src/backend/access/heap/heapam.c *************** *** 2596,2601 **** l1: --- 2596,2621 ---- TransactionId xwait; uint16 infomask; + /* + * If any subtransaction of the current top transaction already holds + * a lock on this tuple, (and no one else does,) we must skip sleeping + * on the xwait; that would raise an assert about sleeping on our own + * Xid (or sleep indefinitely in a non-assertion enabled build.) Note + * we don't need to do this when the Xmax is a multi, because that + * code is prepared to deal with transactions of the same session. + * + * This must be done before acquiring our tuple lock, to avoid + * deadlocks with other transaction that are already waiting on the + * lock we hold. + */ + if (!(tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) && + TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetRawXmax(tp.t_data))) + { + Assert(HEAP_XMAX_IS_LOCKED_ONLY(tp.t_data->t_infomask)); + + goto continue_deletion; + } + /* must copy state data before unlocking buffer */ xwait = HeapTupleHeaderGetRawXmax(tp.t_data); infomask = tp.t_data->t_infomask; *************** *** 2669,2674 **** l1: --- 2689,2695 ---- UpdateXmaxHintBits(tp.t_data, buffer, xwait); } + continue_deletion: /* * We may overwrite if previous xmax aborted, or if it committed but * only locked the tuple without updating it. *************** *** 3089,3094 **** l2: --- 3110,3137 ---- * heap_update directly. */ + /* + * If any subtransaction of the current top transaction already holds + * a lock on this tuple, (and no one else does,) we must skip sleeping + * on the xwait; that would raise an assert about sleeping on our own + * Xid (or sleep indefinitely in a non-assertion enabled build.) Note + * we don't need to do this when the Xmax is a multi, because that + * code is prepared to deal with transactions of the same session. + * + * This must be done before acquiring our tuple lock, to avoid + * deadlocks with other transaction that are already waiting on the + * lock we hold. + */ + if (!(oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) && + TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetRawXmax(oldtup.t_data))) + { + Assert(HEAP_XMAX_IS_LOCKED_ONLY(oldtup.t_data->t_infomask)); + + can_continue = true; + locker_remains = true; + goto continue_updating; + } + /* must copy state data before unlocking buffer */ xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data); infomask = oldtup.t_data->t_infomask; *************** *** 3223,3228 **** l2: --- 3266,3272 ---- } } + continue_updating: result = can_continue ? HeapTupleMayBeUpdated : HeapTupleUpdated; } *************** *** 3945,3951 **** l3: TransactionId xwait; uint16 infomask; uint16 infomask2; ! bool require_sleep; ItemPointerData t_ctid; /* must copy state data before unlocking buffer */ --- 3989,3995 ---- TransactionId xwait; uint16 infomask; uint16 infomask2; ! bool require_sleep = true; ItemPointerData t_ctid; /* must copy state data before unlocking buffer */ *************** *** 3997,4002 **** l3: --- 4041,4064 ---- pfree(members); } + else if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) && + TransactionIdIsCurrentTransactionId(xwait)) + { + /* + * If we already hold a lock on this tuple, we can fall through + * to perhaps create a new MultiXact including the previous lock + * and ours, or perhaps just keep the old lock. + */ + result = HeapTupleMayBeUpdated; + + LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); + /* If xmax changed while we weren't looking, start over */ + if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) || + !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), + xwait)) + goto l3; + goto continue_locking; + } /* * Acquire tuple lock to establish our priority for the tuple. *************** *** 4330,4335 **** l3: --- 4392,4399 ---- /* By here, we're certain that we hold buffer exclusive lock again */ + continue_locking: + /* * We may lock if previous xmax aborted, or if it committed but only * locked the tuple without updating it; or if we didn't have to wait *************** *** 4365,4401 **** 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 --- 4429,4434 ---- *************** *** 4585,4591 **** l5: } else if (old_infomask & HEAP_XMAX_IS_MULTI) { ! MultiXactStatus new_status; /* * Currently we don't allow XMAX_COMMITTED to be set for multis, so --- 4618,4624 ---- } else if (old_infomask & HEAP_XMAX_IS_MULTI) { ! bool keepmulti = false; /* * Currently we don't allow XMAX_COMMITTED to be set for multis, so *************** *** 4609,4630 **** l5: } /* ! * If the XMAX is already a MultiXactId, then we need to expand it to ! * include add_to_xmax; but if all the members were lockers and are ! * all gone, we can do away with the IS_MULTI bit and just set ! * add_to_xmax as the only locker/updater. If all lockers are gone ! * and we have an updater that aborted, we can also do without a ! * multi. ! * ! * The cost of doing GetMultiXactIdMembers would be paid by ! * MultiXactIdExpand if we weren't to do this, so this check is not ! * incurring extra work anyhow. */ ! if (!MultiXactIdIsRunning(xmax)) { ! if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) || ! TransactionIdDidAbort(MultiXactIdGetUpdateXid(xmax, ! old_infomask))) { /* * Reset these bits and restart; otherwise fall through to --- 4642,4706 ---- } /* ! * Examine the members of the existing MultiXact. If any of them is ! * the current transaction and holds a lock at least as strong as we ! * are requesting, then the lock request can be satisfied with the ! * existing MultiXact; there's no need to create a new one. Note we ! * only try this for locks, not updates, for three reasons: 1. if the ! * savepoint with the update later tried to abort, we'd be screwed; 2. ! * there's no way to "upgrade" a lock to an update within a multixact ! * anyway; 3. there is no point in trying because if the multixact had ! * a valid (non-aborted) update, the tuple would be invisible and we ! * wouldn't be trying to lock it. */ ! if (!is_update) ! { ! MultiXactMember *members; ! int nmembers; ! ! nmembers = GetMultiXactIdMembers(xmax, &members, true); ! if (nmembers > 0) ! { ! int i; ! MultiXactStatus new_status; ! ! new_status = get_mxact_status_for_lock(mode, false); ! ! for (i = 0; i < nmembers; i++) ! { ! if (TransactionIdIsCurrentTransactionId(members[i].xid) && ! (members[i].status >= new_status)) ! { ! keepmulti = true; ! break; ! } ! } ! pfree(members); ! } ! } ! ! if (keepmulti) ! new_xmax = xmax; ! else { ! MultiXactStatus new_status; ! ! /* ! * If the XMAX is already a MultiXactId, then we need to expand it ! * to include add_to_xmax; but if all the members were lockers and ! * are all gone, we can do away with the IS_MULTI bit and just set ! * add_to_xmax as the only locker/updater. If all lockers are ! * gone and we have an updater that aborted, we can also do ! * without a multi. ! * ! * The cost of doing GetMultiXactIdMembers would be paid by ! * MultiXactIdExpand if we weren't to do this, so this check is ! * not incurring extra work anyhow. ! */ ! if (!MultiXactIdIsRunning(xmax) && ! (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) || ! !TransactionIdDidCommit(MultiXactIdGetUpdateXid(xmax, ! old_infomask)))) { /* * Reset these bits and restart; otherwise fall through to *************** *** 4634,4645 **** l5: old_infomask |= HEAP_XMAX_INVALID; goto l5; } - } ! new_status = get_mxact_status_for_lock(mode, is_update); - new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax, - new_status); GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); } else if (old_infomask & HEAP_XMAX_COMMITTED) --- 4710,4722 ---- old_infomask |= HEAP_XMAX_INVALID; goto l5; } ! new_status = get_mxact_status_for_lock(mode, is_update); ! ! new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax, ! new_status); ! } GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); } else if (old_infomask & HEAP_XMAX_COMMITTED) diff --git a/src/backend/utils/time/tquindex 44b4ddc..0d82523 100644 *** a/src/backend/utils/time/tqual.c --- b/src/backend/utils/time/tqual.c *************** *** 846,852 **** HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid, if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetRawXmax(tuple))) { if (HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask)) ! return HeapTupleMayBeUpdated; if (HeapTupleHeaderGetCmax(tuple) >= curcid) return HeapTupleSelfUpdated; /* updated after scan started */ else --- 846,852 ---- if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetRawXmax(tuple))) { if (HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask)) ! return HeapTupleBeingUpdated; if (HeapTupleHeaderGetCmax(tuple) >= curcid) return HeapTupleSelfUpdated; /* updated after scan started */ else diff --git a/src/include/access/xlog.index 83e5832..16a4c3b 100644 diff --git a/src/test/isolation/index f625650..2dfd872 100644 --bCsyhTFzCvuiizWE 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 --bCsyhTFzCvuiizWE--