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 1xJmBQ-00000000QYI-1WJy for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Oct 2026 06:21:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xJmBO-00000009A72-31wA for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Oct 2026 06:21:46 +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.98.2) (envelope-from ) id 1xJdTl-0000000769A-2GzZ for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 21:04:09 +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 1xJdTi-00000000I5j-1IHr for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 21:04:09 +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=R99IPhcB7U6QXFKQIeT2JFBY/iPFJ6MmzfU+397HwZs=; b=QWk2K3VvcPJnir8SioDQpKjw8m AOLA7+NrZhh4/cXYwik+HwUCdw7+MB2tV+EPJfq9kcjIKv3IldjSo/AjsjFVWQMPt5i6S2IAS5y5t Q45zyAQJE4A7UcIu7Mo+xDWPmAWgQVyhzelQ+Fg6BYnDETGT5ieNIsi5/EJuHymq6NMt0+QmXEruz ND29g4I3Qu+LnTPRW7FQj0AsFWKn7ops7VvwB5vX4bhZuF/l2TFAqxOMxIPjR8pI3YShFBdrQXfwM eRed+2/fJIPlpRtWppBkONOzvG/okigMzsHfEzJyY9xNrDOoCGtgmCumWvtGjaX337uDGqJNDbBaW dlWyK6Pw==; 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 1xJdTh-00000000k9V-2bsa for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 21:04:05 +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 1xJdTg-000000029Hi-0DcQ for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 21:04:04 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19784: "no relation entry for relid 1" when costing = ANY () in a MinMaxAggPath 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, 21 Oct 2026 21:03:30 +0000 Message-ID: <19784-93f76236decd0656@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: 19784 Logged by: Alperen Keles Email address: alpkeles99@gmail.com PostgreSQL version: 18.6 Operating system: Ubuntu 24.04 x86_64 (also macOS 26 arm64) Description: =20 Hello, A query whose aggregates are all min()/max() over an indexed column fails in the planner with an internal error when its target list or HAVING contains "x op ANY/ALL (array)" where the array is an expression over the aggregate (anything but a constant or a plain ARRAY[...]): CREATE TABLE rt (c0 int4); CREATE INDEX ON rt (c0); SELECT 1 =3D ANY (array_append(ARRAY[2], max(c0))) FROM rt; ERROR: no relation entry for relid 1 SELECT max(c0) FROM rt HAVING 1 =3D ANY (array_append(ARRAY[2], max(c0))); ERROR: no relation entry for relid 1 The same happens when the aggregate is in a FROM subquery (or a view, an inlined CTE, a LATERAL subquery) and the condition is in the outer WHERE, because the qual is pushed down into the subquery's HAVING: SELECT * FROM (SELECT max(c0) FROM rt) AS s(c0) WHERE 1 =3D ANY (array_append(ARRAY[2], s.c0)); ERROR: no relation entry for relid 1 It also fails with min(), with ALL, < ANY, <> ALL, with array_prepend, array_cat, array_remove, ||, string_to_array or a CASE yielding an array, with max() of an indexed array column used directly (CREATE TABLE at (a int4[]); CREATE INDEX ON at (a); SELECT 1 =3D ANY (max(a)) FROM at;), with a primary key instead of a plain index, with a text column, and with or without data and ANALYZE. It does not fail without the index, with ARRAY[max(c0), 2] or an IN list, when another aggregate such as count(*) is present, or with a MATERIALIZED CTE: then the min/max optimization is not used, the array length is known without statistics, or the condition does not reach the aggregate query. Reproduced on 18.6 (REL_18_STABLE dfbc1507cdb), 19rc1 (REL_19_STABLE 1212d5d33b1) and master a261fc2b722, Ubuntu 24.04 x86_64, --enable-cassert; also on 18.6 and master on macOS arm64. 14.24 (REL_14_STABLE 14904052e65) returns results. The first failing commit is 9391f7152 ("Teach estimate_array_length() to use statistics where available"); its parent works. As far as I can tell: grouping_planner() calls preprocess_minmax_aggregates() before query_planner(). When the optimization applies, planagg costs the target list (create_pathtarget -> set_pathtarget_cost_width) and the HAVING quals (create_minmaxagg_path -> cost_qual_eval) with the main root. For a ScalarArrayOpExpr whose array is not a Const or ArrayExpr, estimate_array_length() calls examine_variable(); the expression contains the Var c0 of relation 1 inside the Aggref, so examine_variable() calls find_base_rel(root, 1), but no RelOptInfos exist at that point yet. This looks like the same problem as bug #19037 (cost_qual_eval from cost_subplan before the RelOptInfos exist, fixed in aba8f61c3 for SubPlans only), at another call site. Found with a query fuzzer. Regards, Alperen Keles