MERGE loses UPDATE transition rows when moving rows between partitions
Hi,
While working on the fix for MERGE on a partitioned table with a
subplan in the UPDATE action, I noticed that the bug dated back to
the original MERGE implementation. That prompted me to use an LLM
to look for similar omissions in the MERGE executor code. It
identified the following issue, which I reproduced.
CREATE TABLE mx_t (i int, v int) PARTITION BY LIST (i);
CREATE TABLE mx_t1 PARTITION OF mx_t FOR VALUES IN (1);
CREATE TABLE mx_t2 PARTITION OF mx_t FOR VALUES IN (2);
INSERT INTO mx_t VALUES (2, 20);
CREATE FUNCTION mx_dump_update() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
RAISE NOTICE 'old: %, new: %',
(SELECT string_agg(ot::text, ', ') FROM ot),
(SELECT string_agg(nt::text, ', ') FROM nt);
RETURN NULL;
END;
$$;
CREATE TRIGGER mx_upd AFTER UPDATE ON mx_t
REFERENCING OLD TABLE AS ot NEW TABLE AS nt
FOR EACH STATEMENT EXECUTE FUNCTION mx_dump_update();
MERGE INTO mx_t USING (VALUES (2)) s(i) ON mx_t.i = s.i
WHEN MATCHED THEN UPDATE SET i = 1, v = 999;
The MERGE updates one row, but the UPDATE trigger reports both
transition tables as empty. A plain UPDATE mx_t SET i = 1, v = 999
WHERE i = 2, run separately on the same initial data, reports
(2,20) in the old table and (1,999) in the new table.
A cross-partition UPDATE moves the row via a DELETE and an INSERT.
The transition capture code in ExecDeleteEpilogue() and ExecInsert()
checks for CMD_UPDATE, but the statement operation is CMD_MERGE
when a MERGE action performs the move. As a result, the row versions
are omitted from the UPDATE transition tables. If the MERGE also has
DELETE and INSERT actions with transition tables, the moved row
versions can be captured in those tables instead.
The attached patch recognizes both halves of the move as part of
the UPDATE action. It also keeps moved rows out of the DELETE and
INSERT transition tables when no UPDATE transition table is requested.
The patch includes regression tests for both cases.
--
Thanks,
Tender Wang
Hi Tender,
Thanks for the patch. Michael Ries reported the same thing on
pgsql-bugs [1]/messages/by-id/CAKWDHLgETF86qEW40HcEzMgn5DdmS6yR-nSYwemXF7zFGrU+sQ@mail.gmail.com. It is a regression from a4f7d33a904, so I think it
should go back to 15.
v1 no longer applies after 398eb35f001, so v2 is a rebase. Also
include all the backport versions.
[1]: /messages/by-id/CAKWDHLgETF86qEW40HcEzMgn5DdmS6yR-nSYwemXF7zFGrU+sQ@mail.gmail.com
/messages/by-id/CAKWDHLgETF86qEW40HcEzMgn5DdmS6yR-nSYwemXF7zFGrU+sQ@mail.gmail.com
Thanks,
Shihao
Attachments:
t254572_2v2-0001-Fix-transition-tables-for-cross-partition-MERGE-u.patchapplication/octet-stream; name=v2-0001-Fix-transition-tables-for-cross-partition-MERGE-u.patchDownload+85-29
nocfbot-v2-REL_17_19-0001-Fix-transition-tables-for-cross-partition-MERGE-upda.patchapplication/octet-stream; name=nocfbot-v2-REL_17_19-0001-Fix-transition-tables-for-cross-partition-MERGE-upda.patchDownload+84-28
nocfbot-v2-REL_15_16-0001-Fix-transition-tables-for-cross-partition-MERGE-upda.patchapplication/octet-stream; name=nocfbot-v2-REL_15_16-0001-Fix-transition-tables-for-cross-partition-MERGE-upda.patchDownload+84-28
Hi Shihao,
shihao zhong <zhong950419@gmail.com> 于2026年11月10日周二 11:51写道:
Thanks for the patch. Michael Ries reported the same thing on
pgsql-bugs [1]. It is a regression from a4f7d33a904, so I think it
should go back to 15.v1 no longer applies after 398eb35f001, so v2 is a rebase. Also
include all the backport versions.[1] /messages/by-id/CAKWDHLgETF86qEW40HcEzMgn5DdmS6yR-nSYwemXF7zFGrU+sQ@mail.gmail.com
Thanks for rebasing the patch, preparing the backport versions, and
identifying the commit responsible.
Since this is a regression from a4f7d33a904, I've CC'd Dean, along
with Michael Ries, who also reported the issue.
--
Thanks,
Tender Wang
On Tue, 10 Nov 2026 at 04:45, Tender Wang <tndrwang@gmail.com> wrote:
Thanks for rebasing the patch, preparing the backport versions, and
identifying the commit responsible.Since this is a regression from a4f7d33a904, I've CC'd Dean, along
with Michael Ries, who also reported the issue.
Thanks. That looked good to me, so I've pushed and back-patched it to
v15, with minor cosmetic adjustments.
I renamed the variables ar_insert_trig_tcs and ar_delete_trig_tcs
because the TransitionCaptureState structure is not command specific,
but applies to all command types, so code like
"ar_insert_trig_tcs->tcs_update_new_table" looked odd.
Regards,
Dean