pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedIncorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
24+ messages / 5 participants
[nested] [flat]
* Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
@ 2015-11-30 11:43 Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-10 15:51 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
0 siblings, 2 replies; 24+ messages in thread
From: Stanislav Grozev @ 2015-11-30 11:43 UTC (permalink / raw)
To: pgsql-bugs
Hello,
I'd like to report a bug in the UPDATE trigger invocation when using the
new INSERT ON CONFLICT UPDATE (UPSERT) functionality.
In short, if an UPDATE trigger is invoked by the ON CONFLICT DO UPDATE
clause of an UPSERT statement - it receives the new values in both the OLD
and NEW variables. Whereas if invoked by a normal UPDATE statement - it
correctly gets the respective values in OLD and NEW.
Here's a short test case to reproduce it:
test=# CREATE OR REPLACE FUNCTION public.test_trigger() RETURNS trigger AS
$$
BEGIN
RAISE NOTICE '%:%: old: %, new: %', TG_NAME, TG_OP, OLD, NEW;
IF OLD.value IS DISTINCT FROM NEW.value THEN
RAISE WARNING '%:%: Values are different!', TG_NAME, TG_OP;
ELSE
RAISE NOTICE '%:%: Values are the same', TG_NAME, TG_OP;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
test=# CREATE table test(id serial primary key, value varchar(32));
test=# CREATE TRIGGER test_trigger after UPDATE on test for each row
execute procedure test_trigger();
test=# INSERT INTO test (value) VALUES('initial value');
test=# SELECT * FROM test;
┌────┬───────────────┐
│ id │ value │
├────┼───────────────┤
│ 1 │ initial value │
└────┴───────────────┘
(1 row)
Now, if we do an UPDATE, everything is as expected:
test=# UPDATE test SET value='plain update value' WHERE id=1;
NOTICE: 00000: test_trigger:UPDATE: old: (1,"initial value"), new:
(1,"plain update value")
LOCATION: exec_stmt_raise, pl_exec.c:3216
WARNING: 01000: test_trigger:UPDATE: Values are different!
LOCATION: exec_stmt_raise, pl_exec.c:3216
UPDATE 1
If we do an UPSERT instead, watch how OLD and NEW are the same (NEW):
test=# INSERT INTO test (id, value) VALUES(1, 'upserted value') ON CONFLICT
ON CONSTRAINT test_pkey DO UPDATE SET value='upserted value';
NOTICE: 00000: test_trigger:UPDATE: old: (1,"upserted value"), new:
(1,"upserted value")
LOCATION: exec_stmt_raise, pl_exec.c:3216
NOTICE: 00000: test_trigger:UPDATE: Values are the same
LOCATION: exec_stmt_raise, pl_exec.c:3216
INSERT 0 1
We have traced the problem to be in the
src/backend/executor/nodeModifyTable.c file, more specifically in the
ExecOnConflictUpdate function. The attached simple patch appears to fix the
issue. Now, the UPSERT behaves correctly (at least what we think should be
correct):
test=# INSERT INTO test (id, value) VALUES(1, 'upserted value') ON CONFLICT
ON CONSTRAINT test_pkey DO UPDATE SET value='upserted value';
NOTICE: 00000: test_trigger:UPDATE: old: (1,"initial value"), new:
(1,"upserted value")
LOCATION: exec_stmt_raise, pl_exec.c:3216
WARNING: 01000: test_trigger:UPDATE: Values are different!
LOCATION: exec_stmt_raise, pl_exec.c:3216
INSERT 0 1
We have verified this behaviour with PostgreSQL 9.5 beta1, beta2 and Git
head.
Thanks.
--
-S
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
upsert-update-trigger-fix.patch (application/octet-stream, 605B) download
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index dabaea9..f724681 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -1182,7 +1182,7 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
ExecProject(resultRelInfo->ri_onConflictSetProj, NULL);
/* Execute UPDATE with projection */
- *returning = ExecUpdate(&tuple.t_data->t_ctid, NULL,
+ *returning = ExecUpdate(&tuple.t_data->t_ctid, &tuple,
mtstate->mt_conflproj, planSlot,
&mtstate->mt_epqstate, mtstate->ps.state,
canSetTag);
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
@ 2015-12-03 07:30 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-03 07:33 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
1 sibling, 2 replies; 24+ messages in thread
From: Michael Paquier @ 2015-12-03 07:30 UTC (permalink / raw)
To: Stanislav Grozev <tacho@daemonz.org>; +Cc: pgsql-bugs
On Mon, Nov 30, 2015 at 8:43 PM, Stanislav Grozev wrote:
> If we do an UPSERT instead, watch how OLD and NEW are the same (NEW):
AFAIK, that's the expected behavior. AFTER UPDATE triggers firing for
ON CONFLICT DO UPDATE will see the same NEW and OLD values. Comments
from others?
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
@ 2015-12-03 07:33 ` Stanislav Grozev <tacho@daemonz.org>
1 sibling, 0 replies; 24+ messages in thread
From: Stanislav Grozev @ 2015-12-03 07:33 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs
I fail to see the logic and why would it be useful for an UPDATE trigger to
get the same values. Especially when it would differ from a normal AFTER
UPDATE firing for an UPDATE query?
On Thu, Dec 3, 2015 at 9:30 AM Michael Paquier <michael.paquier@gmail.com>
wrote:
> On Mon, Nov 30, 2015 at 8:43 PM, Stanislav Grozev wrote:
> > If we do an UPSERT instead, watch how OLD and NEW are the same (NEW):
>
> AFAIK, that's the expected behavior. AFTER UPDATE triggers firing for
> ON CONFLICT DO UPDATE will see the same NEW and OLD values. Comments
> from others?
> --
> Michael
>
--
-S
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
@ 2015-12-03 08:19 ` Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 24+ messages in thread
From: Dean Rasheed @ 2015-12-03 08:19 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
On 3 December 2015 at 07:30, Michael Paquier <michael.paquier@gmail.com> wrote:
> On Mon, Nov 30, 2015 at 8:43 PM, Stanislav Grozev wrote:
>> If we do an UPSERT instead, watch how OLD and NEW are the same (NEW):
>
> AFAIK, that's the expected behavior. AFTER UPDATE triggers firing for
> ON CONFLICT DO UPDATE will see the same NEW and OLD values. Comments
> from others?
That's not what I would expect. I would expect it to be consistent
with AFTER triggers firing after a plain UPDATE, and also with BEFORE
triggers for ON CONFLICT DO UPDATE.
Regards,
Dean
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
@ 2015-12-03 12:34 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
0 siblings, 1 reply; 24+ messages in thread
From: Michael Paquier @ 2015-12-03 12:34 UTC (permalink / raw)
To: Dean Rasheed <dean.a.rasheed@gmail.com>; +Cc: Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Andres Freund <andres@anarazel.de>; pg@heroku.com
On Thu, Dec 3, 2015 at 5:19 PM, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
> On 3 December 2015 at 07:30, Michael Paquier <michael.paquier@gmail.com> wrote:
>> On Mon, Nov 30, 2015 at 8:43 PM, Stanislav Grozev wrote:
>>> If we do an UPSERT instead, watch how OLD and NEW are the same (NEW):
>>
>> AFAIK, that's the expected behavior. AFTER UPDATE triggers firing for
>> ON CONFLICT DO UPDATE will see the same NEW and OLD values. Comments
>> from others?
>
> That's not what I would expect. I would expect it to be consistent
> with AFTER triggers firing after a plain UPDATE, and also with BEFORE
> triggers for ON CONFLICT DO UPDATE.
triggers.out is telling that this may be intended to work this way:
WARNING: after update (old): (5,"updated green trig modified")
WARNING: after update (new): (5,"updated green trig modified")
Though I agree that it is not instinctive...
Btw, the patch provided fails on an assertion with regression tests:
2426 /* Determine lock mode to use */
2427 lockmode = ExecUpdateLockMode(estate, relinfo);
2428
-> 2429 Assert(HeapTupleIsValid(fdw_trigtuple) ^
ItemPointerIsValid(tupleid));
2430 if (fdw_trigtuple == NULL)
Peter, Andres, thoughts?
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
@ 2015-12-03 21:10 ` Peter Geoghegan <pg@heroku.com>
2015-12-03 23:53 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
0 siblings, 2 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-03 21:10 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Andres Freund <andres@anarazel.de>
On Thu, Dec 3, 2015 at 4:34 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> triggers.out is telling that this may be intended to work this way:
> WARNING: after update (old): (5,"updated green trig modified")
> WARNING: after update (new): (5,"updated green trig modified")
> Though I agree that it is not instinctive...
>
> Btw, the patch provided fails on an assertion with regression tests:
> 2426 /* Determine lock mode to use */
> 2427 lockmode = ExecUpdateLockMode(estate, relinfo);
> 2428
> -> 2429 Assert(HeapTupleIsValid(fdw_trigtuple) ^
> ItemPointerIsValid(tupleid));
> 2430 if (fdw_trigtuple == NULL)
>
> Peter, Andres, thoughts?
I agree with Stanislav that the behavior is wrong. The fix is more
complicated, though. As it says at the top of ExecUpdate():
* When updating a table, tupleid identifies the tuple to
* update and oldtuple is NULL. When updating a view, oldtuple
Since the ON CONFLICT DO UPDATE variant is always updating a table, it
is always supposed to pass oldtuple = NULL. The problem is that unlike
a regular update, it cannot reconstruct the original/target tuple for
an AFTER UPDATE trigger *correctly* -- the GetTupleForTrigger() logic
depends on executor state, which is not consistent with a regular
update.
I'll need to think about a fix.
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-03 23:53 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-03 23:54 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
1 sibling, 1 reply; 24+ messages in thread
From: Michael Paquier @ 2015-12-03 23:53 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Andres Freund <andres@anarazel.de>
On Fri, Dec 4, 2015 at 6:10 AM, Peter Geoghegan <pg@heroku.com> wrote:
> On Thu, Dec 3, 2015 at 4:34 AM, Michael Paquier
> <michael.paquier@gmail.com> wrote:
>> triggers.out is telling that this may be intended to work this way:
>> WARNING: after update (old): (5,"updated green trig modified")
>> WARNING: after update (new): (5,"updated green trig modified")
>> Though I agree that it is not instinctive...
>>
>> Btw, the patch provided fails on an assertion with regression tests:
>> 2426 /* Determine lock mode to use */
>> 2427 lockmode = ExecUpdateLockMode(estate, relinfo);
>> 2428
>> -> 2429 Assert(HeapTupleIsValid(fdw_trigtuple) ^
>> ItemPointerIsValid(tupleid));
>> 2430 if (fdw_trigtuple == NULL)
>>
>> Peter, Andres, thoughts?
>
> I agree with Stanislav that the behavior is wrong. The fix is more
> complicated, though. As it says at the top of ExecUpdate():
>
> * When updating a table, tupleid identifies the tuple to
> * update and oldtuple is NULL. When updating a view, oldtuple
>
> Since the ON CONFLICT DO UPDATE variant is always updating a table, it
> is always supposed to pass oldtuple = NULL. The problem is that unlike
> a regular update, it cannot reconstruct the original/target tuple for
> an AFTER UPDATE trigger *correctly* -- the GetTupleForTrigger() logic
> depends on executor state, which is not consistent with a regular
> update.
>
> I'll need to think about a fix.
I am adding that to the list of open items for 9.5.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-03 23:53 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
@ 2015-12-03 23:54 ` Peter Geoghegan <pg@heroku.com>
0 siblings, 0 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-03 23:54 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Andres Freund <andres@anarazel.de>
On Thu, Dec 3, 2015 at 3:53 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> I am adding that to the list of open items for 9.5.
I did already.
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-04 01:34 ` Peter Geoghegan <pg@heroku.com>
2015-12-04 05:09 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-04 09:31 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
1 sibling, 3 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-04 01:34 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Andres Freund <andres@anarazel.de>
On Thu, Dec 3, 2015 at 1:10 PM, Peter Geoghegan <pg@heroku.com> wrote:
> I'll need to think about a fix.
The problem was with the pointer we pass to ExecUpdate().
It's a pointer to the target tuple in shared memory. So the field
"tuple.t_data->t_ctid" within ExecOnConflictUpdate() starts out
pointing to an ItemPointerData with the correct ctid (when it
initially points to the current/target tuple, since as an
about-to-be-upserted tuple the "t_ctid" field must be a pointer to the
self-same tuple). Then, it is modified in-place in shared memory by
heap_update(), within its critical section.
The fix is to take a deep copy (pass a pointer to an ItemPointerData
on the stack), as in the attached. I've also fixed up the tests, which
should have caught this, but didn't. Mea culpa.
Many thanks to Stanislav for the report! While I didn't adopt his
suggestion, he certainly almost had it right.
--
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
Attachments:
0001-Fix-stray-pointer-bug-in-ExecOnConflictUpdate.patch (text/x-patch, 5.0K) download
From a46d5d7b984510e1d49050ff7b49be692155aab5 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <peter.geoghegan86@gmail.com>
Date: Thu, 3 Dec 2015 17:07:47 -0800
Subject: [PATCH] Fix stray pointer bug in ExecOnConflictUpdate()
ExecOnConflictUpdate() neglected to ensure that the ItemPointer it
passed to ExecUpdate() did not point anywhere that could be modified
during the execution of ExecUpdate(). The behavior of AFTER UPDATE
triggers was therefore broken, as heap_update() modified the current/new
ItemPointerData in the tuple in shared memory (t_ctid) before AFTER
UPDATE triggers fired. NEW.* and OLD.* tuples were always spuriously
identical within AFTER UPDATE triggers.
To fix, follow the example of ExecInitModifyTable(), and take a deep
copy. Only pass a pointer to the deep copy to ExecUpdate().
Reported by Stanislav Grozev, although this is not the exact fix that he
proposed.
---
src/backend/executor/nodeModifyTable.c | 6 ++++--
src/test/regress/expected/triggers.out | 8 ++++----
src/test/regress/sql/triggers.sql | 2 +-
3 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index dabaea9..348f913 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -1032,6 +1032,7 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
Relation relation = resultRelInfo->ri_RelationDesc;
List *onConflictSetWhere = resultRelInfo->ri_onConflictSetWhere;
HeapTupleData tuple;
+ ItemPointerData tuple_ctid;
HeapUpdateFailureData hufd;
LockTupleMode lockmode;
HTSU_Result test;
@@ -1181,9 +1182,10 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
/* Project the new tuple version */
ExecProject(resultRelInfo->ri_onConflictSetProj, NULL);
+ /* Pass a deep copy of the tupleid. ExecUpdate() modifies the original. */
+ tuple_ctid = tuple.t_data->t_ctid;
/* Execute UPDATE with projection */
- *returning = ExecUpdate(&tuple.t_data->t_ctid, NULL,
- mtstate->mt_conflproj, planSlot,
+ *returning = ExecUpdate(&tuple_ctid, 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;
--
1.9.1
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-04 05:09 ` Michael Paquier <michael.paquier@gmail.com>
2 siblings, 0 replies; 24+ messages in thread
From: Michael Paquier @ 2015-12-04 05:09 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Andres Freund <andres@anarazel.de>
On Fri, Dec 4, 2015 at 10:34 AM, Peter Geoghegan <pg@heroku.com> wrote:
> On Thu, Dec 3, 2015 at 1:10 PM, Peter Geoghegan <pg@heroku.com> wrote:
>> I'll need to think about a fix.
>
> The problem was with the pointer we pass to ExecUpdate().
>
> It's a pointer to the target tuple in shared memory. So the field
> "tuple.t_data->t_ctid" within ExecOnConflictUpdate() starts out
> pointing to an ItemPointerData with the correct ctid (when it
> initially points to the current/target tuple, since as an
> about-to-be-upserted tuple the "t_ctid" field must be a pointer to the
> self-same tuple). Then, it is modified in-place in shared memory by
> heap_update(), within its critical section.
>
> The fix is to take a deep copy (pass a pointer to an ItemPointerData
> on the stack), as in the attached. I've also fixed up the tests, which
> should have caught this, but didn't. Mea culpa.
>
> Many thanks to Stanislav for the report! While I didn't adopt his
> suggestion, he certainly almost had it right.
Cool, that looks right and there are no assertion problems. In the
case of a plain UPDATE the old tuple referenced in both BEFORE/AFTER
triggers is the same, so things are indeed consistent this way
(attached is simple test case I played with to check manually the
patch).
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
upsert.sql (application/octet-stream, 878B) download
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-04 09:31 ` Stanislav Grozev <tacho@daemonz.org>
2 siblings, 0 replies; 24+ messages in thread
From: Stanislav Grozev @ 2015-12-04 09:31 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; Michael Paquier <michael.paquier@gmail.com>; +Cc: Dean Rasheed <dean.a.rasheed@gmail.com>; pgsql-bugs; Andres Freund <andres@anarazel.de>
Peter, thank you very much. We have adopted your patch in our test
environment and I can confirm it works as expected.
Cheers.
On Fri, Dec 4, 2015 at 3:34 AM Peter Geoghegan <pg@heroku.com> wrote:
> On Thu, Dec 3, 2015 at 1:10 PM, Peter Geoghegan <pg@heroku.com> wrote:
> > I'll need to think about a fix.
>
> The problem was with the pointer we pass to ExecUpdate().
>
> It's a pointer to the target tuple in shared memory. So the field
> "tuple.t_data->t_ctid" within ExecOnConflictUpdate() starts out
> pointing to an ItemPointerData with the correct ctid (when it
> initially points to the current/target tuple, since as an
> about-to-be-upserted tuple the "t_ctid" field must be a pointer to the
> self-same tuple). Then, it is modified in-place in shared memory by
> heap_update(), within its critical section.
>
> The fix is to take a deep copy (pass a pointer to an ItemPointerData
> on the stack), as in the attached. I've also fixed up the tests, which
> should have caught this, but didn't. Mea culpa.
>
> Many thanks to Stanislav for the report! While I didn't adopt his
> suggestion, he certainly almost had it right.
>
> --
> Peter Geoghegan
>
--
-S
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-08 14:44 ` Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2 siblings, 1 reply; 24+ messages in thread
From: Andres Freund @ 2015-12-08 14:44 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
On 2015-12-03 17:34:12 -0800, Peter Geoghegan wrote:
> On Thu, Dec 3, 2015 at 1:10 PM, Peter Geoghegan <pg@heroku.com> wrote:
> > I'll need to think about a fix.
>
> The problem was with the pointer we pass to ExecUpdate().
>
> It's a pointer to the target tuple in shared memory. So the field
> "tuple.t_data->t_ctid" within ExecOnConflictUpdate() starts out
> pointing to an ItemPointerData with the correct ctid (when it
> initially points to the current/target tuple, since as an
> about-to-be-upserted tuple the "t_ctid" field must be a pointer to the
> self-same tuple). Then, it is modified in-place in shared memory by
> heap_update(), within its critical section.
Hm. But why it correct to use t_ctid in the first place? I mean if there
was a previously-failed UPDATE t_ctid will point somewhere meaningless?
Shouldn't we use tuple->t_self, or conflictTid here?
Doing that reveals one change in the regression tests. Namely
-- This shows an attempt to update an invisible row, which should really be
-- reported as a cardinality violation, but it doesn't seem worth fixing:
WITH simpletup AS (
SELECT 2 k, 'Green' v),
upsert_cte AS (
INSERT INTO z VALUES(2, 'Blue') ON CONFLICT (k) DO
UPDATE SET (k, v) = (SELECT k, v FROM simpletup WHERE simpletup.k = z.k)
RETURNING k, v)
INSERT INTO z VALUES(2, 'Red') ON CONFLICT (k) DO
UPDATE SET (k, v) = (SELECT k, v FROM upsert_cte WHERE upsert_cte.k = z.k)
RETURNING k, v;
out of with.sql doesn't fail anymore, and instead returns no rows.
At that point in the regression tests there's a conflicting tuple for
'2'. Rewriting the statement to
WITH simpletup AS (
SELECT 2 k, 'Green' v),
upsert_cte AS (
UPDATE z SET (k, v) = (SELECT k, v FROM simpletup WHERE simpletup.k = z.k)
WHERE k = 2
RETURNING k, v)
UPDATE z SET (k, v) = (SELECT k, v FROM upsert_cte WHERE upsert_cte.k = z.k)
WHERE k = 2
RETURNING k, v;
does *not* error out. That's because it hits the HeapTupleSelfUpdated
block in ExecUpdate(). So, working as designed.
The reason the upsert variant gets an error with master/your patch is
solely that t_ctid already points to the new version of the tuple -
which surely is wrong! t_ctid could point to nearly arbitrary things
here (if the previous target for t_ctid was hot pruned and then replaced
with new contents), unless I miss something.
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...
Nasty.
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.
Am I missing something major here?
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-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
@ 2015-12-08 21:17 ` Peter Geoghegan <pg@heroku.com>
2015-12-08 21:25 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
0 siblings, 2 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-08 21:17 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
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. Maybe we should revisit the idea of making jjanes_upsert do
fault injection. After all, the whole origin of that tool is Jeff's
crash recovery tester, which artificially simulated torn pages, went
through recovery, and reconciled the state of the database after
recovery with a known-good state that the tool kept track of.
> 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?
One tricky point here is that things are different between
ExecOnConflictUpdate() and ExecUpdate(). The return value
HeapTupleSelfUpdated is a "can't happen" state within
ExecOnConflictUpdate(), for reasons that I think can be well isolated,
and understood relatively easily (see comments), whereas
HeapTupleSelfUpdated is the "I guess we'll just ignore a second
attempt to update tuple" state within ExecUpdate().
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).
The reason that the with.sql regression test fails when you change the
code to pass &tuple.t_self to ExecUpdate() is that, as you say, we get
the historic ExecUpdate()/plain update tolerance of would-be
"cardinality violations" (the ExecUpdate() HeapTupleSelfUpdated case).
What I don't see is why you suggest we need to worry about each case
(e.g. ExecQual(), ExecWCE(), and ExecProject()) specifically. 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.
[1] Commit 6868ed74
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-08 21:25 ` Peter Geoghegan <pg@heroku.com>
1 sibling, 0 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-08 21:25 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
On Tue, Dec 8, 2015 at 1:17 PM, Peter Geoghegan <pg@heroku.com> wrote:
> 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).
Note that it's possible for ExecOnConflictUpdate() to never see a
HeapTupleSelfUpdated case (it is, as I said, a "can't happen"
condition), and yet for its ExecUpdate call to see a
HeapTupleSelfUpdated case with odd queries like the with.sql test
Andres highlighted.
This is only because, as Andres said, ExecProject() and so on can
change things. But things cannot change between finding a conflict TID
in the first stage, and returning from heap_lock_tuple() as part of
the second, such that HeapTupleSelfUpdated is seen as the
heap_lock_tuple() return value (within ExecOnConflictUpdate()). There
is no contradiction.
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-08 23:12 ` Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
1 sibling, 1 reply; 24+ messages in thread
From: Andres Freund @ 2015-12-08 23:12 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-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-bugs
Attachments:
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.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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
@ 2015-12-08 23:32 ` Peter Geoghegan <pg@heroku.com>
2015-12-08 23:36 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:42 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-09 05:37 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-09 21:43 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
0 siblings, 4 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-08 23:32 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-08 23:36 ` Peter Geoghegan <pg@heroku.com>
3 siblings, 0 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-08 23:36 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
On Tue, Dec 8, 2015 at 3:32 PM, Peter Geoghegan <pg@heroku.com> wrote:
> Note the similarity between that case, and this new one.
It might be best if this new ereport() site doesn't use errcode
ERRCODE_CARDINALITY_VIOLATION at all. Pretty sure it shouldn't be
ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION either, though.
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-08 23:42 ` Peter Geoghegan <pg@heroku.com>
3 siblings, 0 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-08 23:42 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
On Tue, Dec 8, 2015 at 3:32 PM, Peter Geoghegan <pg@heroku.com> wrote:
> 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.
It would also be nice to "Assert(!isOnConflict)" within the
HeapTupleUpdated case within ExecUpdate(), if only to document that
that's not expected or possible. Adding a new isOnConflict argument to
ExecUpdate() (so that it can potentially raise an error to deal with
this case) also makes this possible.
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-09 05:37 ` Peter Geoghegan <pg@heroku.com>
3 siblings, 0 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-09 05:37 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs
On Tue, Dec 8, 2015 at 3:32 PM, Peter Geoghegan <pg@heroku.com> wrote:
> 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.
Attached is a worked out patch, with the small differences I went
into. Take from it what you want.
--
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
Attachments:
0001-Fix-AFTER-UPDATE-trigger-behavior.patch (text/x-patch, 9.2K) download
From 9ae5a4552641b4e0438f20c85c8cb586ab8e1f64 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <peter.geoghegan86@gmail.com>
Date: Thu, 3 Dec 2015 17:07:47 -0800
Subject: [PATCH] Fix AFTER UPDATE trigger behavior.
ExecOnConflictUpdate() neglected to ensure that the ItemPointer it
passed to ExecUpdate() did not point anywhere that could be modified
during the execution of ExecUpdate(). The behavior of AFTER UPDATE
triggers was therefore broken, as heap_update() modified the current/new
ItemPointerData in the tuple in shared memory (t_ctid) before AFTER
UPDATE triggers fired. NEW.* and OLD.* tuples were always spuriously
identical within AFTER UPDATE triggers.
As a further matter, passing a pointer to t_ctid was broken even in the
absence of AFTER triggers, as t_ctid does not reliably indicate the TID
that was locked (e.g. due to an update made by an aborted transaction).
To fix both issues, pass a pointer to t_self instead.
Finally, forbid updating a tuple within ExecUpdate() when it was already
updated from the predicate or targetlist evaluation of ON CONFLICT DO
UPDATE (or perhaps elsewhere within the command). This is similar to,
but distinct from a cardinality violation.
Reported-By: Stanislav Grozev
Author: Andres Freund and Peter Geoghegan
---
src/backend/executor/nodeModifyTable.c | 33 ++++++++++++++++++++++++++++-----
src/test/regress/expected/triggers.out | 8 ++++----
src/test/regress/expected/with.out | 7 ++++---
src/test/regress/sql/triggers.sql | 2 +-
src/test/regress/sql/with.sql | 5 +++--
5 files changed, 40 insertions(+), 15 deletions(-)
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index dabaea9..8e2178b 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -769,6 +769,7 @@ ExecUpdate(ItemPointer tupleid,
TupleTableSlot *planSlot,
EPQState *epqstate,
EState *estate,
+ bool onConflict,
bool canSetTag)
{
HeapTuple tuple;
@@ -919,6 +920,27 @@ lreplace:;
errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
+ /*
+ * It's possible for ON CONFLICT callers to update their target
+ * tuple as part of expression evaluation after it is locked,
+ * but before calling here. This may happen with a
+ * conventional UPDATE or DELETE, before control reaches here.
+ * Since the tuple was locked specifically to ensure the call
+ * here updates without any further conflicts, we throw an
+ * error.
+ *
+ * This is similar to an ON CONFLICT cardinality violation;
+ * it's also not clear what the final value of the tuple ought
+ * to be here. We distinguish this case because it's not
+ * associated with multiple rows that duplicate each other (in
+ * terms of attributes constrained by an arbiter index) being
+ * proposed for insertion by the same INSERT statement.
+ */
+ if (onConflict)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_RECURSION),
+ errmsg("tuple locked for update by ON CONFLICT DO UPDATE was modified early")));
+
/* Else, already updated by self; nothing to do */
return NULL;
@@ -926,6 +948,7 @@ lreplace:;
break;
case HeapTupleUpdated:
+ Assert(!onConflict);
if (IsolationUsesXactSnapshot())
ereport(ERROR,
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
@@ -1182,10 +1205,9 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
ExecProject(resultRelInfo->ri_onConflictSetProj, NULL);
/* Execute UPDATE with projection */
- *returning = ExecUpdate(&tuple.t_data->t_ctid, NULL,
- mtstate->mt_conflproj, planSlot,
- &mtstate->mt_epqstate, mtstate->ps.state,
- canSetTag);
+ *returning = ExecUpdate(&tuple.t_self, NULL, mtstate->mt_conflproj,
+ planSlot, &mtstate->mt_epqstate, mtstate->ps.state,
+ true, canSetTag);
ReleaseBuffer(buffer);
return true;
@@ -1433,7 +1455,8 @@ ExecModifyTable(ModifyTableState *node)
break;
case CMD_UPDATE:
slot = ExecUpdate(tupleid, oldtuple, slot, planSlot,
- &node->mt_epqstate, estate, node->canSetTag);
+ &node->mt_epqstate, estate, false,
+ node->canSetTag);
break;
case CMD_DELETE:
slot = ExecDelete(tupleid, oldtuple, planSlot,
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/expected/with.out b/src/test/regress/expected/with.out
index 2c9226c..d9c7b3e 100644
--- a/src/test/regress/expected/with.out
+++ b/src/test/regress/expected/with.out
@@ -1875,8 +1875,9 @@ ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 'a' L
WITH aa AS (SELECT 1 a, 2 b)
INSERT INTO z VALUES(1, (SELECT b || ' insert' FROM aa WHERE a = 1 ))
ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 1 LIMIT 1);
--- This shows an attempt to update an invisible row, which should really be
--- reported as a cardinality violation, but it doesn't seem worth fixing:
+-- Attempt to update a row more than once following locking a row to update
+-- from ON CONFLICT DO UPDATE. This is rejected, much like a cardinality
+-- violation.
WITH simpletup AS (
SELECT 2 k, 'Green' v),
upsert_cte AS (
@@ -1886,7 +1887,7 @@ upsert_cte AS (
INSERT INTO z VALUES(2, 'Red') ON CONFLICT (k) DO
UPDATE SET (k, v) = (SELECT k, v FROM upsert_cte WHERE upsert_cte.k = z.k)
RETURNING k, v;
-ERROR: attempted to update invisible tuple
+ERROR: tuple locked for update by ON CONFLICT DO UPDATE was modified early
DROP TABLE z;
-- check that run to completion happens in proper ordering
TRUNCATE TABLE y;
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;
diff --git a/src/test/regress/sql/with.sql b/src/test/regress/sql/with.sql
index 3fd55f9..71dbce5 100644
--- a/src/test/regress/sql/with.sql
+++ b/src/test/regress/sql/with.sql
@@ -838,8 +838,9 @@ WITH aa AS (SELECT 1 a, 2 b)
INSERT INTO z VALUES(1, (SELECT b || ' insert' FROM aa WHERE a = 1 ))
ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 1 LIMIT 1);
--- This shows an attempt to update an invisible row, which should really be
--- reported as a cardinality violation, but it doesn't seem worth fixing:
+-- Attempt to update a row more than once following locking a row to update
+-- from ON CONFLICT DO UPDATE. This is rejected, much like a cardinality
+-- violation.
WITH simpletup AS (
SELECT 2 k, 'Green' v),
upsert_cte AS (
--
1.9.1
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-09 21:43 ` Andres Freund <andres@anarazel.de>
2015-12-09 21:52 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-09 22:36 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
3 siblings, 2 replies; 24+ messages in thread
From: Andres Freund @ 2015-12-09 21:43 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Robert Haas <robertmhaas@gmail.com>
On 2015-12-08 15:32:03 -0800, Peter Geoghegan wrote:
> I actually think that there is an argument to be made for doing
> nothing here, and not allowing a cardinality violation at all.
Works for me. I'll add a short comment before the ExecUpdate() detailing
that the row could, in rather uncommon cases, be updated by that
point. Adding an extra parameter to ExecUpdate() + additional concerns
to it's already nontrivial HTSV codepath doesn't seem worth it to
me.
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-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-09 21:43 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
@ 2015-12-09 21:52 ` Peter Geoghegan <pg@heroku.com>
2015-12-09 21:55 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
1 sibling, 1 reply; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-09 21:52 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Robert Haas <robertmhaas@gmail.com>
On Wed, Dec 9, 2015 at 1:43 PM, Andres Freund <andres@anarazel.de> wrote:
> Works for me. I'll add a short comment before the ExecUpdate() detailing
> that the row could, in rather uncommon cases, be updated by that
> point. Adding an extra parameter to ExecUpdate() + additional concerns
> to it's already nontrivial HTSV codepath doesn't seem worth it to
> me.
HeapTupleSatisfiesVacuum? What of it?
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-09 21:43 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-09 21:52 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
@ 2015-12-09 21:55 ` Andres Freund <andres@anarazel.de>
0 siblings, 0 replies; 24+ messages in thread
From: Andres Freund @ 2015-12-09 21:55 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Robert Haas <robertmhaas@gmail.com>
On 2015-12-09 13:52:35 -0800, Peter Geoghegan wrote:
> On Wed, Dec 9, 2015 at 1:43 PM, Andres Freund <andres@anarazel.de> wrote:
> > Works for me. I'll add a short comment before the ExecUpdate() detailing
> > that the row could, in rather uncommon cases, be updated by that
> > point. Adding an extra parameter to ExecUpdate() + additional concerns
> > to it's already nontrivial HTSV codepath doesn't seem worth it to
> > me.
>
> HeapTupleSatisfiesVacuum? What of it?
Err, HTSU, via heap_update()'s result.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 08:19 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 14:44 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Peter Geoghegan <pg@heroku.com>
2015-12-09 21:43 ` Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Andres Freund <andres@anarazel.de>
@ 2015-12-09 22:36 ` Peter Geoghegan <pg@heroku.com>
1 sibling, 0 replies; 24+ messages in thread
From: Peter Geoghegan @ 2015-12-09 22:36 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Dean Rasheed <dean.a.rasheed@gmail.com>; Stanislav Grozev <tacho@daemonz.org>; pgsql-bugs; Robert Haas <robertmhaas@gmail.com>
On Wed, Dec 9, 2015 at 1:43 PM, Andres Freund <andres@anarazel.de> wrote:
> On 2015-12-08 15:32:03 -0800, Peter Geoghegan wrote:
>> I actually think that there is an argument to be made for doing
>> nothing here, and not allowing a cardinality violation at all.
>
> Works for me. I'll add a short comment before the ExecUpdate() detailing
> that the row could, in rather uncommon cases, be updated by that
> point. Adding an extra parameter to ExecUpdate() + additional concerns
> to it's already nontrivial HTSV codepath doesn't seem worth it to
> me.
I would prefer to throw an error (not a cardinality violation error),
since there is a critical change between locking the tuple, and the
corresponding DO UPDATE call to ExecUpdate().
I'm not going to insist on that, though (just as I didn't insist on
the exact style of the fix). I only insist that this new
HealTupleSatisfiesSelf-in-ExecUpdate() case should not be treated as a
cardinality violation specifically, and should be separately
acknowledge at some level.
--
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
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
@ 2015-12-10 15:51 ` Andres Freund <andres@anarazel.de>
1 sibling, 0 replies; 24+ messages in thread
From: Andres Freund @ 2015-12-10 15:51 UTC (permalink / raw)
To: Stanislav Grozev <tacho@daemonz.org>; Peter Geoghegan <pg@heroku.com>; +Cc: pgsql-bugs
Hi,
On 2015-11-30 11:43:12 +0000, Stanislav Grozev wrote:
> I'd like to report a bug in the UPDATE trigger invocation when using the
> new INSERT ON CONFLICT UPDATE (UPSERT) functionality.
>
> In short, if an UPDATE trigger is invoked by the ON CONFLICT DO UPDATE
> clause of an UPSERT statement - it receives the new values in both the OLD
> and NEW variables. Whereas if invoked by a normal UPDATE statement - it
> correctly gets the respective values in OLD and NEW.
I've pushed a fix for this. Thanks for the report!
- Andres
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 24+ messages in thread
end of thread, other threads:[~2015-12-10 15:51 UTC | newest]
Thread overview: 24+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-11-30 11:43 Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. Stanislav Grozev <tacho@daemonz.org>
2015-12-03 07:30 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-03 07:33 ` Stanislav Grozev <tacho@daemonz.org>
2015-12-03 08:19 ` Dean Rasheed <dean.a.rasheed@gmail.com>
2015-12-03 12:34 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-03 21:10 ` Peter Geoghegan <pg@heroku.com>
2015-12-03 23:53 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-03 23:54 ` Peter Geoghegan <pg@heroku.com>
2015-12-04 01:34 ` Peter Geoghegan <pg@heroku.com>
2015-12-04 05:09 ` Michael Paquier <michael.paquier@gmail.com>
2015-12-04 09:31 ` Stanislav Grozev <tacho@daemonz.org>
2015-12-08 14:44 ` Andres Freund <andres@anarazel.de>
2015-12-08 21:17 ` Peter Geoghegan <pg@heroku.com>
2015-12-08 21:25 ` Peter Geoghegan <pg@heroku.com>
2015-12-08 23:12 ` Andres Freund <andres@anarazel.de>
2015-12-08 23:32 ` Peter Geoghegan <pg@heroku.com>
2015-12-08 23:36 ` Peter Geoghegan <pg@heroku.com>
2015-12-08 23:42 ` Peter Geoghegan <pg@heroku.com>
2015-12-09 05:37 ` Peter Geoghegan <pg@heroku.com>
2015-12-09 21:43 ` Andres Freund <andres@anarazel.de>
2015-12-09 21:52 ` Peter Geoghegan <pg@heroku.com>
2015-12-09 21:55 ` Andres Freund <andres@anarazel.de>
2015-12-09 22:36 ` Peter Geoghegan <pg@heroku.com>
2015-12-10 15:51 ` Andres Freund <andres@anarazel.de>
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