PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
selfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * selfuncs.c
4  * Selectivity functions and index cost estimation functions for
5  * standard operators and index access methods.
6  *
7  * Selectivity routines are registered in the pg_operator catalog
8  * in the "oprrest" and "oprjoin" attributes.
9  *
10  * Index cost functions are located via the index AM's API struct,
11  * which is obtained from the handler function registered in pg_am.
12  *
13  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  *
17  * IDENTIFICATION
18  * src/backend/utils/adt/selfuncs.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 
23 /*----------
24  * Operator selectivity estimation functions are called to estimate the
25  * selectivity of WHERE clauses whose top-level operator is their operator.
26  * We divide the problem into two cases:
27  * Restriction clause estimation: the clause involves vars of just
28  * one relation.
29  * Join clause estimation: the clause involves vars of multiple rels.
30  * Join selectivity estimation is far more difficult and usually less accurate
31  * than restriction estimation.
32  *
33  * When dealing with the inner scan of a nestloop join, we consider the
34  * join's joinclauses as restriction clauses for the inner relation, and
35  * treat vars of the outer relation as parameters (a/k/a constants of unknown
36  * values). So, restriction estimators need to be able to accept an argument
37  * telling which relation is to be treated as the variable.
38  *
39  * The call convention for a restriction estimator (oprrest function) is
40  *
41  * Selectivity oprrest (PlannerInfo *root,
42  * Oid operator,
43  * List *args,
44  * int varRelid);
45  *
46  * root: general information about the query (rtable and RelOptInfo lists
47  * are particularly important for the estimator).
48  * operator: OID of the specific operator in question.
49  * args: argument list from the operator clause.
50  * varRelid: if not zero, the relid (rtable index) of the relation to
51  * be treated as the variable relation. May be zero if the args list
52  * is known to contain vars of only one relation.
53  *
54  * This is represented at the SQL level (in pg_proc) as
55  *
56  * float8 oprrest (internal, oid, internal, int4);
57  *
58  * The result is a selectivity, that is, a fraction (0 to 1) of the rows
59  * of the relation that are expected to produce a TRUE result for the
60  * given operator.
61  *
62  * The call convention for a join estimator (oprjoin function) is similar
63  * except that varRelid is not needed, and instead join information is
64  * supplied:
65  *
66  * Selectivity oprjoin (PlannerInfo *root,
67  * Oid operator,
68  * List *args,
69  * JoinType jointype,
70  * SpecialJoinInfo *sjinfo);
71  *
72  * float8 oprjoin (internal, oid, internal, int2, internal);
73  *
74  * (Before Postgres 8.4, join estimators had only the first four of these
75  * parameters. That signature is still allowed, but deprecated.) The
76  * relationship between jointype and sjinfo is explained in the comments for
77  * clause_selectivity() --- the short version is that jointype is usually
78  * best ignored in favor of examining sjinfo.
79  *
80  * Join selectivity for regular inner and outer joins is defined as the
81  * fraction (0 to 1) of the cross product of the relations that is expected
82  * to produce a TRUE result for the given operator. For both semi and anti
83  * joins, however, the selectivity is defined as the fraction of the left-hand
84  * side relation's rows that are expected to have a match (ie, at least one
85  * row with a TRUE result) in the right-hand side.
86  *
87  * For both oprrest and oprjoin functions, the operator's input collation OID
88  * (if any) is passed using the standard fmgr mechanism, so that the estimator
89  * function can fetch it with PG_GET_COLLATION(). Note, however, that all
90  * statistics in pg_statistic are currently built using the database's default
91  * collation. Thus, in most cases where we are looking at statistics, we
92  * should ignore the actual operator collation and use DEFAULT_COLLATION_OID.
93  * We expect that the error induced by doing this is usually not large enough
94  * to justify complicating matters.
95  *----------
96  */
97 
98 #include "postgres.h"
99 
100 #include <ctype.h>
101 #include <float.h>
102 #include <math.h>
103 
104 #include "access/brin.h"
105 #include "access/gin.h"
106 #include "access/htup_details.h"
107 #include "access/sysattr.h"
108 #include "catalog/index.h"
109 #include "catalog/pg_am.h"
110 #include "catalog/pg_collation.h"
111 #include "catalog/pg_operator.h"
112 #include "catalog/pg_opfamily.h"
113 #include "catalog/pg_statistic.h"
115 #include "catalog/pg_type.h"
116 #include "executor/executor.h"
117 #include "mb/pg_wchar.h"
118 #include "miscadmin.h"
119 #include "nodes/makefuncs.h"
120 #include "nodes/nodeFuncs.h"
121 #include "optimizer/clauses.h"
122 #include "optimizer/cost.h"
123 #include "optimizer/pathnode.h"
124 #include "optimizer/paths.h"
125 #include "optimizer/plancat.h"
126 #include "optimizer/predtest.h"
127 #include "optimizer/restrictinfo.h"
128 #include "optimizer/var.h"
129 #include "parser/parse_clause.h"
130 #include "parser/parse_coerce.h"
131 #include "parser/parsetree.h"
132 #include "statistics/statistics.h"
133 #include "utils/acl.h"
134 #include "utils/builtins.h"
135 #include "utils/bytea.h"
136 #include "utils/date.h"
137 #include "utils/datum.h"
138 #include "utils/fmgroids.h"
139 #include "utils/index_selfuncs.h"
140 #include "utils/lsyscache.h"
141 #include "utils/nabstime.h"
142 #include "utils/pg_locale.h"
143 #include "utils/rel.h"
144 #include "utils/selfuncs.h"
145 #include "utils/spccache.h"
146 #include "utils/syscache.h"
147 #include "utils/timestamp.h"
148 #include "utils/tqual.h"
149 #include "utils/typcache.h"
150 #include "utils/varlena.h"
151 
152 
153 /* Hooks for plugins to get control when we ask for stats */
156 
157 static double eqsel_internal(PG_FUNCTION_ARGS, bool negate);
158 static double var_eq_const(VariableStatData *vardata, Oid operator,
159  Datum constval, bool constisnull,
160  bool varonleft, bool negate);
161 static double var_eq_non_const(VariableStatData *vardata, Oid operator,
162  Node *other,
163  bool varonleft, bool negate);
164 static double ineq_histogram_selectivity(PlannerInfo *root,
165  VariableStatData *vardata,
166  FmgrInfo *opproc, bool isgt,
167  Datum constval, Oid consttype);
168 static double eqjoinsel_inner(Oid operator,
169  VariableStatData *vardata1, VariableStatData *vardata2);
170 static double eqjoinsel_semi(Oid operator,
171  VariableStatData *vardata1, VariableStatData *vardata2,
172  RelOptInfo *inner_rel);
174  RelOptInfo *rel, List **varinfos, double *ndistinct);
175 static bool convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
176  Datum lobound, Datum hibound, Oid boundstypid,
177  double *scaledlobound, double *scaledhibound);
178 static double convert_numeric_to_scalar(Datum value, Oid typid);
179 static void convert_string_to_scalar(char *value,
180  double *scaledvalue,
181  char *lobound,
182  double *scaledlobound,
183  char *hibound,
184  double *scaledhibound);
186  double *scaledvalue,
187  Datum lobound,
188  double *scaledlobound,
189  Datum hibound,
190  double *scaledhibound);
191 static double convert_one_string_to_scalar(char *value,
192  int rangelo, int rangehi);
193 static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
194  int rangelo, int rangehi);
195 static char *convert_string_datum(Datum value, Oid typid);
196 static double convert_timevalue_to_scalar(Datum value, Oid typid);
197 static void examine_simple_variable(PlannerInfo *root, Var *var,
198  VariableStatData *vardata);
199 static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata,
200  Oid sortop, Datum *min, Datum *max);
201 static bool get_actual_variable_range(PlannerInfo *root,
202  VariableStatData *vardata,
203  Oid sortop,
204  Datum *min, Datum *max);
205 static RelOptInfo *find_join_input_rel(PlannerInfo *root, Relids relids);
207  VariableStatData *vardata,
208  Oid vartype, Oid opfamily, Const *prefixcon);
209 static Selectivity like_selectivity(const char *patt, int pattlen,
210  bool case_insensitive);
211 static Selectivity regex_selectivity(const char *patt, int pattlen,
212  bool case_insensitive,
213  int fixed_prefix_len);
214 static Datum string_to_datum(const char *str, Oid datatype);
215 static Const *string_to_const(const char *str, Oid datatype);
216 static Const *string_to_bytea_const(const char *str, size_t str_len);
217 static List *add_predicate_to_quals(IndexOptInfo *index, List *indexQuals);
218 
219 
220 /*
221  * eqsel - Selectivity of "=" for any data types.
222  *
223  * Note: this routine is also used to estimate selectivity for some
224  * operators that are not "=" but have comparable selectivity behavior,
225  * such as "~=" (geometric approximate-match). Even for "=", we must
226  * keep in mind that the left and right datatypes may differ.
227  */
228 Datum
230 {
231  PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, false));
232 }
233 
234 /*
235  * Common code for eqsel() and neqsel()
236  */
237 static double
239 {
241  Oid operator = PG_GETARG_OID(1);
242  List *args = (List *) PG_GETARG_POINTER(2);
243  int varRelid = PG_GETARG_INT32(3);
244  VariableStatData vardata;
245  Node *other;
246  bool varonleft;
247  double selec;
248 
249  /*
250  * When asked about <>, we do the estimation using the corresponding =
251  * operator, then convert to <> via "1.0 - eq_selectivity - nullfrac".
252  */
253  if (negate)
254  {
255  operator = get_negator(operator);
256  if (!OidIsValid(operator))
257  {
258  /* Use default selectivity (should we raise an error instead?) */
259  return 1.0 - DEFAULT_EQ_SEL;
260  }
261  }
262 
263  /*
264  * If expression is not variable = something or something = variable, then
265  * punt and return a default estimate.
266  */
267  if (!get_restriction_variable(root, args, varRelid,
268  &vardata, &other, &varonleft))
269  return negate ? (1.0 - DEFAULT_EQ_SEL) : DEFAULT_EQ_SEL;
270 
271  /*
272  * We can do a lot better if the something is a constant. (Note: the
273  * Const might result from estimation rather than being a simple constant
274  * in the query.)
275  */
276  if (IsA(other, Const))
277  selec = var_eq_const(&vardata, operator,
278  ((Const *) other)->constvalue,
279  ((Const *) other)->constisnull,
280  varonleft, negate);
281  else
282  selec = var_eq_non_const(&vardata, operator, other,
283  varonleft, negate);
284 
285  ReleaseVariableStats(vardata);
286 
287  return selec;
288 }
289 
290 /*
291  * var_eq_const --- eqsel for var = const case
292  *
293  * This is split out so that some other estimation functions can use it.
294  */
295 static double
296 var_eq_const(VariableStatData *vardata, Oid operator,
297  Datum constval, bool constisnull,
298  bool varonleft, bool negate)
299 {
300  double selec;
301  double nullfrac = 0.0;
302  bool isdefault;
303  Oid opfuncoid;
304 
305  /*
306  * If the constant is NULL, assume operator is strict and return zero, ie,
307  * operator will never return TRUE. (It's zero even for a negator op.)
308  */
309  if (constisnull)
310  return 0.0;
311 
312  /*
313  * Grab the nullfrac for use below. Note we allow use of nullfrac
314  * regardless of security check.
315  */
316  if (HeapTupleIsValid(vardata->statsTuple))
317  {
318  Form_pg_statistic stats;
319 
320  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
321  nullfrac = stats->stanullfrac;
322  }
323 
324  /*
325  * If we matched the var to a unique index or DISTINCT clause, assume
326  * there is exactly one match regardless of anything else. (This is
327  * slightly bogus, since the index or clause's equality operator might be
328  * different from ours, but it's much more likely to be right than
329  * ignoring the information.)
330  */
331  if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
332  {
333  selec = 1.0 / vardata->rel->tuples;
334  }
335  else if (HeapTupleIsValid(vardata->statsTuple) &&
337  (opfuncoid = get_opcode(operator))))
338  {
339  AttStatsSlot sslot;
340  bool match = false;
341  int i;
342 
343  /*
344  * Is the constant "=" to any of the column's most common values?
345  * (Although the given operator may not really be "=", we will assume
346  * that seeing whether it returns TRUE is an appropriate test. If you
347  * don't like this, maybe you shouldn't be using eqsel for your
348  * operator...)
349  */
350  if (get_attstatsslot(&sslot, vardata->statsTuple,
353  {
354  FmgrInfo eqproc;
355 
356  fmgr_info(opfuncoid, &eqproc);
357 
358  for (i = 0; i < sslot.nvalues; i++)
359  {
360  /* be careful to apply operator right way 'round */
361  if (varonleft)
362  match = DatumGetBool(FunctionCall2Coll(&eqproc,
364  sslot.values[i],
365  constval));
366  else
367  match = DatumGetBool(FunctionCall2Coll(&eqproc,
369  constval,
370  sslot.values[i]));
371  if (match)
372  break;
373  }
374  }
375  else
376  {
377  /* no most-common-value info available */
378  i = 0; /* keep compiler quiet */
379  }
380 
381  if (match)
382  {
383  /*
384  * Constant is "=" to this common value. We know selectivity
385  * exactly (or as exactly as ANALYZE could calculate it, anyway).
386  */
387  selec = sslot.numbers[i];
388  }
389  else
390  {
391  /*
392  * Comparison is against a constant that is neither NULL nor any
393  * of the common values. Its selectivity cannot be more than
394  * this:
395  */
396  double sumcommon = 0.0;
397  double otherdistinct;
398 
399  for (i = 0; i < sslot.nnumbers; i++)
400  sumcommon += sslot.numbers[i];
401  selec = 1.0 - sumcommon - nullfrac;
402  CLAMP_PROBABILITY(selec);
403 
404  /*
405  * and in fact it's probably a good deal less. We approximate that
406  * all the not-common values share this remaining fraction
407  * equally, so we divide by the number of other distinct values.
408  */
409  otherdistinct = get_variable_numdistinct(vardata, &isdefault) -
410  sslot.nnumbers;
411  if (otherdistinct > 1)
412  selec /= otherdistinct;
413 
414  /*
415  * Another cross-check: selectivity shouldn't be estimated as more
416  * than the least common "most common value".
417  */
418  if (sslot.nnumbers > 0 && selec > sslot.numbers[sslot.nnumbers - 1])
419  selec = sslot.numbers[sslot.nnumbers - 1];
420  }
421 
422  free_attstatsslot(&sslot);
423  }
424  else
425  {
426  /*
427  * No ANALYZE stats available, so make a guess using estimated number
428  * of distinct values and assuming they are equally common. (The guess
429  * is unlikely to be very good, but we do know a few special cases.)
430  */
431  selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
432  }
433 
434  /* now adjust if we wanted <> rather than = */
435  if (negate)
436  selec = 1.0 - selec - nullfrac;
437 
438  /* result should be in range, but make sure... */
439  CLAMP_PROBABILITY(selec);
440 
441  return selec;
442 }
443 
444 /*
445  * var_eq_non_const --- eqsel for var = something-other-than-const case
446  */
447 static double
449  Node *other,
450  bool varonleft, bool negate)
451 {
452  double selec;
453  double nullfrac = 0.0;
454  bool isdefault;
455 
456  /*
457  * Grab the nullfrac for use below.
458  */
459  if (HeapTupleIsValid(vardata->statsTuple))
460  {
461  Form_pg_statistic stats;
462 
463  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
464  nullfrac = stats->stanullfrac;
465  }
466 
467  /*
468  * If we matched the var to a unique index or DISTINCT clause, assume
469  * there is exactly one match regardless of anything else. (This is
470  * slightly bogus, since the index or clause's equality operator might be
471  * different from ours, but it's much more likely to be right than
472  * ignoring the information.)
473  */
474  if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
475  {
476  selec = 1.0 / vardata->rel->tuples;
477  }
478  else if (HeapTupleIsValid(vardata->statsTuple))
479  {
480  double ndistinct;
481  AttStatsSlot sslot;
482 
483  /*
484  * Search is for a value that we do not know a priori, but we will
485  * assume it is not NULL. Estimate the selectivity as non-null
486  * fraction divided by number of distinct values, so that we get a
487  * result averaged over all possible values whether common or
488  * uncommon. (Essentially, we are assuming that the not-yet-known
489  * comparison value is equally likely to be any of the possible
490  * values, regardless of their frequency in the table. Is that a good
491  * idea?)
492  */
493  selec = 1.0 - nullfrac;
494  ndistinct = get_variable_numdistinct(vardata, &isdefault);
495  if (ndistinct > 1)
496  selec /= ndistinct;
497 
498  /*
499  * Cross-check: selectivity should never be estimated as more than the
500  * most common value's.
501  */
502  if (get_attstatsslot(&sslot, vardata->statsTuple,
505  {
506  if (sslot.nnumbers > 0 && selec > sslot.numbers[0])
507  selec = sslot.numbers[0];
508  free_attstatsslot(&sslot);
509  }
510  }
511  else
512  {
513  /*
514  * No ANALYZE stats available, so make a guess using estimated number
515  * of distinct values and assuming they are equally common. (The guess
516  * is unlikely to be very good, but we do know a few special cases.)
517  */
518  selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
519  }
520 
521  /* now adjust if we wanted <> rather than = */
522  if (negate)
523  selec = 1.0 - selec - nullfrac;
524 
525  /* result should be in range, but make sure... */
526  CLAMP_PROBABILITY(selec);
527 
528  return selec;
529 }
530 
531 /*
532  * neqsel - Selectivity of "!=" for any data types.
533  *
534  * This routine is also used for some operators that are not "!="
535  * but have comparable selectivity behavior. See above comments
536  * for eqsel().
537  */
538 Datum
540 {
541  PG_RETURN_FLOAT8((float8) eqsel_internal(fcinfo, true));
542 }
543 
544 /*
545  * scalarineqsel - Selectivity of "<", "<=", ">", ">=" for scalars.
546  *
547  * This is the guts of both scalarltsel and scalargtsel. The caller has
548  * commuted the clause, if necessary, so that we can treat the variable as
549  * being on the left. The caller must also make sure that the other side
550  * of the clause is a non-null Const, and dissect same into a value and
551  * datatype.
552  *
553  * This routine works for any datatype (or pair of datatypes) known to
554  * convert_to_scalar(). If it is applied to some other datatype,
555  * it will return a default estimate.
556  */
557 static double
558 scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
559  VariableStatData *vardata, Datum constval, Oid consttype)
560 {
561  Form_pg_statistic stats;
562  FmgrInfo opproc;
563  double mcv_selec,
564  hist_selec,
565  sumcommon;
566  double selec;
567 
568  if (!HeapTupleIsValid(vardata->statsTuple))
569  {
570  /* no stats available, so default result */
571  return DEFAULT_INEQ_SEL;
572  }
573  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
574 
575  fmgr_info(get_opcode(operator), &opproc);
576 
577  /*
578  * If we have most-common-values info, add up the fractions of the MCV
579  * entries that satisfy MCV OP CONST. These fractions contribute directly
580  * to the result selectivity. Also add up the total fraction represented
581  * by MCV entries.
582  */
583  mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
584  &sumcommon);
585 
586  /*
587  * If there is a histogram, determine which bin the constant falls in, and
588  * compute the resulting contribution to selectivity.
589  */
590  hist_selec = ineq_histogram_selectivity(root, vardata, &opproc, isgt,
591  constval, consttype);
592 
593  /*
594  * Now merge the results from the MCV and histogram calculations,
595  * realizing that the histogram covers only the non-null values that are
596  * not listed in MCV.
597  */
598  selec = 1.0 - stats->stanullfrac - sumcommon;
599 
600  if (hist_selec >= 0.0)
601  selec *= hist_selec;
602  else
603  {
604  /*
605  * If no histogram but there are values not accounted for by MCV,
606  * arbitrarily assume half of them will match.
607  */
608  selec *= 0.5;
609  }
610 
611  selec += mcv_selec;
612 
613  /* result should be in range, but make sure... */
614  CLAMP_PROBABILITY(selec);
615 
616  return selec;
617 }
618 
619 /*
620  * mcv_selectivity - Examine the MCV list for selectivity estimates
621  *
622  * Determine the fraction of the variable's MCV population that satisfies
623  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
624  * compute the fraction of the total column population represented by the MCV
625  * list. This code will work for any boolean-returning predicate operator.
626  *
627  * The function result is the MCV selectivity, and the fraction of the
628  * total population is returned into *sumcommonp. Zeroes are returned
629  * if there is no MCV list.
630  */
631 double
633  Datum constval, bool varonleft,
634  double *sumcommonp)
635 {
636  double mcv_selec,
637  sumcommon;
638  AttStatsSlot sslot;
639  int i;
640 
641  mcv_selec = 0.0;
642  sumcommon = 0.0;
643 
644  if (HeapTupleIsValid(vardata->statsTuple) &&
645  statistic_proc_security_check(vardata, opproc->fn_oid) &&
646  get_attstatsslot(&sslot, vardata->statsTuple,
649  {
650  for (i = 0; i < sslot.nvalues; i++)
651  {
652  if (varonleft ?
655  sslot.values[i],
656  constval)) :
659  constval,
660  sslot.values[i])))
661  mcv_selec += sslot.numbers[i];
662  sumcommon += sslot.numbers[i];
663  }
664  free_attstatsslot(&sslot);
665  }
666 
667  *sumcommonp = sumcommon;
668  return mcv_selec;
669 }
670 
671 /*
672  * histogram_selectivity - Examine the histogram for selectivity estimates
673  *
674  * Determine the fraction of the variable's histogram entries that satisfy
675  * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
676  *
677  * This code will work for any boolean-returning predicate operator, whether
678  * or not it has anything to do with the histogram sort operator. We are
679  * essentially using the histogram just as a representative sample. However,
680  * small histograms are unlikely to be all that representative, so the caller
681  * should be prepared to fall back on some other estimation approach when the
682  * histogram is missing or very small. It may also be prudent to combine this
683  * approach with another one when the histogram is small.
684  *
685  * If the actual histogram size is not at least min_hist_size, we won't bother
686  * to do the calculation at all. Also, if the n_skip parameter is > 0, we
687  * ignore the first and last n_skip histogram elements, on the grounds that
688  * they are outliers and hence not very representative. Typical values for
689  * these parameters are 10 and 1.
690  *
691  * The function result is the selectivity, or -1 if there is no histogram
692  * or it's smaller than min_hist_size.
693  *
694  * The output parameter *hist_size receives the actual histogram size,
695  * or zero if no histogram. Callers may use this number to decide how
696  * much faith to put in the function result.
697  *
698  * Note that the result disregards both the most-common-values (if any) and
699  * null entries. The caller is expected to combine this result with
700  * statistics for those portions of the column population. It may also be
701  * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
702  */
703 double
705  Datum constval, bool varonleft,
706  int min_hist_size, int n_skip,
707  int *hist_size)
708 {
709  double result;
710  AttStatsSlot sslot;
711 
712  /* check sanity of parameters */
713  Assert(n_skip >= 0);
714  Assert(min_hist_size > 2 * n_skip);
715 
716  if (HeapTupleIsValid(vardata->statsTuple) &&
717  statistic_proc_security_check(vardata, opproc->fn_oid) &&
718  get_attstatsslot(&sslot, vardata->statsTuple,
721  {
722  *hist_size = sslot.nvalues;
723  if (sslot.nvalues >= min_hist_size)
724  {
725  int nmatch = 0;
726  int i;
727 
728  for (i = n_skip; i < sslot.nvalues - n_skip; i++)
729  {
730  if (varonleft ?
733  sslot.values[i],
734  constval)) :
737  constval,
738  sslot.values[i])))
739  nmatch++;
740  }
741  result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
742  }
743  else
744  result = -1;
745  free_attstatsslot(&sslot);
746  }
747  else
748  {
749  *hist_size = 0;
750  result = -1;
751  }
752 
753  return result;
754 }
755 
756 /*
757  * ineq_histogram_selectivity - Examine the histogram for scalarineqsel
758  *
759  * Determine the fraction of the variable's histogram population that
760  * satisfies the inequality condition, ie, VAR < CONST or VAR > CONST.
761  *
762  * Returns -1 if there is no histogram (valid results will always be >= 0).
763  *
764  * Note that the result disregards both the most-common-values (if any) and
765  * null entries. The caller is expected to combine this result with
766  * statistics for those portions of the column population.
767  */
768 static double
770  VariableStatData *vardata,
771  FmgrInfo *opproc, bool isgt,
772  Datum constval, Oid consttype)
773 {
774  double hist_selec;
775  AttStatsSlot sslot;
776 
777  hist_selec = -1.0;
778 
779  /*
780  * Someday, ANALYZE might store more than one histogram per rel/att,
781  * corresponding to more than one possible sort ordering defined for the
782  * column type. However, to make that work we will need to figure out
783  * which staop to search for --- it's not necessarily the one we have at
784  * hand! (For example, we might have a '<=' operator rather than the '<'
785  * operator that will appear in staop.) For now, assume that whatever
786  * appears in pg_statistic is sorted the same way our operator sorts, or
787  * the reverse way if isgt is TRUE.
788  */
789  if (HeapTupleIsValid(vardata->statsTuple) &&
790  statistic_proc_security_check(vardata, opproc->fn_oid) &&
791  get_attstatsslot(&sslot, vardata->statsTuple,
794  {
795  if (sslot.nvalues > 1)
796  {
797  /*
798  * Use binary search to find proper location, ie, the first slot
799  * at which the comparison fails. (If the given operator isn't
800  * actually sort-compatible with the histogram, you'll get garbage
801  * results ... but probably not any more garbage-y than you would
802  * from the old linear search.)
803  *
804  * If the binary search accesses the first or last histogram
805  * entry, we try to replace that endpoint with the true column min
806  * or max as found by get_actual_variable_range(). This
807  * ameliorates misestimates when the min or max is moving as a
808  * result of changes since the last ANALYZE. Note that this could
809  * result in effectively including MCVs into the histogram that
810  * weren't there before, but we don't try to correct for that.
811  */
812  double histfrac;
813  int lobound = 0; /* first possible slot to search */
814  int hibound = sslot.nvalues; /* last+1 slot to search */
815  bool have_end = false;
816 
817  /*
818  * If there are only two histogram entries, we'll want up-to-date
819  * values for both. (If there are more than two, we need at most
820  * one of them to be updated, so we deal with that within the
821  * loop.)
822  */
823  if (sslot.nvalues == 2)
824  have_end = get_actual_variable_range(root,
825  vardata,
826  sslot.staop,
827  &sslot.values[0],
828  &sslot.values[1]);
829 
830  while (lobound < hibound)
831  {
832  int probe = (lobound + hibound) / 2;
833  bool ltcmp;
834 
835  /*
836  * If we find ourselves about to compare to the first or last
837  * histogram entry, first try to replace it with the actual
838  * current min or max (unless we already did so above).
839  */
840  if (probe == 0 && sslot.nvalues > 2)
841  have_end = get_actual_variable_range(root,
842  vardata,
843  sslot.staop,
844  &sslot.values[0],
845  NULL);
846  else if (probe == sslot.nvalues - 1 && sslot.nvalues > 2)
847  have_end = get_actual_variable_range(root,
848  vardata,
849  sslot.staop,
850  NULL,
851  &sslot.values[probe]);
852 
853  ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
855  sslot.values[probe],
856  constval));
857  if (isgt)
858  ltcmp = !ltcmp;
859  if (ltcmp)
860  lobound = probe + 1;
861  else
862  hibound = probe;
863  }
864 
865  if (lobound <= 0)
866  {
867  /* Constant is below lower histogram boundary. */
868  histfrac = 0.0;
869  }
870  else if (lobound >= sslot.nvalues)
871  {
872  /* Constant is above upper histogram boundary. */
873  histfrac = 1.0;
874  }
875  else
876  {
877  int i = lobound;
878  double val,
879  high,
880  low;
881  double binfrac;
882 
883  /*
884  * We have values[i-1] <= constant <= values[i].
885  *
886  * Convert the constant and the two nearest bin boundary
887  * values to a uniform comparison scale, and do a linear
888  * interpolation within this bin.
889  */
890  if (convert_to_scalar(constval, consttype, &val,
891  sslot.values[i - 1], sslot.values[i],
892  vardata->vartype,
893  &low, &high))
894  {
895  if (high <= low)
896  {
897  /* cope if bin boundaries appear identical */
898  binfrac = 0.5;
899  }
900  else if (val <= low)
901  binfrac = 0.0;
902  else if (val >= high)
903  binfrac = 1.0;
904  else
905  {
906  binfrac = (val - low) / (high - low);
907 
908  /*
909  * Watch out for the possibility that we got a NaN or
910  * Infinity from the division. This can happen
911  * despite the previous checks, if for example "low"
912  * is -Infinity.
913  */
914  if (isnan(binfrac) ||
915  binfrac < 0.0 || binfrac > 1.0)
916  binfrac = 0.5;
917  }
918  }
919  else
920  {
921  /*
922  * Ideally we'd produce an error here, on the grounds that
923  * the given operator shouldn't have scalarXXsel
924  * registered as its selectivity func unless we can deal
925  * with its operand types. But currently, all manner of
926  * stuff is invoking scalarXXsel, so give a default
927  * estimate until that can be fixed.
928  */
929  binfrac = 0.5;
930  }
931 
932  /*
933  * Now, compute the overall selectivity across the values
934  * represented by the histogram. We have i-1 full bins and
935  * binfrac partial bin below the constant.
936  */
937  histfrac = (double) (i - 1) + binfrac;
938  histfrac /= (double) (sslot.nvalues - 1);
939  }
940 
941  /*
942  * Now histfrac = fraction of histogram entries below the
943  * constant.
944  *
945  * Account for "<" vs ">"
946  */
947  hist_selec = isgt ? (1.0 - histfrac) : histfrac;
948 
949  /*
950  * The histogram boundaries are only approximate to begin with,
951  * and may well be out of date anyway. Therefore, don't believe
952  * extremely small or large selectivity estimates --- unless we
953  * got actual current endpoint values from the table.
954  */
955  if (have_end)
956  CLAMP_PROBABILITY(hist_selec);
957  else
958  {
959  if (hist_selec < 0.0001)
960  hist_selec = 0.0001;
961  else if (hist_selec > 0.9999)
962  hist_selec = 0.9999;
963  }
964  }
965 
966  free_attstatsslot(&sslot);
967  }
968 
969  return hist_selec;
970 }
971 
972 /*
973  * scalarltsel - Selectivity of "<" (also "<=") for scalars.
974  */
975 Datum
977 {
979  Oid operator = PG_GETARG_OID(1);
980  List *args = (List *) PG_GETARG_POINTER(2);
981  int varRelid = PG_GETARG_INT32(3);
982  VariableStatData vardata;
983  Node *other;
984  bool varonleft;
985  Datum constval;
986  Oid consttype;
987  bool isgt;
988  double selec;
989 
990  /*
991  * If expression is not variable op something or something op variable,
992  * then punt and return a default estimate.
993  */
994  if (!get_restriction_variable(root, args, varRelid,
995  &vardata, &other, &varonleft))
997 
998  /*
999  * Can't do anything useful if the something is not a constant, either.
1000  */
1001  if (!IsA(other, Const))
1002  {
1003  ReleaseVariableStats(vardata);
1005  }
1006 
1007  /*
1008  * If the constant is NULL, assume operator is strict and return zero, ie,
1009  * operator will never return TRUE.
1010  */
1011  if (((Const *) other)->constisnull)
1012  {
1013  ReleaseVariableStats(vardata);
1014  PG_RETURN_FLOAT8(0.0);
1015  }
1016  constval = ((Const *) other)->constvalue;
1017  consttype = ((Const *) other)->consttype;
1018 
1019  /*
1020  * Force the var to be on the left to simplify logic in scalarineqsel.
1021  */
1022  if (varonleft)
1023  {
1024  /* we have var < other */
1025  isgt = false;
1026  }
1027  else
1028  {
1029  /* we have other < var, commute to make var > other */
1030  operator = get_commutator(operator);
1031  if (!operator)
1032  {
1033  /* Use default selectivity (should we raise an error instead?) */
1034  ReleaseVariableStats(vardata);
1036  }
1037  isgt = true;
1038  }
1039 
1040  selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
1041 
1042  ReleaseVariableStats(vardata);
1043 
1044  PG_RETURN_FLOAT8((float8) selec);
1045 }
1046 
1047 /*
1048  * scalargtsel - Selectivity of ">" (also ">=") for integers.
1049  */
1050 Datum
1052 {
1053  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1054  Oid operator = PG_GETARG_OID(1);
1055  List *args = (List *) PG_GETARG_POINTER(2);
1056  int varRelid = PG_GETARG_INT32(3);
1057  VariableStatData vardata;
1058  Node *other;
1059  bool varonleft;
1060  Datum constval;
1061  Oid consttype;
1062  bool isgt;
1063  double selec;
1064 
1065  /*
1066  * If expression is not variable op something or something op variable,
1067  * then punt and return a default estimate.
1068  */
1069  if (!get_restriction_variable(root, args, varRelid,
1070  &vardata, &other, &varonleft))
1072 
1073  /*
1074  * Can't do anything useful if the something is not a constant, either.
1075  */
1076  if (!IsA(other, Const))
1077  {
1078  ReleaseVariableStats(vardata);
1080  }
1081 
1082  /*
1083  * If the constant is NULL, assume operator is strict and return zero, ie,
1084  * operator will never return TRUE.
1085  */
1086  if (((Const *) other)->constisnull)
1087  {
1088  ReleaseVariableStats(vardata);
1089  PG_RETURN_FLOAT8(0.0);
1090  }
1091  constval = ((Const *) other)->constvalue;
1092  consttype = ((Const *) other)->consttype;
1093 
1094  /*
1095  * Force the var to be on the left to simplify logic in scalarineqsel.
1096  */
1097  if (varonleft)
1098  {
1099  /* we have var > other */
1100  isgt = true;
1101  }
1102  else
1103  {
1104  /* we have other > var, commute to make var < other */
1105  operator = get_commutator(operator);
1106  if (!operator)
1107  {
1108  /* Use default selectivity (should we raise an error instead?) */
1109  ReleaseVariableStats(vardata);
1111  }
1112  isgt = false;
1113  }
1114 
1115  selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
1116 
1117  ReleaseVariableStats(vardata);
1118 
1119  PG_RETURN_FLOAT8((float8) selec);
1120 }
1121 
1122 /*
1123  * patternsel - Generic code for pattern-match selectivity.
1124  */
1125 static double
1127 {
1128  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1129  Oid operator = PG_GETARG_OID(1);
1130  List *args = (List *) PG_GETARG_POINTER(2);
1131  int varRelid = PG_GETARG_INT32(3);
1132  Oid collation = PG_GET_COLLATION();
1133  VariableStatData vardata;
1134  Node *other;
1135  bool varonleft;
1136  Datum constval;
1137  Oid consttype;
1138  Oid vartype;
1139  Oid opfamily;
1140  Pattern_Prefix_Status pstatus;
1141  Const *patt;
1142  Const *prefix = NULL;
1143  Selectivity rest_selec = 0;
1144  double nullfrac = 0.0;
1145  double result;
1146 
1147  /*
1148  * If this is for a NOT LIKE or similar operator, get the corresponding
1149  * positive-match operator and work with that. Set result to the correct
1150  * default estimate, too.
1151  */
1152  if (negate)
1153  {
1154  operator = get_negator(operator);
1155  if (!OidIsValid(operator))
1156  elog(ERROR, "patternsel called for operator without a negator");
1157  result = 1.0 - DEFAULT_MATCH_SEL;
1158  }
1159  else
1160  {
1161  result = DEFAULT_MATCH_SEL;
1162  }
1163 
1164  /*
1165  * If expression is not variable op constant, then punt and return a
1166  * default estimate.
1167  */
1168  if (!get_restriction_variable(root, args, varRelid,
1169  &vardata, &other, &varonleft))
1170  return result;
1171  if (!varonleft || !IsA(other, Const))
1172  {
1173  ReleaseVariableStats(vardata);
1174  return result;
1175  }
1176 
1177  /*
1178  * If the constant is NULL, assume operator is strict and return zero, ie,
1179  * operator will never return TRUE. (It's zero even for a negator op.)
1180  */
1181  if (((Const *) other)->constisnull)
1182  {
1183  ReleaseVariableStats(vardata);
1184  return 0.0;
1185  }
1186  constval = ((Const *) other)->constvalue;
1187  consttype = ((Const *) other)->consttype;
1188 
1189  /*
1190  * The right-hand const is type text or bytea for all supported operators.
1191  * We do not expect to see binary-compatible types here, since
1192  * const-folding should have relabeled the const to exactly match the
1193  * operator's declared type.
1194  */
1195  if (consttype != TEXTOID && consttype != BYTEAOID)
1196  {
1197  ReleaseVariableStats(vardata);
1198  return result;
1199  }
1200 
1201  /*
1202  * Similarly, the exposed type of the left-hand side should be one of
1203  * those we know. (Do not look at vardata.atttype, which might be
1204  * something binary-compatible but different.) We can use it to choose
1205  * the index opfamily from which we must draw the comparison operators.
1206  *
1207  * NOTE: It would be more correct to use the PATTERN opfamilies than the
1208  * simple ones, but at the moment ANALYZE will not generate statistics for
1209  * the PATTERN operators. But our results are so approximate anyway that
1210  * it probably hardly matters.
1211  */
1212  vartype = vardata.vartype;
1213 
1214  switch (vartype)
1215  {
1216  case TEXTOID:
1217  opfamily = TEXT_BTREE_FAM_OID;
1218  break;
1219  case BPCHAROID:
1220  opfamily = BPCHAR_BTREE_FAM_OID;
1221  break;
1222  case NAMEOID:
1223  opfamily = NAME_BTREE_FAM_OID;
1224  break;
1225  case BYTEAOID:
1226  opfamily = BYTEA_BTREE_FAM_OID;
1227  break;
1228  default:
1229  ReleaseVariableStats(vardata);
1230  return result;
1231  }
1232 
1233  /*
1234  * Grab the nullfrac for use below.
1235  */
1236  if (HeapTupleIsValid(vardata.statsTuple))
1237  {
1238  Form_pg_statistic stats;
1239 
1240  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1241  nullfrac = stats->stanullfrac;
1242  }
1243 
1244  /*
1245  * Pull out any fixed prefix implied by the pattern, and estimate the
1246  * fractional selectivity of the remainder of the pattern. Unlike many of
1247  * the other functions in this file, we use the pattern operator's actual
1248  * collation for this step. This is not because we expect the collation
1249  * to make a big difference in the selectivity estimate (it seldom would),
1250  * but because we want to be sure we cache compiled regexps under the
1251  * right cache key, so that they can be re-used at runtime.
1252  */
1253  patt = (Const *) other;
1254  pstatus = pattern_fixed_prefix(patt, ptype, collation,
1255  &prefix, &rest_selec);
1256 
1257  /*
1258  * If necessary, coerce the prefix constant to the right type.
1259  */
1260  if (prefix && prefix->consttype != vartype)
1261  {
1262  char *prefixstr;
1263 
1264  switch (prefix->consttype)
1265  {
1266  case TEXTOID:
1267  prefixstr = TextDatumGetCString(prefix->constvalue);
1268  break;
1269  case BYTEAOID:
1271  prefix->constvalue));
1272  break;
1273  default:
1274  elog(ERROR, "unrecognized consttype: %u",
1275  prefix->consttype);
1276  ReleaseVariableStats(vardata);
1277  return result;
1278  }
1279  prefix = string_to_const(prefixstr, vartype);
1280  pfree(prefixstr);
1281  }
1282 
1283  if (pstatus == Pattern_Prefix_Exact)
1284  {
1285  /*
1286  * Pattern specifies an exact match, so pretend operator is '='
1287  */
1288  Oid eqopr = get_opfamily_member(opfamily, vartype, vartype,
1290 
1291  if (eqopr == InvalidOid)
1292  elog(ERROR, "no = operator for opfamily %u", opfamily);
1293  result = var_eq_const(&vardata, eqopr, prefix->constvalue,
1294  false, true, false);
1295  }
1296  else
1297  {
1298  /*
1299  * Not exact-match pattern. If we have a sufficiently large
1300  * histogram, estimate selectivity for the histogram part of the
1301  * population by counting matches in the histogram. If not, estimate
1302  * selectivity of the fixed prefix and remainder of pattern
1303  * separately, then combine the two to get an estimate of the
1304  * selectivity for the part of the column population represented by
1305  * the histogram. (For small histograms, we combine these
1306  * approaches.)
1307  *
1308  * We then add up data for any most-common-values values; these are
1309  * not in the histogram population, and we can get exact answers for
1310  * them by applying the pattern operator, so there's no reason to
1311  * approximate. (If the MCVs cover a significant part of the total
1312  * population, this gives us a big leg up in accuracy.)
1313  */
1314  Selectivity selec;
1315  int hist_size;
1316  FmgrInfo opproc;
1317  double mcv_selec,
1318  sumcommon;
1319 
1320  /* Try to use the histogram entries to get selectivity */
1321  fmgr_info(get_opcode(operator), &opproc);
1322 
1323  selec = histogram_selectivity(&vardata, &opproc, constval, true,
1324  10, 1, &hist_size);
1325 
1326  /* If not at least 100 entries, use the heuristic method */
1327  if (hist_size < 100)
1328  {
1329  Selectivity heursel;
1330  Selectivity prefixsel;
1331 
1332  if (pstatus == Pattern_Prefix_Partial)
1333  prefixsel = prefix_selectivity(root, &vardata, vartype,
1334  opfamily, prefix);
1335  else
1336  prefixsel = 1.0;
1337  heursel = prefixsel * rest_selec;
1338 
1339  if (selec < 0) /* fewer than 10 histogram entries? */
1340  selec = heursel;
1341  else
1342  {
1343  /*
1344  * For histogram sizes from 10 to 100, we combine the
1345  * histogram and heuristic selectivities, putting increasingly
1346  * more trust in the histogram for larger sizes.
1347  */
1348  double hist_weight = hist_size / 100.0;
1349 
1350  selec = selec * hist_weight + heursel * (1.0 - hist_weight);
1351  }
1352  }
1353 
1354  /* In any case, don't believe extremely small or large estimates. */
1355  if (selec < 0.0001)
1356  selec = 0.0001;
1357  else if (selec > 0.9999)
1358  selec = 0.9999;
1359 
1360  /*
1361  * If we have most-common-values info, add up the fractions of the MCV
1362  * entries that satisfy MCV OP PATTERN. These fractions contribute
1363  * directly to the result selectivity. Also add up the total fraction
1364  * represented by MCV entries.
1365  */
1366  mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
1367  &sumcommon);
1368 
1369  /*
1370  * Now merge the results from the MCV and histogram calculations,
1371  * realizing that the histogram covers only the non-null values that
1372  * are not listed in MCV.
1373  */
1374  selec *= 1.0 - nullfrac - sumcommon;
1375  selec += mcv_selec;
1376  result = selec;
1377  }
1378 
1379  /* now adjust if we wanted not-match rather than match */
1380  if (negate)
1381  result = 1.0 - result - nullfrac;
1382 
1383  /* result should be in range, but make sure... */
1384  CLAMP_PROBABILITY(result);
1385 
1386  if (prefix)
1387  {
1388  pfree(DatumGetPointer(prefix->constvalue));
1389  pfree(prefix);
1390  }
1391 
1392  ReleaseVariableStats(vardata);
1393 
1394  return result;
1395 }
1396 
1397 /*
1398  * regexeqsel - Selectivity of regular-expression pattern match.
1399  */
1400 Datum
1402 {
1404 }
1405 
1406 /*
1407  * icregexeqsel - Selectivity of case-insensitive regex match.
1408  */
1409 Datum
1411 {
1413 }
1414 
1415 /*
1416  * likesel - Selectivity of LIKE pattern match.
1417  */
1418 Datum
1420 {
1422 }
1423 
1424 /*
1425  * iclikesel - Selectivity of ILIKE pattern match.
1426  */
1427 Datum
1429 {
1431 }
1432 
1433 /*
1434  * regexnesel - Selectivity of regular-expression pattern non-match.
1435  */
1436 Datum
1438 {
1440 }
1441 
1442 /*
1443  * icregexnesel - Selectivity of case-insensitive regex non-match.
1444  */
1445 Datum
1447 {
1449 }
1450 
1451 /*
1452  * nlikesel - Selectivity of LIKE pattern non-match.
1453  */
1454 Datum
1456 {
1458 }
1459 
1460 /*
1461  * icnlikesel - Selectivity of ILIKE pattern non-match.
1462  */
1463 Datum
1465 {
1467 }
1468 
1469 /*
1470  * boolvarsel - Selectivity of Boolean variable.
1471  *
1472  * This can actually be called on any boolean-valued expression. If it
1473  * involves only Vars of the specified relation, and if there are statistics
1474  * about the Var or expression (the latter is possible if it's indexed) then
1475  * we'll produce a real estimate; otherwise it's just a default.
1476  */
1478 boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
1479 {
1480  VariableStatData vardata;
1481  double selec;
1482 
1483  examine_variable(root, arg, varRelid, &vardata);
1484  if (HeapTupleIsValid(vardata.statsTuple))
1485  {
1486  /*
1487  * A boolean variable V is equivalent to the clause V = 't', so we
1488  * compute the selectivity as if that is what we have.
1489  */
1490  selec = var_eq_const(&vardata, BooleanEqualOperator,
1491  BoolGetDatum(true), false, true, false);
1492  }
1493  else if (is_funcclause(arg))
1494  {
1495  /*
1496  * If we have no stats and it's a function call, estimate 0.3333333.
1497  * This seems a pretty unprincipled choice, but Postgres has been
1498  * using that estimate for function calls since 1992. The hoariness
1499  * of this behavior suggests that we should not be in too much hurry
1500  * to use another value.
1501  */
1502  selec = 0.3333333;
1503  }
1504  else
1505  {
1506  /* Otherwise, the default estimate is 0.5 */
1507  selec = 0.5;
1508  }
1509  ReleaseVariableStats(vardata);
1510  return selec;
1511 }
1512 
1513 /*
1514  * booltestsel - Selectivity of BooleanTest Node.
1515  */
1518  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1519 {
1520  VariableStatData vardata;
1521  double selec;
1522 
1523  examine_variable(root, arg, varRelid, &vardata);
1524 
1525  if (HeapTupleIsValid(vardata.statsTuple))
1526  {
1527  Form_pg_statistic stats;
1528  double freq_null;
1529  AttStatsSlot sslot;
1530 
1531  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1532  freq_null = stats->stanullfrac;
1533 
1534  if (get_attstatsslot(&sslot, vardata.statsTuple,
1537  && sslot.nnumbers > 0)
1538  {
1539  double freq_true;
1540  double freq_false;
1541 
1542  /*
1543  * Get first MCV frequency and derive frequency for true.
1544  */
1545  if (DatumGetBool(sslot.values[0]))
1546  freq_true = sslot.numbers[0];
1547  else
1548  freq_true = 1.0 - sslot.numbers[0] - freq_null;
1549 
1550  /*
1551  * Next derive frequency for false. Then use these as appropriate
1552  * to derive frequency for each case.
1553  */
1554  freq_false = 1.0 - freq_true - freq_null;
1555 
1556  switch (booltesttype)
1557  {
1558  case IS_UNKNOWN:
1559  /* select only NULL values */
1560  selec = freq_null;
1561  break;
1562  case IS_NOT_UNKNOWN:
1563  /* select non-NULL values */
1564  selec = 1.0 - freq_null;
1565  break;
1566  case IS_TRUE:
1567  /* select only TRUE values */
1568  selec = freq_true;
1569  break;
1570  case IS_NOT_TRUE:
1571  /* select non-TRUE values */
1572  selec = 1.0 - freq_true;
1573  break;
1574  case IS_FALSE:
1575  /* select only FALSE values */
1576  selec = freq_false;
1577  break;
1578  case IS_NOT_FALSE:
1579  /* select non-FALSE values */
1580  selec = 1.0 - freq_false;
1581  break;
1582  default:
1583  elog(ERROR, "unrecognized booltesttype: %d",
1584  (int) booltesttype);
1585  selec = 0.0; /* Keep compiler quiet */
1586  break;
1587  }
1588 
1589  free_attstatsslot(&sslot);
1590  }
1591  else
1592  {
1593  /*
1594  * No most-common-value info available. Still have null fraction
1595  * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1596  * for null fraction and assume a 50-50 split of TRUE and FALSE.
1597  */
1598  switch (booltesttype)
1599  {
1600  case IS_UNKNOWN:
1601  /* select only NULL values */
1602  selec = freq_null;
1603  break;
1604  case IS_NOT_UNKNOWN:
1605  /* select non-NULL values */
1606  selec = 1.0 - freq_null;
1607  break;
1608  case IS_TRUE:
1609  case IS_FALSE:
1610  /* Assume we select half of the non-NULL values */
1611  selec = (1.0 - freq_null) / 2.0;
1612  break;
1613  case IS_NOT_TRUE:
1614  case IS_NOT_FALSE:
1615  /* Assume we select NULLs plus half of the non-NULLs */
1616  /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
1617  selec = (freq_null + 1.0) / 2.0;
1618  break;
1619  default:
1620  elog(ERROR, "unrecognized booltesttype: %d",
1621  (int) booltesttype);
1622  selec = 0.0; /* Keep compiler quiet */
1623  break;
1624  }
1625  }
1626  }
1627  else
1628  {
1629  /*
1630  * If we can't get variable statistics for the argument, perhaps
1631  * clause_selectivity can do something with it. We ignore the
1632  * possibility of a NULL value when using clause_selectivity, and just
1633  * assume the value is either TRUE or FALSE.
1634  */
1635  switch (booltesttype)
1636  {
1637  case IS_UNKNOWN:
1638  selec = DEFAULT_UNK_SEL;
1639  break;
1640  case IS_NOT_UNKNOWN:
1641  selec = DEFAULT_NOT_UNK_SEL;
1642  break;
1643  case IS_TRUE:
1644  case IS_NOT_FALSE:
1645  selec = (double) clause_selectivity(root, arg,
1646  varRelid,
1647  jointype, sjinfo);
1648  break;
1649  case IS_FALSE:
1650  case IS_NOT_TRUE:
1651  selec = 1.0 - (double) clause_selectivity(root, arg,
1652  varRelid,
1653  jointype, sjinfo);
1654  break;
1655  default:
1656  elog(ERROR, "unrecognized booltesttype: %d",
1657  (int) booltesttype);
1658  selec = 0.0; /* Keep compiler quiet */
1659  break;
1660  }
1661  }
1662 
1663  ReleaseVariableStats(vardata);
1664 
1665  /* result should be in range, but make sure... */
1666  CLAMP_PROBABILITY(selec);
1667 
1668  return (Selectivity) selec;
1669 }
1670 
1671 /*
1672  * nulltestsel - Selectivity of NullTest Node.
1673  */
1676  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1677 {
1678  VariableStatData vardata;
1679  double selec;
1680 
1681  examine_variable(root, arg, varRelid, &vardata);
1682 
1683  if (HeapTupleIsValid(vardata.statsTuple))
1684  {
1685  Form_pg_statistic stats;
1686  double freq_null;
1687 
1688  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1689  freq_null = stats->stanullfrac;
1690 
1691  switch (nulltesttype)
1692  {
1693  case IS_NULL:
1694 
1695  /*
1696  * Use freq_null directly.
1697  */
1698  selec = freq_null;
1699  break;
1700  case IS_NOT_NULL:
1701 
1702  /*
1703  * Select not unknown (not null) values. Calculate from
1704  * freq_null.
1705  */
1706  selec = 1.0 - freq_null;
1707  break;
1708  default:
1709  elog(ERROR, "unrecognized nulltesttype: %d",
1710  (int) nulltesttype);
1711  return (Selectivity) 0; /* keep compiler quiet */
1712  }
1713  }
1714  else
1715  {
1716  /*
1717  * No ANALYZE stats available, so make a guess
1718  */
1719  switch (nulltesttype)
1720  {
1721  case IS_NULL:
1722  selec = DEFAULT_UNK_SEL;
1723  break;
1724  case IS_NOT_NULL:
1725  selec = DEFAULT_NOT_UNK_SEL;
1726  break;
1727  default:
1728  elog(ERROR, "unrecognized nulltesttype: %d",
1729  (int) nulltesttype);
1730  return (Selectivity) 0; /* keep compiler quiet */
1731  }
1732  }
1733 
1734  ReleaseVariableStats(vardata);
1735 
1736  /* result should be in range, but make sure... */
1737  CLAMP_PROBABILITY(selec);
1738 
1739  return (Selectivity) selec;
1740 }
1741 
1742 /*
1743  * strip_array_coercion - strip binary-compatible relabeling from an array expr
1744  *
1745  * For array values, the parser normally generates ArrayCoerceExpr conversions,
1746  * but it seems possible that RelabelType might show up. Also, the planner
1747  * is not currently tense about collapsing stacked ArrayCoerceExpr nodes,
1748  * so we need to be ready to deal with more than one level.
1749  */
1750 static Node *
1752 {
1753  for (;;)
1754  {
1755  if (node && IsA(node, ArrayCoerceExpr) &&
1756  ((ArrayCoerceExpr *) node)->elemfuncid == InvalidOid)
1757  {
1758  node = (Node *) ((ArrayCoerceExpr *) node)->arg;
1759  }
1760  else if (node && IsA(node, RelabelType))
1761  {
1762  /* We don't really expect this case, but may as well cope */
1763  node = (Node *) ((RelabelType *) node)->arg;
1764  }
1765  else
1766  break;
1767  }
1768  return node;
1769 }
1770 
1771 /*
1772  * scalararraysel - Selectivity of ScalarArrayOpExpr Node.
1773  */
1776  ScalarArrayOpExpr *clause,
1777  bool is_join_clause,
1778  int varRelid,
1779  JoinType jointype,
1780  SpecialJoinInfo *sjinfo)
1781 {
1782  Oid operator = clause->opno;
1783  bool useOr = clause->useOr;
1784  bool isEquality = false;
1785  bool isInequality = false;
1786  Node *leftop;
1787  Node *rightop;
1788  Oid nominal_element_type;
1789  Oid nominal_element_collation;
1790  TypeCacheEntry *typentry;
1791  RegProcedure oprsel;
1792  FmgrInfo oprselproc;
1793  Selectivity s1;
1794  Selectivity s1disjoint;
1795 
1796  /* First, deconstruct the expression */
1797  Assert(list_length(clause->args) == 2);
1798  leftop = (Node *) linitial(clause->args);
1799  rightop = (Node *) lsecond(clause->args);
1800 
1801  /* aggressively reduce both sides to constants */
1802  leftop = estimate_expression_value(root, leftop);
1803  rightop = estimate_expression_value(root, rightop);
1804 
1805  /* get nominal (after relabeling) element type of rightop */
1806  nominal_element_type = get_base_element_type(exprType(rightop));
1807  if (!OidIsValid(nominal_element_type))
1808  return (Selectivity) 0.5; /* probably shouldn't happen */
1809  /* get nominal collation, too, for generating constants */
1810  nominal_element_collation = exprCollation(rightop);
1811 
1812  /* look through any binary-compatible relabeling of rightop */
1813  rightop = strip_array_coercion(rightop);
1814 
1815  /*
1816  * Detect whether the operator is the default equality or inequality
1817  * operator of the array element type.
1818  */
1819  typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1820  if (OidIsValid(typentry->eq_opr))
1821  {
1822  if (operator == typentry->eq_opr)
1823  isEquality = true;
1824  else if (get_negator(operator) == typentry->eq_opr)
1825  isInequality = true;
1826  }
1827 
1828  /*
1829  * If it is equality or inequality, we might be able to estimate this as a
1830  * form of array containment; for instance "const = ANY(column)" can be
1831  * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1832  * that, and returns the selectivity estimate if successful, or -1 if not.
1833  */
1834  if ((isEquality || isInequality) && !is_join_clause)
1835  {
1836  s1 = scalararraysel_containment(root, leftop, rightop,
1837  nominal_element_type,
1838  isEquality, useOr, varRelid);
1839  if (s1 >= 0.0)
1840  return s1;
1841  }
1842 
1843  /*
1844  * Look up the underlying operator's selectivity estimator. Punt if it
1845  * hasn't got one.
1846  */
1847  if (is_join_clause)
1848  oprsel = get_oprjoin(operator);
1849  else
1850  oprsel = get_oprrest(operator);
1851  if (!oprsel)
1852  return (Selectivity) 0.5;
1853  fmgr_info(oprsel, &oprselproc);
1854 
1855  /*
1856  * In the array-containment check above, we must only believe that an
1857  * operator is equality or inequality if it is the default btree equality
1858  * operator (or its negator) for the element type, since those are the
1859  * operators that array containment will use. But in what follows, we can
1860  * be a little laxer, and also believe that any operators using eqsel() or
1861  * neqsel() as selectivity estimator act like equality or inequality.
1862  */
1863  if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
1864  isEquality = true;
1865  else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
1866  isInequality = true;
1867 
1868  /*
1869  * We consider three cases:
1870  *
1871  * 1. rightop is an Array constant: deconstruct the array, apply the
1872  * operator's selectivity function for each array element, and merge the
1873  * results in the same way that clausesel.c does for AND/OR combinations.
1874  *
1875  * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1876  * function for each element of the ARRAY[] construct, and merge.
1877  *
1878  * 3. otherwise, make a guess ...
1879  */
1880  if (rightop && IsA(rightop, Const))
1881  {
1882  Datum arraydatum = ((Const *) rightop)->constvalue;
1883  bool arrayisnull = ((Const *) rightop)->constisnull;
1884  ArrayType *arrayval;
1885  int16 elmlen;
1886  bool elmbyval;
1887  char elmalign;
1888  int num_elems;
1889  Datum *elem_values;
1890  bool *elem_nulls;
1891  int i;
1892 
1893  if (arrayisnull) /* qual can't succeed if null array */
1894  return (Selectivity) 0.0;
1895  arrayval = DatumGetArrayTypeP(arraydatum);
1897  &elmlen, &elmbyval, &elmalign);
1898  deconstruct_array(arrayval,
1899  ARR_ELEMTYPE(arrayval),
1900  elmlen, elmbyval, elmalign,
1901  &elem_values, &elem_nulls, &num_elems);
1902 
1903  /*
1904  * For generic operators, we assume the probability of success is
1905  * independent for each array element. But for "= ANY" or "<> ALL",
1906  * if the array elements are distinct (which'd typically be the case)
1907  * then the probabilities are disjoint, and we should just sum them.
1908  *
1909  * If we were being really tense we would try to confirm that the
1910  * elements are all distinct, but that would be expensive and it
1911  * doesn't seem to be worth the cycles; it would amount to penalizing
1912  * well-written queries in favor of poorly-written ones. However, we
1913  * do protect ourselves a little bit by checking whether the
1914  * disjointness assumption leads to an impossible (out of range)
1915  * probability; if so, we fall back to the normal calculation.
1916  */
1917  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1918 
1919  for (i = 0; i < num_elems; i++)
1920  {
1921  List *args;
1922  Selectivity s2;
1923 
1924  args = list_make2(leftop,
1925  makeConst(nominal_element_type,
1926  -1,
1927  nominal_element_collation,
1928  elmlen,
1929  elem_values[i],
1930  elem_nulls[i],
1931  elmbyval));
1932  if (is_join_clause)
1933  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1934  clause->inputcollid,
1935  PointerGetDatum(root),
1936  ObjectIdGetDatum(operator),
1937  PointerGetDatum(args),
1938  Int16GetDatum(jointype),
1939  PointerGetDatum(sjinfo)));
1940  else
1941  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1942  clause->inputcollid,
1943  PointerGetDatum(root),
1944  ObjectIdGetDatum(operator),
1945  PointerGetDatum(args),
1946  Int32GetDatum(varRelid)));
1947 
1948  if (useOr)
1949  {
1950  s1 = s1 + s2 - s1 * s2;
1951  if (isEquality)
1952  s1disjoint += s2;
1953  }
1954  else
1955  {
1956  s1 = s1 * s2;
1957  if (isInequality)
1958  s1disjoint += s2 - 1.0;
1959  }
1960  }
1961 
1962  /* accept disjoint-probability estimate if in range */
1963  if ((useOr ? isEquality : isInequality) &&
1964  s1disjoint >= 0.0 && s1disjoint <= 1.0)
1965  s1 = s1disjoint;
1966  }
1967  else if (rightop && IsA(rightop, ArrayExpr) &&
1968  !((ArrayExpr *) rightop)->multidims)
1969  {
1970  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1971  int16 elmlen;
1972  bool elmbyval;
1973  ListCell *l;
1974 
1975  get_typlenbyval(arrayexpr->element_typeid,
1976  &elmlen, &elmbyval);
1977 
1978  /*
1979  * We use the assumption of disjoint probabilities here too, although
1980  * the odds of equal array elements are rather higher if the elements
1981  * are not all constants (which they won't be, else constant folding
1982  * would have reduced the ArrayExpr to a Const). In this path it's
1983  * critical to have the sanity check on the s1disjoint estimate.
1984  */
1985  s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1986 
1987  foreach(l, arrayexpr->elements)
1988  {
1989  Node *elem = (Node *) lfirst(l);
1990  List *args;
1991  Selectivity s2;
1992 
1993  /*
1994  * Theoretically, if elem isn't of nominal_element_type we should
1995  * insert a RelabelType, but it seems unlikely that any operator
1996  * estimation function would really care ...
1997  */
1998  args = list_make2(leftop, elem);
1999  if (is_join_clause)
2000  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2001  clause->inputcollid,
2002  PointerGetDatum(root),
2003  ObjectIdGetDatum(operator),
2004  PointerGetDatum(args),
2005  Int16GetDatum(jointype),
2006  PointerGetDatum(sjinfo)));
2007  else
2008  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2009  clause->inputcollid,
2010  PointerGetDatum(root),
2011  ObjectIdGetDatum(operator),
2012  PointerGetDatum(args),
2013  Int32GetDatum(varRelid)));
2014 
2015  if (useOr)
2016  {
2017  s1 = s1 + s2 - s1 * s2;
2018  if (isEquality)
2019  s1disjoint += s2;
2020  }
2021  else
2022  {
2023  s1 = s1 * s2;
2024  if (isInequality)
2025  s1disjoint += s2 - 1.0;
2026  }
2027  }
2028 
2029  /* accept disjoint-probability estimate if in range */
2030  if ((useOr ? isEquality : isInequality) &&
2031  s1disjoint >= 0.0 && s1disjoint <= 1.0)
2032  s1 = s1disjoint;
2033  }
2034  else
2035  {
2036  CaseTestExpr *dummyexpr;
2037  List *args;
2038  Selectivity s2;
2039  int i;
2040 
2041  /*
2042  * We need a dummy rightop to pass to the operator selectivity
2043  * routine. It can be pretty much anything that doesn't look like a
2044  * constant; CaseTestExpr is a convenient choice.
2045  */
2046  dummyexpr = makeNode(CaseTestExpr);
2047  dummyexpr->typeId = nominal_element_type;
2048  dummyexpr->typeMod = -1;
2049  dummyexpr->collation = clause->inputcollid;
2050  args = list_make2(leftop, dummyexpr);
2051  if (is_join_clause)
2052  s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2053  clause->inputcollid,
2054  PointerGetDatum(root),
2055  ObjectIdGetDatum(operator),
2056  PointerGetDatum(args),
2057  Int16GetDatum(jointype),
2058  PointerGetDatum(sjinfo)));
2059  else
2060  s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2061  clause->inputcollid,
2062  PointerGetDatum(root),
2063  ObjectIdGetDatum(operator),
2064  PointerGetDatum(args),
2065  Int32GetDatum(varRelid)));
2066  s1 = useOr ? 0.0 : 1.0;
2067 
2068  /*
2069  * Arbitrarily assume 10 elements in the eventual array value (see
2070  * also estimate_array_length). We don't risk an assumption of
2071  * disjoint probabilities here.
2072  */
2073  for (i = 0; i < 10; i++)
2074  {
2075  if (useOr)
2076  s1 = s1 + s2 - s1 * s2;
2077  else
2078  s1 = s1 * s2;
2079  }
2080  }
2081 
2082  /* result should be in range, but make sure... */
2083  CLAMP_PROBABILITY(s1);
2084 
2085  return s1;
2086 }
2087 
2088 /*
2089  * Estimate number of elements in the array yielded by an expression.
2090  *
2091  * It's important that this agree with scalararraysel.
2092  */
2093 int
2095 {
2096  /* look through any binary-compatible relabeling of arrayexpr */
2097  arrayexpr = strip_array_coercion(arrayexpr);
2098 
2099  if (arrayexpr && IsA(arrayexpr, Const))
2100  {
2101  Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2102  bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2103  ArrayType *arrayval;
2104 
2105  if (arrayisnull)
2106  return 0;
2107  arrayval = DatumGetArrayTypeP(arraydatum);
2108  return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2109  }
2110  else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2111  !((ArrayExpr *) arrayexpr)->multidims)
2112  {
2113  return list_length(((ArrayExpr *) arrayexpr)->elements);
2114  }
2115  else
2116  {
2117  /* default guess --- see also scalararraysel */
2118  return 10;
2119  }
2120 }
2121 
2122 /*
2123  * rowcomparesel - Selectivity of RowCompareExpr Node.
2124  *
2125  * We estimate RowCompare selectivity by considering just the first (high
2126  * order) columns, which makes it equivalent to an ordinary OpExpr. While
2127  * this estimate could be refined by considering additional columns, it
2128  * seems unlikely that we could do a lot better without multi-column
2129  * statistics.
2130  */
2133  RowCompareExpr *clause,
2134  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
2135 {
2136  Selectivity s1;
2137  Oid opno = linitial_oid(clause->opnos);
2138  Oid inputcollid = linitial_oid(clause->inputcollids);
2139  List *opargs;
2140  bool is_join_clause;
2141 
2142  /* Build equivalent arg list for single operator */
2143  opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
2144 
2145  /*
2146  * Decide if it's a join clause. This should match clausesel.c's
2147  * treat_as_join_clause(), except that we intentionally consider only the
2148  * leading columns and not the rest of the clause.
2149  */
2150  if (varRelid != 0)
2151  {
2152  /*
2153  * Caller is forcing restriction mode (eg, because we are examining an
2154  * inner indexscan qual).
2155  */
2156  is_join_clause = false;
2157  }
2158  else if (sjinfo == NULL)
2159  {
2160  /*
2161  * It must be a restriction clause, since it's being evaluated at a
2162  * scan node.
2163  */
2164  is_join_clause = false;
2165  }
2166  else
2167  {
2168  /*
2169  * Otherwise, it's a join if there's more than one relation used.
2170  */
2171  is_join_clause = (NumRelids((Node *) opargs) > 1);
2172  }
2173 
2174  if (is_join_clause)
2175  {
2176  /* Estimate selectivity for a join clause. */
2177  s1 = join_selectivity(root, opno,
2178  opargs,
2179  inputcollid,
2180  jointype,
2181  sjinfo);
2182  }
2183  else
2184  {
2185  /* Estimate selectivity for a restriction clause. */
2186  s1 = restriction_selectivity(root, opno,
2187  opargs,
2188  inputcollid,
2189  varRelid);
2190  }
2191 
2192  return s1;
2193 }
2194 
2195 /*
2196  * eqjoinsel - Join selectivity of "="
2197  */
2198 Datum
2200 {
2201  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2202  Oid operator = PG_GETARG_OID(1);
2203  List *args = (List *) PG_GETARG_POINTER(2);
2204 
2205 #ifdef NOT_USED
2206  JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2207 #endif
2209  double selec;
2210  VariableStatData vardata1;
2211  VariableStatData vardata2;
2212  bool join_is_reversed;
2213  RelOptInfo *inner_rel;
2214 
2215  get_join_variables(root, args, sjinfo,
2216  &vardata1, &vardata2, &join_is_reversed);
2217 
2218  switch (sjinfo->jointype)
2219  {
2220  case JOIN_INNER:
2221  case JOIN_LEFT:
2222  case JOIN_FULL:
2223  selec = eqjoinsel_inner(operator, &vardata1, &vardata2);
2224  break;
2225  case JOIN_SEMI:
2226  case JOIN_ANTI:
2227 
2228  /*
2229  * Look up the join's inner relation. min_righthand is sufficient
2230  * information because neither SEMI nor ANTI joins permit any
2231  * reassociation into or out of their RHS, so the righthand will
2232  * always be exactly that set of rels.
2233  */
2234  inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
2235 
2236  if (!join_is_reversed)
2237  selec = eqjoinsel_semi(operator, &vardata1, &vardata2,
2238  inner_rel);
2239  else
2240  selec = eqjoinsel_semi(get_commutator(operator),
2241  &vardata2, &vardata1,
2242  inner_rel);
2243  break;
2244  default:
2245  /* other values not expected here */
2246  elog(ERROR, "unrecognized join type: %d",
2247  (int) sjinfo->jointype);
2248  selec = 0; /* keep compiler quiet */
2249  break;
2250  }
2251 
2252  ReleaseVariableStats(vardata1);
2253  ReleaseVariableStats(vardata2);
2254 
2255  CLAMP_PROBABILITY(selec);
2256 
2257  PG_RETURN_FLOAT8((float8) selec);
2258 }
2259 
2260 /*
2261  * eqjoinsel_inner --- eqjoinsel for normal inner join
2262  *
2263  * We also use this for LEFT/FULL outer joins; it's not presently clear
2264  * that it's worth trying to distinguish them here.
2265  */
2266 static double
2268  VariableStatData *vardata1, VariableStatData *vardata2)
2269 {
2270  double selec;
2271  double nd1;
2272  double nd2;
2273  bool isdefault1;
2274  bool isdefault2;
2275  Oid opfuncoid;
2276  Form_pg_statistic stats1 = NULL;
2277  Form_pg_statistic stats2 = NULL;
2278  bool have_mcvs1 = false;
2279  bool have_mcvs2 = false;
2280  AttStatsSlot sslot1;
2281  AttStatsSlot sslot2;
2282 
2283  nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2284  nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2285 
2286  opfuncoid = get_opcode(operator);
2287 
2288  memset(&sslot1, 0, sizeof(sslot1));
2289  memset(&sslot2, 0, sizeof(sslot2));
2290 
2291  if (HeapTupleIsValid(vardata1->statsTuple))
2292  {
2293  /* note we allow use of nullfrac regardless of security check */
2294  stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2295  if (statistic_proc_security_check(vardata1, opfuncoid))
2296  have_mcvs1 = get_attstatsslot(&sslot1, vardata1->statsTuple,
2299  }
2300 
2301  if (HeapTupleIsValid(vardata2->statsTuple))
2302  {
2303  /* note we allow use of nullfrac regardless of security check */
2304  stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
2305  if (statistic_proc_security_check(vardata2, opfuncoid))
2306  have_mcvs2 = get_attstatsslot(&sslot2, vardata2->statsTuple,
2309  }
2310 
2311  if (have_mcvs1 && have_mcvs2)
2312  {
2313  /*
2314  * We have most-common-value lists for both relations. Run through
2315  * the lists to see which MCVs actually join to each other with the
2316  * given operator. This allows us to determine the exact join
2317  * selectivity for the portion of the relations represented by the MCV
2318  * lists. We still have to estimate for the remaining population, but
2319  * in a skewed distribution this gives us a big leg up in accuracy.
2320  * For motivation see the analysis in Y. Ioannidis and S.
2321  * Christodoulakis, "On the propagation of errors in the size of join
2322  * results", Technical Report 1018, Computer Science Dept., University
2323  * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
2324  */
2325  FmgrInfo eqproc;
2326  bool *hasmatch1;
2327  bool *hasmatch2;
2328  double nullfrac1 = stats1->stanullfrac;
2329  double nullfrac2 = stats2->stanullfrac;
2330  double matchprodfreq,
2331  matchfreq1,
2332  matchfreq2,
2333  unmatchfreq1,
2334  unmatchfreq2,
2335  otherfreq1,
2336  otherfreq2,
2337  totalsel1,
2338  totalsel2;
2339  int i,
2340  nmatches;
2341 
2342  fmgr_info(opfuncoid, &eqproc);
2343  hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
2344  hasmatch2 = (bool *) palloc0(sslot2.nvalues * sizeof(bool));
2345 
2346  /*
2347  * Note we assume that each MCV will match at most one member of the
2348  * other MCV list. If the operator isn't really equality, there could
2349  * be multiple matches --- but we don't look for them, both for speed
2350  * and because the math wouldn't add up...
2351  */
2352  matchprodfreq = 0.0;
2353  nmatches = 0;
2354  for (i = 0; i < sslot1.nvalues; i++)
2355  {
2356  int j;
2357 
2358  for (j = 0; j < sslot2.nvalues; j++)
2359  {
2360  if (hasmatch2[j])
2361  continue;
2362  if (DatumGetBool(FunctionCall2Coll(&eqproc,
2364  sslot1.values[i],
2365  sslot2.values[j])))
2366  {
2367  hasmatch1[i] = hasmatch2[j] = true;
2368  matchprodfreq += sslot1.numbers[i] * sslot2.numbers[j];
2369  nmatches++;
2370  break;
2371  }
2372  }
2373  }
2374  CLAMP_PROBABILITY(matchprodfreq);
2375  /* Sum up frequencies of matched and unmatched MCVs */
2376  matchfreq1 = unmatchfreq1 = 0.0;
2377  for (i = 0; i < sslot1.nvalues; i++)
2378  {
2379  if (hasmatch1[i])
2380  matchfreq1 += sslot1.numbers[i];
2381  else
2382  unmatchfreq1 += sslot1.numbers[i];
2383  }
2384  CLAMP_PROBABILITY(matchfreq1);
2385  CLAMP_PROBABILITY(unmatchfreq1);
2386  matchfreq2 = unmatchfreq2 = 0.0;
2387  for (i = 0; i < sslot2.nvalues; i++)
2388  {
2389  if (hasmatch2[i])
2390  matchfreq2 += sslot2.numbers[i];
2391  else
2392  unmatchfreq2 += sslot2.numbers[i];
2393  }
2394  CLAMP_PROBABILITY(matchfreq2);
2395  CLAMP_PROBABILITY(unmatchfreq2);
2396  pfree(hasmatch1);
2397  pfree(hasmatch2);
2398 
2399  /*
2400  * Compute total frequency of non-null values that are not in the MCV
2401  * lists.
2402  */
2403  otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
2404  otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
2405  CLAMP_PROBABILITY(otherfreq1);
2406  CLAMP_PROBABILITY(otherfreq2);
2407 
2408  /*
2409  * We can estimate the total selectivity from the point of view of
2410  * relation 1 as: the known selectivity for matched MCVs, plus
2411  * unmatched MCVs that are assumed to match against random members of
2412  * relation 2's non-MCV population, plus non-MCV values that are
2413  * assumed to match against random members of relation 2's unmatched
2414  * MCVs plus non-MCV values.
2415  */
2416  totalsel1 = matchprodfreq;
2417  if (nd2 > sslot2.nvalues)
2418  totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - sslot2.nvalues);
2419  if (nd2 > nmatches)
2420  totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
2421  (nd2 - nmatches);
2422  /* Same estimate from the point of view of relation 2. */
2423  totalsel2 = matchprodfreq;
2424  if (nd1 > sslot1.nvalues)
2425  totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - sslot1.nvalues);
2426  if (nd1 > nmatches)
2427  totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
2428  (nd1 - nmatches);
2429 
2430  /*
2431  * Use the smaller of the two estimates. This can be justified in
2432  * essentially the same terms as given below for the no-stats case: to
2433  * a first approximation, we are estimating from the point of view of
2434  * the relation with smaller nd.
2435  */
2436  selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
2437  }
2438  else
2439  {
2440  /*
2441  * We do not have MCV lists for both sides. Estimate the join
2442  * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
2443  * is plausible if we assume that the join operator is strict and the
2444  * non-null values are about equally distributed: a given non-null
2445  * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
2446  * of rel2, so total join rows are at most
2447  * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
2448  * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
2449  * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
2450  * with MIN() is an upper bound. Using the MIN() means we estimate
2451  * from the point of view of the relation with smaller nd (since the
2452  * larger nd is determining the MIN). It is reasonable to assume that
2453  * most tuples in this rel will have join partners, so the bound is
2454  * probably reasonably tight and should be taken as-is.
2455  *
2456  * XXX Can we be smarter if we have an MCV list for just one side? It
2457  * seems that if we assume equal distribution for the other side, we
2458  * end up with the same answer anyway.
2459  */
2460  double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2461  double nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
2462 
2463  selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
2464  if (nd1 > nd2)
2465  selec /= nd1;
2466  else
2467  selec /= nd2;
2468  }
2469 
2470  free_attstatsslot(&sslot1);
2471  free_attstatsslot(&sslot2);
2472 
2473  return selec;
2474 }
2475 
2476 /*
2477  * eqjoinsel_semi --- eqjoinsel for semi join
2478  *
2479  * (Also used for anti join, which we are supposed to estimate the same way.)
2480  * Caller has ensured that vardata1 is the LHS variable.
2481  * Unlike eqjoinsel_inner, we have to cope with operator being InvalidOid.
2482  */
2483 static double
2485  VariableStatData *vardata1, VariableStatData *vardata2,
2486  RelOptInfo *inner_rel)
2487 {
2488  double selec;
2489  double nd1;
2490  double nd2;
2491  bool isdefault1;
2492  bool isdefault2;
2493  Oid opfuncoid;
2494  Form_pg_statistic stats1 = NULL;
2495  bool have_mcvs1 = false;
2496  bool have_mcvs2 = false;
2497  AttStatsSlot sslot1;
2498  AttStatsSlot sslot2;
2499 
2500  nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2501  nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2502 
2503  opfuncoid = OidIsValid(operator) ? get_opcode(operator) : InvalidOid;
2504 
2505  memset(&sslot1, 0, sizeof(sslot1));
2506  memset(&sslot2, 0, sizeof(sslot2));
2507 
2508  /*
2509  * We clamp nd2 to be not more than what we estimate the inner relation's
2510  * size to be. This is intuitively somewhat reasonable since obviously
2511  * there can't be more than that many distinct values coming from the
2512  * inner rel. The reason for the asymmetry (ie, that we don't clamp nd1
2513  * likewise) is that this is the only pathway by which restriction clauses
2514  * applied to the inner rel will affect the join result size estimate,
2515  * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
2516  * only the outer rel's size. If we clamped nd1 we'd be double-counting
2517  * the selectivity of outer-rel restrictions.
2518  *
2519  * We can apply this clamping both with respect to the base relation from
2520  * which the join variable comes (if there is just one), and to the
2521  * immediate inner input relation of the current join.
2522  *
2523  * If we clamp, we can treat nd2 as being a non-default estimate; it's not
2524  * great, maybe, but it didn't come out of nowhere either. This is most
2525  * helpful when the inner relation is empty and consequently has no stats.
2526  */
2527  if (vardata2->rel)
2528  {
2529  if (nd2 >= vardata2->rel->rows)
2530  {
2531  nd2 = vardata2->rel->rows;
2532  isdefault2 = false;
2533  }
2534  }
2535  if (nd2 >= inner_rel->rows)
2536  {
2537  nd2 = inner_rel->rows;
2538  isdefault2 = false;
2539  }
2540 
2541  if (HeapTupleIsValid(vardata1->statsTuple))
2542  {
2543  /* note we allow use of nullfrac regardless of security check */
2544  stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2545  if (statistic_proc_security_check(vardata1, opfuncoid))
2546  have_mcvs1 = get_attstatsslot(&sslot1, vardata1->statsTuple,
2549  }
2550 
2551  if (HeapTupleIsValid(vardata2->statsTuple) &&
2552  statistic_proc_security_check(vardata2, opfuncoid))
2553  {
2554  have_mcvs2 = get_attstatsslot(&sslot2, vardata2->statsTuple,
2557  /* note: currently don't need stanumbers from RHS */
2558  }
2559 
2560  if (have_mcvs1 && have_mcvs2 && OidIsValid(operator))
2561  {
2562  /*
2563  * We have most-common-value lists for both relations. Run through
2564  * the lists to see which MCVs actually join to each other with the
2565  * given operator. This allows us to determine the exact join
2566  * selectivity for the portion of the relations represented by the MCV
2567  * lists. We still have to estimate for the remaining population, but
2568  * in a skewed distribution this gives us a big leg up in accuracy.
2569  */
2570  FmgrInfo eqproc;
2571  bool *hasmatch1;
2572  bool *hasmatch2;
2573  double nullfrac1 = stats1->stanullfrac;
2574  double matchfreq1,
2575  uncertainfrac,
2576  uncertain;
2577  int i,
2578  nmatches,
2579  clamped_nvalues2;
2580 
2581  /*
2582  * The clamping above could have resulted in nd2 being less than
2583  * sslot2.nvalues; in which case, we assume that precisely the nd2
2584  * most common values in the relation will appear in the join input,
2585  * and so compare to only the first nd2 members of the MCV list. Of
2586  * course this is frequently wrong, but it's the best bet we can make.
2587  */
2588  clamped_nvalues2 = Min(sslot2.nvalues, nd2);
2589 
2590  fmgr_info(opfuncoid, &eqproc);
2591  hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
2592  hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool));
2593 
2594  /*
2595  * Note we assume that each MCV will match at most one member of the
2596  * other MCV list. If the operator isn't really equality, there could
2597  * be multiple matches --- but we don't look for them, both for speed
2598  * and because the math wouldn't add up...
2599  */
2600  nmatches = 0;
2601  for (i = 0; i < sslot1.nvalues; i++)
2602  {
2603  int j;
2604 
2605  for (j = 0; j < clamped_nvalues2; j++)
2606  {
2607  if (hasmatch2[j])
2608  continue;
2609  if (DatumGetBool(FunctionCall2Coll(&eqproc,
2611  sslot1.values[i],
2612  sslot2.values[j])))
2613  {
2614  hasmatch1[i] = hasmatch2[j] = true;
2615  nmatches++;
2616  break;
2617  }
2618  }
2619  }
2620  /* Sum up frequencies of matched MCVs */
2621  matchfreq1 = 0.0;
2622  for (i = 0; i < sslot1.nvalues; i++)
2623  {
2624  if (hasmatch1[i])
2625  matchfreq1 += sslot1.numbers[i];
2626  }
2627  CLAMP_PROBABILITY(matchfreq1);
2628  pfree(hasmatch1);
2629  pfree(hasmatch2);
2630 
2631  /*
2632  * Now we need to estimate the fraction of relation 1 that has at
2633  * least one join partner. We know for certain that the matched MCVs
2634  * do, so that gives us a lower bound, but we're really in the dark
2635  * about everything else. Our crude approach is: if nd1 <= nd2 then
2636  * assume all non-null rel1 rows have join partners, else assume for
2637  * the uncertain rows that a fraction nd2/nd1 have join partners. We
2638  * can discount the known-matched MCVs from the distinct-values counts
2639  * before doing the division.
2640  *
2641  * Crude as the above is, it's completely useless if we don't have
2642  * reliable ndistinct values for both sides. Hence, if either nd1 or
2643  * nd2 is default, punt and assume half of the uncertain rows have
2644  * join partners.
2645  */
2646  if (!isdefault1 && !isdefault2)
2647  {
2648  nd1 -= nmatches;
2649  nd2 -= nmatches;
2650  if (nd1 <= nd2 || nd2 < 0)
2651  uncertainfrac = 1.0;
2652  else
2653  uncertainfrac = nd2 / nd1;
2654  }
2655  else
2656  uncertainfrac = 0.5;
2657  uncertain = 1.0 - matchfreq1 - nullfrac1;
2658  CLAMP_PROBABILITY(uncertain);
2659  selec = matchfreq1 + uncertainfrac * uncertain;
2660  }
2661  else
2662  {
2663  /*
2664  * Without MCV lists for both sides, we can only use the heuristic
2665  * about nd1 vs nd2.
2666  */
2667  double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2668 
2669  if (!isdefault1 && !isdefault2)
2670  {
2671  if (nd1 <= nd2 || nd2 < 0)
2672  selec = 1.0 - nullfrac1;
2673  else
2674  selec = (nd2 / nd1) * (1.0 - nullfrac1);
2675  }
2676  else
2677  selec = 0.5 * (1.0 - nullfrac1);
2678  }
2679 
2680  free_attstatsslot(&sslot1);
2681  free_attstatsslot(&sslot2);
2682 
2683  return selec;
2684 }
2685 
2686 /*
2687  * neqjoinsel - Join selectivity of "!="
2688  */
2689 Datum
2691 {
2692  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2693  Oid operator = PG_GETARG_OID(1);
2694  List *args = (List *) PG_GETARG_POINTER(2);
2695  JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2697  Oid eqop;
2698  float8 result;
2699 
2700  /*
2701  * We want 1 - eqjoinsel() where the equality operator is the one
2702  * associated with this != operator, that is, its negator.
2703  */
2704  eqop = get_negator(operator);
2705  if (eqop)
2706  {
2708  PointerGetDatum(root),
2709  ObjectIdGetDatum(eqop),
2710  PointerGetDatum(args),
2711  Int16GetDatum(jointype),
2712  PointerGetDatum(sjinfo)));
2713  }
2714  else
2715  {
2716  /* Use default selectivity (should we raise an error instead?) */
2717  result = DEFAULT_EQ_SEL;
2718  }
2719  result = 1.0 - result;
2720  PG_RETURN_FLOAT8(result);
2721 }
2722 
2723 /*
2724  * scalarltjoinsel - Join selectivity of "<" and "<=" for scalars
2725  */
2726 Datum
2728 {
2730 }
2731 
2732 /*
2733  * scalargtjoinsel - Join selectivity of ">" and ">=" for scalars
2734  */
2735 Datum
2737 {
2739 }
2740 
2741 /*
2742  * patternjoinsel - Generic code for pattern-match join selectivity.
2743  */
2744 static double
2746 {
2747  /* For the moment we just punt. */
2748  return negate ? (1.0 - DEFAULT_MATCH_SEL) : DEFAULT_MATCH_SEL;
2749 }
2750 
2751 /*
2752  * regexeqjoinsel - Join selectivity of regular-expression pattern match.
2753  */
2754 Datum
2756 {
2758 }
2759 
2760 /*
2761  * icregexeqjoinsel - Join selectivity of case-insensitive regex match.
2762  */
2763 Datum
2765 {
2767 }
2768 
2769 /*
2770  * likejoinsel - Join selectivity of LIKE pattern match.
2771  */
2772 Datum
2774 {
2776 }
2777 
2778 /*
2779  * iclikejoinsel - Join selectivity of ILIKE pattern match.
2780  */
2781 Datum
2783 {
2785 }
2786 
2787 /*
2788  * regexnejoinsel - Join selectivity of regex non-match.
2789  */
2790 Datum
2792 {
2794 }
2795 
2796 /*
2797  * icregexnejoinsel - Join selectivity of case-insensitive regex non-match.
2798  */
2799 Datum
2801 {
2803 }
2804 
2805 /*
2806  * nlikejoinsel - Join selectivity of LIKE pattern non-match.
2807  */
2808 Datum
2810 {
2812 }
2813 
2814 /*
2815  * icnlikejoinsel - Join selectivity of ILIKE pattern non-match.
2816  */
2817 Datum
2819 {
2821 }
2822 
2823 /*
2824  * mergejoinscansel - Scan selectivity of merge join.
2825  *
2826  * A merge join will stop as soon as it exhausts either input stream.
2827  * Therefore, if we can estimate the ranges of both input variables,
2828  * we can estimate how much of the input will actually be read. This
2829  * can have a considerable impact on the cost when using indexscans.
2830  *
2831  * Also, we can estimate how much of each input has to be read before the
2832  * first join pair is found, which will affect the join's startup time.
2833  *
2834  * clause should be a clause already known to be mergejoinable. opfamily,
2835  * strategy, and nulls_first specify the sort ordering being used.
2836  *
2837  * The outputs are:
2838  * *leftstart is set to the fraction of the left-hand variable expected
2839  * to be scanned before the first join pair is found (0 to 1).
2840  * *leftend is set to the fraction of the left-hand variable expected
2841  * to be scanned before the join terminates (0 to 1).
2842  * *rightstart, *rightend similarly for the right-hand variable.
2843  */
2844 void
2846  Oid opfamily, int strategy, bool nulls_first,
2847  Selectivity *leftstart, Selectivity *leftend,
2848  Selectivity *rightstart, Selectivity *rightend)
2849 {
2850  Node *left,
2851  *right;
2852  VariableStatData leftvar,
2853  rightvar;
2854  int op_strategy;
2855  Oid op_lefttype;
2856  Oid op_righttype;
2857  Oid opno,
2858  lsortop,
2859  rsortop,
2860  lstatop,
2861  rstatop,
2862  ltop,
2863  leop,
2864  revltop,
2865  revleop;
2866  bool isgt;
2867  Datum leftmin,
2868  leftmax,
2869  rightmin,
2870  rightmax;
2871  double selec;
2872 
2873  /* Set default results if we can't figure anything out. */
2874  /* XXX should default "start" fraction be a bit more than 0? */
2875  *leftstart = *rightstart = 0.0;
2876  *leftend = *rightend = 1.0;
2877 
2878  /* Deconstruct the merge clause */
2879  if (!is_opclause(clause))
2880  return; /* shouldn't happen */
2881  opno = ((OpExpr *) clause)->opno;
2882  left = get_leftop((Expr *) clause);
2883  right = get_rightop((Expr *) clause);
2884  if (!right)
2885  return; /* shouldn't happen */
2886 
2887  /* Look for stats for the inputs */
2888  examine_variable(root, left, 0, &leftvar);
2889  examine_variable(root, right, 0, &rightvar);
2890 
2891  /* Extract the operator's declared left/right datatypes */
2892  get_op_opfamily_properties(opno, opfamily, false,
2893  &op_strategy,
2894  &op_lefttype,
2895  &op_righttype);
2896  Assert(op_strategy == BTEqualStrategyNumber);
2897 
2898  /*
2899  * Look up the various operators we need. If we don't find them all, it
2900  * probably means the opfamily is broken, but we just fail silently.
2901  *
2902  * Note: we expect that pg_statistic histograms will be sorted by the '<'
2903  * operator, regardless of which sort direction we are considering.
2904  */
2905  switch (strategy)
2906  {
2907  case BTLessStrategyNumber:
2908  isgt = false;
2909  if (op_lefttype == op_righttype)
2910  {
2911  /* easy case */
2912  ltop = get_opfamily_member(opfamily,
2913  op_lefttype, op_righttype,
2915  leop = get_opfamily_member(opfamily,
2916  op_lefttype, op_righttype,
2918  lsortop = ltop;
2919  rsortop = ltop;
2920  lstatop = lsortop;
2921  rstatop = rsortop;
2922  revltop = ltop;
2923  revleop = leop;
2924  }
2925  else
2926  {
2927  ltop = get_opfamily_member(opfamily,
2928  op_lefttype, op_righttype,
2930  leop = get_opfamily_member(opfamily,
2931  op_lefttype, op_righttype,
2933  lsortop = get_opfamily_member(opfamily,
2934  op_lefttype, op_lefttype,
2936  rsortop = get_opfamily_member(opfamily,
2937  op_righttype, op_righttype,
2939  lstatop = lsortop;
2940  rstatop = rsortop;
2941  revltop = get_opfamily_member(opfamily,
2942  op_righttype, op_lefttype,
2944  revleop = get_opfamily_member(opfamily,
2945  op_righttype, op_lefttype,
2947  }
2948  break;
2950  /* descending-order case */
2951  isgt = true;
2952  if (op_lefttype == op_righttype)
2953  {
2954  /* easy case */
2955  ltop = get_opfamily_member(opfamily,
2956  op_lefttype, op_righttype,
2958  leop = get_opfamily_member(opfamily,
2959  op_lefttype, op_righttype,
2961  lsortop = ltop;
2962  rsortop = ltop;
2963  lstatop = get_opfamily_member(opfamily,
2964  op_lefttype, op_lefttype,
2966  rstatop = lstatop;
2967  revltop = ltop;
2968  revleop = leop;
2969  }
2970  else
2971  {
2972  ltop = get_opfamily_member(opfamily,
2973  op_lefttype, op_righttype,
2975  leop = get_opfamily_member(opfamily,
2976  op_lefttype, op_righttype,
2978  lsortop = get_opfamily_member(opfamily,
2979  op_lefttype, op_lefttype,
2981  rsortop = get_opfamily_member(opfamily,
2982  op_righttype, op_righttype,
2984  lstatop = get_opfamily_member(opfamily,
2985  op_lefttype, op_lefttype,
2987  rstatop = get_opfamily_member(opfamily,
2988  op_righttype, op_righttype,
2990  revltop = get_opfamily_member(opfamily,
2991  op_righttype, op_lefttype,
2993  revleop = get_opfamily_member(opfamily,
2994  op_righttype, op_lefttype,
2996  }
2997  break;
2998  default:
2999  goto fail; /* shouldn't get here */
3000  }
3001 
3002  if (!OidIsValid(lsortop) ||
3003  !OidIsValid(rsortop) ||
3004  !OidIsValid(lstatop) ||
3005  !OidIsValid(rstatop) ||
3006  !OidIsValid(ltop) ||
3007  !OidIsValid(leop) ||
3008  !OidIsValid(revltop) ||
3009  !OidIsValid(revleop))
3010  goto fail; /* insufficient info in catalogs */
3011 
3012  /* Try to get ranges of both inputs */
3013  if (!isgt)
3014  {
3015  if (!get_variable_range(root, &leftvar, lstatop,
3016  &leftmin, &leftmax))
3017  goto fail; /* no range available from stats */
3018  if (!get_variable_range(root, &rightvar, rstatop,
3019  &rightmin, &rightmax))
3020  goto fail; /* no range available from stats */
3021  }
3022  else
3023  {
3024  /* need to swap the max and min */
3025  if (!get_variable_range(root, &leftvar, lstatop,
3026  &leftmax, &leftmin))
3027  goto fail; /* no range available from stats */
3028  if (!get_variable_range(root, &rightvar, rstatop,
3029  &rightmax, &rightmin))
3030  goto fail; /* no range available from stats */
3031  }
3032 
3033  /*
3034  * Now, the fraction of the left variable that will be scanned is the
3035  * fraction that's <= the right-side maximum value. But only believe
3036  * non-default estimates, else stick with our 1.0.
3037  */
3038  selec = scalarineqsel(root, leop, isgt, &leftvar,
3039  rightmax, op_righttype);
3040  if (selec != DEFAULT_INEQ_SEL)
3041  *leftend = selec;
3042 
3043  /* And similarly for the right variable. */
3044  selec = scalarineqsel(root, revleop, isgt, &rightvar,
3045  leftmax, op_lefttype);
3046  if (selec != DEFAULT_INEQ_SEL)
3047  *rightend = selec;
3048 
3049  /*
3050  * Only one of the two "end" fractions can really be less than 1.0;
3051  * believe the smaller estimate and reset the other one to exactly 1.0. If
3052  * we get exactly equal estimates (as can easily happen with self-joins),
3053  * believe neither.
3054  */
3055  if (*leftend > *rightend)
3056  *leftend = 1.0;
3057  else if (*leftend < *rightend)
3058  *rightend = 1.0;
3059  else
3060  *leftend = *rightend = 1.0;
3061 
3062  /*
3063  * Also, the fraction of the left variable that will be scanned before the
3064  * first join pair is found is the fraction that's < the right-side
3065  * minimum value. But only believe non-default estimates, else stick with
3066  * our own default.
3067  */
3068  selec = scalarineqsel(root, ltop, isgt, &leftvar,
3069  rightmin, op_righttype);
3070  if (selec != DEFAULT_INEQ_SEL)
3071  *leftstart = selec;
3072 
3073  /* And similarly for the right variable. */
3074  selec = scalarineqsel(root, revltop, isgt, &rightvar,
3075  leftmin, op_lefttype);
3076  if (selec != DEFAULT_INEQ_SEL)
3077  *rightstart = selec;
3078 
3079  /*
3080  * Only one of the two "start" fractions can really be more than zero;
3081  * believe the larger estimate and reset the other one to exactly 0.0. If
3082  * we get exactly equal estimates (as can easily happen with self-joins),
3083  * believe neither.
3084  */
3085  if (*leftstart < *rightstart)
3086  *leftstart = 0.0;
3087  else if (*leftstart > *rightstart)
3088  *rightstart = 0.0;
3089  else
3090  *leftstart = *rightstart = 0.0;
3091 
3092  /*
3093  * If the sort order is nulls-first, we're going to have to skip over any
3094  * nulls too. These would not have been counted by scalarineqsel, and we
3095  * can safely add in this fraction regardless of whether we believe
3096  * scalarineqsel's results or not. But be sure to clamp the sum to 1.0!
3097  */
3098  if (nulls_first)
3099  {
3100  Form_pg_statistic stats;
3101 
3102  if (HeapTupleIsValid(leftvar.statsTuple))
3103  {
3104  stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
3105  *leftstart += stats->stanullfrac;
3106  CLAMP_PROBABILITY(*leftstart);
3107  *leftend += stats->stanullfrac;
3108  CLAMP_PROBABILITY(*leftend);
3109  }
3110  if (HeapTupleIsValid(rightvar.statsTuple))
3111  {
3112  stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
3113  *rightstart += stats->stanullfrac;
3114  CLAMP_PROBABILITY(*rightstart);
3115  *rightend += stats->stanullfrac;
3116  CLAMP_PROBABILITY(*rightend);
3117  }
3118  }
3119 
3120  /* Disbelieve start >= end, just in case that can happen */
3121  if (*leftstart >= *leftend)
3122  {
3123  *leftstart = 0.0;
3124  *leftend = 1.0;
3125  }
3126  if (*rightstart >= *rightend)
3127  {
3128  *rightstart = 0.0;
3129  *rightend = 1.0;
3130  }
3131 
3132 fail:
3133  ReleaseVariableStats(leftvar);
3134  ReleaseVariableStats(rightvar);
3135 }
3136 
3137 
3138 /*
3139  * Helper routine for estimate_num_groups: add an item to a list of
3140  * GroupVarInfos, but only if it's not known equal to any of the existing
3141  * entries.
3142  */
3143 typedef struct
3144 {
3145  Node *var; /* might be an expression, not just a Var */
3146  RelOptInfo *rel; /* relation it belongs to */
3147  double ndistinct; /* # distinct values */
3148 } GroupVarInfo;
3149 
3150 static List *
3152  Node *var, VariableStatData *vardata)
3153 {
3154  GroupVarInfo *varinfo;
3155  double ndistinct;
3156  bool isdefault;
3157  ListCell *lc;
3158 
3159  ndistinct = get_variable_numdistinct(vardata, &isdefault);
3160 
3161  /* cannot use foreach here because of possible list_delete */
3162  lc = list_head(varinfos);
3163  while (lc)
3164  {
3165  varinfo = (GroupVarInfo *) lfirst(lc);
3166 
3167  /* must advance lc before list_delete possibly pfree's it */
3168  lc = lnext(lc);
3169 
3170  /* Drop exact duplicates */
3171  if (equal(var, varinfo->var))
3172  return varinfos;
3173 
3174  /*
3175  * Drop known-equal vars, but only if they belong to different
3176  * relations (see comments for estimate_num_groups)
3177  */
3178  if (vardata->rel != varinfo->rel &&
3179  exprs_known_equal(root, var, varinfo->var))
3180  {
3181  if (varinfo->ndistinct <= ndistinct)
3182  {
3183  /* Keep older item, forget new one */
3184  return varinfos;
3185  }
3186  else
3187  {
3188  /* Delete the older item */
3189  varinfos = list_delete_ptr(varinfos, varinfo);
3190  }
3191  }
3192  }
3193 
3194  varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
3195 
3196  varinfo->var = var;
3197  varinfo->rel = vardata->rel;
3198  varinfo->ndistinct = ndistinct;
3199  varinfos = lappend(varinfos, varinfo);
3200  return varinfos;
3201 }
3202 
3203 /*
3204  * estimate_num_groups - Estimate number of groups in a grouped query
3205  *
3206  * Given a query having a GROUP BY clause, estimate how many groups there
3207  * will be --- ie, the number of distinct combinations of the GROUP BY
3208  * expressions.
3209  *
3210  * This routine is also used to estimate the number of rows emitted by
3211  * a DISTINCT filtering step; that is an isomorphic problem. (Note:
3212  * actually, we only use it for DISTINCT when there's no grouping or
3213  * aggregation ahead of the DISTINCT.)
3214  *
3215  * Inputs:
3216  * root - the query
3217  * groupExprs - list of expressions being grouped by
3218  * input_rows - number of rows estimated to arrive at the group/unique
3219  * filter step
3220  * pgset - NULL, or a List** pointing to a grouping set to filter the
3221  * groupExprs against
3222  *
3223  * Given the lack of any cross-correlation statistics in the system, it's
3224  * impossible to do anything really trustworthy with GROUP BY conditions
3225  * involving multiple Vars. We should however avoid assuming the worst
3226  * case (all possible cross-product terms actually appear as groups) since
3227  * very often the grouped-by Vars are highly correlated. Our current approach
3228  * is as follows:
3229  * 1. Expressions yielding boolean are assumed to contribute two groups,
3230  * independently of their content, and are ignored in the subsequent
3231  * steps. This is mainly because tests like "col IS NULL" break the
3232  * heuristic used in step 2 especially badly.
3233  * 2. Reduce the given expressions to a list of unique Vars used. For
3234  * example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
3235  * It is clearly correct not to count the same Var more than once.
3236  * It is also reasonable to treat f(x) the same as x: f() cannot
3237  * increase the number of distinct values (unless it is volatile,
3238  * which we consider unlikely for grouping), but it probably won't
3239  * reduce the number of distinct values much either.
3240  * As a special case, if a GROUP BY expression can be matched to an
3241  * expressional index for which we have statistics, then we treat the
3242  * whole expression as though it were just a Var.
3243  * 3. If the list contains Vars of different relations that are known equal
3244  * due to equivalence classes, then drop all but one of the Vars from each
3245  * known-equal set, keeping the one with smallest estimated # of values
3246  * (since the extra values of the others can't appear in joined rows).
3247  * Note the reason we only consider Vars of different relations is that
3248  * if we considered ones of the same rel, we'd be double-counting the
3249  * restriction selectivity of the equality in the next step.
3250  * 4. For Vars within a single source rel, we multiply together the numbers
3251  * of values, clamp to the number of rows in the rel (divided by 10 if
3252  * more than one Var), and then multiply by a factor based on the
3253  * selectivity of the restriction clauses for that rel. When there's
3254  * more than one Var, the initial product is probably too high (it's the
3255  * worst case) but clamping to a fraction of the rel's rows seems to be a
3256  * helpful heuristic for not letting the estimate get out of hand. (The
3257  * factor of 10 is derived from pre-Postgres-7.4 practice.) The factor
3258  * we multiply by to adjust for the restriction selectivity assumes that
3259  * the restriction clauses are independent of the grouping, which may not
3260  * be a valid assumption, but it's hard to do better.
3261  * 5. If there are Vars from multiple rels, we repeat step 4 for each such
3262  * rel, and multiply the results together.
3263  * Note that rels not containing grouped Vars are ignored completely, as are
3264  * join clauses. Such rels cannot increase the number of groups, and we
3265  * assume such clauses do not reduce the number either (somewhat bogus,
3266  * but we don't have the info to do better).
3267  */
3268 double
3269 estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
3270  List **pgset)
3271 {
3272  List *varinfos = NIL;
3273  double numdistinct;
3274  ListCell *l;
3275  int i;
3276 
3277  /*
3278  * We don't ever want to return an estimate of zero groups, as that tends
3279  * to lead to division-by-zero and other unpleasantness. The input_rows
3280  * estimate is usually already at least 1, but clamp it just in case it
3281  * isn't.
3282  */
3283  input_rows = clamp_row_est(input_rows);
3284 
3285  /*
3286  * If no grouping columns, there's exactly one group. (This can't happen
3287  * for normal cases with GROUP BY or DISTINCT, but it is possible for
3288  * corner cases with set operations.)
3289  */
3290  if (groupExprs == NIL || (pgset && list_length(*pgset) < 1))
3291  return 1.0;
3292 
3293  /*
3294  * Count groups derived from boolean grouping expressions. For other
3295  * expressions, find the unique Vars used, treating an expression as a Var
3296  * if we can find stats for it. For each one, record the statistical
3297  * estimate of number of distinct values (total in its table, without
3298  * regard for filtering).
3299  */
3300  numdistinct = 1.0;
3301 
3302  i = 0;
3303  foreach(l, groupExprs)
3304  {
3305  Node *groupexpr = (Node *) lfirst(l);
3306  VariableStatData vardata;
3307  List *varshere;
3308  ListCell *l2;
3309 
3310  /* is expression in this grouping set? */
3311  if (pgset && !list_member_int(*pgset, i++))
3312  continue;
3313 
3314  /* Short-circuit for expressions returning boolean */
3315  if (exprType(groupexpr) == BOOLOID)
3316  {
3317  numdistinct *= 2.0;
3318  continue;
3319  }
3320 
3321  /*
3322  * If examine_variable is able to deduce anything about the GROUP BY
3323  * expression, treat it as a single variable even if it's really more
3324  * complicated.
3325  */
3326  examine_variable(root, groupexpr, 0, &vardata);
3327  if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
3328  {
3329  varinfos = add_unique_group_var(root, varinfos,
3330  groupexpr, &vardata);
3331  ReleaseVariableStats(vardata);
3332  continue;
3333  }
3334  ReleaseVariableStats(vardata);
3335 
3336  /*
3337  * Else pull out the component Vars. Handle PlaceHolderVars by
3338  * recursing into their arguments (effectively assuming that the
3339  * PlaceHolderVar doesn't change the number of groups, which boils
3340  * down to ignoring the possible addition of nulls to the result set).
3341  */
3342  varshere = pull_var_clause(groupexpr,
3346 
3347  /*
3348  * If we find any variable-free GROUP BY item, then either it is a
3349  * constant (and we can ignore it) or it contains a volatile function;
3350  * in the latter case we punt and assume that each input row will
3351  * yield a distinct group.
3352  */
3353  if (varshere == NIL)
3354  {
3355  if (contain_volatile_functions(groupexpr))
3356  return input_rows;
3357  continue;
3358  }
3359 
3360  /*
3361  * Else add variables to varinfos list
3362  */
3363  foreach(l2, varshere)
3364  {
3365  Node *var = (Node *) lfirst(l2);
3366 
3367  examine_variable(root, var, 0, &vardata);
3368  varinfos = add_unique_group_var(root, varinfos, var, &vardata);
3369  ReleaseVariableStats(vardata);
3370  }
3371  }
3372 
3373  /*
3374  * If now no Vars, we must have an all-constant or all-boolean GROUP BY
3375  * list.
3376  */
3377  if (varinfos == NIL)
3378  {
3379  /* Guard against out-of-range answers */
3380  if (numdistinct > input_rows)
3381  numdistinct = input_rows;
3382  return numdistinct;
3383  }
3384 
3385  /*
3386  * Group Vars by relation and estimate total numdistinct.
3387  *
3388  * For each iteration of the outer loop, we process the frontmost Var in
3389  * varinfos, plus all other Vars in the same relation. We remove these
3390  * Vars from the newvarinfos list for the next iteration. This is the
3391  * easiest way to group Vars of same rel together.
3392  */
3393  do
3394  {
3395  GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
3396  RelOptInfo *rel = varinfo1->rel;
3397  double reldistinct = 1;
3398  double relmaxndistinct = reldistinct;
3399  int relvarcount = 0;
3400  List *newvarinfos = NIL;
3401  List *relvarinfos = NIL;
3402 
3403  /*
3404  * Split the list of varinfos in two - one for the current rel, one
3405  * for remaining Vars on other rels.
3406  */
3407  relvarinfos = lcons(varinfo1, relvarinfos);
3408  for_each_cell(l, lnext(list_head(varinfos)))
3409  {
3410  GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3411 
3412  if (varinfo2->rel == varinfo1->rel)
3413  {
3414  /* varinfos on current rel */
3415  relvarinfos = lcons(varinfo2, relvarinfos);
3416  }
3417  else
3418  {
3419  /* not time to process varinfo2 yet */
3420  newvarinfos = lcons(varinfo2, newvarinfos);
3421  }
3422  }
3423 
3424  /*
3425  * Get the numdistinct estimate for the Vars of this rel. We
3426  * iteratively search for multivariate n-distinct with maximum number
3427  * of vars; assuming that each var group is independent of the others,
3428  * we multiply them together. Any remaining relvarinfos after no more
3429  * multivariate matches are found are assumed independent too, so
3430  * their individual ndistinct estimates are multiplied also.
3431  *
3432  * While iterating, count how many separate numdistinct values we
3433  * apply. We apply a fudge factor below, but only if we multiplied
3434  * more than one such values.
3435  */
3436  while (relvarinfos)
3437  {
3438  double mvndistinct;
3439 
3440  if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
3441  &mvndistinct))
3442  {
3443  reldistinct *= mvndistinct;
3444  if (relmaxndistinct < mvndistinct)
3445  relmaxndistinct = mvndistinct;
3446  relvarcount++;
3447  }
3448  else
3449  {
3450  foreach(l, relvarinfos)
3451  {
3452  GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3453 
3454  reldistinct *= varinfo2->ndistinct;
3455  if (relmaxndistinct < varinfo2->ndistinct)
3456  relmaxndistinct = varinfo2->ndistinct;
3457  relvarcount++;
3458  }
3459 
3460  /* we're done with this relation */
3461  relvarinfos = NIL;
3462  }
3463  }
3464 
3465  /*
3466  * Sanity check --- don't divide by zero if empty relation.
3467  */
3468  Assert(IS_SIMPLE_REL(rel));
3469  if (rel->tuples > 0)
3470  {
3471  /*
3472  * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
3473  * fudge factor is because the Vars are probably correlated but we
3474  * don't know by how much. We should never clamp to less than the
3475  * largest ndistinct value for any of the Vars, though, since
3476  * there will surely be at least that many groups.
3477  */
3478  double clamp = rel->tuples;
3479 
3480  if (relvarcount > 1)
3481  {
3482  clamp *= 0.1;
3483  if (clamp < relmaxndistinct)
3484  {
3485  clamp = relmaxndistinct;
3486  /* for sanity in case some ndistinct is too large: */
3487  if (clamp > rel->tuples)
3488  clamp = rel->tuples;
3489  }
3490  }
3491  if (reldistinct > clamp)
3492  reldistinct = clamp;
3493 
3494  /*
3495  * Update the estimate based on the restriction selectivity,
3496  * guarding against division by zero when reldistinct is zero.
3497  * Also skip this if we know that we are returning all rows.
3498  */
3499  if (reldistinct > 0 && rel->rows < rel->tuples)
3500  {
3501  /*
3502  * Given a table containing N rows with n distinct values in a
3503  * uniform distribution, if we select p rows at random then
3504  * the expected number of distinct values selected is
3505  *
3506  * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
3507  *
3508  * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
3509  *
3510  * See "Approximating block accesses in database
3511  * organizations", S. B. Yao, Communications of the ACM,
3512  * Volume 20 Issue 4, April 1977 Pages 260-261.
3513  *
3514  * Alternatively, re-arranging the terms from the factorials,
3515  * this may be written as
3516  *
3517  * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
3518  *
3519  * This form of the formula is more efficient to compute in
3520  * the common case where p is larger than N/n. Additionally,
3521  * as pointed out by Dell'Era, if i << N for all terms in the
3522  * product, it can be approximated by
3523  *
3524  * n * (1 - ((N-p)/N)^(N/n))
3525  *
3526  * See "Expected distinct values when selecting from a bag
3527  * without replacement", Alberto Dell'Era,
3528  * http://www.adellera.it/investigations/distinct_balls/.
3529  *
3530  * The condition i << N is equivalent to n >> 1, so this is a
3531  * good approximation when the number of distinct values in
3532  * the table is large. It turns out that this formula also
3533  * works well even when n is small.
3534  */
3535  reldistinct *=
3536  (1 - pow((rel->tuples - rel->rows) / rel->tuples,
3537  rel->tuples / reldistinct));
3538  }
3539  reldistinct = clamp_row_est(reldistinct);
3540 
3541  /*
3542  * Update estimate of total distinct groups.
3543  */
3544  numdistinct *= reldistinct;
3545  }
3546 
3547  varinfos = newvarinfos;
3548  } while (varinfos != NIL);
3549 
3550  numdistinct = ceil(numdistinct);
3551 
3552  /* Guard against out-of-range answers */
3553  if (numdistinct > input_rows)
3554  numdistinct = input_rows;
3555  if (numdistinct < 1.0)
3556  numdistinct = 1.0;
3557 
3558  return numdistinct;
3559 }
3560 
3561 /*
3562  * Estimate hash bucket statistics when the specified expression is used
3563  * as a hash key for the given number of buckets.
3564  *
3565  * This attempts to determine two values:
3566  *
3567  * 1. The frequency of the most common value of the expression (returns
3568  * zero into *mcv_freq if we can't get that).
3569  *
3570  * 2. The "bucketsize fraction", ie, average number of entries in a bucket
3571  * divided by total tuples in relation.
3572  *
3573  * XXX This is really pretty bogus since we're effectively assuming that the
3574  * distribution of hash keys will be the same after applying restriction
3575  * clauses as it was in the underlying relation. However, we are not nearly
3576  * smart enough to figure out how the restrict clauses might change the
3577  * distribution, so this will have to do for now.
3578  *
3579  * We are passed the number of buckets the executor will use for the given
3580  * input relation. If the data were perfectly distributed, with the same
3581  * number of tuples going into each available bucket, then the bucketsize
3582  * fraction would be 1/nbuckets. But this happy state of affairs will occur
3583  * only if (a) there are at least nbuckets distinct data values, and (b)
3584  * we have a not-too-skewed data distribution. Otherwise the buckets will
3585  * be nonuniformly occupied. If the other relation in the join has a key
3586  * distribution similar to this one's, then the most-loaded buckets are
3587  * exactly those that will be probed most often. Therefore, the "average"
3588  * bucket size for costing purposes should really be taken as something close
3589  * to the "worst case" bucket size. We try to estimate this by adjusting the
3590  * fraction if there are too few distinct data values, and then scaling up
3591  * by the ratio of the most common value's frequency to the average frequency.
3592  *
3593  * If no statistics are available, use a default estimate of 0.1. This will
3594  * discourage use of a hash rather strongly if the inner relation is large,
3595  * which is what we want. We do not want to hash unless we know that the
3596  * inner rel is well-dispersed (or the alternatives seem much worse).
3597  *
3598  * The caller should also check that the mcv_freq is not so large that the
3599  * most common value would by itself require an impractically large bucket.
3600  * In a hash join, the executor can split buckets if they get too big, but
3601  * obviously that doesn't help for a bucket that contains many duplicates of
3602  * the same value.
3603  */
3604 void
3605 estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets,
3606  Selectivity *mcv_freq,
3607  Selectivity *bucketsize_frac)
3608 {
3609  VariableStatData vardata;
3610  double estfract,
3611  ndistinct,
3612  stanullfrac,
3613  avgfreq;
3614  bool isdefault;
3615  AttStatsSlot sslot;
3616 
3617  examine_variable(root, hashkey, 0, &vardata);
3618 
3619  /* Look up the frequency of the most common value, if available */
3620  *mcv_freq = 0.0;
3621 
3622  if (HeapTupleIsValid(vardata.statsTuple))
3623  {
3624  if (get_attstatsslot(&sslot, vardata.statsTuple,
3627  {
3628  /*
3629  * The first MCV stat is for the most common value.
3630  */
3631  if (sslot.nnumbers > 0)
3632  *mcv_freq = sslot.numbers[0];
3633  free_attstatsslot(&sslot);
3634  }
3635  }
3636 
3637  /* Get number of distinct values */
3638  ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3639 
3640  /*
3641  * If ndistinct isn't real, punt. We normally return 0.1, but if the
3642  * mcv_freq is known to be even higher than that, use it instead.
3643  */
3644  if (isdefault)
3645  {
3646  *bucketsize_frac = (Selectivity) Max(0.1, *mcv_freq);
3647  ReleaseVariableStats(vardata);
3648  return;
3649  }
3650 
3651  /* Get fraction that are null */
3652  if (HeapTupleIsValid(vardata.statsTuple))
3653  {
3654  Form_pg_statistic stats;
3655 
3656  stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3657  stanullfrac = stats->stanullfrac;
3658  }
3659  else
3660  stanullfrac = 0.0;
3661 
3662  /* Compute avg freq of all distinct data values in raw relation */
3663  avgfreq = (1.0 - stanullfrac) / ndistinct;
3664 
3665  /*
3666  * Adjust ndistinct to account for restriction clauses. Observe we are
3667  * assuming that the data distribution is affected uniformly by the
3668  * restriction clauses!
3669  *
3670  * XXX Possibly better way, but much more expensive: multiply by
3671  * selectivity of rel's restriction clauses that mention the target Var.
3672  */
3673  if (vardata.rel && vardata.rel->tuples > 0)
3674  {
3675  ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3676  ndistinct = clamp_row_est(ndistinct);
3677  }
3678 
3679  /*
3680  * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3681  * number of buckets is less than the expected number of distinct values;
3682  * otherwise it is 1/ndistinct.
3683  */
3684  if (ndistinct > nbuckets)
3685  estfract = 1.0 / nbuckets;
3686  else
3687  estfract = 1.0 / ndistinct;
3688 
3689  /*
3690  * Adjust estimated bucketsize upward to account for skewed distribution.
3691  */
3692  if (avgfreq > 0.0 && *mcv_freq > avgfreq)
3693  estfract *= *mcv_freq / avgfreq;
3694 
3695  /*
3696  * Clamp bucketsize to sane range (the above adjustment could easily
3697  * produce an out-of-range result). We set the lower bound a little above
3698  * zero, since zero isn't a very sane result.
3699  */
3700  if (estfract < 1.0e-6)
3701  estfract = 1.0e-6;
3702  else if (estfract > 1.0)
3703  estfract = 1.0;
3704 
3705  *bucketsize_frac = (Selectivity) estfract;
3706 
3707  ReleaseVariableStats(vardata);
3708 }
3709 
3710 
3711 /*-------------------------------------------------------------------------
3712  *
3713  * Support routines
3714  *
3715  *-------------------------------------------------------------------------
3716  */
3717 
3718 /*
3719  * Find applicable ndistinct statistics for the given list of VarInfos (which
3720  * must all belong to the given rel), and update *ndistinct to the estimate of
3721  * the MVNDistinctItem that best matches. If a match it found, *varinfos is
3722  * updated to remove the list of matched varinfos.
3723  *
3724  * Varinfos that aren't for simple Vars are ignored.
3725  *
3726  * Return TRUE if we're able to find a match, FALSE otherwise.
3727  */
3728 static bool
3730  List **varinfos, double *ndistinct)
3731 {
3732  ListCell *lc;
3733  Bitmapset *attnums = NULL;
3734  int nmatches;
3735  Oid statOid = InvalidOid;
3736  MVNDistinct *stats;
3737  Bitmapset *matched = NULL;
3738 
3739  /* bail out immediately if the table has no extended statistics */
3740  if (!rel->statlist)
3741  return false;
3742 
3743  /* Determine the attnums we're looking for */
3744  foreach(lc, *varinfos)
3745  {
3746  GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3747 
3748  Assert(varinfo->rel == rel);
3749 
3750  if (IsA(varinfo->var, Var))
3751  {
3752  attnums = bms_add_member(attnums,
3753  ((Var *) varinfo->var)->varattno);
3754  }
3755  }
3756 
3757  /* look for the ndistinct statistics matching the most vars */
3758  nmatches = 1; /* we require at least two matches */
3759  foreach(lc, rel->statlist)
3760  {
3761  StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
3762  Bitmapset *shared;
3763  int nshared;
3764 
3765  /* skip statistics of other kinds */
3766  if (info->kind != STATS_EXT_NDISTINCT)
3767  continue;
3768 
3769  /* compute attnums shared by the vars and the statistics object */
3770  shared = bms_intersect(info->keys, attnums);
3771  nshared = bms_num_members(shared);
3772 
3773  /*
3774  * Does this statistics object match more columns than the currently
3775  * best object? If so, use this one instead.
3776  *
3777  * XXX This should break ties using name of the object, or something
3778  * like that, to make the outcome stable.
3779  */
3780  if (nshared > nmatches)
3781  {
3782  statOid = info->statOid;
3783  nmatches = nshared;
3784  matched = shared;
3785  }
3786  }
3787 
3788  /* No match? */
3789  if (statOid == InvalidOid)
3790  return false;
3791  Assert(nmatches > 1 && matched != NULL);
3792 
3793  stats = statext_ndistinct_load(statOid);
3794 
3795  /*
3796  * If we have a match, search it for the specific item that matches (there
3797  * must be one), and construct the output values.
3798  */
3799  if (stats)
3800  {
3801  int i;
3802  List *newlist = NIL;
3803  MVNDistinctItem *item = NULL;
3804 
3805  /* Find the specific item that exactly matches the combination */
3806  for (i = 0; i < stats->nitems; i++)
3807  {
3808  MVNDistinctItem *tmpitem = &stats->items[i];
3809 
3810  if (bms_subset_compare(tmpitem->attrs, matched) == BMS_EQUAL)
3811  {
3812  item = tmpitem;
3813  break;
3814  }
3815  }
3816 
3817  /* make sure we found an item */
3818  if (!item)
3819  elog(ERROR, "corrupt MVNDistinct entry");
3820 
3821  /* Form the output varinfo list, keeping only unmatched ones */
3822  foreach(lc, *varinfos)
3823  {
3824  GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3825  AttrNumber attnum;
3826 
3827  if (!IsA(varinfo->var, Var))
3828  {
3829  newlist = lappend(newlist, varinfo);
3830  continue;
3831  }
3832 
3833  attnum = ((Var *) varinfo->var)->varattno;
3834  if (!bms_is_member(attnum, matched))
3835  newlist = lappend(newlist, varinfo);
3836  }
3837 
3838  *varinfos = newlist;
3839  *ndistinct = item->ndistinct;
3840  return true;
3841  }
3842 
3843  return false;
3844 }
3845 
3846 /*
3847  * convert_to_scalar
3848  * Convert non-NULL values of the indicated types to the comparison
3849  * scale needed by scalarineqsel().
3850  * Returns "true" if successful.
3851  *
3852  * XXX this routine is a hack: ideally we should look up the conversion
3853  * subroutines in pg_type.
3854  *
3855  * All numeric datatypes are simply converted to their equivalent
3856  * "double" values. (NUMERIC values that are outside the range of "double"
3857  * are clamped to +/- HUGE_VAL.)
3858  *
3859  * String datatypes are converted by convert_string_to_scalar(),
3860  * which is explained below. The reason why this routine deals with
3861  * three values at a time, not just one, is that we need it for strings.
3862  *
3863  * The bytea datatype is just enough different from strings that it has
3864  * to be treated separately.
3865  *
3866  * The several datatypes representing absolute times are all converted
3867  * to Timestamp, which is actually a double, and then we just use that
3868  * double value. Note this will give correct results even for the "special"
3869  * values of Timestamp, since those are chosen to compare correctly;
3870  * see timestamp_cmp.
3871  *
3872  * The several datatypes representing relative times (intervals) are all
3873  * converted to measurements expressed in seconds.
3874  */
3875 static bool
3876 convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
3877  Datum lobound, Datum hibound, Oid boundstypid,
3878  double *scaledlobound, double *scaledhibound)
3879 {
3880  /*
3881  * Both the valuetypid and the boundstypid should exactly match the
3882  * declared input type(s) of the operator we are invoked for, so we just
3883  * error out if either is not recognized.
3884  *
3885  * XXX The histogram we are interpolating between points of could belong
3886  * to a column that's only binary-compatible with the declared type. In
3887  * essence we are assuming that the semantics of binary-compatible types
3888  * are enough alike that we can use a histogram generated with one type's
3889  * operators to estimate selectivity for the other's. This is outright
3890  * wrong in some cases --- in particular signed versus unsigned
3891  * interpretation could trip us up. But it's useful enough in the
3892  * majority of cases that we do it anyway. Should think about more
3893  * rigorous ways to do it.
3894  */
3895  switch (valuetypid)
3896  {
3897  /*
3898  * Built-in numeric types
3899  */
3900  case BOOLOID:
3901  case INT2OID:
3902  case INT4OID:
3903  case INT8OID:
3904  case FLOAT4OID:
3905  case FLOAT8OID:
3906  case NUMERICOID:
3907  case OIDOID:
3908  case REGPROCOID:
3909  case REGPROCEDUREOID:
3910  case REGOPEROID:
3911  case REGOPERATOROID:
3912  case REGCLASSOID:
3913  case REGTYPEOID:
3914  case REGCONFIGOID:
3915  case REGDICTIONARYOID:
3916  case REGROLEOID:
3917  case REGNAMESPACEOID:
3918  *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
3919  *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
3920  *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
3921  return true;
3922 
3923  /*
3924  * Built-in string types
3925  */
3926  case CHAROID:
3927  case BPCHAROID:
3928  case VARCHAROID:
3929  case TEXTOID:
3930  case NAMEOID:
3931  {
3932  char *valstr = convert_string_datum(value, valuetypid);
3933  char *lostr = convert_string_datum(lobound, boundstypid);
3934  char *histr = convert_string_datum(hibound, boundstypid);
3935 
3936  convert_string_to_scalar(valstr, scaledvalue,
3937  lostr, scaledlobound,
3938  histr, scaledhibound);
3939  pfree(valstr);
3940  pfree(lostr);
3941  pfree(histr);
3942  return true;
3943  }
3944 
3945  /*
3946  * Built-in bytea type
3947  */
3948  case BYTEAOID:
3949  {
3950  convert_bytea_to_scalar(value, scaledvalue,
3951  lobound, scaledlobound,
3952  hibound, scaledhibound);
3953  return true;
3954  }
3955 
3956  /*
3957  * Built-in time types
3958  */
3959  case TIMESTAMPOID:
3960  case TIMESTAMPTZOID:
3961  case ABSTIMEOID:
3962  case DATEOID:
3963  case INTERVALOID:
3964  case RELTIMEOID:
3965  case TINTERVALOID:
3966  case TIMEOID:
3967  case TIMETZOID:
3968  *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
3969  *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
3970  *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
3971  return true;
3972 
3973  /*
3974  * Built-in network types
3975  */
3976  case INETOID:
3977  case CIDROID:
3978  case MACADDROID:
3979  case MACADDR8OID:
3980  *scaledvalue = convert_network_to_scalar(value, valuetypid);
3981  *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
3982  *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
3983  return true;
3984  }
3985  /* Don't know how to convert */
3986  *scaledvalue = *scaledlobound = *scaledhibound = 0;
3987  return false;
3988 }
3989 
3990 /*
3991  * Do convert_to_scalar()'s work for any numeric data type.
3992  */
3993 static double
3995 {
3996  switch (typid)
3997  {
3998  case BOOLOID:
3999  return (double) DatumGetBool(value);
4000  case INT2OID:
4001  return (double) DatumGetInt16(value);
4002  case INT4OID:
4003  return (double) DatumGetInt32(value);
4004  case INT8OID:
4005  return (double) DatumGetInt64(value);
4006  case FLOAT4OID:
4007  return (double) DatumGetFloat4(value);
4008  case FLOAT8OID:
4009  return (double) DatumGetFloat8(value);
4010  case NUMERICOID:
4011  /* Note: out-of-range values will be clamped to +-HUGE_VAL */
4012  return (double)
4014  value));
4015  case OIDOID:
4016  case REGPROCOID:
4017  case REGPROCEDUREOID:
4018  case REGOPEROID:
4019  case REGOPERATOROID:
4020  case REGCLASSOID:
4021  case REGTYPEOID:
4022  case REGCONFIGOID:
4023  case REGDICTIONARYOID:
4024  case REGROLEOID:
4025  case REGNAMESPACEOID:
4026  /* we can treat OIDs as integers... */
4027  return (double) DatumGetObjectId(value);
4028  }
4029 
4030  /*
4031  * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4032  * an operator with one numeric and one non-numeric operand.
4033  */
4034  elog(ERROR, "unsupported type: %u", typid);
4035  return 0;
4036 }
4037 
4038 /*
4039  * Do convert_to_scalar()'s work for any character-string data type.
4040  *
4041  * String datatypes are converted to a scale that ranges from 0 to 1,
4042  * where we visualize the bytes of the string as fractional digits.
4043  *
4044  * We do not want the base to be 256, however, since that tends to
4045  * generate inflated selectivity estimates; few databases will have
4046  * occurrences of all 256 possible byte values at each position.
4047  * Instead, use the smallest and largest byte values seen in the bounds
4048  * as the estimated range for each byte, after some fudging to deal with
4049  * the fact that we probably aren't going to see the full range that way.
4050  *
4051  * An additional refinement is that we discard any common prefix of the
4052  * three strings before computing the scaled values. This allows us to
4053  * "zoom in" when we encounter a narrow data range. An example is a phone
4054  * number database where all the values begin with the same area code.
4055  * (Actually, the bounds will be adjacent histogram-bin-boundary values,
4056  * so this is more likely to happen than you might think.)
4057  */
4058 static void
4060  double *scaledvalue,
4061  char *lobound,
4062  double *scaledlobound,
4063  char *hibound,
4064  double *scaledhibound)
4065 {
4066  int rangelo,
4067  rangehi;
4068  char *sptr;
4069 
4070  rangelo = rangehi = (unsigned char) hibound[0];
4071  for (sptr = lobound; *sptr; sptr++)
4072  {
4073  if (rangelo > (unsigned char) *sptr)
4074  rangelo = (unsigned char) *sptr;
4075  if (rangehi < (unsigned char) *sptr)
4076  rangehi = (unsigned char) *sptr;
4077  }
4078  for (sptr = hibound; *sptr; sptr++)
4079  {
4080  if (rangelo > (unsigned char) *sptr)
4081  rangelo = (unsigned char) *sptr;
4082  if (rangehi < (unsigned char) *sptr)
4083  rangehi = (unsigned char) *sptr;
4084  }
4085  /* If range includes any upper-case ASCII chars, make it include all */
4086  if (rangelo <= 'Z' && rangehi >= 'A')
4087  {
4088  if (rangelo > 'A')
4089  rangelo = 'A';
4090  if (rangehi < 'Z')
4091  rangehi = 'Z';
4092  }
4093  /* Ditto lower-case */
4094  if (rangelo <= 'z' && rangehi >= 'a')
4095  {
4096  if (rangelo > 'a')
4097  rangelo = 'a';
4098  if (rangehi < 'z')
4099  rangehi = 'z';
4100  }
4101  /* Ditto digits */
4102  if (rangelo <= '9' && rangehi >= '0')
4103  {
4104  if (rangelo > '0')
4105  rangelo = '0';
4106  if (rangehi < '9')
4107  rangehi = '9';
4108  }
4109 
4110  /*
4111  * If range includes less than 10 chars, assume we have not got enough
4112  * data, and make it include regular ASCII set.
4113  */
4114  if (rangehi - rangelo < 9)
4115  {
4116  rangelo = ' ';
4117  rangehi = 127;
4118  }
4119 
4120  /*
4121  * Now strip any common prefix of the three strings.
4122  */
4123  while (*lobound)
4124  {
4125  if (*lobound != *hibound || *lobound != *value)
4126  break;
4127  lobound++, hibound++, value++;
4128  }
4129 
4130  /*
4131  * Now we can do the conversions.
4132  */
4133  *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
4134  *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
4135  *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
4136 }
4137 
4138 static double
4139 convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
4140 {
4141  int slen = strlen(value);
4142  double num,
4143  denom,
4144  base;
4145 
4146  if (slen <= 0)
4147  return 0.0; /* empty string has scalar value 0 */
4148 
4149  /*
4150  * There seems little point in considering more than a dozen bytes from
4151  * the string. Since base is at least 10, that will give us nominal
4152  * resolution of at least 12 decimal digits, which is surely far more
4153  * precision than this estimation technique has got anyway (especially in
4154  * non-C locales). Also, even with the maximum possible base of 256, this
4155  * ensures denom cannot grow larger than 256^13 = 2.03e31, which will not
4156  * overflow on any known machine.
4157  */
4158  if (slen > 12)
4159  slen = 12;
4160 
4161  /* Convert initial characters to fraction */
4162  base = rangehi - rangelo + 1;
4163  num = 0.0;
4164  denom = base;
4165  while (slen-- > 0)
4166  {
4167  int ch = (unsigned char) *value++;
4168 
4169  if (ch < rangelo)
4170  ch = rangelo - 1;
4171  else if (ch > rangehi)
4172  ch = rangehi + 1;
4173  num += ((double) (ch - rangelo)) / denom;
4174  denom *= base;
4175  }
4176 
4177  return num;
4178 }
4179 
4180 /*
4181  * Convert a string-type Datum into a palloc'd, null-terminated string.
4182  *
4183  * When using a non-C locale, we must pass the string through strxfrm()
4184  * before continuing, so as to generate correct locale-specific results.
4185  */
4186 static char *
4188 {
4189  char *val;
4190 
4191  switch (typid)
4192  {
4193  case CHAROID:
4194  val = (char *) palloc(2);
4195  val[0] = DatumGetChar(value);
4196  val[1] = '\0';
4197  break;
4198  case BPCHAROID:
4199  case VARCHAROID:
4200  case TEXTOID:
4201  val = TextDatumGetCString(value);
4202  break;
4203  case NAMEOID:
4204  {
4205  NameData *nm = (NameData *) DatumGetPointer(value);
4206 
4207  val = pstrdup(NameStr(*nm));
4208  break;
4209  }
4210  default:
4211 
4212  /*
4213  * Can't get here unless someone tries to use scalarltsel on an
4214  * operator with one string and one non-string operand.
4215  */
4216  elog(ERROR, "unsupported type: %u", typid);
4217  return NULL;
4218  }
4219 
4221  {
4222  char *xfrmstr;
4223  size_t xfrmlen;
4224  size_t xfrmlen2 PG_USED_FOR_ASSERTS_ONLY;
4225 
4226  /*
4227  * XXX: We could guess at a suitable output buffer size and only call
4228  * strxfrm twice if our guess is too small.
4229  *
4230  * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
4231  * bogus data or set an error. This is not really a problem unless it
4232  * crashes since it will only give an estimation error and nothing
4233  * fatal.
4234  */
4235 #if _MSC_VER == 1400 /* VS.Net 2005 */
4236 
4237  /*
4238  *
4239  * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99694
4240  */
4241  {
4242  char x[1];
4243 
4244  xfrmlen = strxfrm(x, val, 0);
4245  }
4246 #else
4247  xfrmlen = strxfrm(NULL, val, 0);
4248 #endif
4249 #ifdef WIN32
4250 
4251  /*
4252  * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
4253  * of trying to allocate this much memory (and fail), just return the
4254  * original string unmodified as if we were in the C locale.
4255  */
4256  if (xfrmlen == INT_MAX)
4257  return val;
4258 #endif
4259  xfrmstr = (char *) palloc(xfrmlen + 1);
4260  xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
4261 
4262  /*
4263  * Some systems (e.g., glibc) can return a smaller value from the
4264  * second call than the first; thus the Assert must be <= not ==.
4265  */
4266  Assert(xfrmlen2 <= xfrmlen);
4267  pfree(val);
4268  val = xfrmstr;
4269  }
4270 
4271  return val;
4272 }
4273 
4274 /*
4275  * Do convert_to_scalar()'s work for any bytea data type.
4276  *
4277  * Very similar to convert_string_to_scalar except we can't assume
4278  * null-termination and therefore pass explicit lengths around.
4279  *
4280  * Also, assumptions about likely "normal" ranges of characters have been
4281  * removed - a data range of 0..255 is always used, for now. (Perhaps
4282  * someday we will add information about actual byte data range to
4283  * pg_statistic.)
4284  */
4285 static void
4287  double *scaledvalue,
4288  Datum lobound,
4289  double *scaledlobound,
4290  Datum hibound,
4291  double *scaledhibound)
4292 {
4293  int rangelo,
4294  rangehi,
4295  valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
4296  loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
4297  hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
4298  i,
4299  minlen;
4300  unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
4301  *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
4302  *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
4303 
4304  /*
4305  * Assume bytea data is uniformly distributed across all byte values.
4306  */
4307  rangelo = 0;
4308  rangehi = 255;
4309 
4310  /*
4311  * Now strip any common prefix of the three strings.
4312  */
4313  minlen = Min(Min(valuelen, loboundlen), hiboundlen);
4314  for (i = 0; i < minlen; i++)
4315  {
4316  if (*lostr != *histr || *lostr != *valstr)
4317  break;
4318  lostr++, histr++, valstr++;
4319  loboundlen--, hiboundlen--, valuelen--;
4320  }
4321 
4322  /*
4323  * Now we can do the conversions.
4324  */
4325  *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
4326  *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
4327  *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
4328 }
4329 
4330 static double
4331 convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
4332  int rangelo, int rangehi)
4333 {
4334  double num,
4335  denom,
4336  base;
4337 
4338  if (valuelen <= 0)
4339  return 0.0; /* empty string has scalar value 0 */
4340 
4341  /*
4342  * Since base is 256, need not consider more than about 10 chars (even
4343  * this many seems like overkill)
4344  */
4345  if (valuelen > 10)
4346  valuelen = 10;
4347 
4348  /* Convert initial characters to fraction */
4349  base = rangehi - rangelo + 1;
4350  num = 0.0;
4351  denom = base;
4352  while (valuelen-- > 0)
4353  {
4354  int ch = *value++;
4355 
4356  if (ch < rangelo)
4357  ch = rangelo - 1;
4358  else if (ch > rangehi)
4359  ch = rangehi + 1;
4360  num += ((double) (ch - rangelo)) / denom;
4361  denom *= base;
4362  }
4363 
4364  return num;
4365 }
4366 
4367 /*
4368  * Do convert_to_scalar()'s work for any timevalue data type.
4369  */
4370 static double
4372 {
4373  switch (typid)
4374  {
4375  case TIMESTAMPOID:
4376  return DatumGetTimestamp(value);
4377  case TIMESTAMPTZOID:
4378  return DatumGetTimestampTz(value);
4379  case ABSTIMEOID:
4381  value));
4382  case DATEOID:
4384  case INTERVALOID:
4385  {
4387 
4388  /*
4389  * Convert the month part of Interval to days using assumed
4390  * average month length of 365.25/12.0 days. Not too
4391  * accurate, but plenty good enough for our purposes.
4392  */
4393  return interval->time + interval->day * (double) USECS_PER_DAY +
4394  interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
4395  }
4396  case RELTIMEOID:
4397  return (DatumGetRelativeTime(value) * 1000000.0);
4398  case TINTERVALOID:
4399  {
4400  TimeInterval tinterval = DatumGetTimeInterval(value);
4401 
4402  if (tinterval->status != 0)
4403  return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
4404  return 0; /* for lack of a better idea */
4405  }
4406  case TIMEOID:
4407  return DatumGetTimeADT(value);
4408  case TIMETZOID:
4409  {
4410  TimeTzADT *timetz = DatumGetTimeTzADTP(value);
4411 
4412  /* use GMT-equivalent time */
4413  return (double) (timetz->time + (timetz->zone * 1000000.0));
4414  }
4415  }
4416 
4417  /*
4418  * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4419  * an operator with one timevalue and one non-timevalue operand.
4420  */
4421  elog(ERROR, "unsupported type: %u", typid);
4422  return 0;
4423 }
4424 
4425 
4426 /*
4427  * get_restriction_variable
4428  * Examine the args of a restriction clause to see if it's of the
4429  * form (variable op pseudoconstant) or (pseudoconstant op variable),
4430  * where "variable" could be either a Var or an expression in vars of a
4431  * single relation. If so, extract information about the variable,
4432  * and also indicate which side it was on and the other argument.
4433  *
4434  * Inputs:
4435  * root: the planner info
4436  * args: clause argument list
4437  * varRelid: see specs for restriction selectivity functions
4438  *
4439  * Outputs: (these are valid only if TRUE is returned)
4440  * *vardata: gets information about variable (see examine_variable)
4441  * *other: gets other clause argument, aggressively reduced to a constant
4442  * *varonleft: set TRUE if variable is on the left, FALSE if on the right
4443  *
4444  * Returns TRUE if a variable is identified, otherwise FALSE.
4445  *
4446  * Note: if there are Vars on both sides of the clause, we must fail, because
4447  * callers are expecting that the other side will act like a pseudoconstant.
4448  */
4449 bool
4451  VariableStatData *vardata, Node **other,
4452  bool *varonleft)
4453 {
4454  Node *left,
4455  *right;
4456  VariableStatData rdata;
4457 
4458  /* Fail if not a binary opclause (probably shouldn't happen) */
4459  if (list_length(args) != 2)
4460  return false;
4461 
4462  left = (Node *) linitial(args);
4463  right = (Node *) lsecond(args);
4464 
4465  /*
4466  * Examine both sides. Note that when varRelid is nonzero, Vars of other
4467  * relations will be treated as pseudoconstants.
4468  */
4469  examine_variable(root, left, varRelid, vardata);
4470  examine_variable(root, right, varRelid, &rdata);
4471 
4472  /*
4473  * If one side is a variable and the other not, we win.
4474  */
4475  if (vardata->rel && rdata.rel == NULL)
4476  {
4477  *varonleft = true;
4478  *other = estimate_expression_value(root, rdata.var);
4479  /* Assume we need no ReleaseVariableStats(rdata) here */
4480  return true;
4481  }
4482 
4483  if (vardata->rel == NULL && rdata.rel)
4484  {
4485  *varonleft = false;
4486  *other = estimate_expression_value(root, vardata->var);
4487  /* Assume we need no ReleaseVariableStats(*vardata) here */
4488  *vardata = rdata;
4489  return true;
4490  }
4491 
4492  /* Oops, clause has wrong structure (probably var op var) */
4493  ReleaseVariableStats(*vardata);
4494  ReleaseVariableStats(rdata);
4495 
4496  return false;
4497 }
4498 
4499 /*
4500  * get_join_variables
4501  * Apply examine_variable() to each side of a join clause.
4502  * Also, attempt to identify whether the join clause has the same
4503  * or reversed sense compared to the SpecialJoinInfo.
4504  *
4505  * We consider the join clause "normal" if it is "lhs_var OP rhs_var",
4506  * or "reversed" if it is "rhs_var OP lhs_var". In complicated cases
4507  * where we can't tell for sure, we default to assuming it's normal.
4508  */
4509 void
4511  VariableStatData *vardata1, VariableStatData *vardata2,
4512  bool *join_is_reversed)
4513 {
4514  Node *left,
4515  *right;
4516 
4517  if (list_length(args) != 2)
4518  elog(ERROR, "join operator should take two arguments");
4519 
4520  left = (Node *) linitial(args);
4521  right = (Node *) lsecond(args);
4522 
4523  examine_variable(root, left, 0, vardata1);
4524  examine_variable(root, right, 0, vardata2);
4525 
4526  if (vardata1->rel &&
4527  bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4528  *join_is_reversed = true; /* var1 is on RHS */
4529  else if (vardata2->rel &&
4530  bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4531  *join_is_reversed = true; /* var2 is on LHS */
4532  else
4533  *join_is_reversed = false;
4534 }
4535 
4536 /*
4537  * examine_variable
4538  * Try to look up statistical data about an expression.
4539  * Fill in a VariableStatData struct to describe the expression.
4540  *
4541  * Inputs:
4542  * root: the planner info
4543  * node: the expression tree to examine
4544  * varRelid: see specs for restriction selectivity functions
4545  *
4546  * Outputs: *vardata is filled as follows:
4547  * var: the input expression (with any binary relabeling stripped, if
4548  * it is or contains a variable; but otherwise the type is preserved)
4549  * rel: RelOptInfo for relation containing variable; NULL if expression
4550  * contains no Vars (NOTE this could point to a RelOptInfo of a
4551  * subquery, not one in the current query).
4552  * statsTuple: the pg_statistic entry for the variable, if one exists;
4553  * otherwise NULL.
4554  * freefunc: pointer to a function to release statsTuple with.
4555  * vartype: exposed type of the expression; this should always match
4556  * the declared input type of the operator we are estimating for.
4557  * atttype, atttypmod: actual type/typmod of the "var" expression. This is
4558  * commonly the same as the exposed type of the variable argument,
4559  * but can be different in binary-compatible-type cases.
4560  * isunique: TRUE if we were able to match the var to a unique index or a
4561  * single-column DISTINCT clause, implying its values are unique for
4562  * this query. (Caution: this should be trusted for statistical
4563  * purposes only, since we do not check indimmediate nor verify that
4564  * the exact same definition of equality applies.)
4565  * acl_ok: TRUE if current user has permission to read the column(s)
4566  * underlying the pg_statistic entry. This is consulted by
4567  * statistic_proc_security_check().
4568  *
4569  * Caller is responsible for doing ReleaseVariableStats() before exiting.
4570  */
4571 void
4572 examine_variable(PlannerInfo *root, Node *node, int varRelid,
4573  VariableStatData *vardata)
4574 {
4575  Node *basenode;
4576  Relids varnos;
4577  RelOptInfo *onerel;
4578 
4579  /* Make sure we don't return dangling pointers in vardata */
4580  MemSet(vardata, 0, sizeof(VariableStatData));
4581 
4582  /* Save the exposed type of the expression */
4583  vardata->vartype = exprType(node);
4584 
4585  /* Look inside any binary-compatible relabeling */
4586 
4587  if (IsA(node, RelabelType))
4588  basenode = (Node *) ((RelabelType *) node)->arg;
4589  else
4590  basenode = node;
4591 
4592  /* Fast path for a simple Var */
4593 
4594  if (IsA(basenode, Var) &&
4595  (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4596  {
4597  Var *var = (Var *) basenode;
4598 
4599  /* Set up result fields other than the stats tuple */
4600  vardata->var = basenode; /* return Var without relabeling */
4601  vardata->rel = find_base_rel(root, var->varno);
4602  vardata->atttype = var->vartype;
4603  vardata->atttypmod = var->vartypmod;
4604  vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4605 
4606  /* Try to locate some stats */
4607  examine_simple_variable(root, var, vardata);
4608 
4609  return;
4610  }
4611 
4612  /*
4613  * Okay, it's a more complicated expression. Determine variable
4614  * membership. Note that when varRelid isn't zero, only vars of that
4615  * relation are considered "real" vars.
4616  */
4617  varnos = pull_varnos(basenode);
4618 
4619  onerel = NULL;
4620 
4621  switch (bms_membership(varnos))
4622  {
4623  case BMS_EMPTY_SET:
4624  /* No Vars at all ... must be pseudo-constant clause */
4625  break;
4626  case BMS_SINGLETON:
4627  if (varRelid == 0 || bms_is_member(varRelid, varnos))
4628  {
4629  onerel = find_base_rel(root,
4630  (varRelid ? varRelid : bms_singleton_member(varnos)));
4631  vardata->rel = onerel;
4632  node = basenode; /* strip any relabeling */
4633  }
4634  /* else treat it as a constant */
4635  break;
4636  case BMS_MULTIPLE:
4637  if (varRelid == 0)
4638  {
4639  /* treat it as a variable of a join relation */
4640  vardata->rel = find_join_rel(root, varnos);
4641  node = basenode; /* strip any relabeling */
4642  }
4643  else if (bms_is_member(varRelid, varnos))
4644  {
4645  /* ignore the vars belonging to other relations */
4646  vardata->rel = find_base_rel(root, varRelid);
4647  node = basenode; /* strip any relabeling */
4648  /* note: no point in expressional-index search here */
4649  }
4650  /* else treat it as a constant */
4651  break;
4652  }
4653 
4654  bms_free(varnos);
4655 
4656  vardata->var = node;
4657  vardata->atttype = exprType(node);
4658  vardata->atttypmod = exprTypmod(node);
4659 
4660  if (onerel)
4661  {
4662  /*
4663  * We have an expression in vars of a single relation. Try to match
4664  * it to expressional index columns, in hopes of finding some
4665  * statistics.
4666  *
4667  * XXX it's conceivable that there are multiple matches with different
4668  * index opfamilies; if so, we need to pick one that matches the
4669  * operator we are estimating for. FIXME later.
4670  */
4671  ListCell *ilist;
4672 
4673  foreach(ilist, onerel->indexlist)
4674  {
4675  IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4676  ListCell *indexpr_item;
4677  int pos;
4678 
4679  indexpr_item = list_head(index->indexprs);
4680  if (indexpr_item == NULL)
4681  continue; /* no expressions here... */
4682 
4683  for (pos = 0; pos < index->ncolumns; pos++)
4684  {
4685  if (index->indexkeys[pos] == 0)
4686  {
4687  Node *indexkey;
4688 
4689  if (indexpr_item == NULL)
4690  elog(ERROR, "too few entries in indexprs list");
4691  indexkey = (Node *) lfirst(indexpr_item);
4692  if (indexkey && IsA(indexkey, RelabelType))
4693  indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4694  if (equal(node, indexkey))
4695  {
4696  /*
4697  * Found a match ... is it a unique index? Tests here
4698  * should match has_unique_index().
4699  */
4700  if (index->unique &&
4701  index->ncolumns == 1 &&
4702  (index->indpred == NIL || index->predOK))
4703  vardata->isunique = true;
4704 
4705  /*
4706  * Has it got stats? We only consider stats for
4707  * non-partial indexes, since partial indexes probably
4708  * don't reflect whole-relation statistics; the above
4709  * check for uniqueness is the only info we take from
4710  * a partial index.
4711  *
4712  * An index stats hook, however, must make its own
4713  * decisions about what to do with partial indexes.
4714  */
4715  if (get_index_stats_hook &&
4716  (*get_index_stats_hook) (root, index->indexoid,
4717  pos + 1, vardata))
4718  {
4719  /*
4720  * The hook took control of acquiring a stats
4721  * tuple. If it did supply a tuple, it'd better
4722  * have supplied a freefunc.
4723  */
4724  if (HeapTupleIsValid(vardata->statsTuple) &&
4725  !vardata->freefunc)
4726  elog(ERROR, "no function provided to release variable stats with");
4727  }
4728  else if (index->indpred == NIL)
4729  {
4730  vardata->statsTuple =
4732  ObjectIdGetDatum(index->indexoid),
4733  Int16GetDatum(pos + 1),
4734  BoolGetDatum(false));
4735  vardata->freefunc = ReleaseSysCache;
4736 
4737  if (HeapTupleIsValid(vardata->statsTuple))
4738  {
4739  /* Get index's table for permission check */
4740  RangeTblEntry *rte;
4741 
4742  rte = planner_rt_fetch(index->rel->relid, root);
4743  Assert(rte->rtekind == RTE_RELATION);
4744 
4745  /*
4746  * For simplicity, we insist on the whole
4747  * table being selectable, rather than trying
4748  * to identify which column(s) the index
4749  * depends on.
4750  */
4751  vardata->acl_ok =
4753  ACL_SELECT) == ACLCHECK_OK);
4754  }
4755  else
4756  {
4757  /* suppress leakproofness checks later */
4758  vardata->acl_ok = true;
4759  }
4760  }
4761  if (vardata->statsTuple)
4762  break;
4763  }
4764  indexpr_item = lnext(indexpr_item);
4765  }
4766  }
4767  if (vardata->statsTuple)
4768  break;
4769  }
4770  }
4771 }
4772 
4773 /*
4774  * examine_simple_variable
4775  * Handle a simple Var for examine_variable
4776  *
4777  * This is split out as a subroutine so that we can recurse to deal with
4778  * Vars referencing subqueries.
4779  *
4780  * We already filled in all the fields of *vardata except for the stats tuple.
4781  */
4782 static void
4784  VariableStatData *vardata)
4785 {
4786  RangeTblEntry *rte = root->simple_rte_array[var->varno];
4787 
4788  Assert(IsA(rte, RangeTblEntry));
4789 
4791  (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
4792  {
4793  /*
4794  * The hook took control of acquiring a stats tuple. If it did supply
4795  * a tuple, it'd better have supplied a freefunc.
4796  */
4797  if (HeapTupleIsValid(vardata->statsTuple) &&
4798  !vardata->freefunc)
4799  elog(ERROR, "no function provided to release variable stats with");
4800  }
4801  else if (rte->rtekind == RTE_RELATION)
4802  {
4803  /*
4804  * Plain table or parent of an inheritance appendrel, so look up the
4805  * column in pg_statistic
4806  */
4808  ObjectIdGetDatum(rte->relid),
4809  Int16GetDatum(var->varattno),
4810  BoolGetDatum(rte->inh));
4811  vardata->freefunc = ReleaseSysCache;
4812 
4813  if (HeapTupleIsValid(vardata->statsTuple))
4814  {
4815  /* check if user has permission to read this column */
4816  vardata->acl_ok =
4818  ACL_SELECT) == ACLCHECK_OK) ||
4820  ACL_SELECT) == ACLCHECK_OK);
4821  }
4822  else
4823  {
4824  /* suppress any possible leakproofness checks later */
4825  vardata->acl_ok = true;
4826  }
4827  }
4828  else if (rte->rtekind == RTE_SUBQUERY && !rte->inh)
4829  {
4830  /*
4831  * Plain subquery (not one that was converted to an appendrel).
4832  */
4833  Query *subquery = rte->subquery;
4834  RelOptInfo *rel;
4835  TargetEntry *ste;
4836 
4837  /*
4838  * Punt if it's a whole-row var rather than a plain column reference.
4839  */
4840  if (var->varattno == InvalidAttrNumber)
4841  return;
4842 
4843  /*
4844  * Punt if subquery uses set operations or GROUP BY, as these will
4845  * mash underlying columns' stats beyond recognition. (Set ops are
4846  * particularly nasty; if we forged ahead, we would return stats
4847  * relevant to only the leftmost subselect...) DISTINCT is also
4848  * problematic, but we check that later because there is a possibility
4849  * of learning something even with it.
4850  */
4851  if (subquery->setOperations ||
4852  subquery->groupClause)
4853  return;
4854 
4855  /*
4856  * OK, fetch RelOptInfo for subquery. Note that we don't change the
4857  * rel returned in vardata, since caller expects it to be a rel of the
4858  * caller's query level. Because we might already be recursing, we
4859  * can't use that rel pointer either, but have to look up the Var's
4860  * rel afresh.
4861  */
4862  rel = find_base_rel(root, var->varno);
4863 
4864  /* If the subquery hasn't been planned yet, we have to punt */
4865  if (rel->subroot == NULL)
4866  return;
4867  Assert(IsA(rel->subroot, PlannerInfo));
4868 
4869  /*
4870  * Switch our attention to the subquery as mangled by the planner. It
4871  * was okay to look at the pre-planning version for the tests above,
4872  * but now we need a Var that will refer to the subroot's live
4873  * RelOptInfos. For instance, if any subquery pullup happened during
4874  * planning, Vars in the targetlist might have gotten replaced, and we
4875  * need to see the replacement expressions.
4876  */
4877  subquery = rel->subroot->parse;
4878  Assert(IsA(subquery, Query));
4879 
4880  /* Get the subquery output expression referenced by the upper Var */
4881  ste = get_tle_by_resno(subquery->targetList, var->varattno);
4882  if (ste == NULL || ste->resjunk)
4883  elog(ERROR, "subquery %s does not have attribute %d",
4884  rte->eref->aliasname, var->varattno);
4885  var = (Var *) ste->expr;
4886 
4887  /*
4888  * If subquery uses DISTINCT, we can't make use of any stats for the
4889  * variable ... but, if it's the only DISTINCT column, we are entitled
4890  * to consider it unique. We do the test this way so that it works
4891  * for cases involving DISTINCT ON.
4892  */
4893  if (subquery->distinctClause)
4894  {
4895  if (list_length(subquery->distinctClause) == 1 &&
4896  targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
4897  vardata->isunique = true;
4898  /* cannot go further */
4899  return;
4900  }
4901 
4902  /*
4903  * If the sub-query originated from a view with the security_barrier
4904  * attribute, we must not look at the variable's statistics, though it
4905  * seems all right to notice the existence of a DISTINCT clause. So
4906  * stop here.
4907  *
4908  * This is probably a harsher restriction than necessary; it's
4909  * certainly OK for the selectivity estimator (which is a C function,
4910  * and therefore omnipotent anyway) to look at the statistics. But
4911  * many selectivity estimators will happily *invoke the operator
4912  * function* to try to work out a good estimate - and that's not OK.
4913  * So for now, don't dig down for stats.
4914  */
4915  if (rte->security_barrier)
4916  return;
4917 
4918  /* Can only handle a simple Var of subquery's query level */
4919  if (var && IsA(var, Var) &&
4920  var->varlevelsup == 0)
4921  {
4922  /*
4923  * OK, recurse into the subquery. Note that the original setting
4924  * of vardata->isunique (which will surely be false) is left
4925  * unchanged in this situation. That's what we want, since even
4926  * if the underlying column is unique, the subquery may have
4927  * joined to other tables in a way that creates duplicates.
4928  */
4929  examine_simple_variable(rel->subroot, var, vardata);
4930  }
4931  }
4932  else
4933  {
4934  /*
4935  * Otherwise, the Var comes from a FUNCTION, VALUES, or CTE RTE. (We
4936  * won't see RTE_JOIN here because join alias Vars have already been
4937  * flattened.) There's not much we can do with function outputs, but
4938  * maybe someday try to be smarter about VALUES and/or CTEs.
4939  */
4940  }
4941 }
4942 
4943 /*
4944  * Check whether it is permitted to call func_oid passing some of the
4945  * pg_statistic data in vardata. We allow this either if the user has SELECT
4946  * privileges on the table or column underlying the pg_statistic data or if
4947  * the function is marked leak-proof.
4948  */
4949 bool
4951 {
4952  if (vardata->acl_ok)
4953  return true;
4954 
4955  if (!OidIsValid(func_oid))
4956  return false;
4957 
4958  if (get_func_leakproof(func_oid))
4959  return true;
4960 
4961  ereport(DEBUG2,
4962  (errmsg_internal("not using statistics because function \"%s\" is not leak-proof",
4963  get_func_name(func_oid))));
4964  return false;
4965 }
4966 
4967 /*
4968  * get_variable_numdistinct
4969  * Estimate the number of distinct values of a variable.
4970  *
4971  * vardata: results of examine_variable
4972  * *isdefault: set to TRUE if the result is a default rather than based on
4973  * anything meaningful.
4974  *
4975  * NB: be careful to produce a positive integral result, since callers may
4976  * compare the result to exact integer counts, or might divide by it.
4977  */
4978 double
4979 get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
4980 {
4981  double stadistinct;
4982  double stanullfrac = 0.0;
4983  double ntuples;
4984 
4985  *isdefault = false;
4986 
4987  /*
4988  * Determine the stadistinct value to use. There are cases where we can
4989  * get an estimate even without a pg_statistic entry, or can get a better
4990  * value than is in pg_statistic. Grab stanullfrac too if we can find it
4991  * (otherwise, assume no nulls, for lack of any better idea).
4992  */
4993  if (HeapTupleIsValid(vardata->statsTuple))
4994  {
4995  /* Use the pg_statistic entry */
4996  Form_pg_statistic stats;
4997 
4998  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
4999  stadistinct = stats->stadistinct;
5000  stanullfrac = stats->stanullfrac;
5001  }
5002  else if (vardata->vartype == BOOLOID)
5003  {
5004  /*
5005  * Special-case boolean columns: presumably, two distinct values.
5006  *
5007  * Are there any other datatypes we should wire in special estimates
5008  * for?
5009  */
5010  stadistinct = 2.0;
5011  }
5012  else if (vardata->rel && vardata->rel->rtekind == RTE_VALUES)
5013  {
5014  /*
5015  * If the Var represents a column of a VALUES RTE, assume it's unique.
5016  * This could of course be very wrong, but it should tend to be true
5017  * in well-written queries. We could consider examining the VALUES'
5018  * contents to get some real statistics; but that only works if the
5019  * entries are all constants, and it would be pretty expensive anyway.
5020  */
5021  stadistinct = -1.0; /* unique (and all non null) */
5022  }
5023  else
5024  {
5025  /*
5026  * We don't keep statistics for system columns, but in some cases we
5027  * can infer distinctness anyway.
5028  */
5029  if (vardata->var && IsA(vardata->var, Var))
5030  {
5031  switch (((Var *) vardata->var)->varattno)
5032  {
5035  stadistinct = -1.0; /* unique (and all non null) */
5036  break;
5038  stadistinct = 1.0; /* only 1 value */
5039  break;
5040  default:
5041  stadistinct = 0.0; /* means "unknown" */
5042  break;
5043  }
5044  }
5045  else
5046  stadistinct = 0.0; /* means "unknown" */
5047 
5048  /*
5049  * XXX consider using estimate_num_groups on expressions?
5050  */
5051  }
5052 
5053  /*
5054  * If there is a unique index or DISTINCT clause for the variable, assume
5055  * it is unique no matter what pg_statistic says; the statistics could be
5056  * out of date, or we might have found a partial unique index that proves
5057  * the var is unique for this query. However, we'd better still believe
5058  * the null-fraction statistic.
5059  */
5060  if (vardata->isunique)
5061  stadistinct = -1.0 * (1.0 - stanullfrac);
5062 
5063  /*
5064  * If we had an absolute estimate, use that.
5065  */
5066  if (stadistinct > 0.0)
5067  return clamp_row_est(stadistinct);
5068 
5069  /*
5070  * Otherwise we need to get the relation size; punt if not available.
5071  */
5072  if (vardata->rel == NULL)
5073  {
5074  *isdefault = true;
5075  return DEFAULT_NUM_DISTINCT;
5076  }
5077  ntuples = vardata->rel->tuples;
5078  if (ntuples <= 0.0)
5079  {
5080  *isdefault = true;
5081  return DEFAULT_NUM_DISTINCT;
5082  }
5083 
5084  /*
5085  * If we had a relative estimate, use that.
5086  */
5087  if (stadistinct < 0.0)
5088  return clamp_row_est(-stadistinct * ntuples);
5089 
5090  /*
5091  * With no data, estimate ndistinct = ntuples if the table is small, else
5092  * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5093  * that the behavior isn't discontinuous.
5094  */
5095  if (ntuples < DEFAULT_NUM_DISTINCT)
5096  return clamp_row_est(ntuples);
5097 
5098  *isdefault = true;
5099  return DEFAULT_NUM_DISTINCT;
5100 }
5101 
5102 /*
5103  * get_variable_range
5104  * Estimate the minimum and maximum value of the specified variable.
5105  * If successful, store values in *min and *max, and return TRUE.
5106  * If no data available, return FALSE.
5107  *
5108  * sortop is the "<" comparison operator to use. This should generally
5109  * be "<" not ">", as only the former is likely to be found in pg_statistic.
5110  */
5111 static bool
5113  Datum *min, Datum *max)
5114 {
5115  Datum tmin = 0;
5116  Datum tmax = 0;
5117  bool have_data = false;
5118  int16 typLen;
5119  bool typByVal;
5120  Oid opfuncoid;
5121  AttStatsSlot sslot;
5122  int i;
5123 
5124  /*
5125  * XXX It's very tempting to try to use the actual column min and max, if
5126  * we can get them relatively-cheaply with an index probe. However, since
5127  * this function is called many times during join planning, that could
5128  * have unpleasant effects on planning speed. Need more investigation
5129  * before enabling this.
5130  */
5131 #ifdef NOT_USED
5132  if (get_actual_variable_range(root, vardata, sortop, min, max))
5133  return true;
5134 #endif
5135 
5136  if (!HeapTupleIsValid(vardata->statsTuple))
5137  {
5138  /* no stats available, so default result */
5139  return false;
5140  }
5141 
5142  /*
5143  * If we can't apply the sortop to the stats data, just fail. In
5144  * principle, if there's a histogram and no MCVs, we could return the
5145  * histogram endpoints without ever applying the sortop ... but it's
5146  * probably not worth trying, because whatever the caller wants to do with
5147  * the endpoints would likely fail the security check too.
5148  */
5149  if (!statistic_proc_security_check(vardata,
5150  (opfuncoid = get_opcode(sortop))))
5151  return false;
5152 
5153  get_typlenbyval(vardata->atttype, &typLen, &typByVal);
5154 
5155  /*
5156  * If there is a histogram, grab the first and last values.
5157  *
5158  * If there is a histogram that is sorted with some other operator than
5159  * the one we want, fail --- this suggests that there is data we can't
5160  * use.
5161  */
5162  if (get_attstatsslot(&sslot, vardata->statsTuple,
5163  STATISTIC_KIND_HISTOGRAM, sortop,
5165  {
5166  if (sslot.nvalues > 0)
5167  {
5168  tmin = datumCopy(sslot.values[0], typByVal, typLen);
5169  tmax = datumCopy(sslot.values[sslot.nvalues - 1], typByVal, typLen);
5170  have_data = true;
5171  }
5172  free_attstatsslot(&sslot);
5173  }
5174  else if (get_attstatsslot(&sslot, vardata->statsTuple,
5176  0))
5177  {
5178  free_attstatsslot(&sslot);
5179  return false;
5180  }
5181 
5182  /*
5183  * If we have most-common-values info, look for extreme MCVs. This is
5184  * needed even if we also have a histogram, since the histogram excludes
5185  * the MCVs. However, usually the MCVs will not be the extreme values, so
5186  * avoid unnecessary data copying.
5187  */
5188  if (get_attstatsslot(&sslot, vardata->statsTuple,
5191  {
5192  bool tmin_is_mcv = false;
5193  bool tmax_is_mcv = false;
5194  FmgrInfo opproc;
5195 
5196  fmgr_info(opfuncoid, &opproc);
5197 
5198  for (i = 0; i < sslot.nvalues; i++)
5199  {
5200  if (!have_data)
5201  {
5202  tmin = tmax = sslot.values[i];
5203  tmin_is_mcv = tmax_is_mcv = have_data = true;
5204  continue;
5205  }
5206  if (DatumGetBool(FunctionCall2Coll(&opproc,
5208  sslot.values[i], tmin)))
5209  {
5210  tmin = sslot.values[i];
5211  tmin_is_mcv = true;
5212  }
5213  if (DatumGetBool(FunctionCall2Coll(&opproc,
5215  tmax, sslot.values[i])))
5216  {
5217  tmax = sslot.values[i];
5218  tmax_is_mcv = true;
5219  }
5220  }
5221  if (tmin_is_mcv)
5222  tmin = datumCopy(tmin, typByVal, typLen);
5223  if (tmax_is_mcv)
5224  tmax = datumCopy(tmax, typByVal, typLen);
5225  free_attstatsslot(&sslot);
5226  }
5227 
5228  *min = tmin;
5229  *max = tmax;
5230  return have_data;
5231 }
5232 
5233 
5234 /*
5235  * get_actual_variable_range
5236  * Attempt to identify the current *actual* minimum and/or maximum
5237  * of the specified variable, by looking for a suitable btree index
5238  * and fetching its low and/or high values.
5239  * If successful, store values in *min and *max, and return TRUE.
5240  * (Either pointer can be NULL if that endpoint isn't needed.)
5241  * If no data available, return FALSE.
5242  *
5243  * sortop is the "<" comparison operator to use.
5244  */
5245 static bool
5247  Oid sortop,
5248  Datum *min, Datum *max)
5249 {
5250  bool have_data = false;
5251  RelOptInfo *rel = vardata->rel;
5252  RangeTblEntry *rte;
5253  ListCell *lc;
5254 
5255  /* No hope if no relation or it doesn't have indexes */
5256  if (rel == NULL || rel->indexlist == NIL)
5257  return false;
5258  /* If it has indexes it must be a plain relation */
5259  rte = root->simple_rte_array[rel->relid];
5260  Assert(rte->rtekind == RTE_RELATION);
5261 
5262  /* Search through the indexes to see if any match our problem */
5263  foreach(lc, rel->indexlist)
5264  {
5266  ScanDirection indexscandir;
5267 
5268  /* Ignore non-btree indexes */
5269  if (index->relam != BTREE_AM_OID)
5270  continue;
5271 
5272  /*
5273  * Ignore partial indexes --- we only want stats that cover the entire
5274  * relation.
5275  */
5276  if (index->indpred != NIL)
5277  continue;
5278 
5279  /*
5280  * The index list might include hypothetical indexes inserted by a
5281  * get_relation_info hook --- don't try to access them.
5282  */
5283  if (index->hypothetical)
5284  continue;
5285 
5286  /*
5287  * The first index column must match the desired variable and sort
5288  * operator --- but we can use a descending-order index.
5289  */
5290  if (!match_index_to_operand(vardata->var, 0, index))
5291  continue;
5292  switch (get_op_opfamily_strategy(sortop, index->sortopfamily[0]))
5293  {
5294  case BTLessStrategyNumber:
5295  if (index->reverse_sort[0])
5296  indexscandir = BackwardScanDirection;
5297  else
5298  indexscandir = ForwardScanDirection;
5299  break;
5301  if (index->reverse_sort[0])
5302  indexscandir = ForwardScanDirection;
5303  else
5304  indexscandir = BackwardScanDirection;
5305  break;
5306  default:
5307  /* index doesn't match the sortop */
5308  continue;
5309  }
5310 
5311  /*
5312  * Found a suitable index to extract data from. We'll need an EState
5313  * and a bunch of other infrastructure.
5314  */
5315  {
5316  EState *estate;
5317  ExprContext *econtext;
5318  MemoryContext tmpcontext;
5319  MemoryContext oldcontext;
5320  Relation heapRel;
5321  Relation indexRel;
5322  IndexInfo *indexInfo;
5323  TupleTableSlot *slot;
5324  int16 typLen;
5325  bool typByVal;
5326  ScanKeyData scankeys[1];
5327  IndexScanDesc index_scan;
5328  HeapTuple tup;
5330  bool isnull[INDEX_MAX_KEYS];
5331  SnapshotData SnapshotDirty;
5332 
5333  estate = CreateExecutorState();
5334  econtext = GetPerTupleExprContext(estate);
5335  /* Make sure any cruft is generated in the econtext's memory */
5336  tmpcontext = econtext->ecxt_per_tuple_memory;
5337  oldcontext = MemoryContextSwitchTo(tmpcontext);
5338 
5339  /*
5340  * Open the table and index so we can read from them. We should
5341  * already have at least AccessShareLock on the table, but not
5342  * necessarily on the index.
5343  */
5344  heapRel = heap_open(rte->relid, NoLock);
5345  indexRel = index_open(index->indexoid, AccessShareLock);
5346 
5347  /* extract index key information from the index's pg_index info */
5348  indexInfo = BuildIndexInfo(indexRel);
5349 
5350  /* some other stuff */
5351  slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRel));
5352  econtext->ecxt_scantuple = slot;
5353  get_typlenbyval(vardata->atttype, &typLen, &typByVal);
5354  InitDirtySnapshot(SnapshotDirty);
5355 
5356  /* set up an IS NOT NULL scan key so that we ignore nulls */
5357  ScanKeyEntryInitialize(&scankeys[0],
5359  1, /* index col to scan */
5360  InvalidStrategy, /* no strategy */
5361  InvalidOid, /* no strategy subtype */
5362  InvalidOid, /* no collation */
5363  InvalidOid, /* no reg proc for this */
5364  (Datum) 0); /* constant */
5365 
5366  have_data = true;
5367 
5368  /* If min is requested ... */
5369  if (min)
5370  {
5371  /*
5372  * In principle, we should scan the index with our current
5373  * active snapshot, which is the best approximation we've got
5374  * to what the query will see when executed. But that won't
5375  * be exact if a new snap is taken before running the query,
5376  * and it can be very expensive if a lot of uncommitted rows
5377  * exist at the end of the index (because we'll laboriously
5378  * fetch each one and reject it). What seems like a good
5379  * compromise is to use SnapshotDirty. That will accept
5380  * uncommitted rows, and thus avoid fetching multiple heap
5381  * tuples in this scenario. On the other hand, it will reject
5382  * known-dead rows, and thus not give a bogus answer when the
5383  * extreme value has been deleted; that case motivates not
5384  * using SnapshotAny here.
5385  */
5386  index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
5387  1, 0);
5388  index_rescan(index_scan, scankeys, 1, NULL, 0);
5389 
5390  /* Fetch first tuple in sortop's direction */
5391  if ((tup = index_getnext(index_scan,
5392  indexscandir)) != NULL)
5393  {
5394  /* Extract the index column values from the heap tuple */
5395  ExecStoreTuple(tup, slot, InvalidBuffer, false);
5396  FormIndexDatum(indexInfo, slot, estate,
5397  values, isnull);
5398 
5399  /* Shouldn't have got a null, but be careful */
5400  if (isnull[0])
5401  elog(ERROR, "found unexpected null value in index \"%s\"",
5402  RelationGetRelationName(indexRel));
5403 
5404  /* Copy the index column value out to caller's context */
5405  MemoryContextSwitchTo(oldcontext);
5406  *min = datumCopy(values[0], typByVal, typLen);
5407  MemoryContextSwitchTo(tmpcontext);
5408  }
5409  else
5410  have_data = false;
5411 
5412  index_endscan(index_scan);
5413  }
5414 
5415  /* If max is requested, and we didn't find the index is empty */
5416  if (max && have_data)
5417  {
5418  index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
5419  1, 0);
5420  index_rescan(index_scan, scankeys, 1, NULL, 0);
5421 
5422  /* Fetch first tuple in reverse direction */
5423  if ((tup = index_getnext(index_scan,
5424  -indexscandir)) != NULL)
5425  {
5426  /* Extract the index column values from the heap tuple */
5427  ExecStoreTuple(tup, slot, InvalidBuffer, false);
5428  FormIndexDatum(indexInfo, slot, estate,
5429  values, isnull);
5430 
5431  /* Shouldn't have got a null, but be careful */
5432  if (isnull[0])
5433  elog(ERROR, "found unexpected null value in index \"%s\"",
5434  RelationGetRelationName(indexRel));
5435 
5436  /* Copy the index column value out to caller's context */
5437  MemoryContextSwitchTo(oldcontext);
5438  *max = datumCopy(values[0], typByVal, typLen);
5439  MemoryContextSwitchTo(tmpcontext);
5440  }
5441  else
5442  have_data = false;
5443 
5444  index_endscan(index_scan);
5445  }
5446 
5447  /* Clean everything up */
5449 
5450  index_close(indexRel, AccessShareLock);
5451  heap_close(heapRel, NoLock);
5452 
5453  MemoryContextSwitchTo(oldcontext);
5454  FreeExecutorState(estate);
5455 
5456  /* And we're done */
5457  break;
5458  }
5459  }
5460 
5461  return have_data;
5462 }
5463 
5464 /*
5465  * find_join_input_rel
5466  * Look up the input relation for a join.
5467  *
5468  * We assume that the input relation's RelOptInfo must have been constructed
5469  * already.
5470  */
5471 static RelOptInfo *
5473 {
5474  RelOptInfo *rel = NULL;
5475 
5476  switch (bms_membership(relids))
5477  {
5478  case BMS_EMPTY_SET:
5479  /* should not happen */
5480  break;
5481  case BMS_SINGLETON:
5482  rel = find_base_rel(root, bms_singleton_member(relids));
5483  break;
5484  case BMS_MULTIPLE:
5485  rel = find_join_rel(root, relids);
5486  break;
5487  }
5488 
5489  if (rel == NULL)
5490  elog(ERROR, "could not find RelOptInfo for given relids");
5491 
5492  return rel;
5493 }
5494 
5495 
5496 /*-------------------------------------------------------------------------
5497  *
5498  * Pattern analysis functions
5499  *
5500  * These routines support analysis of LIKE and regular-expression patterns
5501  * by the planner/optimizer. It's important that they agree with the
5502  * regular-expression code in backend/regex/ and the LIKE code in
5503  * backend/utils/adt/like.c. Also, the computation of the fixed prefix
5504  * must be conservative: if we report a string longer than the true fixed
5505  * prefix, the query may produce actually wrong answers, rather than just
5506  * getting a bad selectivity estimate!
5507  *
5508  * Note that the prefix-analysis functions are called from
5509  * backend/optimizer/path/indxpath.c as well as from routines in this file.
5510  *
5511  *-------------------------------------------------------------------------
5512  */
5513 
5514 /*
5515  * Check whether char is a letter (and, hence, subject to case-folding)
5516  *
5517  * In multibyte character sets or with ICU, we can't use isalpha, and it does not seem
5518  * worth trying to convert to wchar_t to use iswalpha. Instead, just assume
5519  * any multibyte char is potentially case-varying.
5520  */
5521 static int
5522 pattern_char_isalpha(char c, bool is_multibyte,
5523  pg_locale_t locale, bool locale_is_c)
5524 {
5525  if (locale_is_c)
5526  return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
5527  else if (is_multibyte && IS_HIGHBIT_SET(c))
5528  return true;
5529  else if (locale && locale->provider == COLLPROVIDER_ICU)
5530  return IS_HIGHBIT_SET(c) ? true : false;
5531 #ifdef HAVE_LOCALE_T
5532  else if (locale && locale->provider == COLLPROVIDER_LIBC)
5533  return isalpha_l((unsigned char) c, locale->info.lt);
5534 #endif
5535  else
5536  return isalpha((unsigned char) c);
5537 }
5538 
5539 /*
5540  * Extract the fixed prefix, if any, for a pattern.
5541  *
5542  * *prefix is set to a palloc'd prefix string (in the form of a Const node),
5543  * or to NULL if no fixed prefix exists for the pattern.
5544  * If rest_selec is not NULL, *rest_selec is set to an estimate of the
5545  * selectivity of the remainder of the pattern (without any fixed prefix).
5546  * The prefix Const has the same type (TEXT or BYTEA) as the input pattern.
5547  *
5548  * The return value distinguishes no fixed prefix, a partial prefix,
5549  * or an exact-match-only pattern.
5550  */
5551 
5552 static Pattern_Prefix_Status
5553 like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
5554  Const **prefix_const, Selectivity *rest_selec)
5555 {
5556  char *match;
5557  char *patt;
5558  int pattlen;
5559  Oid typeid = patt_const->consttype;
5560  int pos,
5561  match_pos;
5562  bool is_multibyte = (pg_database_encoding_max_length() > 1);
5563  pg_locale_t locale = 0;
5564  bool locale_is_c = false;
5565 
5566  /* the right-hand const is type text or bytea */
5567  Assert(typeid == BYTEAOID || typeid == TEXTOID);
5568 
5569  if (case_insensitive)
5570  {
5571  if (typeid == BYTEAOID)
5572  ereport(ERROR,
5573  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5574  errmsg("case insensitive matching not supported on type bytea")));
5575 
5576  /* If case-insensitive, we need locale info */
5577  if (lc_ctype_is_c(collation))
5578  locale_is_c = true;
5579  else if (collation != DEFAULT_COLLATION_OID)
5580  {
5581  if (!OidIsValid(collation))
5582  {
5583  /*
5584  * This typically means that the parser could not resolve a
5585  * conflict of implicit collations, so report it that way.
5586  */
5587  ereport(ERROR,
5588  (errcode(ERRCODE_INDETERMINATE_COLLATION),
5589  errmsg("could not determine which collation to use for ILIKE"),
5590  errhint("Use the COLLATE clause to set the collation explicitly.")));
5591  }
5592  locale = pg_newlocale_from_collation(collation);
5593  }
5594  }
5595 
5596  if (typeid != BYTEAOID)
5597  {
5598  patt = TextDatumGetCString(patt_const->constvalue);
5599  pattlen = strlen(patt);
5600  }
5601  else
5602  {
5603  bytea *bstr = DatumGetByteaPP(patt_const->constvalue);
5604 
5605  pattlen = VARSIZE_ANY_EXHDR(bstr);
5606  patt = (char *) palloc(pattlen);
5607  memcpy(patt, VARDATA_ANY(bstr), pattlen);
5608  Assert((Pointer) bstr == DatumGetPointer(patt_const->constvalue));
5609  }
5610 
5611  match = palloc(pattlen + 1);
5612  match_pos = 0;
5613  for (pos = 0; pos < pattlen; pos++)
5614  {
5615  /* % and _ are wildcard characters in LIKE */
5616  if (patt[pos] == '%' ||
5617  patt[pos] == '_')
5618  break;
5619 
5620  /* Backslash escapes the next character */
5621  if (patt[pos] == '\\')
5622  {
5623  pos++;
5624  if (pos >= pattlen)
5625  break;
5626  }
5627 
5628  /* Stop if case-varying character (it's sort of a wildcard) */
5629  if (case_insensitive &&
5630  pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c))
5631  break;
5632 
5633  match[match_pos++] = patt[pos];
5634  }
5635 
5636  match[match_pos] = '\0';
5637 
5638  if (typeid != BYTEAOID)
5639  *prefix_const = string_to_const(match, typeid);
5640  else
5641  *prefix_const = string_to_bytea_const(match, match_pos);
5642 
5643  if (rest_selec != NULL)
5644  *rest_selec = like_selectivity(&patt[pos], pattlen - pos,
5645  case_insensitive);
5646 
5647  pfree(patt);
5648  pfree(match);
5649 
5650  /* in LIKE, an empty pattern is an exact match! */
5651  if (pos == pattlen)
5652  return Pattern_Prefix_Exact; /* reached end of pattern, so exact */
5653 
5654  if (match_pos > 0)
5655  return Pattern_Prefix_Partial;
5656 
5657  return Pattern_Prefix_None;
5658 }
5659 
5660 static Pattern_Prefix_Status
5661 regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
5662  Const **prefix_const, Selectivity *rest_selec)
5663 {
5664  Oid typeid = patt_const->consttype;
5665  char *prefix;
5666  bool exact;
5667 
5668  /*
5669  * Should be unnecessary, there are no bytea regex operators defined. As
5670  * such, it should be noted that the rest of this function has *not* been
5671  * made safe for binary (possibly NULL containing) strings.
5672  */
5673  if (typeid == BYTEAOID)
5674  ereport(ERROR,
5675  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5676  errmsg("regular-expression matching not supported on type bytea")));
5677 
5678  /* Use the regexp machinery to extract the prefix, if any */
5679  prefix = regexp_fixed_prefix(DatumGetTextPP(patt_const->constvalue),
5680  case_insensitive, collation,
5681  &exact);
5682 
5683  if (prefix == NULL)
5684  {
5685  *prefix_const = NULL;
5686 
5687  if (rest_selec != NULL)
5688  {
5689  char *patt = TextDatumGetCString(patt_const->constvalue);
5690 
5691  *rest_selec = regex_selectivity(patt, strlen(patt),
5692  case_insensitive,
5693  0);
5694  pfree(patt);
5695  }
5696 
5697  return Pattern_Prefix_None;
5698  }
5699 
5700  *prefix_const = string_to_const(prefix, typeid);
5701 
5702  if (rest_selec != NULL)
5703  {
5704  if (exact)
5705  {
5706  /* Exact match, so there's no additional selectivity */
5707  *rest_selec = 1.0;
5708  }
5709  else
5710  {
5711  char *patt = TextDatumGetCString(patt_const->constvalue);
5712 
5713  *rest_selec = regex_selectivity(patt, strlen(patt),
5714  case_insensitive,
5715  strlen(prefix));
5716  pfree(patt);
5717  }
5718  }
5719 
5720  pfree(prefix);
5721 
5722  if (exact)
5723  return Pattern_Prefix_Exact; /* pattern specifies exact match */
5724  else
5725  return Pattern_Prefix_Partial;
5726 }
5727 
5729 pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation,
5730  Const **prefix, Selectivity *rest_selec)
5731 {
5733 
5734  switch (ptype)
5735  {
5736  case Pattern_Type_Like:
5737  result = like_fixed_prefix(patt, false, collation,
5738  prefix, rest_selec);
5739  break;
5740  case Pattern_Type_Like_IC:
5741  result = like_fixed_prefix(patt, true, collation,
5742  prefix, rest_selec);
5743  break;
5744  case Pattern_Type_Regex:
5745  result = regex_fixed_prefix(patt, false, collation,
5746  prefix, rest_selec);
5747  break;
5748  case Pattern_Type_Regex_IC:
5749  result = regex_fixed_prefix(patt, true, collation,
5750  prefix, rest_selec);
5751  break;
5752  default:
5753  elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5754  result = Pattern_Prefix_None; /* keep compiler quiet */
5755  break;
5756  }
5757  return result;
5758 }
5759 
5760 /*
5761  * Estimate the selectivity of a fixed prefix for a pattern match.
5762  *
5763  * A fixed prefix "foo" is estimated as the selectivity of the expression
5764  * "variable >= 'foo' AND variable < 'fop'" (see also indxpath.c).
5765  *
5766  * The selectivity estimate is with respect to the portion of the column
5767  * population represented by the histogram --- the caller must fold this
5768  * together with info about MCVs and NULLs.
5769  *
5770  * We use the >= and < operators from the specified btree opfamily to do the
5771  * estimation. The given variable and Const must be of the associated
5772  * datatype.
5773  *
5774  * XXX Note: we make use of the upper bound to estimate operator selectivity
5775  * even if the locale is such that we cannot rely on the upper-bound string.
5776  * The selectivity only needs to be approximately right anyway, so it seems
5777  * more useful to use the upper-bound code than not.
5778  */
5779 static Selectivity
5781  Oid vartype, Oid opfamily, Const *prefixcon)
5782 {
5783  Selectivity prefixsel;
5784  Oid cmpopr;
5785  FmgrInfo opproc;
5786  Const *greaterstrcon;
5787  Selectivity eq_sel;
5788 
5789  cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5791  if (cmpopr == InvalidOid)
5792  elog(ERROR, "no >= operator for opfamily %u", opfamily);
5793  fmgr_info(get_opcode(cmpopr), &opproc);
5794 
5795  prefixsel = ineq_histogram_selectivity(root, vardata, &opproc, true,
5796  prefixcon->constvalue,
5797  prefixcon->consttype);
5798 
5799  if (prefixsel < 0.0)
5800  {
5801  /* No histogram is present ... return a suitable default estimate */
5802  return DEFAULT_MATCH_SEL;
5803  }
5804 
5805  /*-------
5806  * If we can create a string larger than the prefix, say
5807  * "x < greaterstr".
5808  *-------
5809  */
5810  cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5812  if (cmpopr == InvalidOid)
5813  elog(ERROR, "no < operator for opfamily %u", opfamily);
5814  fmgr_info(get_opcode(cmpopr), &opproc);
5815  greaterstrcon = make_greater_string(prefixcon, &opproc,
5817  if (greaterstrcon)
5818  {
5819  Selectivity topsel;
5820 
5821  topsel = ineq_histogram_selectivity(root, vardata, &opproc, false,
5822  greaterstrcon->constvalue,
5823  greaterstrcon->consttype);
5824 
5825  /* ineq_histogram_selectivity worked before, it shouldn't fail now */
5826  Assert(topsel >= 0.0);
5827 
5828  /*
5829  * Merge the two selectivities in the same way as for a range query
5830  * (see clauselist_selectivity()). Note that we don't need to worry
5831  * about double-exclusion of nulls, since ineq_histogram_selectivity
5832  * doesn't count those anyway.
5833  */
5834  prefixsel = topsel + prefixsel - 1.0;
5835  }
5836 
5837  /*
5838  * If the prefix is long then the two bounding values might be too close
5839  * together for the histogram to distinguish them usefully, resulting in a
5840  * zero estimate (plus or minus roundoff error). To avoid returning a
5841  * ridiculously small estimate, compute the estimated selectivity for
5842  * "variable = 'foo'", and clamp to that. (Obviously, the resultant
5843  * estimate should be at least that.)
5844  *
5845  * We apply this even if we couldn't make a greater string. That case
5846  * suggests that the prefix is near the maximum possible, and thus
5847  * probably off the end of the histogram, and thus we probably got a very
5848  * small estimate from the >= condition; so we still need to clamp.
5849  */
5850  cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5852  if (cmpopr == InvalidOid)
5853  elog(ERROR, "no = operator for opfamily %u", opfamily);
5854  eq_sel = var_eq_const(vardata, cmpopr, prefixcon->constvalue,
5855  false, true, false);
5856 
5857  prefixsel = Max(prefixsel, eq_sel);
5858 
5859  return prefixsel;
5860 }
5861 
5862 
5863 /*
5864  * Estimate the selectivity of a pattern of the specified type.
5865  * Note that any fixed prefix of the pattern will have been removed already,
5866  * so actually we may be looking at just a fragment of the pattern.
5867  *
5868  * For now, we use a very simplistic approach: fixed characters reduce the
5869  * selectivity a good deal, character ranges reduce it a little,
5870  * wildcards (such as % for LIKE or .* for regex) increase it.
5871  */
5872 
5873 #define FIXED_CHAR_SEL 0.20 /* about 1/5 */
5874 #define CHAR_RANGE_SEL 0.25
5875 #define ANY_CHAR_SEL 0.9 /* not 1, since it won't match end-of-string */
5876 #define FULL_WILDCARD_SEL 5.0
5877 #define PARTIAL_WILDCARD_SEL 2.0
5878 
5879 static Selectivity
5880 like_selectivity(const char *patt, int pattlen, bool case_insensitive)
5881 {
5882  Selectivity sel = 1.0;
5883  int pos;
5884 
5885  /* Skip any leading wildcard; it's already factored into initial sel */
5886  for (pos = 0; pos < pattlen; pos++)
5887  {
5888  if (patt[pos] != '%' && patt[pos] != '_')
5889  break;
5890  }
5891 
5892  for (; pos < pattlen; pos++)
5893  {
5894  /* % and _ are wildcard characters in LIKE */
5895  if (patt[pos] == '%')
5896  sel *= FULL_WILDCARD_SEL;
5897  else if (patt[pos] == '_')
5898  sel *= ANY_CHAR_SEL;
5899  else if (patt[pos] == '\\')
5900  {
5901  /* Backslash quotes the next character */
5902  pos++;
5903  if (pos >= pattlen)
5904  break;
5905  sel *= FIXED_CHAR_SEL;
5906  }
5907  else
5908  sel *= FIXED_CHAR_SEL;
5909  }
5910  /* Could get sel > 1 if multiple wildcards */
5911  if (sel > 1.0)
5912  sel = 1.0;
5913  return sel;
5914 }
5915 
5916 static Selectivity
5917 regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive)
5918 {
5919  Selectivity sel = 1.0;
5920  int paren_depth = 0;
5921  int paren_pos = 0; /* dummy init to keep compiler quiet */
5922  int pos;
5923 
5924  for (pos = 0; pos < pattlen; pos++)
5925  {
5926  if (patt[pos] == '(')
5927  {
5928  if (paren_depth == 0)
5929  paren_pos = pos; /* remember start of parenthesized item */
5930  paren_depth++;
5931  }
5932  else if (patt[pos] == ')' && paren_depth > 0)
5933  {
5934  paren_depth--;
5935  if (paren_depth == 0)
5936  sel *= regex_selectivity_sub(patt + (paren_pos + 1),
5937  pos - (paren_pos + 1),
5938  case_insensitive);
5939  }
5940  else if (patt[pos] == '|' && paren_depth == 0)
5941  {
5942  /*
5943  * If unquoted | is present at paren level 0 in pattern, we have
5944  * multiple alternatives; sum their probabilities.
5945  */
5946  sel += regex_selectivity_sub(patt + (pos + 1),
5947  pattlen - (pos + 1),
5948  case_insensitive);
5949  break; /* rest of pattern is now processed */
5950  }
5951  else if (patt[pos] == '[')
5952  {
5953  bool negclass = false;
5954 
5955  if (patt[++pos] == '^')
5956  {
5957  negclass = true;
5958  pos++;
5959  }
5960  if (patt[pos] == ']') /* ']' at start of class is not special */
5961  pos++;
5962  while (pos < pattlen && patt[pos] != ']')
5963  pos++;
5964  if (paren_depth == 0)
5965  sel *= (negclass ? (1.0 - CHAR_RANGE_SEL) : CHAR_RANGE_SEL);
5966  }
5967  else if (patt[pos] == '.')
5968  {
5969  if (paren_depth == 0)
5970  sel *= ANY_CHAR_SEL;
5971  }
5972  else if (patt[pos] == '*' ||
5973  patt[pos] == '?' ||
5974  patt[pos] == '+')
5975  {
5976  /* Ought to be smarter about quantifiers... */
5977  if (paren_depth == 0)
5978  sel *= PARTIAL_WILDCARD_SEL;
5979  }
5980  else if (patt[pos] == '{')
5981  {
5982  while (pos < pattlen && patt[pos] != '}')
5983  pos++;
5984  if (paren_depth == 0)
5985  sel *= PARTIAL_WILDCARD_SEL;
5986  }
5987  else if (patt[pos] == '\\')
5988  {
5989  /* backslash quotes the next character */
5990  pos++;
5991  if (pos >= pattlen)
5992  break;
5993  if (paren_depth == 0)
5994  sel *= FIXED_CHAR_SEL;
5995  }
5996  else
5997  {
5998  if (paren_depth == 0)
5999  sel *= FIXED_CHAR_SEL;
6000  }
6001  }
6002  /* Could get sel > 1 if multiple wildcards */
6003  if (sel > 1.0)
6004  sel = 1.0;
6005  return sel;
6006 }
6007 
6008 static Selectivity
6009 regex_selectivity(const char *patt, int pattlen, bool case_insensitive,
6010  int fixed_prefix_len)
6011 {
6012  Selectivity sel;
6013 
6014  /* If patt doesn't end with $, consider it to have a trailing wildcard */
6015  if (pattlen > 0 && patt[pattlen - 1] == '$' &&
6016  (pattlen == 1 || patt[pattlen - 2] != '\\'))
6017  {
6018  /* has trailing $ */
6019  sel = regex_selectivity_sub(patt, pattlen - 1, case_insensitive);
6020  }
6021  else
6022  {
6023  /* no trailing $ */
6024  sel = regex_selectivity_sub(patt, pattlen, case_insensitive);
6025  sel *= FULL_WILDCARD_SEL;
6026  }
6027 
6028  /* If there's a fixed prefix, discount its selectivity */
6029  if (fixed_prefix_len > 0)
6030  sel /= pow(FIXED_CHAR_SEL, fixed_prefix_len);
6031 
6032  /* Make sure result stays in range */
6033  CLAMP_PROBABILITY(sel);
6034  return sel;
6035 }
6036 
6037 
6038 /*
6039  * For bytea, the increment function need only increment the current byte
6040  * (there are no multibyte characters to worry about).
6041  */
6042 static bool
6043 byte_increment(unsigned char *ptr, int len)
6044 {
6045  if (*ptr >= 255)
6046  return false;
6047  (*ptr)++;
6048  return true;
6049 }
6050 
6051 /*
6052  * Try to generate a string greater than the given string or any
6053  * string it is a prefix of. If successful, return a palloc'd string
6054  * in the form of a Const node; else return NULL.
6055  *
6056  * The caller must provide the appropriate "less than" comparison function
6057  * for testing the strings, along with the collation to use.
6058  *
6059  * The key requirement here is that given a prefix string, say "foo",
6060  * we must be able to generate another string "fop" that is greater than
6061  * all strings "foobar" starting with "foo". We can test that we have
6062  * generated a string greater than the prefix string, but in non-C collations
6063  * that is not a bulletproof guarantee that an extension of the string might
6064  * not sort after it; an example is that "foo " is less than "foo!", but it
6065  * is not clear that a "dictionary" sort ordering will consider "foo!" less
6066  * than "foo bar". CAUTION: Therefore, this function should be used only for
6067  * estimation purposes when working in a non-C collation.
6068  *
6069  * To try to catch most cases where an extended string might otherwise sort
6070