public inbox for [email protected]  
help / color / mirror / Atom feed
From: PG Bug reporting form <[email protected]>
To: [email protected]
Cc: [email protected]
Subject: BUG #19461: MERGE with EXISTS subquery referencing USING clause alias fails with "variable not found in subplan
Date: Mon, 20 Apr 2026 09:29:49 +0000
Message-ID: <[email protected]> (raw)

The following bug has been logged on the website:

Bug reference:      19461
Logged by:          chunling qin
Email address:      [email protected]
PostgreSQL version: 15.0
Operating system:   centos
Description:        

## Summary

A MERGE statement that includes an EXISTS condition with a subquery
referencing an alias from the USING clause fails with the error: `ERROR:
variable not found in subplan target lists`.

## 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
DROP TABLE IF EXISTS t1, t2, t3 CASCADE;
CREATE TABLE t1 (col_int INT);
CREATE TABLE t2 (col_int INT);
CREATE TABLE t3 (col_int INT);
INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (1);
INSERT INTO t3 VALUES (1);

-- Bug reproduction
MERGE INTO t1 t11
USING (t2 t12 NATURAL INNER JOIN t3 t13)
ON TRUE
WHEN MATCHED AND EXISTS (
    SELECT 1
    FROM (
        SELECT col_int FROM t1
        EXCEPT
        SELECT col_int FROM t3 WHERE t13.col_int IS NOT NULL
    ) mm
) THEN DELETE;
```

## Expected Behavior

The MERGE statement should execute successfully. The EXISTS subquery
references `t13.col_int` from the USING clause, which should be valid within
the WHEN MATCHED condition context.


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.

postgres=# postgres=# postgres=# postgres-# postgres-# postgres-# postgres(#
postgres(# postgres(# postgres(# postgres(# postgres(# postgres(#
ERROR:  variable not found in subplan target lists
postgres=# select version();
                                                                version
                                                 
--------------------------------------------------------------------------------------
-------------------------------------------------
 PostgreSQL 15devel on x86_64-pc-linux-gnu, compiled by clang version 17.0.6
(TencentO
S 17.0.6-8.tl4.ap.2), 64-bit (commit:7103ebb7aa








reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: BUG #19461: MERGE with EXISTS subquery referencing USING clause alias fails with "variable not found in subplan
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox