pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: shihao zhong <zhong950419@gmail.com>
Cc: alpkeles99@gmail.com, 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: Fri, 23 Oct 2026 11:02:19 -0400
Message-ID: <2962069.1792767739@sss.pgh.pa.us> (raw)
In-Reply-To: <CAGRkXqSqDDwfPFyvpbb7APi3KM0SEH7yX9zbZUG9_yyhHMFjKA@mail.gmail.com>
References: <19784-93f76236decd0656@postgresql.org>
	<2828180.1792700071@sss.pgh.pa.us>
	<CAGRkXqSqDDwfPFyvpbb7APi3KM0SEH7yX9zbZUG9_yyhHMFjKA@mail.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 */ }

view thread (8+ messages)  latest in thread

Message-ID: <2962069.1792767739@sss.pgh.pa.us>
Permalink:  ../2962069.1792767739@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2962069.1792767739@sss.pgh.pa.us

 ·  · 

reply

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, zhong950419@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: <2962069.1792767739@sss.pgh.pa.us>
  References: <19784-93f76236decd0656@postgresql.org> <2828180.1792700071@sss.pgh.pa.us> <CAGRkXqSqDDwfPFyvpbb7APi3KM0SEH7yX9zbZUG9_yyhHMFjKA@mail.gmail.com> <2962069.1792767739@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