From: Andres Freund <andres@anarazel.de>
To: Peter Geoghegan <pg@heroku.com>
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: Wed, 9 Dec 2015 00:12:20 +0100
Message-ID: <20151208231220.GG28762@awork2.anarazel.de> (raw)
In-Reply-To: <CAM3SWZSEF00OaCxFQErv_rOVLBtsM9n=xoxknLxs7YxMjSxYuQ@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>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On 2015-12-08 13:17:15 -0800, Peter Geoghegan wrote:
> On Tue, Dec 8, 2015 at 6:44 AM, Andres Freund <andres@anarazel.de> wrote:
> > It sounds to me like the real fix would be to just use
> > &tuple.t_self. That'll "break" the above regression test. But the reason
> > for that seems to be entirely independent: Namely that in this case the
> > tuple is only modified after the heap_lock_tuple(), in the course of the
> > ExecProject() computing the new tuple version - the SELECT FROM
> > upsert_cte...
>
> I think you're right. I had a feeling that there was some unanswered
> question about that particular regression test.
>
> FWIW, t_ctid could not really point to arbitrary things, because
> heap_lock_tuple() is not asked to follow the update chain, and we
> start from the first phase at the first sign of a conflict within
> ExecOnConflictUpdate() (it succeeds only when it locks the
> *definitive* conflict tuple, with no future tuple version). As you say
> t_ctid could still point to a dead tuple, unfortunately, because that
> doesn't count as a new version, which would result in raising an
> error.
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.
> > > ISTM, that if we really want to protect against UpdatedSelf we need to
> > to do so after ExecQual(), ExecWCE(), and ExecProject(). Each of those
> > can trigger such an update.
>
> 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 think you might be confusing ExecUpdate()'s use of
> HeapTupleSelfUpdated with ExecOnConflictUpdate()'s similar use of
> HeapTupleInvisible (where it's almost the same to the user -- it's the
> "I guess we'll throw a cardinality violation error to reject a second
> attempt at updating the tuple" state there). And, contrariwise,
> ExecUpdate() has HeapTupleInvisible as its own "can't happen" error.
> Of course, these differences are due to the different types of
> snapshots used in each case (dirty snapshot for
> ExecOnConflictUpdate(), MVCC snapshot for ExecUpdate() -- at least
> prior to 9.5/this bug).
I don't think so. Check the attached, very rough, WIP patch.
> What I don't see is why you suggest we need to worry about each case
> (e.g. ExecQual(), ExecWCE(), and ExecProject()) specifically.
I don't mean individually, just after all of those, but before calling ExecUpdate()
> 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.
Greetings,
Andres Freund
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:0001-Fix-AFTER-UPDATE-trigger-behaviour-in-combination-wi.patch (text/x-patch, 4.9K) download
From e6b21647aac5e0f4eaf5d2c57fb30c1de8088b9f Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 9 Dec 2015 00:08:35 +0100
Subject: [PATCH] Fix AFTER UPDATE trigger behaviour in combination with ON
CONFLICT and other things.
Two bugs:
* ctid of on-disk tuple was passed to ExecUpdate
* the error check against repeatedly updating tuples via ON CONFLICT
wasn't ironclad.
Reported-By: Stanislav Grozev
Author: Andres Freund and Peter Geoghegan
Discussion: CAA78GVqy1+LisN-8DygekD_Ldfy=BJLarSpjGhytOsgkpMavfQ@mail.gmail.com
Backpatch: 9.5, where ON CONFLICT was introduced
---
src/backend/executor/nodeModifyTable.c | 14 ++++++++++++--
src/test/regress/expected/triggers.out | 8 ++++----
src/test/regress/sql/triggers.sql | 2 +-
3 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.cindex dabaea9..3cad7a1 100644--- a/src/backend/executor/nodeModifyTable.c+++ b/src/backend/executor/nodeModifyTable.c@@ -1181,9 +1181,19 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
/* Project the new tuple version */
ExecProject(resultRelInfo->ri_onConflictSetProj, NULL);
+ /*+ * Finally recheck whether the row has been updated since the+ * heap_lock_tuple() above. The ExecQual(), ExecWithCheckOptions() and,+ * more likely, ExecProject() could have triggered an update of the row.+ */+ if (HeapTupleSatisfiesUpdate(&tuple, estate->es_output_cid, buffer) == HeapTupleSelfUpdated)+ ereport(ERROR,+ (errcode(ERRCODE_CARDINALITY_VIOLATION),+ errmsg("ON CONFLICT DO UPDATE command cannot affect row a second time"),+ errhint("XXX: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.")));+
/* Execute UPDATE with projection */
- *returning = ExecUpdate(&tuple.t_data->t_ctid, NULL,- mtstate->mt_conflproj, planSlot,+ *returning = ExecUpdate(&tuple.t_self, NULL, mtstate->mt_conflproj, planSlot,
&mtstate->mt_epqstate, mtstate->ps.state,
canSetTag);
diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.outindex dd4a99b..a7bf5dc 100644--- a/src/test/regress/expected/triggers.out+++ b/src/test/regress/expected/triggers.out@@ -1703,7 +1703,7 @@ create function upsert_after_func()
$$
begin
if (TG_OP = 'UPDATE') then
- raise warning 'after update (old): %', new.*::text;+ raise warning 'after update (old): %', old.*::text;
raise warning 'after update (new): %', new.*::text;
elsif (TG_OP = 'INSERT') then
raise warning 'after insert (new): %', new.*::text;
@@ -1724,7 +1724,7 @@ insert into upsert values(3, 'orange') on conflict (key) do update set color = '
WARNING: before insert (new): (3,orange)
WARNING: before update (old): (3,"red trig modified")
WARNING: before update (new): (3,"updated red trig modified")
-WARNING: after update (old): (3,"updated red trig modified")+WARNING: after update (old): (3,"red trig modified")
WARNING: after update (new): (3,"updated red trig modified")
insert into upsert values(4, 'green') on conflict (key) do update set color = 'updated ' || upsert.color;
WARNING: before insert (new): (4,green)
@@ -1734,7 +1734,7 @@ insert into upsert values(5, 'purple') on conflict (key) do update set color = '
WARNING: before insert (new): (5,purple)
WARNING: before update (old): (5,"green trig modified")
WARNING: before update (new): (5,"updated green trig modified")
-WARNING: after update (old): (5,"updated green trig modified")+WARNING: after update (old): (5,"green trig modified")
WARNING: after update (new): (5,"updated green trig modified")
insert into upsert values(6, 'white') on conflict (key) do update set color = 'updated ' || upsert.color;
WARNING: before insert (new): (6,white)
@@ -1744,7 +1744,7 @@ insert into upsert values(7, 'pink') on conflict (key) do update set color = 'up
WARNING: before insert (new): (7,pink)
WARNING: before update (old): (7,"white trig modified")
WARNING: before update (new): (7,"updated white trig modified")
-WARNING: after update (old): (7,"updated white trig modified")+WARNING: after update (old): (7,"white trig modified")
WARNING: after update (new): (7,"updated white trig modified")
insert into upsert values(8, 'yellow') on conflict (key) do update set color = 'updated ' || upsert.color;
WARNING: before insert (new): (8,yellow)
diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sqlindex 9f66702..b6de1b3 100644--- a/src/test/regress/sql/triggers.sql+++ b/src/test/regress/sql/triggers.sql@@ -1215,7 +1215,7 @@ create function upsert_after_func()
$$
begin
if (TG_OP = 'UPDATE') then
- raise warning 'after update (old): %', new.*::text;+ raise warning 'after update (old): %', old.*::text;
raise warning 'after update (new): %', new.*::text;
elsif (TG_OP = 'INSERT') then
raise warning 'after insert (new): %', new.*::text;
--
2.5.0.400.gff86faf.dirty
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: andres@anarazel.de, pg@heroku.com, 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: <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>
* 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