postgresql/src/include/commands
Dean Rasheed 5022ff250e Fix concurrent update trigger issues with MERGE in a CTE.
If a MERGE inside a CTE attempts an UPDATE or DELETE on a table with
BEFORE ROW triggers, and a concurrent UPDATE or DELETE happens, the
merge code would fail (crashing in the case of an UPDATE action, and
potentially executing the wrong action for a DELETE action).

This is the same issue that 9321c79c86 attempted to fix, except now
for a MERGE inside a CTE. As noted in 9321c79c86, what needs to happen
is for the trigger code to exit early, returning the TM_Result and
TM_FailureData information to the merge code, if a concurrent
modification is detected, rather than attempting to do an EPQ
recheck. The merge code will then do its own rechecking, and rescan
the action list, potentially executing a different action in light of
the concurrent update. In particular, the trigger code must never call
ExecGetUpdateNewTuple() for MERGE, since that is bound to fail because
MERGE has its own per-action projection information.

Commit 9321c79c86 did this using estate->es_plannedstmt->commandType
in the trigger code to detect that a MERGE was being executed, which
is fine for a plain MERGE command, but does not work for a MERGE
inside a CTE. Fix by passing that information to the trigger code as
an additional parameter passed to ExecBRUpdateTriggers() and
ExecBRDeleteTriggers().

Back-patch as far as v17 only, since MERGE cannot appear inside a CTE
prior to that. Additionally, take care to preserve the trigger ABI in
v17 (though not in v18, which is still in beta).

Bug: #18986
Reported-by: Yaroslav Syrytsia <me@ys.lc>
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/18986-e7a8aac3d339fa47@postgresql.org
Backpatch-through: 17
2025-07-18 09:55:43 +01:00
..
alter.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
async.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
cluster.h Adjust signature of cluster_rel() and its subroutines 2025-01-10 13:09:38 +01:00
collationcmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
comment.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
conversioncmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
copy.h Support multi-line headers in COPY FROM command. 2025-07-03 15:27:26 +09:00
copyapi.h Refactor COPY FROM to use format callback functions. 2025-02-28 10:29:36 -08:00
copyfrom_internal.h Refactor COPY FROM to use format callback functions. 2025-02-28 10:29:36 -08:00
createas.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
dbcommands.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
dbcommands_xlog.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
defrem.h pg_noreturn to replace pg_attribute_noreturn() 2025-03-13 12:37:26 +01:00
discard.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
event_trigger.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
explain.h Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
explain_dr.h Avoid including explain.h in explain_format.h and explain_dr.h 2025-02-28 13:17:29 -05:00
explain_format.h Avoid including explain.h in explain_format.h and explain_dr.h 2025-02-28 13:17:29 -05:00
explain_state.h Add an additional hook for EXPLAIN option validation. 2025-03-20 13:47:55 -04:00
extension.h extension_control_path 2025-03-19 07:03:20 +01:00
lockcmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
matview.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
policy.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
portalcmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
prepare.h Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
proclang.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
progress.h Add cost-based vacuum delay time to progress views. 2025-02-11 16:38:14 -06:00
publicationcmds.h Use correct variable name in publicationcmds.c. 2025-03-18 14:06:51 +05:30
schemacmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
seclabel.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
sequence.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
subscriptioncmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
tablecmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
tablespace.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
trigger.h Fix concurrent update trigger issues with MERGE in a CTE. 2025-07-18 09:55:43 +01:00
typecmds.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
user.h Add new GUC createrole_self_grant. 2023-01-10 12:44:49 -05:00
vacuum.h Rationalize handling of VacuumParams 2025-06-30 15:42:50 +09:00
view.h Update copyright for 2025 2025-01-01 11:21:55 -05:00