Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn5So-00065M-C4 for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Dec 2013 11:45:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vn5Sn-0000Kw-Kv for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Dec 2013 11:45:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn5Sm-0000Kp-KV for pgsql-bugs@postgresql.org; Sun, 01 Dec 2013 11:45:24 +0000 Received: from mail.anarazel.de ([217.115.131.40]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn5Se-0001x8-Tx for pgsql-bugs@postgresql.org; Sun, 01 Dec 2013 11:45:24 +0000 Received: from intern.anarazel.de (p54AF4AA8.dip0.t-ipconnect.de [84.175.74.168]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id 6FC464E8003; Sun, 1 Dec 2013 12:45:15 +0100 (CET) Date: Sun, 1 Dec 2013 12:45:14 +0100 From: Andres Freund To: Tom Lane , Alvaro Herrera Cc: Oliver Seemann , pgsql-bugs@postgresql.org Subject: Re: Update with subselect sometimes returns wrong result Message-ID: <20131201114514.GG18793@alap2.anarazel.de> References: <25601.1385834264@sss.pgh.pa.us> <20131130214018.GJ31100@awork2.anarazel.de> <22220.1385880062@sss.pgh.pa.us> <20131201081919.GF18793@alap2.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Dxnq1zWXvFF0Q93v" Content-Disposition: inline In-Reply-To: <20131201081919.GF18793@alap2.anarazel.de> 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 --Dxnq1zWXvFF0Q93v Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On 2013-12-01 09:19:19 +0100, Andres Freund wrote: > On 2013-12-01 01:41:02 -0500, Tom Lane wrote: > > Now, the thing about this is that the tuple heap_lock_tuple is rejecting > > in the second pass is the one that we just updated, up at the ModifyTable > > plan node. So I can't find it exactly surprising that it says > > HeapTupleSelfUpdated. But tracing through tqual.c shows that the tuple > > has got the HEAP_XMAX_IS_MULTI bit set, which might be thought a bit > > peculiar. There's not multiple transactions involved. > > Hm. Haven't looked at that, but if so that seems like an independent bug. Afaics it's a missed optimization, nothing more. There's a branch in compute_new_xmax_infomask() where it considers xmax == add_to_xmax with imo somewhat strange conditions for the optimization: /* * If the existing lock mode is identical to or weaker than the new * one, we can act as though there is no existing lock, so set * XMAX_INVALID and restart. */ if (xmax == add_to_xmax) { LockTupleMode old_mode = TUPLOCK_from_mxstatus(status); bool old_isupd = ISUPDATE_from_mxstatus(status); /* * We can do this if the new LockTupleMode is higher or equal than * the old one; and if there was previously an update, we need an * update, but if there wasn't, then we can accept there not being * one. */ if ((mode >= old_mode) && (is_update || !old_isupd)) { /* * Note that the infomask might contain some other dirty bits. * However, since the new infomask is reset to zero, we only * set what's minimally necessary, and that the case that * checks HEAP_XMAX_INVALID is the very first above, there is * no need for extra cleanup of the infomask here. */ old_infomask |= HEAP_XMAX_INVALID; goto l5; } } Several things: a) If the old lockmode is stronger than the new one, we can just promote the new one. That's fine. b) the old xmax cannot be an update, we wouldn't see the row version in that case. And in any way, ISUPDATE_from_mxstatus() isn't sufficient to determine whether the old row was an update, one needs to look at LOCK_ONLY as well, no? c) Any reason we can't apply this optimization for subtransactions in some scenarios? a), b) are relatively easy. Patch attached. Being a clear regression, I think it should be backpatched, but I'm not sure if it has to be this point release. It's simple enough, but ... The reason we didn't hit the (mode == old_mode && is_update) case above is that the UPDATE uses only LockTupleNoKeyExclusive since there are no keys, but FOR UPDATE was used before. I am not awake enough to think about c) and it seems somewhat more complicated. I think we can only optimize it if the previous lock was in a subcommited subtransaction and not if it's a transaction higher up the chain. On the note of superflous MultiXactIdCreate() calls, the latter contains the following assert: Assert(!TransactionIdEquals(xid1, xid2) || (status1 != status2)); Is there any reason we *ever* should add two times the same xid? Afaics there's several code paths to get there today, but imo we should work towards that never happening. Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --Dxnq1zWXvFF0Q93v Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Avoid-MultiXacts-in-more-situations-when-updating-a-.patch" From 4c7fda01078c368478fbc1ffa916e689cc61ef91 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sun, 1 Dec 2013 12:32:59 +0100 Subject: [PATCH] Avoid MultiXacts in more situations when updating a previously locked row. Since the introduction of the foreign key locking feature, when updating a row previously locked in the same transaction, we generated a MultiXact when the new lock was weaker than the old one. This could easily happen in applications using explicit FOR UPDATE clauses before UPDATEing, since an UPDATE now often only uses the NO KEY UPDATE lock level. To do so, simply promote the new lock to the old, stronger, lock level when computing the new infomask for the updated tuple. --- src/backend/access/heap/heapam.c | 42 +++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index c13f87c..c03ff80 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4725,33 +4725,35 @@ l5: new_status = get_mxact_status_for_lock(mode, is_update); /* - * If the existing lock mode is identical to or weaker than the new - * one, we can act as though there is no existing lock, so set - * XMAX_INVALID and restart. + * Already locked by the current transaction, so we can take a + * shortcut: If the existing lock mode is identical to or weaker than + * the new one, we can act as though there is no existing lock. If the + * old lock mode is stronger than the new one, we just promote the new + * lock mode. To do so, set XMAX_INVALID and restart. */ if (xmax == add_to_xmax) { LockTupleMode old_mode = TUPLOCK_from_mxstatus(status); - bool old_isupd = ISUPDATE_from_mxstatus(status); /* - * We can do this if the new LockTupleMode is higher or equal than - * the old one; and if there was previously an update, we need an - * update, but if there wasn't, then we can accept there not being - * one. + * The old xmax cannot not be an update, since otherwise this row + * version would have been invisible. */ - if ((mode >= old_mode) && (is_update || !old_isupd)) - { - /* - * Note that the infomask might contain some other dirty bits. - * However, since the new infomask is reset to zero, we only - * set what's minimally necessary, and that the case that - * checks HEAP_XMAX_INVALID is the very first above, there is - * no need for extra cleanup of the infomask here. - */ - old_infomask |= HEAP_XMAX_INVALID; - goto l5; - } + Assert(HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)); + + /* Promote lockmode if necessary. */ + if (mode < old_mode) + mode = old_mode; + + /* + * Note that the infomask might contain some other dirty bits. + * However, since the new infomask is reset to zero, we only set + * what's minimally necessary. The case that checks + * HEAP_XMAX_INVALID is the very first above, there is no need for + * extra cleanup of the infomask here. + */ + old_infomask |= HEAP_XMAX_INVALID; + goto l5; } new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status); GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); -- 1.8.3.251.g1462b67 --Dxnq1zWXvFF0Q93v 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 --Dxnq1zWXvFF0Q93v--