Re: BUG #15613: Bug in PG Planner for Foreign Data Wrappers - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #15613: Bug in PG Planner for Foreign Data Wrappers
Date
Msg-id 32029.1548878858@sss.pgh.pa.us
Whole thread Raw
In response to Re: BUG #15613: Bug in PG Planner for Foreign Data Wrappers  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
I wrote:
> The source of all the problems seems to be that the IS NOT NULL
> variable is getting wrapped into a PlaceHolderVar incorrectly(?):
> by the time it gets to distribute_qual_to_rels, it looks like
> ...
> I haven't tracked down why that's happening, but this parsetree
> clearly says that Var 1/2 (that is, ref_0.t1_col2) should get
> evaluated at the scan of RTE 6 (ref_1), which would then make
> ref_1 be the natural place to do the filter check too.

Ah: that is expected after all.  I hadn't looked closely enough at the
structure of the test query, but what the IS NOT NULL is supposed to test
is actually a lateral reference in the tlist of the ref_1 scan (subq_0).
That's underneath an outer join that could potentially force it to null,
so it needs to be evaluated under the outer join, and the PlaceHolderVar
is inserted to ensure that that happens.

Of course, since the outer join gets strength-reduced to a plain join,
we didn't really need to do any such thing.  But PlaceHolderVar insertion
happens during pull_up_subqueries, which runs before reduce_outer_joins,
so the where-to-evaluate decision is made first.  (And that explains why
the bug goes away if we manually reduce the join type.)

Switching the order of those two steps would not be an improvement,
either.  In this example, for instance, the fact that we can reduce
the outer join depends on the WHERE IS NOT NULL test, which initially
is one subquery level above the outer join, so if we hadn't flattened
the subqueries we'd fail to prove that we could reduce the outer join.

In short, while this could theoretically be better it's pretty hard
to see how we could make it so, short of a vast and complex rewrite.
And the case is weird enough that it seems unlikely we'd be improving
very many real-world queries.  (I'm betting, given what the test case
looks like, that this is a sqlsmith find rather than an anonymization
of a real query.)

So we're down to one bug: the FDWs are being careless about including
lateral_relids in their path parameterization.

            regards, tom lane


pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #15613: Bug in PG Planner for Foreign Data Wrappers
Next
From: Peter Geoghegan
Date:
Subject: Re: BUG #15609: synchronous_commit=off insert performance regressionwith secondary indexes