Re: BUG #15613: Bug in PG Planner for Foreign Data Wrappers
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Etsuro Fujita <fujita.etsuro@lab.ntt.co.jp>
Cc: srinivasan.sa@zohocorp.com, pgsql-bugs@lists.postgresql.org
Date: 2019-01-30T20:07:38Z
Lists: 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
Commits
-
Ensure that foreign scans with lateral refs are planned correctly.
- 1eeee69099e0 9.5.16 landed
- d468da0d6590 9.6.12 landed
- e3101a0317a9 10.7 landed
- 9d6d2b21343d 11.2 landed
- 876fd37fc812 9.4.21 landed
-
Split create_foreignscan_path() into three functions.
- 34ea1ab7fd30 12.0 landed
-
Change more places to be less trusting of RestrictInfo.is_pushed_down.
- c792c7db4146 11.0 cited
-
Remove quick path in ExecInitPartitionInfo for equal tupdescs
- 79b2e52615fa 11.0 cited