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.98.2) (envelope-from ) id 1xMNd0-00000001zq1-1TxM for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Oct 2026 10:45:02 +0000 Received: from localhost ([127.0.0.1]:40284 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xMNcz-000000041Gc-2Ob6 for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Oct 2026 10:45:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:44918) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xM7Km-000000007wp-3UIR for pgsql-bugs@lists.postgresql.org; Wed, 28 Oct 2026 17:21:08 +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 1xM7Kj-00000001S1I-36c2 for pgsql-bugs@lists.postgresql.org; Wed, 28 Oct 2026 17:21:08 +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=LYMSHyw6mgYS3QppjWqM7dXgoqHYrLGLCNmPBte5Yfo=; b=fo0mR4gU4XpcWnNnIiaZ497skg SCFsj9+Je3NwVRfdjL1HiTq+eLam8CuYnElHL+XOLrSoEWnoLKN/yKkFiEsZ4qOIxxgFO9DYXasz5 nAKRGhyih6WSFbTJW5ry8lYX+nJ3jtdWH7Osk66THxURsAl4sjZsUq3XK1xS8wZ1g0HDQRtzGb1QH 6xOtaXrJxvm1O/4qJCZ+7AU1nO0O2Y5ovGvmpJ2W9ZHDXWG7icfDmf8y7eZzSfZdYG+Q7/gZcNIe3 BDrzWuR8VF59d33tnCDPFS16EG1ttjEUQ5tEDhwDu0/acR0fZbJIg0ZLX6k3+UVb9HhVDu9LxQrpp zbi8NY0Q==; 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.98.2) (envelope-from ) id 1xM7Ki-000000041MC-2BKp for pgsql-bugs@lists.postgresql.org; Wed, 28 Oct 2026 17:21:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xM7Kg-00000004kkA-1MTy for pgsql-bugs@lists.postgresql.org; Wed, 28 Oct 2026 17:21:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19804: "variable not found in subplan target list", DISTINCT agg over Gather` To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: alpkeles99@gmail.com Reply-To: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org Date: Wed, 28 Oct 2026 17:20:03 +0000 Message-ID: <19804-594c35d8647fd7ef@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: 19804 Logged by: Alperen Keles Email address: alpkeles99@gmail.com PostgreSQL version: 18.6 Operating system: Ubuntu 24.04 x86_64 Description: =20 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 =3D 0; SET parallel_tuple_cost =3D 0; SELECT count(DISTINCT a.x + 1) FROM a JOIN b TABLESAMPLE BERNOULLI (100) ON b.y =3D 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 =3D off, with the join clause written as a.x + 1 =3D 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