Incorrect command tag row count for MERGE with a cross-partition update

Started by Dean Rasheedalmost 4 years ago3 messageshackers
Jump to latest
#1Dean Rasheed
dean.a.rasheed@gmail.com

Playing around with MERGE some more, I noticed that the command tag
row count is wrong if it does a cross-partition update:

CREATE TABLE target (a int, b int) PARTITION BY LIST (b);
CREATE TABLE target_p1 PARTITION OF target FOR VALUES IN (1);
CREATE TABLE target_p2 PARTITION OF target FOR VALUES IN (2);
INSERT INTO target VALUES (1,1);

MERGE INTO target t USING (VALUES (1)) v(a) ON t.a = v.a
WHEN MATCHED THEN UPDATE SET b = 2;

which returns "MERGE 2" when only 1 row was updated, because
ExecUpdateAct() will update estate->es_processed for a cross-partition
update (but not for a normal update), and then ExecMergeMatched() will
update it again.

I think the best fix is to have ExecMergeMatched() pass canSetTag =
false to ExecUpdateAct(), so that ExecMergeMatched() takes
responsibility for updating estate->es_processed in all cases.

Regards,
Dean

Attachments:

merge-cp-update-command-tag-fix.patchtext/x-patch; charset=US-ASCII; name=merge-cp-update-command-tag-fix.patchDownload+1-1
#2Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Dean Rasheed (#1)
Re: Incorrect command tag row count for MERGE with a cross-partition update

On 2023-Feb-20, Dean Rasheed wrote:

Playing around with MERGE some more, I noticed that the command tag
row count is wrong if it does a cross-partition update:

CREATE TABLE target (a int, b int) PARTITION BY LIST (b);
CREATE TABLE target_p1 PARTITION OF target FOR VALUES IN (1);
CREATE TABLE target_p2 PARTITION OF target FOR VALUES IN (2);
INSERT INTO target VALUES (1,1);

MERGE INTO target t USING (VALUES (1)) v(a) ON t.a = v.a
WHEN MATCHED THEN UPDATE SET b = 2;

which returns "MERGE 2" when only 1 row was updated, because
ExecUpdateAct() will update estate->es_processed for a cross-partition
update (but not for a normal update), and then ExecMergeMatched() will
update it again.

Hah.

I think the best fix is to have ExecMergeMatched() pass canSetTag =
false to ExecUpdateAct(), so that ExecMergeMatched() takes
responsibility for updating estate->es_processed in all cases.

Sounds sensible.

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
"Las mujeres son como hondas: mientras más resistencia tienen,
más lejos puedes llegar con ellas" (Jonas Nightingale, Leap of Faith)

#3Dean Rasheed
dean.a.rasheed@gmail.com
In reply to: Alvaro Herrera (#2)
Re: Incorrect command tag row count for MERGE with a cross-partition update

On Tue, 21 Feb 2023 at 09:34, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:

I think the best fix is to have ExecMergeMatched() pass canSetTag =
false to ExecUpdateAct(), so that ExecMergeMatched() takes
responsibility for updating estate->es_processed in all cases.

Sounds sensible.

I decided it was also probably worth having a regression test covering
this, since it would be quite easy to break if the code is ever
refactored.

Pushed and back-patched.

Regards,
Dean