BUG #19804: "variable not found in subplan target list", DISTINCT agg over Gather`

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: alpkeles99(at)gmail(dot)com
Subject: BUG #19804: "variable not found in subplan target list", DISTINCT agg over Gather`
Date: 2026-10-28 17:20:03
Message-ID: 19804-594c35d8647fd7ef@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: 19804
Logged by: Alperen Keles
Email address: alpkeles99(at)gmail(dot)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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David Geier 2026-10-28 19:19:59 Re: crash in ltree_compare_distance() in 14.24
Previous Message Andrey Rachitskiy 2026-10-28 16:43:49 Re: BUG #19752: GROUP BY on a constant and a cast to a length-limited array type crashes the server while planning