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