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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: 303677365(at)qq(dot)com
Subject: BUG #19463: Server crash (Assertion failure) when using MERGE statement in CTE
Date: 2026-04-20 09:42:33
Message-ID: 19463-f59d5d2a41969756@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: 19463
Logged by: chunling qin
Email address: 303677365(at)qq(dot)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.

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-04-20 09:45:19 BUG #19464: Server crash (SIGABRT) with UPDATE containing multiple multi-column assignments
Previous Message PG Bug reporting form 2026-04-20 09:34:09 BUG #19462: MERGE on partitioned table with INSERT DEFAULT VALUES fails with "unknown action in MERGE WHEN clau