BUG #19463: Server crash (Assertion failure) when using MERGE statement in CTE

Started by PG Bug reporting form10 days ago3 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19463
Logged by: chunling qin
Email address: 303677365@qq.com
PostgreSQL version: 15.0
Operating system: centos
Description:

Using a MERGE statement as a Common Table Expression (CTE) causes the
PostgreSQL server to crash with an assertion failure. The assertion in
`transformWithClause` does not recognize `MergeStmt` as a valid
data-modifying statement type.

## PostgreSQL Version

```
PostgreSQL 15devel on x86_64-pc-linux-gnu, compiled by clang version 17.0.6,
64-bit
```

Tested on REL_15_STABLE branch.

## Steps to Reproduce

```sql
-- Setup
CREATE TABLE target (col_int INT, col_varchar VARCHAR(2000));
CREATE TABLE source (col_int INT, col_varchar VARCHAR(2000));
INSERT INTO source VALUES (1, 'test');

-- This query causes server crash
WITH merge_cte AS (
MERGE INTO target t
USING source s ON t.col_int = s.col_int
WHEN NOT MATCHED THEN INSERT (col_int, col_varchar) VALUES (s.col_int,
s.col_varchar)
)
SELECT col_int FROM merge_cte;
```

## Actual Behavior

```
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
connection to server was lost
```

The server crashes with SIGABRT due to a failed assertion.

## Stack Trace

```
#0 0x00007fb27809d294 __pthread_kill_implementation (libc.so.6 + 0x91294)
#1 0x00007fb27804aaf6 raise (libc.so.6 + 0x3eaf6)
#2 0x00007fb278032897 abort (libc.so.6 + 0x26897)
#3 0x00005571eb6e064a ExceptionalCondition (postgres + 0xb3364a)
#4 0x00005571eae8f452 transformWithClause (postgres + 0x2e2452)
#5 0x00005571eae20ca1 transformSelectStmt (postgres + 0x273ca1)
#6 0x00005571eae1dd49 transformStmt (postgres + 0x270d49)
#7 0x00005571eae1e14c transformOptionalSelectInto (postgres + 0x27114c)
#8 0x00005571eae1d713 transformTopLevelStmt (postgres + 0x270713)
#9 0x00005571eae1d647 parse_analyze_fixedparams (postgres + 0x270647)
#10 0x00005571eb415471 pg_analyze_and_rewrite_fixedparams (postgres +
0x868471)
#11 0x00005571eb41a3db exec_simple_query (postgres + 0x86d3db)
#12 0x00005571eb419312 PostgresMain (postgres + 0x86c312)
```
This issue could not be reproduced with the latest version, but we can
easily reproduced by PostgreSQL 15devel. I am submitting this report for
official confirmation.

#2surya poondla
suryapoondla4@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19463: Server crash (Assertion failure) when using MERGE statement in CTE

Hi Chunling,

Thank you for reporting the issue.

I tried to reproduce the issue on postgres19 and I don't see any crash.

psql (19devel)
Type "help" for help.

postgres=#
postgres=#
postgres=#
postgres=# CREATE TABLE target (col_int INT, col_varchar VARCHAR(2000));
CREATE TABLE
postgres=# CREATE TABLE source (col_int INT, col_varchar VARCHAR(2000));
CREATE TABLE
postgres=# INSERT INTO source VALUES (1, 'test');
INSERT 0 1
postgres=#
postgres=#
postgres=# WITH merge_cte AS (
postgres(# MERGE INTO target t
postgres(# USING source s ON t.col_int = s.col_int
postgres(# WHEN NOT MATCHED THEN INSERT (col_int, col_varchar) VALUES
WHEN NOT MATCHED THEN INSERT (col_int, col_varchar) VALUES (s.col_int,
postgres(# s.col_varchar)
postgres(# )
postgres-# SELECT col_int FROM merge_cte;
ERROR: WITH query "merge_cte" does not have a RETURNING clause
LINE 7: SELECT col_int FROM merge_cte;
^
postgres=# WITH merge_cte AS (
postgres(# MERGE INTO target t
postgres(# USING source s ON t.col_int = s.col_int
postgres(# WHEN NOT MATCHED THEN INSERT (col_int, col_varchar) VALUES
(s.col_int, s.col_varchar) RETURNING t.col_int
postgres(# )
postgres-# SELECT col_int FROM merge_cte;
col_int
---------
1
(1 row)

I tried to reproduce the issue on postgres15 too, but i see ERROR: MERGE
not supported in WITH query.

Regards,
Surya Poondla

#3David Rowley
dgrowleyml@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19463: Server crash (Assertion failure) when using MERGE statement in CTE

On Tue, 21 Apr 2026 at 02:05, PG Bug reporting form
<noreply@postgresql.org> wrote:

PostgreSQL version: 15.0

You're reporting a bug on a PostgreSQL version that's missing 3.5
years of bug fixes. 15.17 is the latest minor version for the
PostgreSQL 15 release. Please always test with the latest minor
version of the major release before submitting bug reports. If you can
reproduce in a previous minor version, but not on the latest, then
this is likely because the bug has already been fixed. You need not
report these.

Using a MERGE statement as a Common Table Expression (CTE) causes the
PostgreSQL server to crash with an assertion failure. The assertion in
`transformWithClause` does not recognize `MergeStmt` as a valid
data-modifying statement type.

## PostgreSQL Version

```
PostgreSQL 15devel on x86_64-pc-linux-gnu, compiled by clang version 17.0.6,
64-bit
```

Tested on REL_15_STABLE branch.

Looks like you must be trying that on a very outdated REL_15_STABLE.
If it's tagged as 15devel, then that's from before the 15.0 release.
Please update your git repo (git pull) and try with REL_15_17.

I tried on 15.0 and 15.17 and cannot reproduce the assert failure
you're seeing. So it looks like whatever you've found was fixed before
the 15.0 release.

David