From 4785af9e6318856d45e51fbc328d52f6c5340e13 Mon Sep 17 00:00:00 2001 From: Dean Rasheed Date: Mon, 30 Jan 2023 10:07:32 +0000 Subject: [PATCH] Ensure that MERGE recomputes GENERATED expressions properly. This fixes a bug that, under some circumstances, would cause MERGE to fail to properly recompute expressions for GENERATED STORED columns. Formerly, ExecInitModifyTable() did not call ExecInitStoredGenerated() for a MERGE command, which meant that the generated expressions information was not computed until later, when the first merge action was executed. However, if the first merge action to execute was an UPDATE, then ExecInitStoredGenerated() could decide to skip some some generated columns, if the columns on which they depended were not updated, which was a problem if the MERGE also contained an INSERT action, for which no generated columns should be skipped. So fix by having ExecInitModifyTable() call ExecInitStoredGenerated() for MERGE, and assume that it isn't safe to skip any generated columns in a MERGE. Possibly that could be relaxed, by allowing some generated columns to be skipped for a MERGE without an INSERT action, but it's not clear that it's worth the effort. Noticed while investigating bug #17759. Back-patch to v15, where MERGE was added. Dean Rasheed, reviewed by Tom Lane. Discussion: https://postgr.es/m/17759-e76d9bece1b5421c%40postgresql.org https://postgr.es/m/CAEZATCXb_ezoMCcL0tzKwRGA1x0oeE%3DawTaysRfTPq%2B3wNJn8g%40mail.gmail.com --- src/backend/executor/nodeModifyTable.c | 4 ++-- src/test/regress/expected/generated.out | 20 ++++++++++++++++++++ src/test/regress/sql/generated.sql | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 5121dff7f5b..30fe932aee0 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -4149,12 +4149,12 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) } /* - * For INSERT and UPDATE, prepare to evaluate any generated columns. + * For INSERT/UPDATE/MERGE, prepare to evaluate any generated columns. * We must do this now, even if we never insert or update any rows, * because we have to fill resultRelInfo->ri_extraUpdatedCols for * possible use by the trigger machinery. */ - if (operation == CMD_INSERT || operation == CMD_UPDATE) + if (operation == CMD_INSERT || operation == CMD_UPDATE || operation == CMD_MERGE) ExecInitStoredGenerated(resultRelInfo, estate, operation); } diff --git a/src/test/regress/expected/generated.out b/src/test/regress/expected/generated.out index 1db5f9ed47b..a7954e5f657 100644 --- a/src/test/regress/expected/generated.out +++ b/src/test/regress/expected/generated.out @@ -192,6 +192,26 @@ SELECT * FROM gtest1 ORDER BY a; 3 | 6 (1 row) +-- test MERGE +CREATE TABLE gtestm ( + id int PRIMARY KEY, + f1 int, + f2 int, + f3 int GENERATED ALWAYS AS (f1 * 2) STORED, + f4 int GENERATED ALWAYS AS (f2 * 2) STORED +); +INSERT INTO gtestm VALUES (1, 5, 100); +MERGE INTO gtestm t USING (VALUES (1, 10), (2, 20)) v(id, f1) ON t.id = v.id + WHEN MATCHED THEN UPDATE SET f1 = v.f1 + WHEN NOT MATCHED THEN INSERT VALUES (v.id, v.f1, 200); +SELECT * FROM gtestm ORDER BY id; + id | f1 | f2 | f3 | f4 +----+----+-----+----+----- + 1 | 10 | 100 | 20 | 200 + 2 | 20 | 200 | 40 | 400 +(2 rows) + +DROP TABLE gtestm; -- views CREATE VIEW gtest1v AS SELECT * FROM gtest1; SELECT * FROM gtest1v; diff --git a/src/test/regress/sql/generated.sql b/src/test/regress/sql/generated.sql index 39eec40bce9..d4e0f30f6db 100644 --- a/src/test/regress/sql/generated.sql +++ b/src/test/regress/sql/generated.sql @@ -81,6 +81,21 @@ SELECT * FROM gtest1 ORDER BY a; DELETE FROM gtest1 WHERE b = 2; SELECT * FROM gtest1 ORDER BY a; +-- test MERGE +CREATE TABLE gtestm ( + id int PRIMARY KEY, + f1 int, + f2 int, + f3 int GENERATED ALWAYS AS (f1 * 2) STORED, + f4 int GENERATED ALWAYS AS (f2 * 2) STORED +); +INSERT INTO gtestm VALUES (1, 5, 100); +MERGE INTO gtestm t USING (VALUES (1, 10), (2, 20)) v(id, f1) ON t.id = v.id + WHEN MATCHED THEN UPDATE SET f1 = v.f1 + WHEN NOT MATCHED THEN INSERT VALUES (v.id, v.f1, 200); +SELECT * FROM gtestm ORDER BY id; +DROP TABLE gtestm; + -- views CREATE VIEW gtest1v AS SELECT * FROM gtest1; SELECT * FROM gtest1v;