Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wEpFl-004S56-0q for pgsql-bugs@arkaria.postgresql.org; Mon, 20 Apr 2026 14:05:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wEpFk-003AA9-1E for pgsql-bugs@arkaria.postgresql.org; Mon, 20 Apr 2026 14:05:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wEkxK-001ovl-2a for pgsql-bugs@lists.postgresql.org; Mon, 20 Apr 2026 09:30:14 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wEkxF-000000026pN-0VbW for pgsql-bugs@lists.postgresql.org; Mon, 20 Apr 2026 09:30:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=c6FeoT67f7vaDemJ2BefoB9Ieq5dWr676PVH1Ag/DN4=; b=XSd1XKyuscEErlxCmuTB854q1X br0oYbm75oMtbupURLUqgb+gfrWFrs3kdl6niDWI2vmX+SgYUljFObKHL57pOIFBroALXQPKjt7eJ 8sdVyIn2+BYrcLX4PpxzgRRj1gcqRmZ9kpKROBKRyL2u9h2G5m2aQw2WRd+zZVINhvFR41HyED1Am M4tBo3ZkxAKg7SHrLJI5wRL2yUJ0fMRuIuUxrfrszuRUubO5602gx/lXR9WoHTAvn37KsSBOMZzgR IbXwhmM8MiKrFCN5KhDIXX14ol90Wn9exLmcPsGuE0j0JXszvRInP7DAuV1neaOOToc1Y1lJ3uGXQ qGVUf9VA==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wEkxB-005evI-26 for pgsql-bugs@lists.postgresql.org; Mon, 20 Apr 2026 09:30:07 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wEkx9-00CfHN-3B for pgsql-bugs@lists.postgresql.org; Mon, 20 Apr 2026 09:30:04 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19461: MERGE with EXISTS subquery referencing USING clause alias fails with "variable not found in subplan To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 303677365@qq.com Reply-To: 303677365@qq.com, pgsql-bugs@lists.postgresql.org Date: Mon, 20 Apr 2026 09:29:49 +0000 Message-ID: <19461-8d03395b71f86664@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19461 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 15.0 Operating system: centos Description: =20 ## 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=3D# postgres=3D# postgres=3D# postgres-# postgres-# postgres-# pos= tgres(# postgres(# postgres(# postgres(# postgres(# postgres(# postgres(# ERROR: variable not found in subplan target lists postgres=3D# select version(); version =20 ---------------------------------------------------------------------------= ----------- ------------------------------------------------- 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