1 /*-------------------------------------------------------------------------
4 * RestrictInfo node manipulation routines.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
17 #include "optimizer/clauses.h"
18 #include "optimizer/cost.h"
19 #include "optimizer/paths.h"
20 #include "optimizer/predtest.h"
21 #include "optimizer/restrictinfo.h"
22 #include "optimizer/var.h"
25 static RestrictInfo
*make_restrictinfo_internal(Expr
*clause
,
28 bool outerjoin_delayed
,
30 Relids required_relids
,
31 Relids nullable_relids
);
32 static Expr
*make_sub_restrictinfos(Expr
*clause
,
34 bool outerjoin_delayed
,
36 Relids required_relids
,
37 Relids nullable_relids
);
38 static List
*select_nonredundant_join_list(List
*restrictinfo_list
,
39 List
*reference_list
);
40 static bool join_clause_is_redundant(RestrictInfo
*rinfo
,
41 List
*reference_list
);
47 * Build a RestrictInfo node containing the given subexpression.
49 * The is_pushed_down, outerjoin_delayed, and pseudoconstant flags for the
50 * RestrictInfo must be supplied by the caller, as well as the correct value
51 * for nullable_relids. required_relids can be NULL, in which case it
52 * defaults to the actual clause contents (i.e., clause_relids).
54 * We initialize fields that depend only on the given subexpression, leaving
55 * others that depend on context (or may never be needed at all) to be filled
59 make_restrictinfo(Expr
*clause
,
61 bool outerjoin_delayed
,
63 Relids required_relids
,
64 Relids nullable_relids
)
67 * If it's an OR clause, build a modified copy with RestrictInfos inserted
68 * above each subclause of the top-level AND/OR structure.
70 if (or_clause((Node
*) clause
))
71 return (RestrictInfo
*) make_sub_restrictinfos(clause
,
78 /* Shouldn't be an AND clause, else AND/OR flattening messed up */
79 Assert(!and_clause((Node
*) clause
));
81 return make_restrictinfo_internal(clause
,
91 * make_restrictinfo_from_bitmapqual
93 * Given the bitmapqual Path structure for a bitmap indexscan, generate
94 * RestrictInfo node(s) equivalent to the condition represented by the
95 * indexclauses of the Path structure.
97 * The result is a List (effectively, implicit-AND representation) of
100 * The caller must pass is_pushed_down, but we assume outerjoin_delayed
101 * and pseudoconstant are false and nullable_relids is NULL (no other
102 * kind of qual should ever get into a bitmapqual).
104 * If include_predicates is true, we add any partial index predicates to
105 * the explicit index quals. When this is not true, we return a condition
106 * that might be weaker than the actual scan represents.
108 * To do this through the normal make_restrictinfo() API, callers would have
109 * to strip off the RestrictInfo nodes present in the indexclauses lists, and
110 * then make_restrictinfo() would have to build new ones. It's better to have
111 * a specialized routine to allow sharing of RestrictInfos.
113 * The qual manipulations here are much the same as in create_bitmap_subplan;
114 * keep the two routines in sync!
117 make_restrictinfo_from_bitmapqual(Path
*bitmapqual
,
119 bool include_predicates
)
124 if (IsA(bitmapqual
, BitmapAndPath
))
126 BitmapAndPath
*apath
= (BitmapAndPath
*) bitmapqual
;
129 * There may well be redundant quals among the subplans, since a
130 * top-level WHERE qual might have gotten used to form several
131 * different index quals. We don't try exceedingly hard to eliminate
132 * redundancies, but we do eliminate obvious duplicates by using
133 * list_concat_unique.
136 foreach(l
, apath
->bitmapquals
)
140 sublist
= make_restrictinfo_from_bitmapqual((Path
*) lfirst(l
),
143 result
= list_concat_unique(result
, sublist
);
146 else if (IsA(bitmapqual
, BitmapOrPath
))
148 BitmapOrPath
*opath
= (BitmapOrPath
*) bitmapqual
;
150 List
*withoutris
= NIL
;
153 * Here, we only detect qual-free subplans. A qual-free subplan would
154 * cause us to generate "... OR true ..." which we may as well reduce
155 * to just "true". We do not try to eliminate redundant subclauses
156 * because (a) it's not as likely as in the AND case, and (b) we might
157 * well be working with hundreds or even thousands of OR conditions,
158 * perhaps from a long IN list. The performance of list_append_unique
159 * would be unacceptable.
161 foreach(l
, opath
->bitmapquals
)
165 sublist
= make_restrictinfo_from_bitmapqual((Path
*) lfirst(l
),
171 * If we find a qual-less subscan, it represents a constant
172 * TRUE, and hence the OR result is also constant TRUE, so we
179 * If the sublist contains multiple RestrictInfos, we create an
180 * AND subclause. If there's just one, we have to check if it's
181 * an OR clause, and if so flatten it to preserve AND/OR flatness
184 * We construct lists with and without sub-RestrictInfos, so as
185 * not to have to regenerate duplicate RestrictInfos below.
187 if (list_length(sublist
) > 1)
189 withris
= lappend(withris
, make_andclause(sublist
));
190 sublist
= get_actual_clauses(sublist
);
191 withoutris
= lappend(withoutris
, make_andclause(sublist
));
195 RestrictInfo
*subri
= (RestrictInfo
*) linitial(sublist
);
197 Assert(IsA(subri
, RestrictInfo
));
198 if (restriction_is_or_clause(subri
))
200 BoolExpr
*subor
= (BoolExpr
*) subri
->orclause
;
202 Assert(or_clause((Node
*) subor
));
203 withris
= list_concat(withris
,
204 list_copy(subor
->args
));
205 subor
= (BoolExpr
*) subri
->clause
;
206 Assert(or_clause((Node
*) subor
));
207 withoutris
= list_concat(withoutris
,
208 list_copy(subor
->args
));
212 withris
= lappend(withris
, subri
);
213 withoutris
= lappend(withoutris
, subri
->clause
);
219 * Avoid generating one-element ORs, which could happen due to
220 * redundancy elimination or ScalarArrayOpExpr quals.
222 if (list_length(withris
) <= 1)
226 /* Here's the magic part not available to outside callers */
228 list_make1(make_restrictinfo_internal(make_orclause(withoutris
),
229 make_orclause(withris
),
237 else if (IsA(bitmapqual
, IndexPath
))
239 IndexPath
*ipath
= (IndexPath
*) bitmapqual
;
241 result
= list_copy(ipath
->indexclauses
);
242 if (include_predicates
&& ipath
->indexinfo
->indpred
!= NIL
)
244 foreach(l
, ipath
->indexinfo
->indpred
)
246 Expr
*pred
= (Expr
*) lfirst(l
);
249 * We know that the index predicate must have been implied by
250 * the query condition as a whole, but it may or may not be
251 * implied by the conditions that got pushed into the
252 * bitmapqual. Avoid generating redundant conditions.
254 if (!predicate_implied_by(list_make1(pred
), result
))
255 result
= lappend(result
,
256 make_restrictinfo(pred
,
267 elog(ERROR
, "unrecognized node type: %d", nodeTag(bitmapqual
));
268 result
= NIL
; /* keep compiler quiet */
275 * make_restrictinfos_from_actual_clauses
277 * Given a list of implicitly-ANDed restriction clauses, produce a list
278 * of RestrictInfo nodes. This is used to reconstitute the RestrictInfo
279 * representation after doing transformations of a list of clauses.
281 * We assume that the clauses are relation-level restrictions and therefore
282 * we don't have to worry about is_pushed_down, outerjoin_delayed, or
283 * nullable_relids (these can be assumed true, false, and NULL, respectively).
284 * We do take care to recognize pseudoconstant clauses properly.
287 make_restrictinfos_from_actual_clauses(PlannerInfo
*root
,
293 foreach(l
, clause_list
)
295 Expr
*clause
= (Expr
*) lfirst(l
);
300 * It's pseudoconstant if it contains no Vars and no volatile
301 * functions. We probably can't see any sublinks here, so
302 * contain_var_clause() would likely be enough, but for safety
303 * use contain_vars_of_level() instead.
306 !contain_vars_of_level((Node
*) clause
, 0) &&
307 !contain_volatile_functions((Node
*) clause
);
310 /* tell createplan.c to check for gating quals */
311 root
->hasPseudoConstantQuals
= true;
314 rinfo
= make_restrictinfo(clause
,
320 result
= lappend(result
, rinfo
);
326 * make_restrictinfo_internal
328 * Common code for the main entry points and the recursive cases.
330 static RestrictInfo
*
331 make_restrictinfo_internal(Expr
*clause
,
334 bool outerjoin_delayed
,
336 Relids required_relids
,
337 Relids nullable_relids
)
339 RestrictInfo
*restrictinfo
= makeNode(RestrictInfo
);
341 restrictinfo
->clause
= clause
;
342 restrictinfo
->orclause
= orclause
;
343 restrictinfo
->is_pushed_down
= is_pushed_down
;
344 restrictinfo
->outerjoin_delayed
= outerjoin_delayed
;
345 restrictinfo
->pseudoconstant
= pseudoconstant
;
346 restrictinfo
->can_join
= false; /* may get set below */
347 restrictinfo
->nullable_relids
= nullable_relids
;
350 * If it's a binary opclause, set up left/right relids info. In any case
351 * set up the total clause relids info.
353 if (is_opclause(clause
) && list_length(((OpExpr
*) clause
)->args
) == 2)
355 restrictinfo
->left_relids
= pull_varnos(get_leftop(clause
));
356 restrictinfo
->right_relids
= pull_varnos(get_rightop(clause
));
358 restrictinfo
->clause_relids
= bms_union(restrictinfo
->left_relids
,
359 restrictinfo
->right_relids
);
362 * Does it look like a normal join clause, i.e., a binary operator
363 * relating expressions that come from distinct relations? If so we
364 * might be able to use it in a join algorithm. Note that this is a
365 * purely syntactic test that is made regardless of context.
367 if (!bms_is_empty(restrictinfo
->left_relids
) &&
368 !bms_is_empty(restrictinfo
->right_relids
) &&
369 !bms_overlap(restrictinfo
->left_relids
,
370 restrictinfo
->right_relids
))
372 restrictinfo
->can_join
= true;
373 /* pseudoconstant should certainly not be true */
374 Assert(!restrictinfo
->pseudoconstant
);
379 /* Not a binary opclause, so mark left/right relid sets as empty */
380 restrictinfo
->left_relids
= NULL
;
381 restrictinfo
->right_relids
= NULL
;
382 /* and get the total relid set the hard way */
383 restrictinfo
->clause_relids
= pull_varnos((Node
*) clause
);
386 /* required_relids defaults to clause_relids */
387 if (required_relids
!= NULL
)
388 restrictinfo
->required_relids
= required_relids
;
390 restrictinfo
->required_relids
= restrictinfo
->clause_relids
;
393 * Fill in all the cacheable fields with "not yet set" markers. None of
394 * these will be computed until/unless needed. Note in particular that we
395 * don't mark a binary opclause as mergejoinable or hashjoinable here;
396 * that happens only if it appears in the right context (top level of a
399 restrictinfo
->parent_ec
= NULL
;
401 restrictinfo
->eval_cost
.startup
= -1;
402 restrictinfo
->norm_selec
= -1;
403 restrictinfo
->outer_selec
= -1;
405 restrictinfo
->mergeopfamilies
= NIL
;
407 restrictinfo
->left_ec
= NULL
;
408 restrictinfo
->right_ec
= NULL
;
409 restrictinfo
->left_em
= NULL
;
410 restrictinfo
->right_em
= NULL
;
411 restrictinfo
->scansel_cache
= NIL
;
413 restrictinfo
->outer_is_left
= false;
415 restrictinfo
->hashjoinoperator
= InvalidOid
;
417 restrictinfo
->left_bucketsize
= -1;
418 restrictinfo
->right_bucketsize
= -1;
424 * Recursively insert sub-RestrictInfo nodes into a boolean expression.
426 * We put RestrictInfos above simple (non-AND/OR) clauses and above
427 * sub-OR clauses, but not above sub-AND clauses, because there's no need.
428 * This may seem odd but it is closely related to the fact that we use
429 * implicit-AND lists at top level of RestrictInfo lists. Only ORs and
430 * simple clauses are valid RestrictInfos.
432 * The same is_pushed_down, outerjoin_delayed, and pseudoconstant flag
433 * values can be applied to all RestrictInfo nodes in the result. Likewise
434 * for nullable_relids.
436 * The given required_relids are attached to our top-level output,
437 * but any OR-clause constituents are allowed to default to just the
441 make_sub_restrictinfos(Expr
*clause
,
443 bool outerjoin_delayed
,
445 Relids required_relids
,
446 Relids nullable_relids
)
448 if (or_clause((Node
*) clause
))
453 foreach(temp
, ((BoolExpr
*) clause
)->args
)
454 orlist
= lappend(orlist
,
455 make_sub_restrictinfos(lfirst(temp
),
461 return (Expr
*) make_restrictinfo_internal(clause
,
462 make_orclause(orlist
),
469 else if (and_clause((Node
*) clause
))
474 foreach(temp
, ((BoolExpr
*) clause
)->args
)
475 andlist
= lappend(andlist
,
476 make_sub_restrictinfos(lfirst(temp
),
482 return make_andclause(andlist
);
485 return (Expr
*) make_restrictinfo_internal(clause
,
495 * restriction_is_or_clause
497 * Returns t iff the restrictinfo node contains an 'or' clause.
500 restriction_is_or_clause(RestrictInfo
*restrictinfo
)
502 if (restrictinfo
->orclause
!= NULL
)
511 * Returns a list containing the bare clauses from 'restrictinfo_list'.
513 * This is only to be used in cases where none of the RestrictInfos can
514 * be pseudoconstant clauses (for instance, it's OK on indexqual lists).
517 get_actual_clauses(List
*restrictinfo_list
)
522 foreach(l
, restrictinfo_list
)
524 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(l
);
526 Assert(IsA(rinfo
, RestrictInfo
));
528 Assert(!rinfo
->pseudoconstant
);
530 result
= lappend(result
, rinfo
->clause
);
536 * get_all_actual_clauses
538 * Returns a list containing the bare clauses from 'restrictinfo_list'.
540 * This loses the distinction between regular and pseudoconstant clauses,
541 * so be careful what you use it for.
544 get_all_actual_clauses(List
*restrictinfo_list
)
549 foreach(l
, restrictinfo_list
)
551 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(l
);
553 Assert(IsA(rinfo
, RestrictInfo
));
555 result
= lappend(result
, rinfo
->clause
);
561 * extract_actual_clauses
563 * Extract bare clauses from 'restrictinfo_list', returning either the
564 * regular ones or the pseudoconstant ones per 'pseudoconstant'.
567 extract_actual_clauses(List
*restrictinfo_list
,
573 foreach(l
, restrictinfo_list
)
575 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(l
);
577 Assert(IsA(rinfo
, RestrictInfo
));
579 if (rinfo
->pseudoconstant
== pseudoconstant
)
580 result
= lappend(result
, rinfo
->clause
);
586 * extract_actual_join_clauses
588 * Extract bare clauses from 'restrictinfo_list', separating those that
589 * syntactically match the join level from those that were pushed down.
590 * Pseudoconstant clauses are excluded from the results.
592 * This is only used at outer joins, since for plain joins we don't care
593 * about pushed-down-ness.
596 extract_actual_join_clauses(List
*restrictinfo_list
,
605 foreach(l
, restrictinfo_list
)
607 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(l
);
609 Assert(IsA(rinfo
, RestrictInfo
));
611 if (rinfo
->is_pushed_down
)
613 if (!rinfo
->pseudoconstant
)
614 *otherquals
= lappend(*otherquals
, rinfo
->clause
);
618 /* joinquals shouldn't have been marked pseudoconstant */
619 Assert(!rinfo
->pseudoconstant
);
620 *joinquals
= lappend(*joinquals
, rinfo
->clause
);
627 * select_nonredundant_join_clauses
629 * Given a list of RestrictInfo clauses that are to be applied in a join,
630 * select the ones that are not redundant with any clause that's enforced
631 * by the inner_path. This is used for nestloop joins, wherein any clause
632 * being used in an inner indexscan need not be checked again at the join.
634 * "Redundant" means either equal() or derived from the same EquivalenceClass.
635 * We have to check the latter because indxqual.c may select different derived
636 * clauses than were selected by generate_join_implied_equalities().
638 * Note that we are *not* checking for local redundancies within the given
639 * restrictinfo_list; that should have been handled elsewhere.
642 select_nonredundant_join_clauses(PlannerInfo
*root
,
643 List
*restrictinfo_list
,
646 if (IsA(inner_path
, IndexPath
))
649 * Check the index quals to see if any of them are join clauses.
651 * We can skip this if the index path is an ordinary indexpath and not
652 * a special innerjoin path, since it then wouldn't be using any join
655 IndexPath
*innerpath
= (IndexPath
*) inner_path
;
657 if (innerpath
->isjoininner
)
659 select_nonredundant_join_list(restrictinfo_list
,
660 innerpath
->indexclauses
);
662 else if (IsA(inner_path
, BitmapHeapPath
))
665 * Same deal for bitmapped index scans.
667 * Note: both here and above, we ignore any implicit index
668 * restrictions associated with the use of partial indexes. This is
669 * OK because we're only trying to prove we can dispense with some
670 * join quals; failing to prove that doesn't result in an incorrect
671 * plan. It's quite unlikely that a join qual could be proven
672 * redundant by an index predicate anyway. (Also, if we did manage to
673 * prove it, we'd have to have a special case for update targets; see
674 * notes about EvalPlanQual testing in create_indexscan_plan().)
676 BitmapHeapPath
*innerpath
= (BitmapHeapPath
*) inner_path
;
678 if (innerpath
->isjoininner
)
683 make_restrictinfo_from_bitmapqual(innerpath
->bitmapqual
,
687 select_nonredundant_join_list(restrictinfo_list
,
693 * XXX the inner path of a nestloop could also be an append relation whose
694 * elements use join quals. However, they might each use different quals;
695 * we could only remove join quals that are enforced by all the appendrel
696 * members. For the moment we don't bother to try.
699 return restrictinfo_list
;
703 * select_nonredundant_join_list
704 * Select the members of restrictinfo_list that are not redundant with
705 * any member of reference_list. See above for more info.
708 select_nonredundant_join_list(List
*restrictinfo_list
,
709 List
*reference_list
)
714 foreach(item
, restrictinfo_list
)
716 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(item
);
718 /* drop it if redundant with any reference clause */
719 if (join_clause_is_redundant(rinfo
, reference_list
))
722 /* otherwise, add it to result list */
723 result
= lappend(result
, rinfo
);
730 * join_clause_is_redundant
731 * Test whether rinfo is redundant with any clause in reference_list.
734 join_clause_is_redundant(RestrictInfo
*rinfo
,
735 List
*reference_list
)
739 foreach(refitem
, reference_list
)
741 RestrictInfo
*refrinfo
= (RestrictInfo
*) lfirst(refitem
);
743 /* always consider exact duplicates redundant */
744 if (equal(rinfo
, refrinfo
))
747 /* check if derived from same EquivalenceClass */
748 if (rinfo
->parent_ec
!= NULL
&&
749 rinfo
->parent_ec
== refrinfo
->parent_ec
)