postgres.git / summary / log / commit / refs
commit 1d83c23f2912972a9cd3a6241be92ed30efffaf5
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Date: Thu Nov 12 09:40:54 2026 +0000
First released in: 19.0
Further varnullingrels fixes for MERGE WHEN NOT MATCHED BY SOURCE.
Commit 259a0a99fe3 fixed a "wrong varnullingrels" error in MERGE with
WHEN NOT MATCHED BY SOURCE actions by marking any Vars referring to
the source relation as nullable by the join. However, that overlooked
the fact that the source relation might itself be a join relation, and
there may be Vars referring to relations under the source join.
Depending the query, this could lead to further "wrong varnullingrels"
errors or incorrect query results.
Fix by ensuring that all Vars referring to the source join relation or
any relations under it are marked as nullable by the merge join.
Back-patch to v17, where WHEN NOT MATCHED BY SOURCE support was added
to MERGE.
Reported-by: Shihao Zhong <zhong950419@gmail.com>
Author: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Discussion: https://postgr.es/m/CAGRkXqR7VXacmRP41UBaM5u4aseeCH5ECVmQgn_YS%3DL7%3DxdhTw%40mail.gmail.com
Backpatch-through: 17
src/backend/optimizer/prep/prepjointree.c | 16 ++++++++---
src/test/regress/expected/merge.out | 45 ++++++++++++++++++++++++++++++-
src/test/regress/sql/merge.sql | 28 ++++++++++++++++++-
3 files changed, 83 insertions(+), 6 deletions(-)
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index c4f3b0d2f50..17c2fd53420 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -344,27 +344,35 @@ transform_MERGE_to_join(Query *parse)
*/
if (jointype == JOIN_LEFT || jointype == JOIN_FULL)
{
+ Relids sourcerelids;
+
+ /*
+ * If the source relation is a join, we must mark any Vars referring
+ * to relations under the join as well as the join relation itself.
+ */
+ sourcerelids = get_relids_in_jointree(source, true, true);
+
parse->mergeJoinCondition =
add_nulling_relids(parse->mergeJoinCondition,
- bms_make_singleton(sourcerti),
+ sourcerelids,
bms_make_singleton(joinrti));
foreach_node(MergeAction, action, parse->mergeActionList)
{
action->qual =
add_nulling_relids(action->qual,
- bms_make_singleton(sourcerti),
+ sourcerelids,
bms_make_singleton(joinrti));
action->targetList = (List *)
add_nulling_relids((Node *) action->targetList,
- bms_make_singleton(sourcerti),
+ sourcerelids,
bms_make_singleton(joinrti));
}
parse->returningList = (List *)
add_nulling_relids((Node *) parse->returningList,
- bms_make_singleton(sourcerti),
+ sourcerelids,
bms_make_singleton(joinrti));
}
diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out
index 69a7fb03104..5e2873fa0bd 100644
--- a/src/test/regress/expected/merge.out
+++ b/src/test/regress/expected/merge.out
@@ -2825,6 +2825,7 @@ CREATE VIEW bug18634v AS
SELECT * FROM bug18634t WHERE EXISTS (SELECT 1 FROM bug18634t);
CREATE TABLE bug18634s (a int, b int, c text);
INSERT INTO bug18634s VALUES (1, 2, 'src1');
+BEGIN;
MERGE INTO bug18634v t USING bug18634s s ON s.a = t.a
WHEN MATCHED THEN UPDATE SET b = s.b
WHEN NOT MATCHED BY SOURCE THEN DELETE
@@ -2841,9 +2842,51 @@ SELECT * FROM bug18634t;
1 | 2 | tgt1
(1 row)
+ROLLBACK;
+-- as above, but with a join as the source
+CREATE TABLE bug18634s2 (a int, b int, c text NOT NULL);
+INSERT INTO bug18634s2 VALUES (1, 2, 'src2');
+BEGIN;
+MERGE INTO bug18634t t
+ USING (bug18634s s JOIN bug18634s2 s2 ON s.a = s2.a) ON s.a = t.a
+ WHEN MATCHED THEN UPDATE SET b = s.b
+ WHEN NOT MATCHED BY SOURCE THEN DELETE
+ RETURNING merge_action(), s.c, s2.c AS c2,
+ s.c IS NULL AS c_null, s2.c IS NULL c2_null, t.*;
+ merge_action | c | c2 | c_null | c2_null | a | b | c
+--------------+------+------+--------+---------+---+----+------
+ UPDATE | src1 | src2 | f | f | 1 | 2 | tgt1
+ DELETE | | | t | t | 2 | 20 | tgt2
+(2 rows)
+
+SELECT * FROM bug18634t;
+ a | b | c
+---+---+------
+ 1 | 2 | tgt1
+(1 row)
+
+ROLLBACK;
+MERGE INTO bug18634v t
+ USING (bug18634s s JOIN bug18634s2 s2 ON s.a = s2.a) ON s.a = t.a
+ WHEN MATCHED THEN UPDATE SET b = s.b
+ WHEN NOT MATCHED BY SOURCE THEN DELETE
+ RETURNING merge_action(), s.c, s2.c AS c2,
+ s.c IS NULL AS c_null, s2.c IS NULL c2_null, t.*;
+ merge_action | c | c2 | c_null | c2_null | a | b | c
+--------------+------+------+--------+---------+---+----+------
+ UPDATE | src1 | src2 | f | f | 1 | 2 | tgt1
+ DELETE | | | t | t | 2 | 20 | tgt2
+(2 rows)
+
+SELECT * FROM bug18634t;
+ a | b | c
+---+---+------
+ 1 | 2 | tgt1
+(1 row)
+
DROP TABLE bug18634t CASCADE;
NOTICE: drop cascades to view bug18634v
-DROP TABLE bug18634s;
+DROP TABLE bug18634s, bug18634s2;
-- prepare
RESET SESSION AUTHORIZATION;
-- try a system catalog
diff --git a/src/test/regress/sql/merge.sql b/src/test/regress/sql/merge.sql
index 0cd50714a2b..44cb01439a2 100644
--- a/src/test/regress/sql/merge.sql
+++ b/src/test/regress/sql/merge.sql
@@ -1814,15 +1814,41 @@ CREATE VIEW bug18634v AS
CREATE TABLE bug18634s (a int, b int, c text);
INSERT INTO bug18634s VALUES (1, 2, 'src1');
+BEGIN;
MERGE INTO bug18634v t USING bug18634s s ON s.a = t.a
WHEN MATCHED THEN UPDATE SET b = s.b
WHEN NOT MATCHED BY SOURCE THEN DELETE
RETURNING merge_action(), s.c, t.*;
+SELECT * FROM bug18634t;
+ROLLBACK;
+
+-- as above, but with a join as the source
+CREATE TABLE bug18634s2 (a int, b int, c text NOT NULL);
+INSERT INTO bug18634s2 VALUES (1, 2, 'src2');
+
+BEGIN;
+MERGE INTO bug18634t t
+ USING (bug18634s s JOIN bug18634s2 s2 ON s.a = s2.a) ON s.a = t.a
+ WHEN MATCHED THEN UPDATE SET b = s.b
+ WHEN NOT MATCHED BY SOURCE THEN DELETE
+ RETURNING merge_action(), s.c, s2.c AS c2,
+ s.c IS NULL AS c_null, s2.c IS NULL c2_null, t.*;
+
+SELECT * FROM bug18634t;
+ROLLBACK;
+
+MERGE INTO bug18634v t
+ USING (bug18634s s JOIN bug18634s2 s2 ON s.a = s2.a) ON s.a = t.a
+ WHEN MATCHED THEN UPDATE SET b = s.b
+ WHEN NOT MATCHED BY SOURCE THEN DELETE
+ RETURNING merge_action(), s.c, s2.c AS c2,
+ s.c IS NULL AS c_null, s2.c IS NULL c2_null, t.*;
+
SELECT * FROM bug18634t;
DROP TABLE bug18634t CASCADE;
-DROP TABLE bug18634s;
+DROP TABLE bug18634s, bug18634s2;
-- prepare
[parent: 00e61be2ff8b]