Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtfzT-0004mY-65 for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Dec 2013 15:58:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VtfzS-00026Q-Kg for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Dec 2013 15:58:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtfzR-00024p-7y for pgsql-bugs@postgresql.org; Thu, 19 Dec 2013 15:58:21 +0000 Received: from smtprelay0193.b.hostedemail.com ([64.98.42.193] helo=smtprelay.b.hostedemail.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtfzJ-0003Fu-1j for pgsql-bugs@postgresql.org; Thu, 19 Dec 2013 15:58:20 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay03.b.hostedemail.com (Postfix) with ESMTP id 749A31A738F; Thu, 19 Dec 2013 15:58:10 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 30, 2, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::::::, RULES_HIT:1:41:355:379:599:871:967:973:988:989:1260:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1537:1561: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:3622:3865:3866:3870:3871:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4605:4659:5007:6261:6748:7281:7398:9025:9121:10010:10394:10400:10848:11256:11257:11604:11658:11914:12517:12519: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:3:0 X-HE-Tag: ship03_6452af12e0d07 X-Filterd-Recvd-Size: 11180 Received: from eldon.alvh.no-ip.org (pc-211-245-160-190.cm.vtr.net [190.160.245.211]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf07.b.hostedemail.com (Postfix) with ESMTPA; Thu, 19 Dec 2013 15:58:09 +0000 (UTC) Received: by eldon.alvh.no-ip.org (Postfix, from userid 1000) id B7A421FC8; Thu, 19 Dec 2013 12:58:07 -0300 (CLST) Date: Thu, 19 Dec 2013 12:58:07 -0300 From: Alvaro Herrera To: Andres Freund Cc: Tom Lane , Oliver Seemann , pgsql-bugs@postgresql.org Subject: Re: Update with subselect sometimes returns wrong result Message-ID: <20131219155807.GL11006@eldon.alvh.no-ip.org> References: <25601.1385834264@sss.pgh.pa.us> <20131130214018.GJ31100@awork2.anarazel.de> <22220.1385880062@sss.pgh.pa.us> <20131201081919.GF18793@alap2.anarazel.de> <20131201114514.GG18793@alap2.anarazel.de> <20131218221343.GG11006@eldon.alvh.no-ip.org> <20131218223944.GE26481@alap2.anarazel.de> <20131219005808.GI11006@eldon.alvh.no-ip.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8nsIa27JVQLqB7/C" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20131219005808.GI11006@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 --8nsIa27JVQLqB7/C Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Actually, the original coding is better because it enables easier writing of other optimizations, such as in the attached which should also cure the performance regression noted in bug #8470. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --8nsIa27JVQLqB7/C Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="multixact-optimize.patch" diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 023db6a..2d6f57d 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4534,6 +4534,8 @@ compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask, uint16 new_infomask, new_infomask2; + Assert(TransactionIdIsCurrentTransactionId(add_to_xmax)); + l5: new_infomask = 0; new_infomask2 = 0; @@ -4584,6 +4586,9 @@ l5: else if (old_infomask & HEAP_XMAX_IS_MULTI) { MultiXactStatus new_status; + MultiXactMember *members; + int nmembers; + bool keepmulti = false; /* * Currently we don't allow XMAX_COMMITTED to be set for multis, so @@ -4607,37 +4612,70 @@ 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. + * Examine the Multixact members. 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 MultiXactId; there's no need to create a new one. */ - if (!MultiXactIdIsRunning(xmax)) + nmembers = GetMultiXactIdMembers(xmax, &members, true); + if (nmembers > 0) { - if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) || - TransactionIdDidAbort(MultiXactIdGetUpdateXid(xmax, - old_infomask))) + int i; + + for (i = 0; i < nmembers; i++) { - /* - * Reset these bits and restart; otherwise fall through to - * create a new multi below. - */ - old_infomask &= ~HEAP_XMAX_IS_MULTI; - old_infomask |= HEAP_XMAX_INVALID; - goto l5; + if (TransactionIdIsCurrentTransactionId(members[i].xid) && + (TUPLOCK_from_mxstatus(members[i].status) >= mode) && + (!is_update || ISUPDATE_from_mxstatus(members[i].status))) + { + keepmulti = true; + break; + } } + pfree(members); } - new_status = get_mxact_status_for_lock(mode, is_update); + if (keepmulti) + { + new_status = get_mxact_status_for_lock(mode, is_update); + new_xmax = xmax; + } + else + { + /* + * 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 + * create a new multi below. + */ + old_infomask &= ~HEAP_XMAX_IS_MULTI; + 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); + } - new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax, - new_status); GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); } else if (old_infomask & HEAP_XMAX_COMMITTED) @@ -4664,33 +4702,6 @@ l5: new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status); GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); } - else if (xmax == add_to_xmax && TransactionIdIsCurrentTransactionId(xmax)) - - { - LockTupleMode old_mode = TUPLOCK_from_mxstatus(status); - - /* - * If the lock to be acquired is from the same transaction as the - * existing lock, there's an additional optimization: consider only - * the strongest of both locks as the only one present, and restart. - * - * Note that it's not possible for the original tuple to be updated: - * we wouldn't be here because the tuple would have been invisible and - * we wouldn't try to update it. As a subtlety, this code can also - * run when traversing an update chain to lock future versions of a - * tuple. But we wouldn't be here either, because the add_to_xmax - * would be different from the original updater. - */ - Assert(HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); - - /* acquire the strongest of both */ - if (mode < old_mode) - mode = old_mode; - /* don't need to touch is_update */ - - old_infomask |= HEAP_XMAX_INVALID; - goto l5; - } else if (TransactionIdIsInProgress(xmax)) { /* @@ -4698,23 +4709,22 @@ l5: * create a new MultiXactId that includes both the old locker or * updater and our own TransactionId. */ - MultiXactStatus status; MultiXactStatus new_status; - - Assert(xmax != add_to_xmax); + MultiXactStatus old_status; + LockTupleMode old_mode; if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)) { if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask)) - status = MultiXactStatusForKeyShare; + old_status = MultiXactStatusForKeyShare; else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask)) - status = MultiXactStatusForShare; + old_status = MultiXactStatusForShare; else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) { if (old_infomask2 & HEAP_KEYS_UPDATED) - status = MultiXactStatusForUpdate; + old_status = MultiXactStatusForUpdate; else - status = MultiXactStatusForNoKeyUpdate; + old_status = MultiXactStatusForNoKeyUpdate; } else { @@ -4734,13 +4744,72 @@ l5: { /* it's an update, but which kind? */ if (old_infomask2 & HEAP_KEYS_UPDATED) - status = MultiXactStatusUpdate; + old_status = MultiXactStatusUpdate; else - status = MultiXactStatusNoKeyUpdate; + old_status = MultiXactStatusNoKeyUpdate; } + old_mode = TUPLOCK_from_mxstatus(old_status); + + /* + * If the lock to be acquired is for the same TransactionId as the + * existing lock, there's an optimization possible: consider only the + * strongest of both locks as the only one present, and restart. + */ + if (xmax == add_to_xmax) + { + /* + * Note that it's not possible for the original tuple to be updated: + * we wouldn't be here because the tuple would have been invisible and + * we wouldn't try to update it. As a subtlety, this code can also + * run when traversing an update chain to lock future versions of a + * tuple. But we wouldn't be here either, because the add_to_xmax + * would be different from the original updater. + */ + Assert(HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); + + /* acquire the strongest of both */ + if (mode < old_mode) + mode = old_mode; + /* musn't touch is_update */ + + old_infomask |= HEAP_XMAX_INVALID; + goto l5; + } + + /* + * Another possibility is that xmax is not identical to add_to_xmax, + * but that it is an ancestor. We can simply piggy-back on the + * existing lock, without requiring that a new lock be taken. In this + * case, we require the lock mode being requested to be equal or + * weaker than the original; no upgrading of mode is allowed. + * (otherwise, a third party wanting to acquire a lock that conflicts + * with the stronger new lock but not with the old one would not be + * released if the new subtransaction aborts). + * + * To implement this, make it appear that there is no existing lock, + * and re-request what was present. + * + * Since we know add_to_xmax is the current transaction, then xmax is + * an ancestor of add_to_xmax if it is also a current transaction Id. + */ + if (TransactionIdIsCurrentTransactionId(xmax) && old_mode >= mode) + { + /* as above, the tuple cannot possibly be updated */ + Assert(HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); + + add_to_xmax = xmax; + /* acquire the strongest of both */ + mode = old_mode; + /* musn't touch is_update */ + old_infomask |= HEAP_XMAX_INVALID; + goto l5; + } + + /* otherwise, just fall back to creating a new multixact */ new_status = get_mxact_status_for_lock(mode, is_update); - new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status); + new_xmax = MultiXactIdCreate(xmax, old_status, + add_to_xmax, new_status); GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); } else if (!HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) && --8nsIa27JVQLqB7/C 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 --8nsIa27JVQLqB7/C--