BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

Started by PG Bug reporting form4 days ago8 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19942
Logged by: Chi Zhang
Email address: 798604270@qq.com
PostgreSQL version: 19.0
Operating system: Ubuntu 24.04
Description:

Hi,

I found that executing MERGE ... RETURNING WITH (OLD AS o, NEW AS n) against
a table containing a VIRTUAL generated column causes an immediate backend
SIGSEGV crash (when the virtual column expression is non-strict, e.g.
COALESCE(a, 999), or referenced via whole-row o/n), and silently returns
wrong OLD/NEW values for constant virtual columns (e.g. GENERATED ALWAYS AS
(42) VIRTUAL).

This test case triggers a crash:
```
CREATE TEMP TABLE t1( id int PRIMARY KEY, a int, g int GENERATED ALWAYS AS
(COALESCE(a, 999)) VIRTUAL);
INSERT INTO t1(id, a) VALUES (1, 10), (2, 20);
MERGE INTO t1 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
AND s.id = 2 THEN DELETE WHEN NOT MATCHED THEN INSERT (id, a) VALUES (s.id,
s.a) RETURNING WITH (OLD AS o, NEW AS n) merge_action(), o.g AS old_g, n.g
AS new_g;
```
The backend crashes immediately with signal 11: Segmentation fault (both
release and --enable-cassert builds). GDB shows a NULL-slot dereference in
CheckVarSlotCompatibility(slot=0x0)
(src/backend/executor/execExprInterp.c:2409) while projecting the target
scan node.

This test case triggers a wrong result:
```
CREATE TEMP TABLE t2(id int PRIMARY KEY, a int, g int GENERATED ALWAYS AS
(42) VIRTUAL);
INSERT INTO t2(id, a) VALUES (1, 10), (2, 20);
MERGE INTO t2 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
AND s.id = 2 THEN DELETE WHEN NOT MATCHED THEN INSERT (id, a) VALUES (s.id,
s.a) RETURNING WITH (OLD AS o, NEW AS n) merge_action(), o.g AS old_g, n.g
AS new_g;
```
The output is:
```
merge_action | old_g | new_g
-----------+-------+-------
UPDATE | 42 | 42
DELETE | 42 | 42
INSERT | |
```
I expect that DELETE should return old_g = 42, new_g = NULL, and INSERT
should return old_g = NULL, new_g = 42.

#2Kirill Reshke
reshkekirill@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

On Wed, 25 Nov 2026 at 22:54, PG Bug reporting form
<noreply@postgresql.org> wrote:

The following bug has been logged on the website:

Bug reference: 19942
Logged by: Chi Zhang
Email address: 798604270@qq.com
PostgreSQL version: 19.0
Operating system: Ubuntu 24.04
Description:

Hi,

I found that executing MERGE ... RETURNING WITH (OLD AS o, NEW AS n) against
a table containing a VIRTUAL generated column causes an immediate backend
SIGSEGV crash (when the virtual column expression is non-strict, e.g.
COALESCE(a, 999), or referenced via whole-row o/n), and silently returns
wrong OLD/NEW values for constant virtual columns (e.g. GENERATED ALWAYS AS
(42) VIRTUAL).

This test case triggers a crash:
```
CREATE TEMP TABLE t1( id int PRIMARY KEY, a int, g int GENERATED ALWAYS AS
(COALESCE(a, 999)) VIRTUAL);
INSERT INTO t1(id, a) VALUES (1, 10), (2, 20);
MERGE INTO t1 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
AND s.id = 2 THEN DELETE WHEN NOT MATCHED THEN INSERT (id, a) VALUES (s.id,
s.a) RETURNING WITH (OLD AS o, NEW AS n) merge_action(), o.g AS old_g, n.g
AS new_g;
```
The backend crashes immediately with signal 11: Segmentation fault (both
release and --enable-cassert builds). GDB shows a NULL-slot dereference in
CheckVarSlotCompatibility(slot=0x0)
(src/backend/executor/execExprInterp.c:2409) while projecting the target
scan node.

This test case triggers a wrong result:
```
CREATE TEMP TABLE t2(id int PRIMARY KEY, a int, g int GENERATED ALWAYS AS
(42) VIRTUAL);
INSERT INTO t2(id, a) VALUES (1, 10), (2, 20);
MERGE INTO t2 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
AND s.id = 2 THEN DELETE WHEN NOT MATCHED THEN INSERT (id, a) VALUES (s.id,
s.a) RETURNING WITH (OLD AS o, NEW AS n) merge_action(), o.g AS old_g, n.g
AS new_g;
```
The output is:
```
merge_action | old_g | new_g
-----------+-------+-------
UPDATE | 42 | 42
DELETE | 42 | 42
INSERT | |
```
I expect that DELETE should return old_g = 42, new_g = NULL, and INSERT
should return old_g = NULL, new_g = 42.

Looks like regression since 17550c8a

--
Best regards,
Kirill Reshke

#3Tender Wang
tndrwang@gmail.com
In reply to: Kirill Reshke (#2)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

Hi,

Kirill Reshke <reshkekirill@gmail.com> 于2026年11月26日周四 02:07写道:

This test case triggers a crash:
This test case triggers a wrong result:

Looks like regression since 17550c8a

I confirmed that this is a regression introduced by 17550c8a change to
transform_MERGE_to_join(), which started applying the MERGE join’s
nulling relids to target Vars in the RETURNING list.
Skipping that step for JOIN_RIGHT makes both examples return the
expected results. I also confirmed that the crash still occurs with a
FULL JOIN.

I’m looking into the fix.

--
Tender Wang

#4Tender Wang
tndrwang@gmail.com
In reply to: Tender Wang (#3)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

Hi,

Tender Wang <tndrwang@gmail.com> 于2026年11月26日周四 08:52写道:

Hi,

Kirill Reshke <reshkekirill@gmail.com> 于2026年11月26日周四 02:07写道:

This test case triggers a crash:
This test case triggers a wrong result:

Looks like regression since 17550c8a

I confirmed that this is a regression introduced by 17550c8a change to
transform_MERGE_to_join(), which started applying the MERGE join’s
nulling relids to target Vars in the RETURNING list.
Skipping that step for JOIN_RIGHT makes both examples return the
expected results. I also confirmed that the crash still occurs with a
FULL JOIN.

I’m looking into the fix.

Thanks for the report.
In the trigger-updatable view fix, I overlooked how target Vars in
RETURNING are evaluated.
We applied the same nulling-relid marking to the RETURNING list and
the MERGE action expressions.
As a result, RIGHT and FULL MERGE joins started marking target Vars in
RETURNING as nullable by the join.

Target values in RETURNING come from the row versions supplied by
ModifyTable. For example, an INSERT
has no matching target row in the join output, but RETURNING can still
read the newly inserted row.
The extra nulling relid can cause an expanded virtual generated column
expression to be evaluated in the target scan,
before the OLD and NEW slots are available. This explains both the
crash and the incorrect results.

The attached patch restores the previous marking rules for RETURNING.
It adds the MERGE join’s nulling relid only to
source Vars when a WHEN NOT MATCHED BY SOURCE action requires a LEFT
or FULL join.

The patch includes regression tests for RIGHT and FULL joins covering
both reported examples. I used an AI tool to help write the new test
cases.

--
Tender Wang

Attachments:

t259820_4
0001-Fix-virtual-generated-column-evaluation-in-MERGE-RET.patchtext/plain; charset=US-ASCII; name=0001-Fix-virtual-generated-column-evaluation-in-MERGE-RET.patchDownload+103-8
#5Dean Rasheed
dean.a.rasheed@gmail.com
In reply to: Tender Wang (#4)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

On Thu, 26 Nov 2026 at 02:06, Tender Wang <tndrwang@gmail.com> wrote:

In the trigger-updatable view fix, I overlooked how target Vars in
RETURNING are evaluated.
We applied the same nulling-relid marking to the RETURNING list and
the MERGE action expressions.
As a result, RIGHT and FULL MERGE joins started marking target Vars in
RETURNING as nullable by the join.

Target values in RETURNING come from the row versions supplied by
ModifyTable. For example, an INSERT
has no matching target row in the join output, but RETURNING can still
read the newly inserted row.
The extra nulling relid can cause an expanded virtual generated column
expression to be evaluated in the target scan,
before the OLD and NEW slots are available. This explains both the
crash and the incorrect results.

The attached patch restores the previous marking rules for RETURNING.
It adds the MERGE join’s nulling relid only to
source Vars when a WHEN NOT MATCHED BY SOURCE action requires a LEFT
or FULL join.

Hmm, I was thinking that actually the problem with 17550c8a was that
it didn't properly distinguish MERGE into a trigger-updatable view
from MERGE into a regular table (or auto-updatable view, which is the
same by the time the planner sees it). It's only for a
trigger-updatable view that parse->mergeTargetRelation (the expanded
view sub-query) needs to be marked as nullable by the join. For a
regular table (or auto-updatable view), parse->mergeTargetRelation ==
parse->resultRelation, and its attributes are read directly, not via
the join, so it should not be marked nullable.

So I was thinking of something more like the attached v2.

Regards,
Dean

Attachments:

v2-Fix-virtual-generated-column-evaluation-in-MERGE-RETURNING.patchtext/x-patch; charset=US-ASCII; name=v2-Fix-virtual-generated-column-evaluation-in-MERGE-RETURNING.patchDownload+103-5
#6Kirill Reshke
reshkekirill@gmail.com
In reply to: Dean Rasheed (#5)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

On Thu, 26 Nov 2026 at 19:43, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:

On Thu, 26 Nov 2026 at 02:06, Tender Wang <tndrwang@gmail.com> wrote:

In the trigger-updatable view fix, I overlooked how target Vars in
RETURNING are evaluated.
We applied the same nulling-relid marking to the RETURNING list and
the MERGE action expressions.
As a result, RIGHT and FULL MERGE joins started marking target Vars in
RETURNING as nullable by the join.

Target values in RETURNING come from the row versions supplied by
ModifyTable. For example, an INSERT
has no matching target row in the join output, but RETURNING can still
read the newly inserted row.
The extra nulling relid can cause an expanded virtual generated column
expression to be evaluated in the target scan,
before the OLD and NEW slots are available. This explains both the
crash and the incorrect results.

The attached patch restores the previous marking rules for RETURNING.
It adds the MERGE join’s nulling relid only to
source Vars when a WHEN NOT MATCHED BY SOURCE action requires a LEFT
or FULL join.

Hmm, I was thinking that actually the problem with 17550c8a was that
it didn't properly distinguish MERGE into a trigger-updatable view
from MERGE into a regular table (or auto-updatable view, which is the
same by the time the planner sees it). It's only for a
trigger-updatable view that parse->mergeTargetRelation (the expanded
view sub-query) needs to be marked as nullable by the join. For a
regular table (or auto-updatable view), parse->mergeTargetRelation ==
parse->resultRelation, and its attributes are read directly, not via
the join, so it should not be marked nullable.

So I was thinking of something more like the attached v2.

Regards,
Dean

Hi!

I quickly checked patch - didn't spot any regression or functional
bug. For tests part - maybe add regress for target virtual columns in
WHEN and UPDATE action expressions, e.g.
MERGE_WHEN_NOT_MATCHED_BY_SOURCE + MERGE_WHEN_NOT_MATCHED_BY_TARGET
which uses FULL join? v2 changes nullable_relids for these but does
not exercise this code path.

something simple like

MERGE INTO t
USING s ON t.id = s.id
WHEN NOT MATCHED BY TARGET THEN DO NOTHING
WHEN NOT MATCHED BY SOURCE AND t.twice = 40
THEN UPDATE SET a = t.twice + 1;

This query doesn't show regression (works even without v2) but maybe
useful for coverage.

--
Best regards,
Kirill Reshke

#7Tender Wang
tndrwang@gmail.com
In reply to: Dean Rasheed (#5)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年11月26日周四 22:43写道:

Hmm, I was thinking that actually the problem with 17550c8a was that
it didn't properly distinguish MERGE into a trigger-updatable view
from MERGE into a regular table (or auto-updatable view, which is the
same by the time the planner sees it). It's only for a
trigger-updatable view that parse->mergeTargetRelation (the expanded
view sub-query) needs to be marked as nullable by the join. For a
regular table (or auto-updatable view), parse->mergeTargetRelation ==
parse->resultRelation, and its attributes are read directly, not via
the join, so it should not be marked nullable.

So I was thinking of something more like the attached v2.

Thanks for the v2 patch. I traced the rewrite through
ApplyRetrieveRule(). For a trigger-updatable view,
the action quals still reference the expanded view subquery via
mergeTargetRelation, while the target Vars in RETURNING are redirected
to resultRelation.
That explains why the same nullable_relids set works for both. I’m
happy to go with v2.

One small wording suggestion for the comment: could we change “outer
side of the join” to “nullable side of the join”? I initially read
“outer side” as referring to the outer input.

--
Tender Wang

#8Tender Wang
tndrwang@gmail.com
In reply to: Tender Wang (#7)
Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum

Tender Wang <tndrwang@gmail.com> 于2026年11月27日周五 14:10写道:

Dean Rasheed <dean.a.rasheed@gmail.com> 于2026年11月26日周四 22:43写道:

So I was thinking of something more like the attached v2.

Thanks for the v2 patch. I traced the rewrite through
ApplyRetrieveRule(). For a trigger-updatable view,
the action quals still reference the expanded view subquery via
mergeTargetRelation, while the target Vars in RETURNING are redirected
to resultRelation.
That explains why the same nullable_relids set works for both. I’m
happy to go with v2.

One small wording suggestion for the comment: could we change “outer
side of the join” to “nullable side of the join”? I initially read
“outer side” as referring to the outer input.

One follow-up on v2. When I read the code that builds nullable_relids,
my first thought was that the set would follow directly from the join
type.
In v2, it also reflects which relation references in the expressions
above the join actually read from the join output.

The part I found less obvious was why the same set works for the
action quals and RETURNING on a trigger-updatable view.
ApplyRetrieveRule() leaves the action quals referring to
mergeTargetRelation and redirects the target Vars in RETURNING to
resultRelation.
So adding mergeTargetRelation to the set marks the action quals as
intended and leaves the RETURNING target Vars alone.

Could we mention that rewrite step in the comment? I think it would
make the shared marking in transform_MERGE_to_join() easier to follow.

--
Tender Wang