diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
new file mode 100644
index 1674735..645608e
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -328,18 +328,25 @@ transform_MERGE_to_join(Query *parse)
 	 * Find all relations nullable by the join.  If that includes the source
 	 * relation, and it is itself a join, we must include all relations under
 	 * it, since there may be Vars above the new join referring to relations
-	 * under the source join.  That cannot happen for the target relation,
-	 * even if it's an expanded view subquery.
+	 * under the source join.  If the target relation is a trigger-updatable
+	 * view, we pull data from the expanded view subquery (identified by
+	 * parse->mergeTargetRelation), so it must be included if it's on the
+	 * outer side of the join (but we don't need to include relations under
+	 * it, since the query cannot reference them directly).  If
+	 * parse->mergeTargetRelation == parse->resultRelation, we have a regular
+	 * target table, whose attributes are not read from the join.
 	 */
 	if (jointype == JOIN_LEFT)
 		nullable_relids = get_relids_in_jointree(source, true, true);
-	else if (jointype == JOIN_RIGHT)
+	else if (jointype == JOIN_RIGHT &&
+			 parse->mergeTargetRelation != parse->resultRelation)
 		nullable_relids = bms_make_singleton(parse->mergeTargetRelation);
 	else if (jointype == JOIN_FULL)
 	{
 		nullable_relids = get_relids_in_jointree(source, true, true);
-		nullable_relids = bms_add_member(nullable_relids,
-										 parse->mergeTargetRelation);
+		if (parse->mergeTargetRelation != parse->resultRelation)
+			nullable_relids = bms_add_member(nullable_relids,
+											 parse->mergeTargetRelation);
 	}
 	else
 		nullable_relids = NULL;
diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out
new file mode 100644
index 3a7202a..dfed6e8
--- a/src/test/regress/expected/merge.out
+++ b/src/test/regress/expected/merge.out
@@ -2963,6 +2963,58 @@ RETURNING c.oid;
  2608
 (1 row)
 
+-- Check OLD and NEW virtual columns in MERGE RETURNING.
+CREATE TEMP TABLE merge_virtual (id int PRIMARY KEY, a int,
+    g int GENERATED ALWAYS AS (coalesce(a, 999)) VIRTUAL,
+    c int GENERATED ALWAYS AS (42) VIRTUAL);
+INSERT INTO merge_virtual (id, a) VALUES (1, 10), (2, 20);
+-- RIGHT JOIN.
+WITH m AS (
+    MERGE INTO merge_virtual t
+    USING (VALUES (1, 100), (2, 200), (3, 300)) AS s(id, a)
+    ON t.id = s.id
+    WHEN MATCHED AND s.id = 1 THEN UPDATE SET a = s.a
+    WHEN MATCHED THEN DELETE
+    WHEN NOT MATCHED BY TARGET THEN INSERT (id, a) VALUES (s.id, s.a)
+    RETURNING WITH (OLD AS o, NEW AS n)
+        coalesce(n.id, o.id) AS id, merge_action() AS action,
+        o.g AS old_g, n.g AS new_g, o.c AS old_c, n.c AS new_c,
+        s.id AS src_id
+)
+SELECT * FROM m ORDER BY id;
+ id | action | old_g | new_g | old_c | new_c | src_id 
+----+--------+-------+-------+-------+-------+--------
+  1 | UPDATE |    10 |   100 |    42 |    42 |      1
+  2 | DELETE |    20 |       |    42 |       |      2
+  3 | INSERT |       |   300 |       |    42 |      3
+(3 rows)
+
+TRUNCATE merge_virtual;
+INSERT INTO merge_virtual (id, a) VALUES (1, 10), (2, 20), (4, 40);
+-- FULL JOIN, including an unmatched target row.
+WITH m AS (
+    MERGE INTO merge_virtual t
+    USING (VALUES (1, 100), (2, 200), (3, 300)) AS s(id, a)
+    ON t.id = s.id
+    WHEN MATCHED AND s.id = 1 THEN UPDATE SET a = s.a
+    WHEN MATCHED THEN DELETE
+    WHEN NOT MATCHED BY TARGET THEN INSERT (id, a) VALUES (s.id, s.a)
+    WHEN NOT MATCHED BY SOURCE THEN DELETE
+    RETURNING WITH (OLD AS o, NEW AS n)
+        coalesce(n.id, o.id) AS id, merge_action() AS action,
+        o.g AS old_g, n.g AS new_g, o.c AS old_c, n.c AS new_c,
+        s.id AS src_id
+)
+SELECT * FROM m ORDER BY id;
+ id | action | old_g | new_g | old_c | new_c | src_id 
+----+--------+-------+-------+-------+-------+--------
+  1 | UPDATE |    10 |   100 |    42 |    42 |      1
+  2 | DELETE |    20 |       |    42 |       |      2
+  3 | INSERT |       |   300 |       |    42 |      3
+  4 | DELETE |    40 |       |    42 |       |       
+(4 rows)
+
+DROP TABLE merge_virtual;
 DROP TABLE target, target2;
 DROP TABLE source, source2;
 DROP FUNCTION merge_trigfunc();
diff --git a/src/test/regress/sql/merge.sql b/src/test/regress/sql/merge.sql
new file mode 100644
index 91a8428..91f9e4c
--- a/src/test/regress/sql/merge.sql
+++ b/src/test/regress/sql/merge.sql
@@ -1923,6 +1923,45 @@ WHEN MATCHED AND c.oid = 'pg_depend'::re
 	UPDATE SET reltuples = reltuples - 1
 RETURNING c.oid;
 
+-- Check OLD and NEW virtual columns in MERGE RETURNING.
+CREATE TEMP TABLE merge_virtual (id int PRIMARY KEY, a int,
+    g int GENERATED ALWAYS AS (coalesce(a, 999)) VIRTUAL,
+    c int GENERATED ALWAYS AS (42) VIRTUAL);
+INSERT INTO merge_virtual (id, a) VALUES (1, 10), (2, 20);
+-- RIGHT JOIN.
+WITH m AS (
+    MERGE INTO merge_virtual t
+    USING (VALUES (1, 100), (2, 200), (3, 300)) AS s(id, a)
+    ON t.id = s.id
+    WHEN MATCHED AND s.id = 1 THEN UPDATE SET a = s.a
+    WHEN MATCHED THEN DELETE
+    WHEN NOT MATCHED BY TARGET THEN INSERT (id, a) VALUES (s.id, s.a)
+    RETURNING WITH (OLD AS o, NEW AS n)
+        coalesce(n.id, o.id) AS id, merge_action() AS action,
+        o.g AS old_g, n.g AS new_g, o.c AS old_c, n.c AS new_c,
+        s.id AS src_id
+)
+SELECT * FROM m ORDER BY id;
+
+TRUNCATE merge_virtual;
+INSERT INTO merge_virtual (id, a) VALUES (1, 10), (2, 20), (4, 40);
+-- FULL JOIN, including an unmatched target row.
+WITH m AS (
+    MERGE INTO merge_virtual t
+    USING (VALUES (1, 100), (2, 200), (3, 300)) AS s(id, a)
+    ON t.id = s.id
+    WHEN MATCHED AND s.id = 1 THEN UPDATE SET a = s.a
+    WHEN MATCHED THEN DELETE
+    WHEN NOT MATCHED BY TARGET THEN INSERT (id, a) VALUES (s.id, s.a)
+    WHEN NOT MATCHED BY SOURCE THEN DELETE
+    RETURNING WITH (OLD AS o, NEW AS n)
+        coalesce(n.id, o.id) AS id, merge_action() AS action,
+        o.g AS old_g, n.g AS new_g, o.c AS old_c, n.c AS new_c,
+        s.id AS src_id
+)
+SELECT * FROM m ORDER BY id;
+DROP TABLE merge_virtual;
+
 DROP TABLE target, target2;
 DROP TABLE source, source2;
 DROP FUNCTION merge_trigfunc();
