From: Tom Lane <tgl@sss.pgh.pa.us>
To: shihao zhong <zhong950419@gmail.com>
Cc: Richard Guo <guofenglinux@gmail.com>
Cc: alpkeles99@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #19784: "no relation entry for relid 1" when costing = ANY (<array expr over max(col)>) in a MinMaxAggPath
Date: Sun, 25 Oct 2026 17:16:45 -0400
Message-ID: <412690.1792963005@sss.pgh.pa.us> (raw)
In-Reply-To: <CAGRkXqR3iNffVZqPD-YMbcfHv2Fe8TgDzrungtxYnLKHc7o0EQ@mail.gmail.com>
References: <19784-93f76236decd0656@postgresql.org>
<2828180.1792700071@sss.pgh.pa.us>
<CAGRkXqSqDDwfPFyvpbb7APi3KM0SEH7yX9zbZUG9_yyhHMFjKA@mail.gmail.com>
<2962069.1792767739@sss.pgh.pa.us>
<3087487.1792771798@sss.pgh.pa.us>
<CAGRkXqR3iNffVZqPD-YMbcfHv2Fe8TgDzrungtxYnLKHc7o0EQ@mail.gmail.com>
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
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, alpkeles99@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: <412690.1792963005@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> <3087487.1792771798@sss.pgh.pa.us> <CAGRkXqR3iNffVZqPD-YMbcfHv2Fe8TgDzrungtxYnLKHc7o0EQ@mail.gmail.com> <412690.1792963005@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