]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Ensure that MERGE recomputes GENERATED expressions properly.
authorDean Rasheed <dean.a.rasheed@gmail.com>
Mon, 30 Jan 2023 10:07:32 +0000 (10:07 +0000)
committerDean Rasheed <dean.a.rasheed@gmail.com>
Mon, 30 Jan 2023 10:07:32 +0000 (10:07 +0000)
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
src/test/regress/expected/generated.out
src/test/regress/sql/generated.sql

index 5121dff7f5bbad3b52e6eeedf4aed08e6990970b..30fe932aee0ad5bbe7853b3fbb6e09cb22935c03 100644 (file)
@@ -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);
        }
 
index 1db5f9ed47b1c0da31b8b1a53e347ecc8b4f53c0..a7954e5f6576d176b37395530c36625bb4d52a08 100644 (file)
@@ -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;
index 39eec40bce94b23f2c2344393e5b0c2eecae5334..d4e0f30f6db38976f1eed21aed187d1f203dbe0e 100644 (file)
@@ -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;