Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a6RQz-0004Vk-4B for pgsql-bugs@arkaria.postgresql.org; Tue, 08 Dec 2015 23:12:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a6RQy-0005NW-Mq for pgsql-bugs@arkaria.postgresql.org; Tue, 08 Dec 2015 23:12:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a6RQx-0005N3-S8 for pgsql-bugs@postgresql.org; Tue, 08 Dec 2015 23:12:36 +0000 Received: from mail.anarazel.de ([217.115.131.40]) by makus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1a6RQs-0000Ur-QQ for pgsql-bugs@postgresql.org; Tue, 08 Dec 2015 23:12:34 +0000 Received: by mail.anarazel.de (Postfix, from userid 108) id 80CAC860003; Wed, 9 Dec 2015 00:12:28 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on mail X-Spam-Level: X-Spam-Status: No, score=-5.5 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from anarazel.de (p57B4EE73.dip0.t-ipconnect.de [87.180.238.115]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id 71898860002; Wed, 9 Dec 2015 00:12:21 +0100 (CET) Received: by anarazel.de (Postfix, from userid 1000) id EBF491010E7; Wed, 9 Dec 2015 00:12:20 +0100 (CET) Date: Wed, 9 Dec 2015 00:12:20 +0100 From: Andres Freund To: Peter Geoghegan Cc: Michael Paquier , Dean Rasheed , Stanislav Grozev , PostgreSQL mailing lists Subject: Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Message-ID: <20151208231220.GG28762@awork2.anarazel.de> References: <20151208144440.GU4934@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="lrZ03NoBR/3+SXJZ" Content-Disposition: inline In-Reply-To: X-Pg-Spam-Score: -4.2 (----) 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 --lrZ03NoBR/3+SXJZ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On 2015-12-08 13:17:15 -0800, Peter Geoghegan wrote: > On Tue, Dec 8, 2015 at 6:44 AM, Andres Freund 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 --lrZ03NoBR/3+SXJZ Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Fix-AFTER-UPDATE-trigger-behaviour-in-combination-wi.patch" From e6b21647aac5e0f4eaf5d2c57fb30c1de8088b9f Mon Sep 17 00:00:00 2001 From: Andres Freund 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.c index 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.out index 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.sql index 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 --lrZ03NoBR/3+SXJZ 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 --lrZ03NoBR/3+SXJZ--