PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
clausesel.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clausesel.c
4  * Routines to compute clause selectivities
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/optimizer/path/clausesel.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "nodes/makefuncs.h"
18 #include "optimizer/clauses.h"
19 #include "optimizer/cost.h"
20 #include "optimizer/pathnode.h"
21 #include "optimizer/plancat.h"
22 #include "utils/fmgroids.h"
23 #include "utils/lsyscache.h"
24 #include "utils/selfuncs.h"
25 #include "statistics/statistics.h"
26 
27 
28 /*
29  * Data structure for accumulating info about possible range-query
30  * clause pairs in clauselist_selectivity.
31  */
32 typedef struct RangeQueryClause
33 {
34  struct RangeQueryClause *next; /* next in linked list */
35  Node *var; /* The common variable of the clauses */
36  bool have_lobound; /* found a low-bound clause yet? */
37  bool have_hibound; /* found a high-bound clause yet? */
38  Selectivity lobound; /* Selectivity of a var > something clause */
39  Selectivity hibound; /* Selectivity of a var < something clause */
41 
42 static void addRangeClause(RangeQueryClause **rqlist, Node *clause,
43  bool varonleft, bool isLTsel, Selectivity s2);
45  List *clauses);
46 
47 /****************************************************************************
48  * ROUTINES TO COMPUTE SELECTIVITIES
49  ****************************************************************************/
50 
51 /*
52  * clauselist_selectivity -
53  * Compute the selectivity of an implicitly-ANDed list of boolean
54  * expression clauses. The list can be empty, in which case 1.0
55  * must be returned. List elements may be either RestrictInfos
56  * or bare expression clauses --- the former is preferred since
57  * it allows caching of results.
58  *
59  * See clause_selectivity() for the meaning of the additional parameters.
60  *
61  * Our basic approach is to take the product of the selectivities of the
62  * subclauses. However, that's only right if the subclauses have independent
63  * probabilities, and in reality they are often NOT independent. So,
64  * we want to be smarter where we can.
65  *
66  * If the clauses taken together refer to just one relation, we'll try to
67  * apply selectivity estimates using any extended statistics for that rel.
68  * Currently we only have (soft) functional dependencies, so apply these in as
69  * many cases as possible, and fall back on normal estimates for remaining
70  * clauses.
71  *
72  * We also recognize "range queries", such as "x > 34 AND x < 42". Clauses
73  * are recognized as possible range query components if they are restriction
74  * opclauses whose operators have scalarltsel or a related function as their
75  * restriction selectivity estimator. We pair up clauses of this form that
76  * refer to the same variable. An unpairable clause of this kind is simply
77  * multiplied into the selectivity product in the normal way. But when we
78  * find a pair, we know that the selectivities represent the relative
79  * positions of the low and high bounds within the column's range, so instead
80  * of figuring the selectivity as hisel * losel, we can figure it as hisel +
81  * losel - 1. (To visualize this, see that hisel is the fraction of the range
82  * below the high bound, while losel is the fraction above the low bound; so
83  * hisel can be interpreted directly as a 0..1 value but we need to convert
84  * losel to 1-losel before interpreting it as a value. Then the available
85  * range is 1-losel to hisel. However, this calculation double-excludes
86  * nulls, so really we need hisel + losel + null_frac - 1.)
87  *
88  * If either selectivity is exactly DEFAULT_INEQ_SEL, we forget this equation
89  * and instead use DEFAULT_RANGE_INEQ_SEL. The same applies if the equation
90  * yields an impossible (negative) result.
91  *
92  * A free side-effect is that we can recognize redundant inequalities such
93  * as "x < 4 AND x < 5"; only the tighter constraint will be counted.
94  *
95  * Of course this is all very dependent on the behavior of the inequality
96  * selectivity functions; perhaps some day we can generalize the approach.
97  */
100  List *clauses,
101  int varRelid,
102  JoinType jointype,
103  SpecialJoinInfo *sjinfo)
104 {
105  Selectivity s1 = 1.0;
106  RelOptInfo *rel;
107  Bitmapset *estimatedclauses = NULL;
108  RangeQueryClause *rqlist = NULL;
109  ListCell *l;
110  int listidx;
111 
112  /*
113  * If there's exactly one clause, just go directly to
114  * clause_selectivity(). None of what we might do below is relevant.
115  */
116  if (list_length(clauses) == 1)
117  return clause_selectivity(root, (Node *) linitial(clauses),
118  varRelid, jointype, sjinfo);
119 
120  /*
121  * Determine if these clauses reference a single relation. If so, and if
122  * it has extended statistics, try to apply those.
123  */
124  rel = find_single_rel_for_clauses(root, clauses);
125  if (rel && rel->rtekind == RTE_RELATION && rel->statlist != NIL)
126  {
127  /*
128  * Perform selectivity estimations on any clauses found applicable by
129  * dependencies_clauselist_selectivity. 'estimatedclauses' will be
130  * filled with the 0-based list positions of clauses used that way, so
131  * that we can ignore them below.
132  */
133  s1 *= dependencies_clauselist_selectivity(root, clauses, varRelid,
134  jointype, sjinfo, rel,
135  &estimatedclauses);
136 
137  /*
138  * This would be the place to apply any other types of extended
139  * statistics selectivity estimations for remaining clauses.
140  */
141  }
142 
143  /*
144  * Apply normal selectivity estimates for remaining clauses. We'll be
145  * careful to skip any clauses which were already estimated above.
146  *
147  * Anything that doesn't look like a potential rangequery clause gets
148  * multiplied into s1 and forgotten. Anything that does gets inserted into
149  * an rqlist entry.
150  */
151  listidx = -1;
152  foreach(l, clauses)
153  {
154  Node *clause = (Node *) lfirst(l);
155  RestrictInfo *rinfo;
156  Selectivity s2;
157 
158  listidx++;
159 
160  /*
161  * Skip this clause if it's already been estimated by some other
162  * statistics above.
163  */
164  if (bms_is_member(listidx, estimatedclauses))
165  continue;
166 
167  /* Always compute the selectivity using clause_selectivity */
168  s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo);
169 
170  /*
171  * Check for being passed a RestrictInfo.
172  *
173  * If it's a pseudoconstant RestrictInfo, then s2 is either 1.0 or
174  * 0.0; just use that rather than looking for range pairs.
175  */
176  if (IsA(clause, RestrictInfo))
177  {
178  rinfo = (RestrictInfo *) clause;
179  if (rinfo->pseudoconstant)
180  {
181  s1 = s1 * s2;
182  continue;
183  }
184  clause = (Node *) rinfo->clause;
185  }
186  else
187  rinfo = NULL;
188 
189  /*
190  * See if it looks like a restriction clause with a pseudoconstant on
191  * one side. (Anything more complicated than that might not behave in
192  * the simple way we are expecting.) Most of the tests here can be
193  * done more efficiently with rinfo than without.
194  */
195  if (is_opclause(clause) && list_length(((OpExpr *) clause)->args) == 2)
196  {
197  OpExpr *expr = (OpExpr *) clause;
198  bool varonleft = true;
199  bool ok;
200 
201  if (rinfo)
202  {
203  ok = (bms_membership(rinfo->clause_relids) == BMS_SINGLETON) &&
205  rinfo->right_relids) ||
206  (varonleft = false,
208  rinfo->left_relids)));
209  }
210  else
211  {
212  ok = (NumRelids(clause) == 1) &&
214  (varonleft = false,
216  }
217 
218  if (ok)
219  {
220  /*
221  * If it's not a "<"/"<="/">"/">=" operator, just merge the
222  * selectivity in generically. But if it's the right oprrest,
223  * add the clause to rqlist for later processing.
224  */
225  switch (get_oprrest(expr->opno))
226  {
227  case F_SCALARLTSEL:
228  case F_SCALARLESEL:
229  addRangeClause(&rqlist, clause,
230  varonleft, true, s2);
231  break;
232  case F_SCALARGTSEL:
233  case F_SCALARGESEL:
234  addRangeClause(&rqlist, clause,
235  varonleft, false, s2);
236  break;
237  default:
238  /* Just merge the selectivity in generically */
239  s1 = s1 * s2;
240  break;
241  }
242  continue; /* drop to loop bottom */
243  }
244  }
245 
246  /* Not the right form, so treat it generically. */
247  s1 = s1 * s2;
248  }
249 
250  /*
251  * Now scan the rangequery pair list.
252  */
253  while (rqlist != NULL)
254  {
255  RangeQueryClause *rqnext;
256 
257  if (rqlist->have_lobound && rqlist->have_hibound)
258  {
259  /* Successfully matched a pair of range clauses */
260  Selectivity s2;
261 
262  /*
263  * Exact equality to the default value probably means the
264  * selectivity function punted. This is not airtight but should
265  * be good enough.
266  */
267  if (rqlist->hibound == DEFAULT_INEQ_SEL ||
268  rqlist->lobound == DEFAULT_INEQ_SEL)
269  {
271  }
272  else
273  {
274  s2 = rqlist->hibound + rqlist->lobound - 1.0;
275 
276  /* Adjust for double-exclusion of NULLs */
277  s2 += nulltestsel(root, IS_NULL, rqlist->var,
278  varRelid, jointype, sjinfo);
279 
280  /*
281  * A zero or slightly negative s2 should be converted into a
282  * small positive value; we probably are dealing with a very
283  * tight range and got a bogus result due to roundoff errors.
284  * However, if s2 is very negative, then we probably have
285  * default selectivity estimates on one or both sides of the
286  * range that we failed to recognize above for some reason.
287  */
288  if (s2 <= 0.0)
289  {
290  if (s2 < -0.01)
291  {
292  /*
293  * No data available --- use a default estimate that
294  * is small, but not real small.
295  */
297  }
298  else
299  {
300  /*
301  * It's just roundoff error; use a small positive
302  * value
303  */
304  s2 = 1.0e-10;
305  }
306  }
307  }
308  /* Merge in the selectivity of the pair of clauses */
309  s1 *= s2;
310  }
311  else
312  {
313  /* Only found one of a pair, merge it in generically */
314  if (rqlist->have_lobound)
315  s1 *= rqlist->lobound;
316  else
317  s1 *= rqlist->hibound;
318  }
319  /* release storage and advance */
320  rqnext = rqlist->next;
321  pfree(rqlist);
322  rqlist = rqnext;
323  }
324 
325  return s1;
326 }
327 
328 /*
329  * addRangeClause --- add a new range clause for clauselist_selectivity
330  *
331  * Here is where we try to match up pairs of range-query clauses
332  */
333 static void
335  bool varonleft, bool isLTsel, Selectivity s2)
336 {
337  RangeQueryClause *rqelem;
338  Node *var;
339  bool is_lobound;
340 
341  if (varonleft)
342  {
343  var = get_leftop((Expr *) clause);
344  is_lobound = !isLTsel; /* x < something is high bound */
345  }
346  else
347  {
348  var = get_rightop((Expr *) clause);
349  is_lobound = isLTsel; /* something < x is low bound */
350  }
351 
352  for (rqelem = *rqlist; rqelem; rqelem = rqelem->next)
353  {
354  /*
355  * We use full equal() here because the "var" might be a function of
356  * one or more attributes of the same relation...
357  */
358  if (!equal(var, rqelem->var))
359  continue;
360  /* Found the right group to put this clause in */
361  if (is_lobound)
362  {
363  if (!rqelem->have_lobound)
364  {
365  rqelem->have_lobound = true;
366  rqelem->lobound = s2;
367  }
368  else
369  {
370 
371  /*------
372  * We have found two similar clauses, such as
373  * x < y AND x <= z.
374  * Keep only the more restrictive one.
375  *------
376  */
377  if (rqelem->lobound > s2)
378  rqelem->lobound = s2;
379  }
380  }
381  else
382  {
383  if (!rqelem->have_hibound)
384  {
385  rqelem->have_hibound = true;
386  rqelem->hibound = s2;
387  }
388  else
389  {
390 
391  /*------
392  * We have found two similar clauses, such as
393  * x > y AND x >= z.
394  * Keep only the more restrictive one.
395  *------
396  */
397  if (rqelem->hibound > s2)
398  rqelem->hibound = s2;
399  }
400  }
401  return;
402  }
403 
404  /* No matching var found, so make a new clause-pair data structure */
405  rqelem = (RangeQueryClause *) palloc(sizeof(RangeQueryClause));
406  rqelem->var = var;
407  if (is_lobound)
408  {
409  rqelem->have_lobound = true;
410  rqelem->have_hibound = false;
411  rqelem->lobound = s2;
412  }
413  else
414  {
415  rqelem->have_lobound = false;
416  rqelem->have_hibound = true;
417  rqelem->hibound = s2;
418  }
419  rqelem->next = *rqlist;
420  *rqlist = rqelem;
421 }
422 
423 /*
424  * find_single_rel_for_clauses
425  * Examine each clause in 'clauses' and determine if all clauses
426  * reference only a single relation. If so return that relation,
427  * otherwise return NULL.
428  */
429 static RelOptInfo *
431 {
432  int lastrelid = 0;
433  ListCell *l;
434 
435  foreach(l, clauses)
436  {
437  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
438  int relid;
439 
440  /*
441  * If we have a list of bare clauses rather than RestrictInfos, we
442  * could pull out their relids the hard way with pull_varnos().
443  * However, currently the extended-stats machinery won't do anything
444  * with non-RestrictInfo clauses anyway, so there's no point in
445  * spending extra cycles; just fail if that's what we have.
446  */
447  if (!IsA(rinfo, RestrictInfo))
448  return NULL;
449 
450  if (bms_is_empty(rinfo->clause_relids))
451  continue; /* we can ignore variable-free clauses */
452  if (!bms_get_singleton_member(rinfo->clause_relids, &relid))
453  return NULL; /* multiple relations in this clause */
454  if (lastrelid == 0)
455  lastrelid = relid; /* first clause referencing a relation */
456  else if (relid != lastrelid)
457  return NULL; /* relation not same as last one */
458  }
459 
460  if (lastrelid != 0)
461  return find_base_rel(root, lastrelid);
462 
463  return NULL; /* no clauses */
464 }
465 
466 /*
467  * bms_is_subset_singleton
468  *
469  * Same result as bms_is_subset(s, bms_make_singleton(x)),
470  * but a little faster and doesn't leak memory.
471  *
472  * Is this of use anywhere else? If so move to bitmapset.c ...
473  */
474 static bool
476 {
477  switch (bms_membership(s))
478  {
479  case BMS_EMPTY_SET:
480  return true;
481  case BMS_SINGLETON:
482  return bms_is_member(x, s);
483  case BMS_MULTIPLE:
484  return false;
485  }
486  /* can't get here... */
487  return false;
488 }
489 
490 /*
491  * treat_as_join_clause -
492  * Decide whether an operator clause is to be handled by the
493  * restriction or join estimator. Subroutine for clause_selectivity().
494  */
495 static inline bool
497  int varRelid, SpecialJoinInfo *sjinfo)
498 {
499  if (varRelid != 0)
500  {
501  /*
502  * Caller is forcing restriction mode (eg, because we are examining an
503  * inner indexscan qual).
504  */
505  return false;
506  }
507  else if (sjinfo == NULL)
508  {
509  /*
510  * It must be a restriction clause, since it's being evaluated at a
511  * scan node.
512  */
513  return false;
514  }
515  else
516  {
517  /*
518  * Otherwise, it's a join if there's more than one relation used. We
519  * can optimize this calculation if an rinfo was passed.
520  *
521  * XXX Since we know the clause is being evaluated at a join, the
522  * only way it could be single-relation is if it was delayed by outer
523  * joins. Although we can make use of the restriction qual estimators
524  * anyway, it seems likely that we ought to account for the
525  * probability of injected nulls somehow.
526  */
527  if (rinfo)
528  return (bms_membership(rinfo->clause_relids) == BMS_MULTIPLE);
529  else
530  return (NumRelids(clause) > 1);
531  }
532 }
533 
534 
535 /*
536  * clause_selectivity -
537  * Compute the selectivity of a general boolean expression clause.
538  *
539  * The clause can be either a RestrictInfo or a plain expression. If it's
540  * a RestrictInfo, we try to cache the selectivity for possible re-use,
541  * so passing RestrictInfos is preferred.
542  *
543  * varRelid is either 0 or a rangetable index.
544  *
545  * When varRelid is not 0, only variables belonging to that relation are
546  * considered in computing selectivity; other vars are treated as constants
547  * of unknown values. This is appropriate for estimating the selectivity of
548  * a join clause that is being used as a restriction clause in a scan of a
549  * nestloop join's inner relation --- varRelid should then be the ID of the
550  * inner relation.
551  *
552  * When varRelid is 0, all variables are treated as variables. This
553  * is appropriate for ordinary join clauses and restriction clauses.
554  *
555  * jointype is the join type, if the clause is a join clause. Pass JOIN_INNER
556  * if the clause isn't a join clause.
557  *
558  * sjinfo is NULL for a non-join clause, otherwise it provides additional
559  * context information about the join being performed. There are some
560  * special cases:
561  * 1. For a special (not INNER) join, sjinfo is always a member of
562  * root->join_info_list.
563  * 2. For an INNER join, sjinfo is just a transient struct, and only the
564  * relids and jointype fields in it can be trusted.
565  * It is possible for jointype to be different from sjinfo->jointype.
566  * This indicates we are considering a variant join: either with
567  * the LHS and RHS switched, or with one input unique-ified.
568  *
569  * Note: when passing nonzero varRelid, it's normally appropriate to set
570  * jointype == JOIN_INNER, sjinfo == NULL, even if the clause is really a
571  * join clause; because we aren't treating it as a join clause.
572  */
575  Node *clause,
576  int varRelid,
577  JoinType jointype,
578  SpecialJoinInfo *sjinfo)
579 {
580  Selectivity s1 = 0.5; /* default for any unhandled clause type */
581  RestrictInfo *rinfo = NULL;
582  bool cacheable = false;
583 
584  if (clause == NULL) /* can this still happen? */
585  return s1;
586 
587  if (IsA(clause, RestrictInfo))
588  {
589  rinfo = (RestrictInfo *) clause;
590 
591  /*
592  * If the clause is marked pseudoconstant, then it will be used as a
593  * gating qual and should not affect selectivity estimates; hence
594  * return 1.0. The only exception is that a constant FALSE may be
595  * taken as having selectivity 0.0, since it will surely mean no rows
596  * out of the plan. This case is simple enough that we need not
597  * bother caching the result.
598  */
599  if (rinfo->pseudoconstant)
600  {
601  if (!IsA(rinfo->clause, Const))
602  return (Selectivity) 1.0;
603  }
604 
605  /*
606  * If the clause is marked redundant, always return 1.0.
607  */
608  if (rinfo->norm_selec > 1)
609  return (Selectivity) 1.0;
610 
611  /*
612  * If possible, cache the result of the selectivity calculation for
613  * the clause. We can cache if varRelid is zero or the clause
614  * contains only vars of that relid --- otherwise varRelid will affect
615  * the result, so mustn't cache. Outer join quals might be examined
616  * with either their join's actual jointype or JOIN_INNER, so we need
617  * two cache variables to remember both cases. Note: we assume the
618  * result won't change if we are switching the input relations or
619  * considering a unique-ified case, so we only need one cache variable
620  * for all non-JOIN_INNER cases.
621  */
622  if (varRelid == 0 ||
623  bms_is_subset_singleton(rinfo->clause_relids, varRelid))
624  {
625  /* Cacheable --- do we already have the result? */
626  if (jointype == JOIN_INNER)
627  {
628  if (rinfo->norm_selec >= 0)
629  return rinfo->norm_selec;
630  }
631  else
632  {
633  if (rinfo->outer_selec >= 0)
634  return rinfo->outer_selec;
635  }
636  cacheable = true;
637  }
638 
639  /*
640  * Proceed with examination of contained clause. If the clause is an
641  * OR-clause, we want to look at the variant with sub-RestrictInfos,
642  * so that per-subclause selectivities can be cached.
643  */
644  if (rinfo->orclause)
645  clause = (Node *) rinfo->orclause;
646  else
647  clause = (Node *) rinfo->clause;
648  }
649 
650  if (IsA(clause, Var))
651  {
652  Var *var = (Var *) clause;
653 
654  /*
655  * We probably shouldn't ever see an uplevel Var here, but if we do,
656  * return the default selectivity...
657  */
658  if (var->varlevelsup == 0 &&
659  (varRelid == 0 || varRelid == (int) var->varno))
660  {
661  /* Use the restriction selectivity function for a bool Var */
662  s1 = boolvarsel(root, (Node *) var, varRelid);
663  }
664  }
665  else if (IsA(clause, Const))
666  {
667  /* bool constant is pretty easy... */
668  Const *con = (Const *) clause;
669 
670  s1 = con->constisnull ? 0.0 :
671  DatumGetBool(con->constvalue) ? 1.0 : 0.0;
672  }
673  else if (IsA(clause, Param))
674  {
675  /* see if we can replace the Param */
676  Node *subst = estimate_expression_value(root, clause);
677 
678  if (IsA(subst, Const))
679  {
680  /* bool constant is pretty easy... */
681  Const *con = (Const *) subst;
682 
683  s1 = con->constisnull ? 0.0 :
684  DatumGetBool(con->constvalue) ? 1.0 : 0.0;
685  }
686  else
687  {
688  /* XXX any way to do better than default? */
689  }
690  }
691  else if (not_clause(clause))
692  {
693  /* inverse of the selectivity of the underlying clause */
694  s1 = 1.0 - clause_selectivity(root,
695  (Node *) get_notclausearg((Expr *) clause),
696  varRelid,
697  jointype,
698  sjinfo);
699  }
700  else if (and_clause(clause))
701  {
702  /* share code with clauselist_selectivity() */
703  s1 = clauselist_selectivity(root,
704  ((BoolExpr *) clause)->args,
705  varRelid,
706  jointype,
707  sjinfo);
708  }
709  else if (or_clause(clause))
710  {
711  /*
712  * Selectivities for an OR clause are computed as s1+s2 - s1*s2 to
713  * account for the probable overlap of selected tuple sets.
714  *
715  * XXX is this too conservative?
716  */
717  ListCell *arg;
718 
719  s1 = 0.0;
720  foreach(arg, ((BoolExpr *) clause)->args)
721  {
723  (Node *) lfirst(arg),
724  varRelid,
725  jointype,
726  sjinfo);
727 
728  s1 = s1 + s2 - s1 * s2;
729  }
730  }
731  else if (is_opclause(clause) || IsA(clause, DistinctExpr))
732  {
733  OpExpr *opclause = (OpExpr *) clause;
734  Oid opno = opclause->opno;
735 
736  if (treat_as_join_clause(clause, rinfo, varRelid, sjinfo))
737  {
738  /* Estimate selectivity for a join clause. */
739  s1 = join_selectivity(root, opno,
740  opclause->args,
741  opclause->inputcollid,
742  jointype,
743  sjinfo);
744  }
745  else
746  {
747  /* Estimate selectivity for a restriction clause. */
748  s1 = restriction_selectivity(root, opno,
749  opclause->args,
750  opclause->inputcollid,
751  varRelid);
752  }
753 
754  /*
755  * DistinctExpr has the same representation as OpExpr, but the
756  * contained operator is "=" not "<>", so we must negate the result.
757  * This estimation method doesn't give the right behavior for nulls,
758  * but it's better than doing nothing.
759  */
760  if (IsA(clause, DistinctExpr))
761  s1 = 1.0 - s1;
762  }
763  else if (IsA(clause, ScalarArrayOpExpr))
764  {
765  /* Use node specific selectivity calculation function */
766  s1 = scalararraysel(root,
767  (ScalarArrayOpExpr *) clause,
768  treat_as_join_clause(clause, rinfo,
769  varRelid, sjinfo),
770  varRelid,
771  jointype,
772  sjinfo);
773  }
774  else if (IsA(clause, RowCompareExpr))
775  {
776  /* Use node specific selectivity calculation function */
777  s1 = rowcomparesel(root,
778  (RowCompareExpr *) clause,
779  varRelid,
780  jointype,
781  sjinfo);
782  }
783  else if (IsA(clause, NullTest))
784  {
785  /* Use node specific selectivity calculation function */
786  s1 = nulltestsel(root,
787  ((NullTest *) clause)->nulltesttype,
788  (Node *) ((NullTest *) clause)->arg,
789  varRelid,
790  jointype,
791  sjinfo);
792  }
793  else if (IsA(clause, BooleanTest))
794  {
795  /* Use node specific selectivity calculation function */
796  s1 = booltestsel(root,
797  ((BooleanTest *) clause)->booltesttype,
798  (Node *) ((BooleanTest *) clause)->arg,
799  varRelid,
800  jointype,
801  sjinfo);
802  }
803  else if (IsA(clause, CurrentOfExpr))
804  {
805  /* CURRENT OF selects at most one row of its table */
806  CurrentOfExpr *cexpr = (CurrentOfExpr *) clause;
807  RelOptInfo *crel = find_base_rel(root, cexpr->cvarno);
808 
809  if (crel->tuples > 0)
810  s1 = 1.0 / crel->tuples;
811  }
812  else if (IsA(clause, RelabelType))
813  {
814  /* Not sure this case is needed, but it can't hurt */
815  s1 = clause_selectivity(root,
816  (Node *) ((RelabelType *) clause)->arg,
817  varRelid,
818  jointype,
819  sjinfo);
820  }
821  else if (IsA(clause, CoerceToDomain))
822  {
823  /* Not sure this case is needed, but it can't hurt */
824  s1 = clause_selectivity(root,
825  (Node *) ((CoerceToDomain *) clause)->arg,
826  varRelid,
827  jointype,
828  sjinfo);
829  }
830  else
831  {
832  /*
833  * For anything else, see if we can consider it as a boolean variable.
834  * This only works if it's an immutable expression in Vars of a single
835  * relation; but there's no point in us checking that here because
836  * boolvarsel() will do it internally, and return a suitable default
837  * selectivity if not.
838  */
839  s1 = boolvarsel(root, clause, varRelid);
840  }
841 
842  /* Cache the result if possible */
843  if (cacheable)
844  {
845  if (jointype == JOIN_INNER)
846  rinfo->norm_selec = s1;
847  else
848  rinfo->outer_selec = s1;
849  }
850 
851 #ifdef SELECTIVITY_DEBUG
852  elog(DEBUG4, "clause_selectivity: s1 %f", s1);
853 #endif /* SELECTIVITY_DEBUG */
854 
855  return s1;
856 }
Datum constvalue
Definition: primnodes.h:196
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:265
#define NIL
Definition: pg_list.h:69
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
Definition: dependencies.c:911
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * statlist
Definition: relation.h:563
bool is_pseudo_constant_clause_relids(Node *clause, Relids relids)
Definition: clauses.c:2216
Index varlevelsup
Definition: primnodes.h:173
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2964
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2454
Expr * orclause
Definition: relation.h:1778
#define DEFAULT_INEQ_SEL
Definition: selfuncs.h:37
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1653
double tuples
Definition: relation.h:565
Selectivity rowcomparesel(PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:2196
Relids clause_relids
Definition: relation.h:1762
bool pseudoconstant
Definition: relation.h:1755
Definition: nodes.h:509
Relids left_relids
Definition: relation.h:1774
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:569
double Selectivity
Definition: nodes.h:639
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
#define DEBUG4
Definition: elog.h:22
#define lsecond(l)
Definition: pg_list.h:116
Selectivity scalararraysel(PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1839
Selectivity norm_selec
Definition: relation.h:1785
JoinType
Definition: nodes.h:673
static bool treat_as_join_clause(Node *clause, RestrictInfo *rinfo, int varRelid, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:496
static bool bms_is_subset_singleton(const Bitmapset *s, int x)
Definition: clausesel.c:475
void pfree(void *pointer)
Definition: mcxt.c:949
#define linitial(l)
Definition: pg_list.h:111
char * s1
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1739
bool and_clause(Node *clause)
Definition: clauses.c:314
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:574
#define DatumGetBool(X)
Definition: postgres.h:399
Selectivity outer_selec
Definition: relation.h:1788
bool is_pseudo_constant_clause(Node *clause)
Definition: clauses.c:2196
struct RangeQueryClause * next
Definition: clausesel.c:34
static void addRangeClause(RangeQueryClause **rqlist, Node *clause, bool varonleft, bool isLTsel, Selectivity s2)
Definition: clausesel.c:334
Selectivity hibound
Definition: clausesel.c:39
bool not_clause(Node *clause)
Definition: clauses.c:236
Expr * clause
Definition: relation.h:1747
Selectivity lobound
Definition: clausesel.c:38
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Index varno
Definition: primnodes.h:166
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1361
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
char * s2
bool or_clause(Node *clause)
Definition: clauses.c:280
RTEKind rtekind
Definition: relation.h:555
static RelOptInfo * find_single_rel_for_clauses(PlannerInfo *root, List *clauses)
Definition: clausesel.c:430
Relids right_relids
Definition: relation.h:1775
struct RangeQueryClause RangeQueryClause
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Oid inputcollid
Definition: primnodes.h:501
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
void * palloc(Size size)
Definition: mcxt.c:848
#define DEFAULT_RANGE_INEQ_SEL
Definition: selfuncs.h:40
void * arg
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1690
Oid opno
Definition: primnodes.h:496
#define elog
Definition: elog.h:219
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:99
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
List * args
Definition: primnodes.h:502
Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1581
Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
Definition: selfuncs.c:1542
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
bool constisnull
Definition: primnodes.h:197
int NumRelids(Node *clause)
Definition: clauses.c:2238