From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: alpkeles99@gmail.com
Subject: BUG #19804: "variable not found in subplan target list", DISTINCT agg over Gather`
Date: Wed, 28 Oct 2026 17:20:03 +0000
Message-ID: <19804-594c35d8647fd7ef@postgresql.org> (raw)
The following bug has been logged on the website:
Bug reference: 19804
Logged by: Alperen Keles
Email address: alpkeles99@gmail.com
PostgreSQL version: 18.6
Operating system: Ubuntu 24.04 x86_64
Description:
Hello,
This query fails at plan time with an internal error:
CREATE TABLE a (x int);
CREATE TABLE b (id int, y int) PARTITION BY RANGE (id);
CREATE TABLE b1 PARTITION OF b FOR VALUES FROM (MINVALUE) TO (0);
CREATE TABLE b2 PARTITION OF b FOR VALUES FROM (0) TO (MAXVALUE);
ANALYZE;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SELECT count(DISTINCT a.x + 1)
FROM a JOIN b TABLESAMPLE BERNOULLI (100) ON b.y = a.x + 1
GROUP BY b.y * 2;
ERROR: XX000: variable not found in subplan target list
LOCATION: fix_upper_expr_mutator, setrefs.c:3314 (18.6)
EXPLAIN fails the same way. It also fails with two rows in each table,
and with array_agg(a.x + 1 ORDER BY a.x + 1) in place of
count(DISTINCT ...). It returns no rows, as expected, with
enable_presorted_aggregate = off, with the join clause written as
a.x + 1 = b.y, with GROUP BY b.y, without the TABLESAMPLE, with b not
partitioned, or with the default parallel cost settings.
Reproduced on 18.6 (REL_18_6, built without assertions), REL_16_STABLE
0d8bfc5e366 (16.15), REL_17_STABLE 4cd931b4314 (17.11), REL_18_STABLE
5b5c68194c5, 19rc1 (REL_19_STABLE 1212d5d33b1) and master
27417270ab4, on Ubuntu 24.04 x86_64. 14 returns no rows. On 16, 19rc1
and master it also fails with TABLESAMPLE SYSTEM (100).
Found with a query fuzzer.
Regards,
Alperen Keles
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: noreply@postgresql.org, alpkeles99@gmail.com
Subject: Re: BUG #19804: "variable not found in subplan target list", DISTINCT agg over Gather`
In-Reply-To: <19804-594c35d8647fd7ef@postgresql.org>
References: <19804-594c35d8647fd7ef@postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox