From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | exclusion(at)gmail(dot)com |
Cc: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Re: BUG #19037: Planner fails on estimating array length with "no relation entry" error |
Date: | 2025-08-30 21:51:16 |
Message-ID: | 50858.1756590676@sss.pgh.pa.us |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-bugs |
PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> The following script:
> create table t(ia int[]);
> select exists (select 1 from (select 1) where case when b then 1 else 0 end
> = 1)
> from (select 1 = any(ia) as b from t);
> triggers:
> ERROR: XX000: no relation entry for relid 2
> LOCATION: find_base_rel, relnode.c:426
Thanks for the report. After a bit of experimentation, I can shorten
the reproducer to
select exists (select 1 where (1 = any(ia))::int = 1) from t;
ERROR: no relation entry for relid 1
but it doesn't happen any more if you simplify further to
select exists (select 1 where 1 = any(ia)) from t;
The reason for the difference seems to be that make_subplan
checks to see if the EXISTS can be converted to a hashable ANY
subplan (cf. convert_EXISTS_to_ANY), and the form where there's
a top-level "=" operator in the sub-select's WHERE clause can
be so converted. Then we hit the failure while trying to do
cost_qual_eval on the converted ANY expression. So you also
get this failure if you manually write out the form that
convert_EXISTS_to_ANY is generating:
select ((1 = any(ia))::int) = any (select 1) from t;
ERROR: no relation entry for relid 1
Anyway that's sort of a sideshow. The real issue here is that
we're applying cost_qual_eval before the planner has created
any RelOptInfos, which means that examine_variable won't work.
I find it surprising that this is the first report of such trouble,
because it certainly isn't obvious that cost_qual_eval shouldn't
be allowed to consult statistics.
The most expedient solution is probably to hack examine_variable
so that it doesn't fail if root->simple_rel_array isn't there yet.
That seems mighty ugly though.
Another low-risk response could be to revert 9391f7152. But I don't
care for that because it's not really addressing the underlying
problem. We might have the same issue elsewhere in cost estimation
already, and even if we don't, it would be quite likely we'd introduce
it again in future.
In some sense the "right" fix would be to do SubPlan generation
later, when we have statistics available for the Vars of the
parent query. But that seems like a rather large task, and
we'd surely not wish to back-patch the results.
So I'm not really seeing another workable answer besides hacking
examine_variable, more or less as attached.
regards, tom lane
Attachment | Content-Type | Size |
---|---|---|
v1-dont-fail-in-early-examine_variable.patch | text/x-diff | 917 bytes |
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Paquier | 2025-08-31 04:53:50 | Re: Broken PQtrace CopyData display |
Previous Message | PG Bug reporting form | 2025-08-30 12:00:02 | BUG #19037: Planner fails on estimating array length with "no relation entry" error |