From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Andres Freund <andres@2ndquadrant.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Oliver Seemann <oseemann@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Update with subselect sometimes returns wrong result
Date: Wed, 18 Dec 2013 21:58:08 -0300
Message-ID: <20131219005808.GI11006@eldon.alvh.no-ip.org> (raw)
In-Reply-To: <20131218223944.GE26481@alap2.anarazel.de>
References: <CANCipfpfzoYnOz5jj=UZ70_R=CwDHv36dqWSpwsi27vpm1z5sA@mail.gmail.com>
<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>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Andres Freund escribió:
> On 2013-12-18 19:13:43 -0300, Alvaro Herrera wrote:
> > 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 don't see how, the EPQ machinery should have ensured we're looking at
> the most recent version. Also, pretty fundamentally, we have to be the
> only locker, otherwise the optimization wouldn't be applicable in this
> way.
EPQ works funny with cursors in the WHERE CURRENT OF stuff; the fact
that it behaves differently in FOR UPDATE case than when there's no
locking clause makes this whole thing pretty misterious.
Anyway I think this whole optimization can be formulated more clearly if
we separate the case into its own block by checking
XidIsCurrentTransaction instead of cramming it into the XidIsInProgress
case as the original is doing; see attached.
Any ideas on possible tests for this stuff? Nothing comes to mind that
doesn't involve pageinspect ...
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:0001-Optimize-updating-a-row-that-s-locked-by-same-xact.patch (text/x-diff, 4.7K) download
From e0520c881bea653e33d9b5ef5bff065840ebacb5 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 18 Dec 2013 21:51:41 -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 | 64 ++++++++++++++++++++------------------
1 file changed, 34 insertions(+), 30 deletions(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex 70748e5..023db6a 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)
{
@@ -4659,6 +4664,33 @@ 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))
{
/*
@@ -4669,6 +4701,8 @@ l5:
MultiXactStatus status;
MultiXactStatus new_status;
+ Assert(xmax != add_to_xmax);+
if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))
{
if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask))
@@ -4706,36 +4740,6 @@ 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.- */- 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;- }- }
new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
}
--
1.7.10.4
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, andres@2ndquadrant.com, tgl@sss.pgh.pa.us, oseemann@gmail.com
Subject: Re: Update with subselect sometimes returns wrong result
In-Reply-To: <20131219005808.GI11006@eldon.alvh.no-ip.org>
References: <CANCipfpfzoYnOz5jj=UZ70_R=CwDHv36dqWSpwsi27vpm1z5sA@mail.gmail.com> <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>
* 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