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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: 798604270(at)qq(dot)com
Subject: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum
Date: 2026-11-25 11:49:56
Message-ID: 19942-267f90afad1dc4e1@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19942
Logged by: Chi Zhang
Email address: 798604270(at)qq(dot)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.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-11-25 12:00:13 BUG #19943: Wrong results under partition with CHECK/NOT NULL on overridden VIRTUAL generated columns
Previous Message Zsolt Parragi 2026-11-25 11:32:10 Re: BUG #19897: Recovery fails after "LOG: consistent recovery state reached"