diff options
author | Andres Freund <andres@anarazel.de> | 2015-12-10 16:26:45 +0100 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2015-12-10 16:29:26 +0100 |
commit | 84ac126ee728ede5b6370d60dd2b1c299f49ed2f (patch) | |
tree | 912b9096b1c9c17a12a4795b9edef4802dda1198 /src/backend/executor/nodeModifyTable.c | |
parent | e3f4cfc7aa33f40b2b283676c9f0d5bf7ef08ccd (diff) | |
download | postgresql-84ac126ee728ede5b6370d60dd2b1c299f49ed2f.tar.gz postgresql-84ac126ee728ede5b6370d60dd2b1c299f49ed2f.zip |
Fix ON CONFLICT UPDATE bug breaking AFTER UPDATE triggers.
ExecOnConflictUpdate() passed t_ctid of the to-be-updated tuple to
ExecUpdate(). That's problematic primarily because of two reason: First
and foremost t_ctid could point to a different tuple. Secondly, and
that's what triggered the complaint by Stanislav, t_ctid is changed by
heap_update() to point to the new tuple version. The behavior of AFTER
UPDATE triggers was therefore broken, with NEW.* and OLD.* tuples
spuriously identical within AFTER UPDATE triggers.
To fix both issues, pass a pointer to t_self of a on-stack HeapTuple
instead.
Fixing this bug lead to one change in regression tests, which previously
failed due to the first issue mentioned above. There's a reasonable
expectation that test fails, as it updates one row repeatedly within one
INSERT ... ON CONFLICT statement. That is only possible if the second
update is triggered via ON CONFLICT ... SET, ON CONFLICT ... WHERE, or
by a WITH CHECK expression, as those are executed after
ExecOnConflictUpdate() does a visibility check. That could easily be
prohibited, but given it's allowed for plain UPDATEs and a rare corner
case, it doesn't seem worthwhile.
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
Diffstat (limited to 'src/backend/executor/nodeModifyTable.c')
-rw-r--r-- | src/backend/executor/nodeModifyTable.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index dabaea99109..9db4c917435 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -1181,8 +1181,17 @@ ExecOnConflictUpdate(ModifyTableState *mtstate, /* Project the new tuple version */ ExecProject(resultRelInfo->ri_onConflictSetProj, NULL); + /* + * Note that it is possible that the target tuple has been modified in + * this session, after the above heap_lock_tuple. We choose to not error + * out in that case, in line with ExecUpdate's treatment of similar + * cases. This can happen if an UPDATE is triggered from within + * ExecQual(), ExecWithCheckOptions() or ExecProject() above, e.g. by + * selecting from a wCTE in the ON CONFLICT's SET. + */ + /* Execute UPDATE with projection */ - *returning = ExecUpdate(&tuple.t_data->t_ctid, NULL, + *returning = ExecUpdate(&tuple.t_self, NULL, mtstate->mt_conflproj, planSlot, &mtstate->mt_epqstate, mtstate->ps.state, canSetTag); |