From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@2ndquadrant.com>
Cc: Oliver Seemann <oseemann@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Update with subselect sometimes returns wrong result
Date: Sun, 01 Dec 2013 01:41:02 -0500
Message-ID: <22220.1385880062@sss.pgh.pa.us> (raw)
In-Reply-To: <20131130214018.GJ31100@awork2.anarazel.de>
References: <CANCipfpfzoYnOz5jj=UZ70_R=CwDHv36dqWSpwsi27vpm1z5sA@mail.gmail.com>
<25601.1385834264@sss.pgh.pa.us>
<20131130214018.GJ31100@awork2.anarazel.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Andres Freund <andres@2ndquadrant.com> writes:
> Slightly hacked up (probably python 2 only) version of the test script
> attached.
Ah, deleting and reinserting the rows each time like that makes it a
lot more reproducible. I don't have the full story but it's got something
to do with screwed-up tuple info flags. The plan that's generated looks
like
Update on t1
-> Nested Loop
Join Filter: (t1.id = subset.id)
-> Seq Scan on t1
-> Subquery Scan on subset
-> Limit
-> LockRows
-> Seq Scan on t1 t1_1
so the subquery scan will be executed twice, once for each row in t1.
The first time through, heap_lock_tuple looks at the first tuple and
locks it. It doesn't get called on the second tuple because the LIMIT
node stops evaluation. But in the second scan, heap_lock_tuple returns
HeapTupleSelfUpdated for the first tuple, so ExecLockRows ignores it,
moves on to the second tuple, and returns that. The LIMIT is happy cause
it just got one row back; it doesn't know it's not the same row as
before. And this row of course passes the join qual with the second
row from the outer scan of t1, so we perform a second update.
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.
Anyway, at this point I'm not so much wondering why it fails as why it
(seems to) work *any* of the time. And how is it that VACUUM sometimes
manages to flip it from working state to not-working state? (Once you're
in the state where the UPDATE will say it updated two rows, it's 100%
reproducible.)
Anyway, it seems pretty clear that the explanation is down somewhere in
the tuple visibility and multixact logic that you and Alvaro have been
hacking on with such vim lately. I'm out of steam for tonight, over
to you ...
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
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: tgl@sss.pgh.pa.us, andres@2ndquadrant.com, oseemann@gmail.com
Subject: Re: Update with subselect sometimes returns wrong result
In-Reply-To: <22220.1385880062@sss.pgh.pa.us>
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>
* 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