pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
8+ messages / 3 participants
[nested] [flat]

* BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
@ 2026-10-21 21:03 PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: PG Bug reporting form @ 2026-10-21 21:03 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: alpkeles99@gmail.com

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:        

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 = ANY (array_append(ARRAY[2], max(c0))) FROM rt;
  ERROR:  no relation entry for relid 1

  SELECT max(c0) FROM rt HAVING 1 = 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 = 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 = 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








^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
@ 2026-10-22 20:14 ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 02:32   ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2026-10-22 20:14 UTC (permalink / raw)
  To: alpkeles99@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org, Richard Guo <guofenglinux@gmail.com>

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) download diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.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_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;
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-10-23 02:32   ` shihao zhong <zhong950419@gmail.com>
  2026-10-23 15:02     ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: shihao zhong @ 2026-10-23 02:32 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org, Richard Guo <guofenglinux@gmail.com>

Hi Tom,

> 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?

The range <@ support function costs its element expression during
eval_const_expressions. So this fails on master too:

  CREATE TABLE at (a int4[]);
  SELECT * FROM at WHERE (1 = ANY (a))::int <@ int4range(0, 5);
  ERROR:  no relation entry for relid 1

Your patch fixes ^^^^^, since simple_rel_array is still NULL there.

It does not fix this:

  CREATE TABLE ei (a int4[]);
  CREATE INDEX ON ei
    ((((1 = ANY (a))::int + (2 = ANY (a))::int) <@ int4range(0, 5)));
  SELECT * FROM ei;
  ERROR:  no relation entry for relid 1

The attached patch on top of yours passes NULL as root to
cost_qual_eval_node() in find_simplified_clause(), like aba8f61c3 did
for cost_subplan(). It fixes all the attached cases.

Thanks,
Shihao

Attachments:

nocfbot-range-support-null-root.patch (application/octet-stream, 1.5K) download From 5495257559bc1eaa0e87ea88247b16a7bc6f7853 Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Thu, 22 Oct 2026 22:25:43 -0400 Subject: [PATCH] Pass NULL root when costing in find_simplified_clause(). It runs during eval_const_expressions, which get_relation_info and apply_child_basequals call before the rel is in simple_rel_array, so estimate_array_length could fail in find_base_rel. Discussion: https://postgr.es/m/2828180.1792700071@sss.pgh.pa.us --- src/backend/utils/adt/rangetypes.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c index 84d71761c1b..2a3e579b974 100644 --- a/src/backend/utils/adt/rangetypes.c +++ b/src/backend/utils/adt/rangetypes.c @@ -3073,11 +3073,12 @@ find_simplified_clause(PlannerInfo *root, Expr *rangeExpr, Expr *elemExpr) * We define "expensive" as "contains any subplan or more than 10 * operators". Note that the subplan search has to be done * explicitly, since cost_qual_eval() will barf on unplanned - * subselects. + * subselects. Pass a NULL root, since we can be called before + * the RelOptInfos of the referenced rels exist. */ if (contain_subplans((Node *) elemExpr)) return NULL; - cost_qual_eval_node(&eval_cost, (Node *) elemExpr, root); + cost_qual_eval_node(&eval_cost, (Node *) elemExpr, NULL); if (eval_cost.startup + eval_cost.per_tuple > 10 * cpu_operator_cost) return NULL; -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 02:32   ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
@ 2026-10-23 15:02     ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 16:09       ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2026-10-23 15:02 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org, Richard Guo <guofenglinux@gmail.com>

shihao zhong <zhong950419@gmail.com> writes:
>> 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?

> It does not fix this:

>   CREATE TABLE ei (a int4[]);
>   CREATE INDEX ON ei
>     ((((1 = ANY (a))::int + (2 = ANY (a))::int) <@ int4range(0, 5)));
>   SELECT * FROM ei;
>   ERROR:  no relation entry for relid 1

Yeah, I thought there were probably other cases :-(

> The attached patch on top of yours passes NULL as root to
> cost_qual_eval_node() in find_simplified_clause(), like aba8f61c3 did
> for cost_subplan(). It fixes all the attached cases.

Surely this way madness lies.  The change you make here seriously
weakens our estimation abilities in normal cases, and it offers no
particular guarantee that there aren't other code paths (now or in
the future) that can trigger the same problem.

I think the only reasonably bulletproof answer is to change
examine_variable() to tolerate not finding a RelOptInfo.
That allows us to take out the hack from 77d0e82e5, though
I left its test case.

			regards, tom lane

Attachments:

allow-examine_variable-before-baserels-are-built.patch (text/x-diff, 3.5K) download diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index 5ee377595bd..0dfe39a88f5 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -2266,18 +2266,6 @@ estimate_array_length(PlannerInfo *root, Node *arrayexpr) AttStatsSlot sslot; double nelem = 0; - /* - * Skip calling examine_variable for Var with varno 0, which has no - * valid relation entry and would error in find_base_rel. Such a Var - * can appear when a nested set operation's output type doesn't match - * the parent's expected type, because recurse_set_operations builds a - * projection target list using generate_setop_tlist with varno 0, and - * if the required type coercion involves an ArrayCoerceExpr, we can - * be called on that Var. - */ - if (IsA(arrayexpr, Var) && ((Var *) arrayexpr)->varno == 0) - return 10; /* default guess, should match scalararraysel */ - examine_variable(root, arrayexpr, 0, &vardata); if (HeapTupleIsValid(vardata.statsTuple)) { @@ -5645,6 +5633,10 @@ ReleaseDummy(HeapTuple tuple) * statistic_proc_security_check(). * * Caller is responsible for doing ReleaseVariableStats() before exiting. + * + * In some code paths this can be called before we've set up RelOptInfos + * for the query's relations. Therefore, we use find_base_rel_noerr() + * and don't complain if it returns NULL. */ void examine_variable(PlannerInfo *root, Node *node, int varRelid, @@ -5683,16 +5675,21 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid, (varRelid == 0 || varRelid == ((Var *) basenode)->varno)) { Var *var = (Var *) basenode; + RelOptInfo *rel = find_base_rel_noerr(root, var->varno); - /* Set up result fields other than the stats tuple */ - vardata->var = basenode; /* return Var without phvs or relabeling */ - vardata->rel = find_base_rel(root, var->varno); - vardata->atttype = var->vartype; - vardata->atttypmod = var->vartypmod; - vardata->isunique = has_unique_index(vardata->rel, var->varattno); - - /* Try to locate some stats */ - examine_simple_variable(root, var, vardata); + if (rel) + { + /* Set up result fields other than the stats tuple */ + vardata->var = basenode; /* Var without phvs or relabeling */ + vardata->rel = rel; + vardata->atttype = var->vartype; + vardata->atttypmod = var->vartypmod; + vardata->isunique = has_unique_index(rel, var->varattno); + + /* Try to locate some stats */ + examine_simple_variable(root, var, vardata); + } + /* If we didn't find a rel, not much we can do */ return; } @@ -5720,7 +5717,7 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid, { if (varRelid == 0 || varRelid == relid) { - onerel = find_base_rel(root, relid); + onerel = find_base_rel_noerr(root, relid); vardata->rel = onerel; node = basenode; /* strip any phvs or relabeling */ } @@ -5732,13 +5729,14 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid, if (varRelid == 0) { /* treat it as a variable of a join relation */ + /* find_join_rel returns null if no such join has been made */ vardata->rel = find_join_rel(root, varnos); node = basenode; /* strip any phvs or relabeling */ } else if (bms_is_member(varRelid, varnos)) { /* ignore the vars belonging to other relations */ - vardata->rel = find_base_rel(root, varRelid); + vardata->rel = find_base_rel_noerr(root, varRelid); node = basenode; /* strip any phvs or relabeling */ /* note: no point in expressional-index search here */ }
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 02:32   ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  2026-10-23 15:02     ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-10-23 16:09       ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-24 04:04         ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2026-10-23 16:09 UTC (permalink / raw)
  To: Richard Guo <guofenglinux@gmail.com>; +Cc: shihao zhong <zhong950419@gmail.com>; alpkeles99@gmail.com; pgsql-bugs@lists.postgresql.org

shihao zhong <zhong950419@gmail.com> writes:
>> It does not fix this:

>> CREATE TABLE ei (a int4[]);
>> CREATE INDEX ON ei
>> ((((1 = ANY (a))::int + (2 = ANY (a))::int) <@ int4range(0, 5)));
>> SELECT * FROM ei;
>> ERROR:  no relation entry for relid 1

BTW, I wondered when that failure started, and found that
git bisect pins it on

c925ad30b047ec1388247539e86729d584c34b8d is the first bad commit
commit c925ad30b047ec1388247539e86729d584c34b8d
Author: Richard Guo <rguo@postgresql.org>
Date:   Tue Dec 9 16:56:26 2025 +0900

    Fix const-simplification for index expressions and predicate
    ...
    Because we need to cache the const-simplified index expressions and
    predicate in the relcache entry, we cannot fix the Vars before
    applying eval_const_expressions.  To ensure proper reduction of
    NullTest clauses, this patch runs eval_const_expressions a second time
    -- after the Vars have been fixed and with a valid root.

So I'm thinking there is a problem here.  This commit appears
to be supposing that it's accomplishing something by running
eval_const_expressions with baserel data available -- but as this
failure shows, the baserel data *isn't* available yet.  So even
with a fix that prevents examine_variable from failing, it seems
likely that we are not managing to reduce index expressions the
same as we will reduce query expressions later.

Not very sure what to do about that.  Maybe index expressions
have to be loaded in a second pass after we've set up baserel data?
Maybe the idea of caching them in relcache has to be abandoned
(or at least we have to rethink whether we can cache an eval'd
form of them)?

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 02:32   ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  2026-10-23 15:02     ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 16:09       ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-10-24 04:04         ` shihao zhong <zhong950419@gmail.com>
  2026-10-25 21:16           ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: shihao zhong @ 2026-10-24 04:04 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Richard Guo <guofenglinux@gmail.com>; alpkeles99@gmail.com; pgsql-bugs@lists.postgresql.org

Hi Tom

> So even with a fix that prevents examine_variable from failing, it
> seems likely that we are not managing to reduce index expressions the
> same as we will reduce query expressions later.

I think they are ok. The NullTest reduction in c925ad30b only needs the
RTE and array, not the RelOptInfo. And query quals are also folded before
any
RelOptInfo exists.

I think your patch already covered all the cases, so no need to do extra
work.

Thanks,
Shihao

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 02:32   ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  2026-10-23 15:02     ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 16:09       ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-24 04:04         ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
@ 2026-10-25 21:16           ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-26 04:01             ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2026-10-25 21:16 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: Richard Guo <guofenglinux@gmail.com>; alpkeles99@gmail.com; pgsql-bugs@lists.postgresql.org

shihao zhong <zhong950419@gmail.com> writes:
>> So even with a fix that prevents examine_variable from failing, it
>> seems likely that we are not managing to reduce index expressions the
>> same as we will reduce query expressions later.

> I think they are ok. The NullTest reduction in c925ad30b only needs the
> RTE and array, not the RelOptInfo. And query quals are also folded before
> any RelOptInfo exists.

I'm not so sure :-(.  Looking at the last example you showed, the
failure occurs because when we do eval_const_expressions on the index
expression, we call elem_contained_by_range_support to see if it can
replace the <@ operator with something else.  That calls
cost_qual_eval_node, and that's what ultimately hits the
examine_variable failure.  However, even if we band-aid that to not
fail, we are far from being okay.  Because what
elem_contained_by_range_support is going to do with the cost estimate
is decide whether or not to transform the <@ operator to something
else.  Presumably, whoever wrote that code verified that it did
something useful, so there must be code paths where it doesn't fail
because the underlying RelOptInfo is available, and so more than
one transformation behavior is possible.

Now, it's a fundamental requirement of the optimizer that
eval_const_expressions will give the same result on multiple copies of
the same expression.  Years ago that was safe because it didn't try to
look at any statistical data, or indeed much of anything except the
presented expression tree.  But we've now injected variable statistics
into its behavior, and that is a problem, not least because it's not
obvious that elem_contained_by_range_support is doing something it
shouldn't.  (There are probably other functions doing the same, and
even if we got rid of every case, people would keep trying to do
that.)  If we band-aid examine_variable to not fail depending on
context, we will mask places that are not producing the same results,
and I don't think we can expect to get away with that for long.

So I think the only really acceptable answer here is going to be to
fix things so we don't try to do eval_const_expressions until we've
loaded up all the RelOptInfo statistical data.  At the least, that'll
require postponing simplification of index expressions until their
baserels' data is available, as well as figuring out where we can
postpone preprocess_minmax_aggregates to, plus whatever else turns
out to be jumping the gun.  That does not sound like it'll be a
back-patchable fix.  So maybe in the back branches we have to do
what I posted upthread, but I'm not happy with it as a permanent fix.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
  2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
  2026-10-22 20:14 ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 02:32   ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  2026-10-23 15:02     ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-23 16:09       ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-24 04:04         ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath shihao zhong <zhong950419@gmail.com>
  2026-10-25 21:16           ` Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-10-26 04:01             ` shihao zhong <zhong950419@gmail.com>
  0 siblings, 0 replies; 8+ messages in thread

From: shihao zhong @ 2026-10-26 04:01 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Richard Guo <guofenglinux@gmail.com>; alpkeles99@gmail.com; pgsql-bugs@lists.postgresql.org

Hi Tom,


> I'm not so sure :-(.  Looking at the last example you showed...

I think I was over optimistic earlier. I took a look this afternoon
and I think you are right here. So the idea is WHERE is simplified
earlier and not able to see the stats, but the CHECK can.

> So I think the only really acceptable answer here is going to be to
> fix things so we don't try to do eval_const_expressions until we've
> loaded up all the RelOptInfo statistical data.

I am not sure we can postpone the WHERE clause, since it is
simplified before any RelOptInfo exists. That feels like a very big change.
Could we load the column stats first instead? We already
do that for NOT NULL columns in preprocess_relation_rtes.

Thanks,
Shihao

^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2026-10-26 04:01 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-21 21:03 BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath PG Bug reporting form <noreply@postgresql.org>
2026-10-22 20:14 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-23 02:32   ` shihao zhong <zhong950419@gmail.com>
2026-10-23 15:02     ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-23 16:09       ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-24 04:04         ` shihao zhong <zhong950419@gmail.com>
2026-10-25 21:16           ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-26 04:01             ` shihao zhong <zhong950419@gmail.com>

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