@@ -274,44 +274,55 @@ add_paths_to_joinrel(PlannerInfo *root,
274
274
* across joins unless there's a join-order-constraint-based reason to do so.
275
275
* So we ignore the param_source_rels restriction when this case applies.
276
276
*
277
- * However, there's a pitfall: suppose the inner rel (call it A) has a
278
- * parameter that is a PlaceHolderVar, and that PHV's minimum eval_at set
279
- * includes the outer rel (B) and some third rel (C). If we treat this as a
280
- * star-schema case and create a B/A nestloop join that's parameterized by C,
281
- * we would end up with a plan in which the PHV's expression has to be
282
- * evaluated as a nestloop parameter at the B/A join; and the executor is only
283
- * set up to handle simple Vars as NestLoopParams. Rather than add complexity
284
- * and overhead to the executor for such corner cases, it seems better to
285
- * forbid the join. (Note that existence of such a PHV probably means there
286
- * is a join order constraint that will cause us to consider joining B and C
287
- * directly; so we can still make use of A's parameterized path, and there is
288
- * no need for the star-schema exception.) To implement this exception to the
289
- * exception, we check whether any PHVs used in the query could pose such a
290
- * hazard. We don't have any simple way of checking whether a risky PHV would
291
- * actually be used in the inner plan, and the case is so unusual that it
292
- * doesn't seem worth working very hard on it.
293
- *
294
277
* allow_star_schema_join() returns TRUE if the param_source_rels restriction
295
278
* should be overridden, ie, it's okay to perform this join.
296
279
*/
297
- static bool
280
+ static inline bool
298
281
allow_star_schema_join (PlannerInfo * root ,
299
282
Path * outer_path ,
300
283
Path * inner_path )
301
284
{
302
285
Relids innerparams = PATH_REQ_OUTER (inner_path );
303
286
Relids outerrelids = outer_path -> parent -> relids ;
304
- ListCell * lc ;
305
287
306
288
/*
307
- * It's not a star-schema case unless the outer rel provides some but not
308
- * all of the inner rel's parameterization.
289
+ * It's a star-schema case if the outer rel provides some but not all of
290
+ * the inner rel's parameterization.
309
291
*/
310
- if (!(bms_overlap (innerparams , outerrelids ) &&
311
- bms_nonempty_difference (innerparams , outerrelids )))
312
- return false;
292
+ return (bms_overlap (innerparams , outerrelids ) &&
293
+ bms_nonempty_difference (innerparams , outerrelids ));
294
+ }
295
+
296
+ /*
297
+ * There's a pitfall for creating parameterized nestloops: suppose the inner
298
+ * rel (call it A) has a parameter that is a PlaceHolderVar, and that PHV's
299
+ * minimum eval_at set includes the outer rel (B) and some third rel (C).
300
+ * We might think we could create a B/A nestloop join that's parameterized by
301
+ * C. But we would end up with a plan in which the PHV's expression has to be
302
+ * evaluated as a nestloop parameter at the B/A join; and the executor is only
303
+ * set up to handle simple Vars as NestLoopParams. Rather than add complexity
304
+ * and overhead to the executor for such corner cases, it seems better to
305
+ * forbid the join. (Note that existence of such a PHV probably means there
306
+ * is a join order constraint that will cause us to consider joining B and C
307
+ * directly; so we can still make use of A's parameterized path with B+C.)
308
+ * So we check whether any PHVs used in the query could pose such a hazard.
309
+ * We don't have any simple way of checking whether a risky PHV would actually
310
+ * be used in the inner plan, and the case is so unusual that it doesn't seem
311
+ * worth working very hard on it.
312
+ *
313
+ * This case can occur whether or not the join's remaining parameterization
314
+ * overlaps param_source_rels, so we have to check for it separately from
315
+ * allow_star_schema_join, even though it looks much like a star-schema case.
316
+ */
317
+ static inline bool
318
+ check_hazardous_phv (PlannerInfo * root ,
319
+ Path * outer_path ,
320
+ Path * inner_path )
321
+ {
322
+ Relids innerparams = PATH_REQ_OUTER (inner_path );
323
+ Relids outerrelids = outer_path -> parent -> relids ;
324
+ ListCell * lc ;
313
325
314
- /* Check for hazardous PHVs */
315
326
foreach (lc , root -> placeholder_list )
316
327
{
317
328
PlaceHolderInfo * phinfo = (PlaceHolderInfo * ) lfirst (lc );
@@ -354,13 +365,15 @@ try_nestloop_path(PlannerInfo *root,
354
365
/*
355
366
* Check to see if proposed path is still parameterized, and reject if the
356
367
* parameterization wouldn't be sensible --- unless allow_star_schema_join
357
- * says to allow it anyway.
368
+ * says to allow it anyway. Also, we must reject if check_hazardous_phv
369
+ * doesn't like the look of it.
358
370
*/
359
371
required_outer = calc_nestloop_required_outer (outer_path ,
360
372
inner_path );
361
373
if (required_outer &&
362
- !bms_overlap (required_outer , param_source_rels ) &&
363
- !allow_star_schema_join (root , outer_path , inner_path ))
374
+ ((!bms_overlap (required_outer , param_source_rels ) &&
375
+ !allow_star_schema_join (root , outer_path , inner_path )) ||
376
+ !check_hazardous_phv (root , outer_path , inner_path )))
364
377
{
365
378
/* Waste no memory when we reject a path here */
366
379
bms_free (required_outer );
0 commit comments