From: Tom Lane <tgl@sss.pgh.pa.us>
To: alpkeles99@gmail.com
Cc: pgsql-bugs@lists.postgresql.org, Richard Guo <guofenglinux@gmail.com>
Subject: Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
Date: Thu, 22 Oct 2026 16:14:31 -0400
Message-ID: <2828180.1792700071@sss.pgh.pa.us> (raw)
In-Reply-To: <19784-93f76236decd0656@postgresql.org>
References: <19784-93f76236decd0656@postgresql.org>
PG Bug reporting form <noreply@postgresql.org> 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
Attachments:avoid-failure-in-estimate_array_length-redux.patch (text/x-diff, 1.0K) downloaddiff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.cindex 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_variable+ * 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 *arrayexpr)
{
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;
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: tgl@sss.pgh.pa.us, alpkeles99@gmail.com, guofenglinux@gmail.com
Subject: Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
In-Reply-To: <2828180.1792700071@sss.pgh.pa.us>
References: <19784-93f76236decd0656@postgresql.org> <2828180.1792700071@sss.pgh.pa.us>
* 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