From: Peter Geoghegan <pg@heroku.com>
To: Andres Freund <andres@anarazel.de>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: Stanislav Grozev <tacho@daemonz.org>
Cc: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
Date: Tue, 8 Dec 2015 15:32:03 -0800
Message-ID: <CAM3SWZQJcdx0y4UBPYPvf-mfs_SMVBi3zKA2JenGeUtcQDd=cg@mail.gmail.com> (raw)
In-Reply-To: <20151208231220.GG28762@awork2.anarazel.de>
References: <CAA78GVqy1+LisN-8DygekD_Ldfy=BJLarSpjGhytOsgkpMavfQ@mail.gmail.com>
<CAB7nPqTjO4decSSm2DxeUrchX10SJ3GfKTddgBH42bvX5k4WjQ@mail.gmail.com>
<CAEZATCXxhKui4hCMXGyeWUeo-pTS7r4BwfzjkM7v0sOvLiAETA@mail.gmail.com>
<CAB7nPqRioCgcGydOV5h1raa21QvYR3EiJVKY7UVdwHx=-KhcUw@mail.gmail.com>
<CAM3SWZR4WOxAbq4=0zy4EPfRw7COufJ8tUqqP1LTypef7kAQPA@mail.gmail.com>
<CAM3SWZSiX3_6H149ZH6QF8Y9LuaYgpg1218bbF9_cjvyykaK4w@mail.gmail.com>
<20151208144440.GU4934@alap3.anarazel.de>
<CAM3SWZSEF00OaCxFQErv_rOVLBtsM9n=xoxknLxs7YxMjSxYuQ@mail.gmail.com>
<20151208231220.GG28762@awork2.anarazel.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On Tue, Dec 8, 2015 at 3:12 PM, Andres Freund <andres@anarazel.de> wrote:
> I'm not 100% sure, but I do think it could point to arbitrary tuples:
> Consider what happens if a previous update to the same tuple failed and
> the new version of the tuple is pruned away, and that space is then
> reused for something else. Bang. That's probably only possible if the
> xmax is a multixact, as heap_lock_tuple() otherwise would clear out
> t_ctid - but that's perfectly possible.
That's an alarming possibility. Either way though, it's wrong, and
the fix is fairly clear.
>> Hmm. You mean having changed things to pass &tuple.t_self to
>> ExecUpdate() instead?
>
> Yes, after that. Passing t_data->t_ctid is just plain wrong.
I agree, obviously.
>> Could we
>> just instruct ExecUpdate() that the caller is ExecOnConflictUpdate(),
>> and so it's appropriate to throw a cardinality violation for its
>> HeapTupleSelfUpdated case? After all, that case already discriminates
>> against updates that are not from the same command in the xact (e.g.
>> due to weird before triggers) by throwing an error [1]. I don't think
>> we need to throw a cardinality violation unless an UPSERT attempts to
>> update a tuple a second time, but not if that occurs within a command
>> that happens to contain an UPSERT not directly doing the updating.
>
> I'm inclined to do the check in ExecOnConflictUpdate() instead - istm
> that's easier to understand.
We're on the same page. I just happen to think we might as well put
the check beside the existing special case check for weird before
triggers -- within ExecUpdate()'s HeapTupleSelfUpdated case. That
avoids an extra HeapTupleSatisfiesUpdate() call for every UPSERT
update. We mostly only call that routine from heapam.c as things are.
But I'm hardly going to insist on that.
I actually think that there is an argument to be made for doing
nothing here, and not allowing a cardinality violation at all. It
doesn't seem too inconsistent to follow the old behavior (just accept
a redundant second update) in the case where you update the row within
an UPSERT after the UPSERT locks the row, but before your UPSERT has a
chance to do its UPDATE of that same row (in other words, something
happened to your tuple in between). I do prefer to do what we've
agreed to, though (whether or not it happens in ExecUpdate()), mostly
because it's closer to the other ExecUpdate() HeapTupleSelfUpdated
special case.
Note the similarity between that case, and this new one.
--
Peter Geoghegan
--
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: pg@heroku.com, andres@anarazel.de, michael.paquier@gmail.com, dean.a.rasheed@gmail.com, tacho@daemonz.org
Subject: Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
In-Reply-To: <CAM3SWZQJcdx0y4UBPYPvf-mfs_SMVBi3zKA2JenGeUtcQDd=cg@mail.gmail.com>
References: <CAA78GVqy1+LisN-8DygekD_Ldfy=BJLarSpjGhytOsgkpMavfQ@mail.gmail.com> <CAB7nPqTjO4decSSm2DxeUrchX10SJ3GfKTddgBH42bvX5k4WjQ@mail.gmail.com> <CAEZATCXxhKui4hCMXGyeWUeo-pTS7r4BwfzjkM7v0sOvLiAETA@mail.gmail.com> <CAB7nPqRioCgcGydOV5h1raa21QvYR3EiJVKY7UVdwHx=-KhcUw@mail.gmail.com> <CAM3SWZR4WOxAbq4=0zy4EPfRw7COufJ8tUqqP1LTypef7kAQPA@mail.gmail.com> <CAM3SWZSiX3_6H149ZH6QF8Y9LuaYgpg1218bbF9_cjvyykaK4w@mail.gmail.com> <20151208144440.GU4934@alap3.anarazel.de> <CAM3SWZSEF00OaCxFQErv_rOVLBtsM9n=xoxknLxs7YxMjSxYuQ@mail.gmail.com> <20151208231220.GG28762@awork2.anarazel.de> <CAM3SWZQJcdx0y4UBPYPvf-mfs_SMVBi3zKA2JenGeUtcQDd=cg@mail.gmail.com>
* 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