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 1xJzBO-00000000XsJ-09gy for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Oct 2026 20:14:38 +0000 Received: from localhost ([127.0.0.1]:33554 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xJzBM-0000000DHAF-2XdO for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Oct 2026 20:14:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:56966) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xJzBM-0000000DHA6-1Gox for pgsql-bugs@lists.postgresql.org; Thu, 22 Oct 2026 20:14:36 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xJzBK-00000000SV4-1w1U for pgsql-bugs@lists.postgresql.org; Thu, 22 Oct 2026 20:14:35 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 69MKEVDS2828181; Thu, 22 Oct 2026 16:14:31 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 69MKEVDS2828181 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1792700072; bh=p+Gs4vsFvickydfmNtaM5HxBVG4F9bnBppNfpHHHiU8=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=lh4pD6wn5IYKCMYfK4iYxxUQLnyidnw9y9ugbgGTb33UIR1hb6wcTzoKiEQulQFYM xofhsPh+mAs0fKagCCo4vC5kdc0+kiDZZ7ofOPzO+5UeZlv27sUziL220b9usFKxa+ QkFuDdtc6ER2ySUKiIPPHM1qfoXePfGEqz/UVD4E= From: Tom Lane To: alpkeles99@gmail.com cc: pgsql-bugs@lists.postgresql.org, Richard Guo Subject: Re: BUG #19784: "no relation entry for relid 1" when costing = ANY () in a MinMaxAggPath In-reply-to: <19784-93f76236decd0656@postgresql.org> References: <19784-93f76236decd0656@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Wed, 21 Oct 2026 21:03:30 -0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2828168.1792700064.0@sss.pgh.pa.us> Date: Thu, 22 Oct 2026 16:14:31 -0400 Message-ID: <2828180.1792700071@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2828168.1792700064.1@sss.pgh.pa.us> PG Bug reporting form writes: > 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 = ANY (array_append(ARRAY[2], max(c0))) FROM rt; > ERROR: no relation entry for relid 1 Thanks for the report! > As far as I can tell: grouping_planner() calls > preprocess_minmax_aggregates() before query_planner(). Yeah. Potential alternatives are (1) Restructure things enough to do that in some other order. Not clear if this is feasible at all, and for sure not something I'd want to back-patch if at all possible. (2) Modify estimate_array_length() to not try to access baserel data in this situation. It's not very hard to do (2), for example as attached. What is bothering me a bit here is that this is surely not the only estimator routine that calls examine_variable, so why don't we have other problems? I'm also wondering if we couldn't adapt this to get rid of the ugly kluge that 77d0e82e5 added (hence cc'ing Richard). Tracing through that example, I see that the query level with setOperations does have a simple_rel_array set up when we reach this, so the attached isn't enough to fix it as-is. But we could test something else that a setOperations query doesn't fill, such as all_baserels. A different line of attack could be to modify examine_variable so it doesn't fail on a Var that has no baserel data available. That's a little sad in that it could easily mask bugs that we'd like to know about; but it would feel less like a band-aid. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="avoid-failure-in-estimate_array_length-redux.patch"; charset="us-ascii" Content-ID: <2828168.1792700064.2@sss.pgh.pa.us> Content-Description: avoid-failure-in-estimate_array_length-redux.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/self= uncs.c index 5ee377595bd..5d8b104d680 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -2235,7 +2235,9 @@ scalararraysel(PlannerInfo *root, * concerns. Most callers will use it in double-type expressions anyway. * * Note: in some code paths root can be passed as NULL, resulting in - * slightly worse estimates. + * slightly worse estimates. It's also possible that we have a root + * but we've not yet reached query_planner, in which case examine_variabl= e + * would fail; guard against that by checking if simple_rel_array is set = up. */ double estimate_array_length(PlannerInfo *root, Node *arrayexpr) @@ -2259,7 +2261,7 @@ estimate_array_length(PlannerInfo *root, Node *array= expr) { return list_length(((ArrayExpr *) arrayexpr)->elements); } - else if (arrayexpr && root) + else if (arrayexpr && root && root->simple_rel_array) { /* See if we can find any statistics about it */ VariableStatData vardata; ------- =_aaaaaaaaaa0--