Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtPNM-0004F6-Ef for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Dec 2013 22:13:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VtPNL-000643-La for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Dec 2013 22:13:55 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtPNJ-00063u-An for pgsql-bugs@postgresql.org; Wed, 18 Dec 2013 22:13:53 +0000 Received: from smtprelay0223.b.hostedemail.com ([64.98.42.223] helo=smtprelay.b.hostedemail.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtPNC-0005D5-Nl for pgsql-bugs@postgresql.org; Wed, 18 Dec 2013 22:13:52 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay01.b.hostedemail.com (Postfix) with ESMTP id D4E442D2B74; Wed, 18 Dec 2013 22:13:45 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 50, 3, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::::::, RULES_HIT:41:355:379:599:871:967:968:973:988:989:1260:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1535:1542:1575:1594:1595:1596:1711:1730:1747:1764:1777:1792:2393:2525:2540:2560:2563:2682:2685:2859:2895:2901:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4119:4605:4659:5007:6119:6261:6748:7281:7903:7904:9025:9121:10012:10394:10400:10450:10455:10848:11026:11232:11256:11257:11604:11658:11914:12043:12295:12296:12438:12517:12519:12663:13149:13161:13229:13230:13894:13895:19904:19999, 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: year54_7806ed5bb244e X-Filterd-Recvd-Size: 8108 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 omf04.b.hostedemail.com (Postfix) with ESMTPA; Wed, 18 Dec 2013 22:13:44 +0000 (UTC) Received: by eldon.alvh.no-ip.org (Postfix, from userid 1000) id 544581FC8; Wed, 18 Dec 2013 19:13:43 -0300 (CLST) Date: Wed, 18 Dec 2013 19:13:43 -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: <20131218221343.GG11006@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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="lEGEL1/lMxI0MVQ2" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20131201114514.GG18793@alap2.anarazel.de> 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 --lEGEL1/lMxI0MVQ2 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Andres Freund escribió: > 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 ... Nice idea. I modified the patch slightly, please see attached. I'm not sure about the added assert that the tuple cannot possibly be locked. I fear cursors provide strange ways to access at tuples. I haven't been able to reproduce a problem but consider an example such as the one below. Is it possible, I wonder, to arrive at the problematic scenario considering that we might try to traverse an update chain to lock future versions of the tuple? I suspect not, because if the tuple was updated (so that there is an update chain to traverse in the first place) then we wouldn't be able to update the original anyway. (I guess I'm mainly talking to myself to assure me that there's no real problem here.) In any case I think it's easy to handle the case by doing something like is_update |= ISUPDATE_from_mxstatus(old_status); and remove the Assert(). alvherre=# create table f (a int primary key, b text); CREATE TABLE alvherre=# insert into f values (1, 'one'); INSERT 0 1 alvherre=# begin; BEGIN alvherre=# select * from f for update; a | b ---+------- 1 | three (1 fila) alvherre=# declare f cursor for select * from f; DECLARE CURSOR alvherre=# fetch 1 from f; a | b ---+------- 1 | three (1 fila) alvherre=# update f set b = 'two'; UPDATE 1 alvherre=# move backward all from f; MOVE 0 alvherre=# fetch 1 from f; a | b ---+------- 1 | three (1 fila) alvherre=# update f set b = 'four' where current of f; UPDATE 1 alvherre=# select * from f; a | b ---+------ 1 | four (1 fila) alvherre=# commit; -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --lEGEL1/lMxI0MVQ2 Content-Type: text/x-diff; charset=iso-8859-1 Content-Disposition: attachment; filename="0001-Optimize-updating-a-row-that-s-locked-by-same-xact.patch" Content-Transfer-Encoding: 8bit From 04004c045204e1dcf1ce0125a890423bbefe9ae4 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 18 Dec 2013 18:26:57 -0300 Subject: [PATCH] Optimize updating a row that's locked by same xact MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updating or locking a row that was already locked by the same transaction caused a MultiXact to be created; but this is unnecessary, because there's no usefulness in being able to differentiate two locks by the same transaction. In particular, if an application does SELECT FOR UPDATE followed by an UPDATE that does not modify columns of the key, we would represent this as a multixact. Optimize the case so that only the strongest of both locks/updates is represented in Xmax. This can save some operations from acquiring MultiXacts, which is a significant optimization. This missed optimization opportunity was spotted by Andres Freund while investigating a bug reported by Oliver Seemann in message CANCipfpfzoYnOz5jj=UZ70_R=CwDHv36dqWSpwsi27vpm1z5sA@mail.gmail.com and also directly as a performance regression reported by Dong Ye in message d54b8387.000012d8.00000010@YED-DEVD1.vmware.com Reportedly, this patch fixes the performance regression. Andres Freund, tweaked by Álvaro Herrera --- src/backend/access/heap/heapam.c | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 70748e5..6b78783 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4541,6 +4541,11 @@ l5: { /* * No previous locker; we just insert our own TransactionId. + * + * Note that it's critical that this case be the first one checked, + * because there are several blocks below that come back to this one + * to implement certain optimizations; old_infomask might contain + * other dirty bits in those cases, but we don't really care. */ if (is_update) { @@ -4705,37 +4710,30 @@ l5: status = MultiXactStatusNoKeyUpdate; } - 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. + * 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. */ 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 possibly 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)); + + /* acquire the strongest of both */ + if (mode < old_mode) + mode = old_mode; + + old_infomask |= HEAP_XMAX_INVALID; + goto l5; } + + new_status = get_mxact_status_for_lock(mode, is_update); new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status); GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2); } -- 1.7.10.4 --lEGEL1/lMxI0MVQ2 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 --lEGEL1/lMxI0MVQ2--