Skip to content

Commit

Permalink
Ensure that MERGE recomputes GENERATED expressions properly.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
deanrasheed committed Jan 30, 2023
1 parent d9f5345 commit 4785af9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/backend/executor/nodeModifyTable.c
Expand Up @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/regress/expected/generated.out
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/test/regress/sql/generated.sql
Expand Up @@ -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;
Expand Down

0 comments on commit 4785af9

Please sign in to comment.