1 /*-------------------------------------------------------------------------
4 * Selectivity functions and index cost estimation functions for
5 * standard operators and index access methods.
7 * Selectivity routines are registered in the pg_operator catalog
8 * in the "oprrest" and "oprjoin" attributes.
10 * Index cost functions are registered in the pg_am catalog
11 * in the "amcostestimate" attribute.
13 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
18 * src/backend/utils/adt/selfuncs.c
20 *-------------------------------------------------------------------------
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
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.
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.
39 * The call convention for a restriction estimator (oprrest function) is
41 * Selectivity oprrest (PlannerInfo *root,
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.
54 * This is represented at the SQL level (in pg_proc) as
56 * float8 oprrest (internal, oid, internal, int4);
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
62 * The call convention for a join estimator (oprjoin function) is similar
63 * except that varRelid is not needed, and instead join information is
66 * Selectivity oprjoin (PlannerInfo *root,
70 * SpecialJoinInfo *sjinfo);
72 * float8 oprjoin (internal, oid, internal, int2, internal);
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.
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.
94 #include "access/gin.h"
95 #include "access/sysattr.h"
96 #include "catalog/index.h"
97 #include "catalog/pg_collation.h"
98 #include "catalog/pg_opfamily.h"
99 #include "catalog/pg_statistic.h"
100 #include "catalog/pg_type.h"
101 #include "executor/executor.h"
102 #include "mb/pg_wchar.h"
103 #include "nodes/makefuncs.h"
104 #include "nodes/nodeFuncs.h"
105 #include "optimizer/clauses.h"
106 #include "optimizer/cost.h"
107 #include "optimizer/pathnode.h"
108 #include "optimizer/paths.h"
109 #include "optimizer/plancat.h"
110 #include "optimizer/predtest.h"
111 #include "optimizer/restrictinfo.h"
112 #include "optimizer/var.h"
113 #include "parser/parse_clause.h"
114 #include "parser/parse_coerce.h"
115 #include "parser/parsetree.h"
116 #include "utils/builtins.h"
117 #include "utils/bytea.h"
118 #include "utils/date.h"
119 #include "utils/datum.h"
120 #include "utils/fmgroids.h"
121 #include "utils/lsyscache.h"
122 #include "utils/nabstime.h"
123 #include "utils/pg_locale.h"
124 #include "utils/rel.h"
125 #include "utils/selfuncs.h"
126 #include "utils/spccache.h"
127 #include "utils/syscache.h"
128 #include "utils/timestamp.h"
129 #include "utils/tqual.h"
130 #include "utils/typcache.h"
133 /* Hooks for plugins to get control when we ask for stats */
134 get_relation_stats_hook_type get_relation_stats_hook = NULL;
135 get_index_stats_hook_type get_index_stats_hook = NULL;
137 static double var_eq_const(VariableStatData *vardata, Oid operator,
138 Datum constval, bool constisnull,
140 static double var_eq_non_const(VariableStatData *vardata, Oid operator,
143 static double ineq_histogram_selectivity(PlannerInfo *root,
144 VariableStatData *vardata,
145 FmgrInfo *opproc, bool isgt,
146 Datum constval, Oid consttype);
147 static double eqjoinsel_inner(Oid operator,
148 VariableStatData *vardata1, VariableStatData *vardata2);
149 static double eqjoinsel_semi(Oid operator,
150 VariableStatData *vardata1, VariableStatData *vardata2,
151 RelOptInfo *inner_rel);
152 static bool convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
153 Datum lobound, Datum hibound, Oid boundstypid,
154 double *scaledlobound, double *scaledhibound);
155 static double convert_numeric_to_scalar(Datum value, Oid typid);
156 static void convert_string_to_scalar(char *value,
159 double *scaledlobound,
161 double *scaledhibound);
162 static void convert_bytea_to_scalar(Datum value,
165 double *scaledlobound,
167 double *scaledhibound);
168 static double convert_one_string_to_scalar(char *value,
169 int rangelo, int rangehi);
170 static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
171 int rangelo, int rangehi);
172 static char *convert_string_datum(Datum value, Oid typid);
173 static double convert_timevalue_to_scalar(Datum value, Oid typid);
174 static void examine_simple_variable(PlannerInfo *root, Var *var,
175 VariableStatData *vardata);
176 static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata,
177 Oid sortop, Datum *min, Datum *max);
178 static bool get_actual_variable_range(PlannerInfo *root,
179 VariableStatData *vardata,
181 Datum *min, Datum *max);
182 static RelOptInfo *find_join_input_rel(PlannerInfo *root, Relids relids);
183 static Selectivity prefix_selectivity(PlannerInfo *root,
184 VariableStatData *vardata,
185 Oid vartype, Oid opfamily, Const *prefixcon);
186 static Selectivity pattern_selectivity(Const *patt, Pattern_Type ptype);
187 static Datum string_to_datum(const char *str, Oid datatype);
188 static Const *string_to_const(const char *str, Oid datatype);
189 static Const *string_to_bytea_const(const char *str, size_t str_len);
193 * eqsel - Selectivity of "=" for any data types.
195 * Note: this routine is also used to estimate selectivity for some
196 * operators that are not "=" but have comparable selectivity behavior,
197 * such as "~=" (geometric approximate-match). Even for "=", we must
198 * keep in mind that the left and right datatypes may differ.
201 eqsel(PG_FUNCTION_ARGS)
203 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
204 Oid operator = PG_GETARG_OID(1);
205 List *args = (List *) PG_GETARG_POINTER(2);
206 int varRelid = PG_GETARG_INT32(3);
207 VariableStatData vardata;
213 * If expression is not variable = something or something = variable, then
214 * punt and return a default estimate.
216 if (!get_restriction_variable(root, args, varRelid,
217 &vardata, &other, &varonleft))
218 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
221 * We can do a lot better if the something is a constant. (Note: the
222 * Const might result from estimation rather than being a simple constant
225 if (IsA(other, Const))
226 selec = var_eq_const(&vardata, operator,
227 ((Const *) other)->constvalue,
228 ((Const *) other)->constisnull,
231 selec = var_eq_non_const(&vardata, operator, other,
234 ReleaseVariableStats(vardata);
236 PG_RETURN_FLOAT8((float8) selec);
240 * var_eq_const --- eqsel for var = const case
242 * This is split out so that some other estimation functions can use it.
245 var_eq_const(VariableStatData *vardata, Oid operator,
246 Datum constval, bool constisnull,
253 * If the constant is NULL, assume operator is strict and return zero, ie,
254 * operator will never return TRUE.
260 * If we matched the var to a unique index or DISTINCT clause, assume
261 * there is exactly one match regardless of anything else. (This is
262 * slightly bogus, since the index or clause's equality operator might be
263 * different from ours, but it's much more likely to be right than
264 * ignoring the information.)
266 if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
267 return 1.0 / vardata->rel->tuples;
269 if (HeapTupleIsValid(vardata->statsTuple))
271 Form_pg_statistic stats;
279 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
282 * Is the constant "=" to any of the column's most common values?
283 * (Although the given operator may not really be "=", we will assume
284 * that seeing whether it returns TRUE is an appropriate test. If you
285 * don't like this, maybe you shouldn't be using eqsel for your
288 if (get_attstatsslot(vardata->statsTuple,
289 vardata->atttype, vardata->atttypmod,
290 STATISTIC_KIND_MCV, InvalidOid,
293 &numbers, &nnumbers))
297 fmgr_info(get_opcode(operator), &eqproc);
299 for (i = 0; i < nvalues; i++)
301 /* be careful to apply operator right way 'round */
303 match = DatumGetBool(FunctionCall2Coll(&eqproc,
304 DEFAULT_COLLATION_OID,
308 match = DatumGetBool(FunctionCall2Coll(&eqproc,
309 DEFAULT_COLLATION_OID,
318 /* no most-common-value info available */
321 i = nvalues = nnumbers = 0;
327 * Constant is "=" to this common value. We know selectivity
328 * exactly (or as exactly as ANALYZE could calculate it, anyway).
335 * Comparison is against a constant that is neither NULL nor any
336 * of the common values. Its selectivity cannot be more than
339 double sumcommon = 0.0;
340 double otherdistinct;
342 for (i = 0; i < nnumbers; i++)
343 sumcommon += numbers[i];
344 selec = 1.0 - sumcommon - stats->stanullfrac;
345 CLAMP_PROBABILITY(selec);
348 * and in fact it's probably a good deal less. We approximate that
349 * all the not-common values share this remaining fraction
350 * equally, so we divide by the number of other distinct values.
352 otherdistinct = get_variable_numdistinct(vardata, &isdefault) - nnumbers;
353 if (otherdistinct > 1)
354 selec /= otherdistinct;
357 * Another cross-check: selectivity shouldn't be estimated as more
358 * than the least common "most common value".
360 if (nnumbers > 0 && selec > numbers[nnumbers - 1])
361 selec = numbers[nnumbers - 1];
364 free_attstatsslot(vardata->atttype, values, nvalues,
370 * No ANALYZE stats available, so make a guess using estimated number
371 * of distinct values and assuming they are equally common. (The guess
372 * is unlikely to be very good, but we do know a few special cases.)
374 selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
377 /* result should be in range, but make sure... */
378 CLAMP_PROBABILITY(selec);
384 * var_eq_non_const --- eqsel for var = something-other-than-const case
387 var_eq_non_const(VariableStatData *vardata, Oid operator,
395 * If we matched the var to a unique index or DISTINCT clause, assume
396 * there is exactly one match regardless of anything else. (This is
397 * slightly bogus, since the index or clause's equality operator might be
398 * different from ours, but it's much more likely to be right than
399 * ignoring the information.)
401 if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
402 return 1.0 / vardata->rel->tuples;
404 if (HeapTupleIsValid(vardata->statsTuple))
406 Form_pg_statistic stats;
411 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
414 * Search is for a value that we do not know a priori, but we will
415 * assume it is not NULL. Estimate the selectivity as non-null
416 * fraction divided by number of distinct values, so that we get a
417 * result averaged over all possible values whether common or
418 * uncommon. (Essentially, we are assuming that the not-yet-known
419 * comparison value is equally likely to be any of the possible
420 * values, regardless of their frequency in the table. Is that a good
423 selec = 1.0 - stats->stanullfrac;
424 ndistinct = get_variable_numdistinct(vardata, &isdefault);
429 * Cross-check: selectivity should never be estimated as more than the
430 * most common value's.
432 if (get_attstatsslot(vardata->statsTuple,
433 vardata->atttype, vardata->atttypmod,
434 STATISTIC_KIND_MCV, InvalidOid,
437 &numbers, &nnumbers))
439 if (nnumbers > 0 && selec > numbers[0])
441 free_attstatsslot(vardata->atttype, NULL, 0, numbers, nnumbers);
447 * No ANALYZE stats available, so make a guess using estimated number
448 * of distinct values and assuming they are equally common. (The guess
449 * is unlikely to be very good, but we do know a few special cases.)
451 selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
454 /* result should be in range, but make sure... */
455 CLAMP_PROBABILITY(selec);
461 * neqsel - Selectivity of "!=" for any data types.
463 * This routine is also used for some operators that are not "!="
464 * but have comparable selectivity behavior. See above comments
468 neqsel(PG_FUNCTION_ARGS)
470 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
471 Oid operator = PG_GETARG_OID(1);
472 List *args = (List *) PG_GETARG_POINTER(2);
473 int varRelid = PG_GETARG_INT32(3);
478 * We want 1 - eqsel() where the equality operator is the one associated
479 * with this != operator, that is, its negator.
481 eqop = get_negator(operator);
484 result = DatumGetFloat8(DirectFunctionCall4(eqsel,
485 PointerGetDatum(root),
486 ObjectIdGetDatum(eqop),
487 PointerGetDatum(args),
488 Int32GetDatum(varRelid)));
492 /* Use default selectivity (should we raise an error instead?) */
493 result = DEFAULT_EQ_SEL;
495 result = 1.0 - result;
496 PG_RETURN_FLOAT8(result);
500 * scalarineqsel - Selectivity of "<", "<=", ">", ">=" for scalars.
502 * This is the guts of both scalarltsel and scalargtsel. The caller has
503 * commuted the clause, if necessary, so that we can treat the variable as
504 * being on the left. The caller must also make sure that the other side
505 * of the clause is a non-null Const, and dissect same into a value and
508 * This routine works for any datatype (or pair of datatypes) known to
509 * convert_to_scalar(). If it is applied to some other datatype,
510 * it will return a default estimate.
513 scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
514 VariableStatData *vardata, Datum constval, Oid consttype)
516 Form_pg_statistic stats;
523 if (!HeapTupleIsValid(vardata->statsTuple))
525 /* no stats available, so default result */
526 return DEFAULT_INEQ_SEL;
528 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
530 fmgr_info(get_opcode(operator), &opproc);
533 * If we have most-common-values info, add up the fractions of the MCV
534 * entries that satisfy MCV OP CONST. These fractions contribute directly
535 * to the result selectivity. Also add up the total fraction represented
538 mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
542 * If there is a histogram, determine which bin the constant falls in, and
543 * compute the resulting contribution to selectivity.
545 hist_selec = ineq_histogram_selectivity(root, vardata, &opproc, isgt,
546 constval, consttype);
549 * Now merge the results from the MCV and histogram calculations,
550 * realizing that the histogram covers only the non-null values that are
553 selec = 1.0 - stats->stanullfrac - sumcommon;
555 if (hist_selec >= 0.0)
560 * If no histogram but there are values not accounted for by MCV,
561 * arbitrarily assume half of them will match.
568 /* result should be in range, but make sure... */
569 CLAMP_PROBABILITY(selec);
575 * mcv_selectivity - Examine the MCV list for selectivity estimates
577 * Determine the fraction of the variable's MCV population that satisfies
578 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
579 * compute the fraction of the total column population represented by the MCV
580 * list. This code will work for any boolean-returning predicate operator.
582 * The function result is the MCV selectivity, and the fraction of the
583 * total population is returned into *sumcommonp. Zeroes are returned
584 * if there is no MCV list.
587 mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
588 Datum constval, bool varonleft,
602 if (HeapTupleIsValid(vardata->statsTuple) &&
603 get_attstatsslot(vardata->statsTuple,
604 vardata->atttype, vardata->atttypmod,
605 STATISTIC_KIND_MCV, InvalidOid,
608 &numbers, &nnumbers))
610 for (i = 0; i < nvalues; i++)
613 DatumGetBool(FunctionCall2Coll(opproc,
614 DEFAULT_COLLATION_OID,
617 DatumGetBool(FunctionCall2Coll(opproc,
618 DEFAULT_COLLATION_OID,
621 mcv_selec += numbers[i];
622 sumcommon += numbers[i];
624 free_attstatsslot(vardata->atttype, values, nvalues,
628 *sumcommonp = sumcommon;
633 * histogram_selectivity - Examine the histogram for selectivity estimates
635 * Determine the fraction of the variable's histogram entries that satisfy
636 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
638 * This code will work for any boolean-returning predicate operator, whether
639 * or not it has anything to do with the histogram sort operator. We are
640 * essentially using the histogram just as a representative sample. However,
641 * small histograms are unlikely to be all that representative, so the caller
642 * should be prepared to fall back on some other estimation approach when the
643 * histogram is missing or very small. It may also be prudent to combine this
644 * approach with another one when the histogram is small.
646 * If the actual histogram size is not at least min_hist_size, we won't bother
647 * to do the calculation at all. Also, if the n_skip parameter is > 0, we
648 * ignore the first and last n_skip histogram elements, on the grounds that
649 * they are outliers and hence not very representative. Typical values for
650 * these parameters are 10 and 1.
652 * The function result is the selectivity, or -1 if there is no histogram
653 * or it's smaller than min_hist_size.
655 * The output parameter *hist_size receives the actual histogram size,
656 * or zero if no histogram. Callers may use this number to decide how
657 * much faith to put in the function result.
659 * Note that the result disregards both the most-common-values (if any) and
660 * null entries. The caller is expected to combine this result with
661 * statistics for those portions of the column population. It may also be
662 * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
665 histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
666 Datum constval, bool varonleft,
667 int min_hist_size, int n_skip,
674 /* check sanity of parameters */
676 Assert(min_hist_size > 2 * n_skip);
678 if (HeapTupleIsValid(vardata->statsTuple) &&
679 get_attstatsslot(vardata->statsTuple,
680 vardata->atttype, vardata->atttypmod,
681 STATISTIC_KIND_HISTOGRAM, InvalidOid,
686 *hist_size = nvalues;
687 if (nvalues >= min_hist_size)
692 for (i = n_skip; i < nvalues - n_skip; i++)
695 DatumGetBool(FunctionCall2Coll(opproc,
696 DEFAULT_COLLATION_OID,
699 DatumGetBool(FunctionCall2Coll(opproc,
700 DEFAULT_COLLATION_OID,
705 result = ((double) nmatch) / ((double) (nvalues - 2 * n_skip));
709 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
721 * ineq_histogram_selectivity - Examine the histogram for scalarineqsel
723 * Determine the fraction of the variable's histogram population that
724 * satisfies the inequality condition, ie, VAR < CONST or VAR > CONST.
726 * Returns -1 if there is no histogram (valid results will always be >= 0).
728 * Note that the result disregards both the most-common-values (if any) and
729 * null entries. The caller is expected to combine this result with
730 * statistics for those portions of the column population.
733 ineq_histogram_selectivity(PlannerInfo *root,
734 VariableStatData *vardata,
735 FmgrInfo *opproc, bool isgt,
736 Datum constval, Oid consttype)
746 * Someday, ANALYZE might store more than one histogram per rel/att,
747 * corresponding to more than one possible sort ordering defined for the
748 * column type. However, to make that work we will need to figure out
749 * which staop to search for --- it's not necessarily the one we have at
750 * hand! (For example, we might have a '<=' operator rather than the '<'
751 * operator that will appear in staop.) For now, assume that whatever
752 * appears in pg_statistic is sorted the same way our operator sorts, or
753 * the reverse way if isgt is TRUE.
755 if (HeapTupleIsValid(vardata->statsTuple) &&
756 get_attstatsslot(vardata->statsTuple,
757 vardata->atttype, vardata->atttypmod,
758 STATISTIC_KIND_HISTOGRAM, InvalidOid,
766 * Use binary search to find proper location, ie, the first slot
767 * at which the comparison fails. (If the given operator isn't
768 * actually sort-compatible with the histogram, you'll get garbage
769 * results ... but probably not any more garbage-y than you would
770 * from the old linear search.)
772 * If the binary search accesses the first or last histogram
773 * entry, we try to replace that endpoint with the true column min
774 * or max as found by get_actual_variable_range(). This
775 * ameliorates misestimates when the min or max is moving as a
776 * result of changes since the last ANALYZE. Note that this could
777 * result in effectively including MCVs into the histogram that
778 * weren't there before, but we don't try to correct for that.
781 int lobound = 0; /* first possible slot to search */
782 int hibound = nvalues; /* last+1 slot to search */
783 bool have_end = false;
786 * If there are only two histogram entries, we'll want up-to-date
787 * values for both. (If there are more than two, we need at most
788 * one of them to be updated, so we deal with that within the
792 have_end = get_actual_variable_range(root,
798 while (lobound < hibound)
800 int probe = (lobound + hibound) / 2;
804 * If we find ourselves about to compare to the first or last
805 * histogram entry, first try to replace it with the actual
806 * current min or max (unless we already did so above).
808 if (probe == 0 && nvalues > 2)
809 have_end = get_actual_variable_range(root,
814 else if (probe == nvalues - 1 && nvalues > 2)
815 have_end = get_actual_variable_range(root,
821 ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
822 DEFAULT_COLLATION_OID,
835 /* Constant is below lower histogram boundary. */
838 else if (lobound >= nvalues)
840 /* Constant is above upper histogram boundary. */
852 * We have values[i-1] <= constant <= values[i].
854 * Convert the constant and the two nearest bin boundary
855 * values to a uniform comparison scale, and do a linear
856 * interpolation within this bin.
858 if (convert_to_scalar(constval, consttype, &val,
859 values[i - 1], values[i],
865 /* cope if bin boundaries appear identical */
870 else if (val >= high)
874 binfrac = (val - low) / (high - low);
877 * Watch out for the possibility that we got a NaN or
878 * Infinity from the division. This can happen
879 * despite the previous checks, if for example "low"
882 if (isnan(binfrac) ||
883 binfrac < 0.0 || binfrac > 1.0)
890 * Ideally we'd produce an error here, on the grounds that
891 * the given operator shouldn't have scalarXXsel
892 * registered as its selectivity func unless we can deal
893 * with its operand types. But currently, all manner of
894 * stuff is invoking scalarXXsel, so give a default
895 * estimate until that can be fixed.
901 * Now, compute the overall selectivity across the values
902 * represented by the histogram. We have i-1 full bins and
903 * binfrac partial bin below the constant.
905 histfrac = (double) (i - 1) + binfrac;
906 histfrac /= (double) (nvalues - 1);
910 * Now histfrac = fraction of histogram entries below the
913 * Account for "<" vs ">"
915 hist_selec = isgt ? (1.0 - histfrac) : histfrac;
918 * The histogram boundaries are only approximate to begin with,
919 * and may well be out of date anyway. Therefore, don't believe
920 * extremely small or large selectivity estimates --- unless we
921 * got actual current endpoint values from the table.
924 CLAMP_PROBABILITY(hist_selec);
927 if (hist_selec < 0.0001)
929 else if (hist_selec > 0.9999)
934 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
941 * scalarltsel - Selectivity of "<" (also "<=") for scalars.
944 scalarltsel(PG_FUNCTION_ARGS)
946 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
947 Oid operator = PG_GETARG_OID(1);
948 List *args = (List *) PG_GETARG_POINTER(2);
949 int varRelid = PG_GETARG_INT32(3);
950 VariableStatData vardata;
959 * If expression is not variable op something or something op variable,
960 * then punt and return a default estimate.
962 if (!get_restriction_variable(root, args, varRelid,
963 &vardata, &other, &varonleft))
964 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
967 * Can't do anything useful if the something is not a constant, either.
969 if (!IsA(other, Const))
971 ReleaseVariableStats(vardata);
972 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
976 * If the constant is NULL, assume operator is strict and return zero, ie,
977 * operator will never return TRUE.
979 if (((Const *) other)->constisnull)
981 ReleaseVariableStats(vardata);
982 PG_RETURN_FLOAT8(0.0);
984 constval = ((Const *) other)->constvalue;
985 consttype = ((Const *) other)->consttype;
988 * Force the var to be on the left to simplify logic in scalarineqsel.
992 /* we have var < other */
997 /* we have other < var, commute to make var > other */
998 operator = get_commutator(operator);
1001 /* Use default selectivity (should we raise an error instead?) */
1002 ReleaseVariableStats(vardata);
1003 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1008 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
1010 ReleaseVariableStats(vardata);
1012 PG_RETURN_FLOAT8((float8) selec);
1016 * scalargtsel - Selectivity of ">" (also ">=") for integers.
1019 scalargtsel(PG_FUNCTION_ARGS)
1021 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1022 Oid operator = PG_GETARG_OID(1);
1023 List *args = (List *) PG_GETARG_POINTER(2);
1024 int varRelid = PG_GETARG_INT32(3);
1025 VariableStatData vardata;
1034 * If expression is not variable op something or something op variable,
1035 * then punt and return a default estimate.
1037 if (!get_restriction_variable(root, args, varRelid,
1038 &vardata, &other, &varonleft))
1039 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1042 * Can't do anything useful if the something is not a constant, either.
1044 if (!IsA(other, Const))
1046 ReleaseVariableStats(vardata);
1047 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1051 * If the constant is NULL, assume operator is strict and return zero, ie,
1052 * operator will never return TRUE.
1054 if (((Const *) other)->constisnull)
1056 ReleaseVariableStats(vardata);
1057 PG_RETURN_FLOAT8(0.0);
1059 constval = ((Const *) other)->constvalue;
1060 consttype = ((Const *) other)->consttype;
1063 * Force the var to be on the left to simplify logic in scalarineqsel.
1067 /* we have var > other */
1072 /* we have other > var, commute to make var < other */
1073 operator = get_commutator(operator);
1076 /* Use default selectivity (should we raise an error instead?) */
1077 ReleaseVariableStats(vardata);
1078 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1083 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
1085 ReleaseVariableStats(vardata);
1087 PG_RETURN_FLOAT8((float8) selec);
1091 * patternsel - Generic code for pattern-match selectivity.
1094 patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
1096 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1097 Oid operator = PG_GETARG_OID(1);
1098 List *args = (List *) PG_GETARG_POINTER(2);
1099 int varRelid = PG_GETARG_INT32(3);
1100 VariableStatData vardata;
1107 Pattern_Prefix_Status pstatus;
1109 Const *prefix = NULL;
1114 * If this is for a NOT LIKE or similar operator, get the corresponding
1115 * positive-match operator and work with that. Set result to the correct
1116 * default estimate, too.
1120 operator = get_negator(operator);
1121 if (!OidIsValid(operator))
1122 elog(ERROR, "patternsel called for operator without a negator");
1123 result = 1.0 - DEFAULT_MATCH_SEL;
1127 result = DEFAULT_MATCH_SEL;
1131 * If expression is not variable op constant, then punt and return a
1134 if (!get_restriction_variable(root, args, varRelid,
1135 &vardata, &other, &varonleft))
1137 if (!varonleft || !IsA(other, Const))
1139 ReleaseVariableStats(vardata);
1144 * If the constant is NULL, assume operator is strict and return zero, ie,
1145 * operator will never return TRUE. (It's zero even for a negator op.)
1147 if (((Const *) other)->constisnull)
1149 ReleaseVariableStats(vardata);
1152 constval = ((Const *) other)->constvalue;
1153 consttype = ((Const *) other)->consttype;
1156 * The right-hand const is type text or bytea for all supported operators.
1157 * We do not expect to see binary-compatible types here, since
1158 * const-folding should have relabeled the const to exactly match the
1159 * operator's declared type.
1161 if (consttype != TEXTOID && consttype != BYTEAOID)
1163 ReleaseVariableStats(vardata);
1168 * Similarly, the exposed type of the left-hand side should be one of
1169 * those we know. (Do not look at vardata.atttype, which might be
1170 * something binary-compatible but different.) We can use it to choose
1171 * the index opfamily from which we must draw the comparison operators.
1173 * NOTE: It would be more correct to use the PATTERN opfamilies than the
1174 * simple ones, but at the moment ANALYZE will not generate statistics for
1175 * the PATTERN operators. But our results are so approximate anyway that
1176 * it probably hardly matters.
1178 vartype = vardata.vartype;
1183 opfamily = TEXT_BTREE_FAM_OID;
1186 opfamily = BPCHAR_BTREE_FAM_OID;
1189 opfamily = NAME_BTREE_FAM_OID;
1192 opfamily = BYTEA_BTREE_FAM_OID;
1195 ReleaseVariableStats(vardata);
1200 * Divide pattern into fixed prefix and remainder. XXX we have to assume
1201 * default collation here, because we don't have access to the actual
1202 * input collation for the operator. FIXME ...
1204 patt = (Const *) other;
1205 pstatus = pattern_fixed_prefix(patt, ptype, DEFAULT_COLLATION_OID,
1209 * If necessary, coerce the prefix constant to the right type. (The "rest"
1210 * constant need not be changed.)
1212 if (prefix && prefix->consttype != vartype)
1216 switch (prefix->consttype)
1219 prefixstr = TextDatumGetCString(prefix->constvalue);
1222 prefixstr = DatumGetCString(DirectFunctionCall1(byteaout,
1223 prefix->constvalue));
1226 elog(ERROR, "unrecognized consttype: %u",
1228 ReleaseVariableStats(vardata);
1231 prefix = string_to_const(prefixstr, vartype);
1235 if (pstatus == Pattern_Prefix_Exact)
1238 * Pattern specifies an exact match, so pretend operator is '='
1240 Oid eqopr = get_opfamily_member(opfamily, vartype, vartype,
1241 BTEqualStrategyNumber);
1243 if (eqopr == InvalidOid)
1244 elog(ERROR, "no = operator for opfamily %u", opfamily);
1245 result = var_eq_const(&vardata, eqopr, prefix->constvalue,
1251 * Not exact-match pattern. If we have a sufficiently large
1252 * histogram, estimate selectivity for the histogram part of the
1253 * population by counting matches in the histogram. If not, estimate
1254 * selectivity of the fixed prefix and remainder of pattern
1255 * separately, then combine the two to get an estimate of the
1256 * selectivity for the part of the column population represented by
1257 * the histogram. (For small histograms, we combine these
1260 * We then add up data for any most-common-values values; these are
1261 * not in the histogram population, and we can get exact answers for
1262 * them by applying the pattern operator, so there's no reason to
1263 * approximate. (If the MCVs cover a significant part of the total
1264 * population, this gives us a big leg up in accuracy.)
1273 /* Try to use the histogram entries to get selectivity */
1274 fmgr_info(get_opcode(operator), &opproc);
1276 selec = histogram_selectivity(&vardata, &opproc, constval, true,
1279 /* If not at least 100 entries, use the heuristic method */
1280 if (hist_size < 100)
1282 Selectivity heursel;
1283 Selectivity prefixsel;
1284 Selectivity restsel;
1286 if (pstatus == Pattern_Prefix_Partial)
1287 prefixsel = prefix_selectivity(root, &vardata, vartype,
1291 restsel = pattern_selectivity(rest, ptype);
1292 heursel = prefixsel * restsel;
1294 if (selec < 0) /* fewer than 10 histogram entries? */
1299 * For histogram sizes from 10 to 100, we combine the
1300 * histogram and heuristic selectivities, putting increasingly
1301 * more trust in the histogram for larger sizes.
1303 double hist_weight = hist_size / 100.0;
1305 selec = selec * hist_weight + heursel * (1.0 - hist_weight);
1309 /* In any case, don't believe extremely small or large estimates. */
1312 else if (selec > 0.9999)
1316 * If we have most-common-values info, add up the fractions of the MCV
1317 * entries that satisfy MCV OP PATTERN. These fractions contribute
1318 * directly to the result selectivity. Also add up the total fraction
1319 * represented by MCV entries.
1321 mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
1324 if (HeapTupleIsValid(vardata.statsTuple))
1325 nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
1330 * Now merge the results from the MCV and histogram calculations,
1331 * realizing that the histogram covers only the non-null values that
1332 * are not listed in MCV.
1334 selec *= 1.0 - nullfrac - sumcommon;
1337 /* result should be in range, but make sure... */
1338 CLAMP_PROBABILITY(selec);
1344 pfree(DatumGetPointer(prefix->constvalue));
1348 ReleaseVariableStats(vardata);
1350 return negate ? (1.0 - result) : result;
1354 * regexeqsel - Selectivity of regular-expression pattern match.
1357 regexeqsel(PG_FUNCTION_ARGS)
1359 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex, false));
1363 * icregexeqsel - Selectivity of case-insensitive regex match.
1366 icregexeqsel(PG_FUNCTION_ARGS)
1368 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC, false));
1372 * likesel - Selectivity of LIKE pattern match.
1375 likesel(PG_FUNCTION_ARGS)
1377 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like, false));
1381 * iclikesel - Selectivity of ILIKE pattern match.
1384 iclikesel(PG_FUNCTION_ARGS)
1386 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC, false));
1390 * regexnesel - Selectivity of regular-expression pattern non-match.
1393 regexnesel(PG_FUNCTION_ARGS)
1395 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex, true));
1399 * icregexnesel - Selectivity of case-insensitive regex non-match.
1402 icregexnesel(PG_FUNCTION_ARGS)
1404 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC, true));
1408 * nlikesel - Selectivity of LIKE pattern non-match.
1411 nlikesel(PG_FUNCTION_ARGS)
1413 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like, true));
1417 * icnlikesel - Selectivity of ILIKE pattern non-match.
1420 icnlikesel(PG_FUNCTION_ARGS)
1422 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC, true));
1426 * booltestsel - Selectivity of BooleanTest Node.
1429 booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
1430 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1432 VariableStatData vardata;
1435 examine_variable(root, arg, varRelid, &vardata);
1437 if (HeapTupleIsValid(vardata.statsTuple))
1439 Form_pg_statistic stats;
1446 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1447 freq_null = stats->stanullfrac;
1449 if (get_attstatsslot(vardata.statsTuple,
1450 vardata.atttype, vardata.atttypmod,
1451 STATISTIC_KIND_MCV, InvalidOid,
1454 &numbers, &nnumbers)
1461 * Get first MCV frequency and derive frequency for true.
1463 if (DatumGetBool(values[0]))
1464 freq_true = numbers[0];
1466 freq_true = 1.0 - numbers[0] - freq_null;
1469 * Next derive frequency for false. Then use these as appropriate
1470 * to derive frequency for each case.
1472 freq_false = 1.0 - freq_true - freq_null;
1474 switch (booltesttype)
1477 /* select only NULL values */
1480 case IS_NOT_UNKNOWN:
1481 /* select non-NULL values */
1482 selec = 1.0 - freq_null;
1485 /* select only TRUE values */
1489 /* select non-TRUE values */
1490 selec = 1.0 - freq_true;
1493 /* select only FALSE values */
1497 /* select non-FALSE values */
1498 selec = 1.0 - freq_false;
1501 elog(ERROR, "unrecognized booltesttype: %d",
1502 (int) booltesttype);
1503 selec = 0.0; /* Keep compiler quiet */
1507 free_attstatsslot(vardata.atttype, values, nvalues,
1513 * No most-common-value info available. Still have null fraction
1514 * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1515 * for null fraction and assume an even split for boolean tests.
1517 switch (booltesttype)
1522 * Use freq_null directly.
1526 case IS_NOT_UNKNOWN:
1529 * Select not unknown (not null) values. Calculate from
1532 selec = 1.0 - freq_null;
1538 selec = (1.0 - freq_null) / 2.0;
1541 elog(ERROR, "unrecognized booltesttype: %d",
1542 (int) booltesttype);
1543 selec = 0.0; /* Keep compiler quiet */
1551 * If we can't get variable statistics for the argument, perhaps
1552 * clause_selectivity can do something with it. We ignore the
1553 * possibility of a NULL value when using clause_selectivity, and just
1554 * assume the value is either TRUE or FALSE.
1556 switch (booltesttype)
1559 selec = DEFAULT_UNK_SEL;
1561 case IS_NOT_UNKNOWN:
1562 selec = DEFAULT_NOT_UNK_SEL;
1566 selec = (double) clause_selectivity(root, arg,
1572 selec = 1.0 - (double) clause_selectivity(root, arg,
1577 elog(ERROR, "unrecognized booltesttype: %d",
1578 (int) booltesttype);
1579 selec = 0.0; /* Keep compiler quiet */
1584 ReleaseVariableStats(vardata);
1586 /* result should be in range, but make sure... */
1587 CLAMP_PROBABILITY(selec);
1589 return (Selectivity) selec;
1593 * nulltestsel - Selectivity of NullTest Node.
1596 nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg,
1597 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1599 VariableStatData vardata;
1602 examine_variable(root, arg, varRelid, &vardata);
1604 if (HeapTupleIsValid(vardata.statsTuple))
1606 Form_pg_statistic stats;
1609 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1610 freq_null = stats->stanullfrac;
1612 switch (nulltesttype)
1617 * Use freq_null directly.
1624 * Select not unknown (not null) values. Calculate from
1627 selec = 1.0 - freq_null;
1630 elog(ERROR, "unrecognized nulltesttype: %d",
1631 (int) nulltesttype);
1632 return (Selectivity) 0; /* keep compiler quiet */
1638 * No ANALYZE stats available, so make a guess
1640 switch (nulltesttype)
1643 selec = DEFAULT_UNK_SEL;
1646 selec = DEFAULT_NOT_UNK_SEL;
1649 elog(ERROR, "unrecognized nulltesttype: %d",
1650 (int) nulltesttype);
1651 return (Selectivity) 0; /* keep compiler quiet */
1655 ReleaseVariableStats(vardata);
1657 /* result should be in range, but make sure... */
1658 CLAMP_PROBABILITY(selec);
1660 return (Selectivity) selec;
1664 * strip_array_coercion - strip binary-compatible relabeling from an array expr
1666 * For array values, the parser normally generates ArrayCoerceExpr conversions,
1667 * but it seems possible that RelabelType might show up. Also, the planner
1668 * is not currently tense about collapsing stacked ArrayCoerceExpr nodes,
1669 * so we need to be ready to deal with more than one level.
1672 strip_array_coercion(Node *node)
1676 if (node && IsA(node, ArrayCoerceExpr) &&
1677 ((ArrayCoerceExpr *) node)->elemfuncid == InvalidOid)
1679 node = (Node *) ((ArrayCoerceExpr *) node)->arg;
1681 else if (node && IsA(node, RelabelType))
1683 /* We don't really expect this case, but may as well cope */
1684 node = (Node *) ((RelabelType *) node)->arg;
1693 * scalararraysel - Selectivity of ScalarArrayOpExpr Node.
1696 scalararraysel(PlannerInfo *root,
1697 ScalarArrayOpExpr *clause,
1698 bool is_join_clause,
1701 SpecialJoinInfo *sjinfo)
1703 Oid operator = clause->opno;
1704 bool useOr = clause->useOr;
1705 bool isEquality = false;
1706 bool isInequality = false;
1709 Oid nominal_element_type;
1710 Oid nominal_element_collation;
1711 TypeCacheEntry *typentry;
1712 RegProcedure oprsel;
1713 FmgrInfo oprselproc;
1716 /* First, deconstruct the expression */
1717 Assert(list_length(clause->args) == 2);
1718 leftop = (Node *) linitial(clause->args);
1719 rightop = (Node *) lsecond(clause->args);
1721 /* get nominal (after relabeling) element type of rightop */
1722 nominal_element_type = get_base_element_type(exprType(rightop));
1723 if (!OidIsValid(nominal_element_type))
1724 return (Selectivity) 0.5; /* probably shouldn't happen */
1725 /* get nominal collation, too, for generating constants */
1726 nominal_element_collation = exprCollation(rightop);
1728 /* look through any binary-compatible relabeling of rightop */
1729 rightop = strip_array_coercion(rightop);
1732 * Detect whether the operator is the default equality or inequality
1733 * operator of the array element type.
1735 typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1736 if (OidIsValid(typentry->eq_opr))
1738 if (operator == typentry->eq_opr)
1740 else if (get_negator(operator) == typentry->eq_opr)
1741 isInequality = true;
1745 * If it is equality or inequality, we might be able to estimate this as
1746 * a form of array containment; for instance "const = ANY(column)" can be
1747 * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1748 * that, and returns the selectivity estimate if successful, or -1 if not.
1750 if ((isEquality || isInequality) && !is_join_clause)
1752 s1 = scalararraysel_containment(root, leftop, rightop,
1753 nominal_element_type,
1754 isEquality, useOr, varRelid);
1760 * Look up the underlying operator's selectivity estimator. Punt if it
1764 oprsel = get_oprjoin(operator);
1766 oprsel = get_oprrest(operator);
1768 return (Selectivity) 0.5;
1769 fmgr_info(oprsel, &oprselproc);
1772 * We consider three cases:
1774 * 1. rightop is an Array constant: deconstruct the array, apply the
1775 * operator's selectivity function for each array element, and merge the
1776 * results in the same way that clausesel.c does for AND/OR combinations.
1778 * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1779 * function for each element of the ARRAY[] construct, and merge.
1781 * 3. otherwise, make a guess ...
1783 if (rightop && IsA(rightop, Const))
1785 Datum arraydatum = ((Const *) rightop)->constvalue;
1786 bool arrayisnull = ((Const *) rightop)->constisnull;
1787 ArrayType *arrayval;
1796 if (arrayisnull) /* qual can't succeed if null array */
1797 return (Selectivity) 0.0;
1798 arrayval = DatumGetArrayTypeP(arraydatum);
1799 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
1800 &elmlen, &elmbyval, &elmalign);
1801 deconstruct_array(arrayval,
1802 ARR_ELEMTYPE(arrayval),
1803 elmlen, elmbyval, elmalign,
1804 &elem_values, &elem_nulls, &num_elems);
1805 s1 = useOr ? 0.0 : 1.0;
1806 for (i = 0; i < num_elems; i++)
1811 args = list_make2(leftop,
1812 makeConst(nominal_element_type,
1814 nominal_element_collation,
1820 s2 = DatumGetFloat8(FunctionCall5(&oprselproc,
1821 PointerGetDatum(root),
1822 ObjectIdGetDatum(operator),
1823 PointerGetDatum(args),
1824 Int16GetDatum(jointype),
1825 PointerGetDatum(sjinfo)));
1827 s2 = DatumGetFloat8(FunctionCall4(&oprselproc,
1828 PointerGetDatum(root),
1829 ObjectIdGetDatum(operator),
1830 PointerGetDatum(args),
1831 Int32GetDatum(varRelid)));
1833 s1 = s1 + s2 - s1 * s2;
1838 else if (rightop && IsA(rightop, ArrayExpr) &&
1839 !((ArrayExpr *) rightop)->multidims)
1841 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1846 get_typlenbyval(arrayexpr->element_typeid,
1847 &elmlen, &elmbyval);
1848 s1 = useOr ? 0.0 : 1.0;
1849 foreach(l, arrayexpr->elements)
1851 Node *elem = (Node *) lfirst(l);
1856 * Theoretically, if elem isn't of nominal_element_type we should
1857 * insert a RelabelType, but it seems unlikely that any operator
1858 * estimation function would really care ...
1860 args = list_make2(leftop, elem);
1862 s2 = DatumGetFloat8(FunctionCall5(&oprselproc,
1863 PointerGetDatum(root),
1864 ObjectIdGetDatum(operator),
1865 PointerGetDatum(args),
1866 Int16GetDatum(jointype),
1867 PointerGetDatum(sjinfo)));
1869 s2 = DatumGetFloat8(FunctionCall4(&oprselproc,
1870 PointerGetDatum(root),
1871 ObjectIdGetDatum(operator),
1872 PointerGetDatum(args),
1873 Int32GetDatum(varRelid)));
1875 s1 = s1 + s2 - s1 * s2;
1882 CaseTestExpr *dummyexpr;
1888 * We need a dummy rightop to pass to the operator selectivity
1889 * routine. It can be pretty much anything that doesn't look like a
1890 * constant; CaseTestExpr is a convenient choice.
1892 dummyexpr = makeNode(CaseTestExpr);
1893 dummyexpr->typeId = nominal_element_type;
1894 dummyexpr->typeMod = -1;
1895 dummyexpr->collation = clause->inputcollid;
1896 args = list_make2(leftop, dummyexpr);
1898 s2 = DatumGetFloat8(FunctionCall5(&oprselproc,
1899 PointerGetDatum(root),
1900 ObjectIdGetDatum(operator),
1901 PointerGetDatum(args),
1902 Int16GetDatum(jointype),
1903 PointerGetDatum(sjinfo)));
1905 s2 = DatumGetFloat8(FunctionCall4(&oprselproc,
1906 PointerGetDatum(root),
1907 ObjectIdGetDatum(operator),
1908 PointerGetDatum(args),
1909 Int32GetDatum(varRelid)));
1910 s1 = useOr ? 0.0 : 1.0;
1913 * Arbitrarily assume 10 elements in the eventual array value (see
1914 * also estimate_array_length)
1916 for (i = 0; i < 10; i++)
1919 s1 = s1 + s2 - s1 * s2;
1925 /* result should be in range, but make sure... */
1926 CLAMP_PROBABILITY(s1);
1932 * Estimate number of elements in the array yielded by an expression.
1934 * It's important that this agree with scalararraysel.
1937 estimate_array_length(Node *arrayexpr)
1939 /* look through any binary-compatible relabeling of arrayexpr */
1940 arrayexpr = strip_array_coercion(arrayexpr);
1942 if (arrayexpr && IsA(arrayexpr, Const))
1944 Datum arraydatum = ((Const *) arrayexpr)->constvalue;
1945 bool arrayisnull = ((Const *) arrayexpr)->constisnull;
1946 ArrayType *arrayval;
1950 arrayval = DatumGetArrayTypeP(arraydatum);
1951 return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1953 else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
1954 !((ArrayExpr *) arrayexpr)->multidims)
1956 return list_length(((ArrayExpr *) arrayexpr)->elements);
1960 /* default guess --- see also scalararraysel */
1966 * rowcomparesel - Selectivity of RowCompareExpr Node.
1968 * We estimate RowCompare selectivity by considering just the first (high
1969 * order) columns, which makes it equivalent to an ordinary OpExpr. While
1970 * this estimate could be refined by considering additional columns, it
1971 * seems unlikely that we could do a lot better without multi-column
1975 rowcomparesel(PlannerInfo *root,
1976 RowCompareExpr *clause,
1977 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1980 Oid opno = linitial_oid(clause->opnos);
1982 bool is_join_clause;
1984 /* Build equivalent arg list for single operator */
1985 opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
1988 * Decide if it's a join clause. This should match clausesel.c's
1989 * treat_as_join_clause(), except that we intentionally consider only the
1990 * leading columns and not the rest of the clause.
1995 * Caller is forcing restriction mode (eg, because we are examining an
1996 * inner indexscan qual).
1998 is_join_clause = false;
2000 else if (sjinfo == NULL)
2003 * It must be a restriction clause, since it's being evaluated at a
2006 is_join_clause = false;
2011 * Otherwise, it's a join if there's more than one relation used.
2013 is_join_clause = (NumRelids((Node *) opargs) > 1);
2018 /* Estimate selectivity for a join clause. */
2019 s1 = join_selectivity(root, opno,
2026 /* Estimate selectivity for a restriction clause. */
2027 s1 = restriction_selectivity(root, opno,
2036 * eqjoinsel - Join selectivity of "="
2039 eqjoinsel(PG_FUNCTION_ARGS)
2041 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2042 Oid operator = PG_GETARG_OID(1);
2043 List *args = (List *) PG_GETARG_POINTER(2);
2046 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2048 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
2050 VariableStatData vardata1;
2051 VariableStatData vardata2;
2052 bool join_is_reversed;
2053 RelOptInfo *inner_rel;
2055 get_join_variables(root, args, sjinfo,
2056 &vardata1, &vardata2, &join_is_reversed);
2058 switch (sjinfo->jointype)
2063 selec = eqjoinsel_inner(operator, &vardata1, &vardata2);
2068 * Look up the join's inner relation. min_righthand is sufficient
2069 * information because neither SEMI nor ANTI joins permit any
2070 * reassociation into or out of their RHS, so the righthand will
2071 * always be exactly that set of rels.
2073 inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
2075 if (!join_is_reversed)
2076 selec = eqjoinsel_semi(operator, &vardata1, &vardata2,
2079 selec = eqjoinsel_semi(get_commutator(operator),
2080 &vardata2, &vardata1,
2084 /* other values not expected here */
2085 elog(ERROR, "unrecognized join type: %d",
2086 (int) sjinfo->jointype);
2087 selec = 0; /* keep compiler quiet */
2091 ReleaseVariableStats(vardata1);
2092 ReleaseVariableStats(vardata2);
2094 CLAMP_PROBABILITY(selec);
2096 PG_RETURN_FLOAT8((float8) selec);
2100 * eqjoinsel_inner --- eqjoinsel for normal inner join
2102 * We also use this for LEFT/FULL outer joins; it's not presently clear
2103 * that it's worth trying to distinguish them here.
2106 eqjoinsel_inner(Oid operator,
2107 VariableStatData *vardata1, VariableStatData *vardata2)
2114 Form_pg_statistic stats1 = NULL;
2115 Form_pg_statistic stats2 = NULL;
2116 bool have_mcvs1 = false;
2117 Datum *values1 = NULL;
2119 float4 *numbers1 = NULL;
2121 bool have_mcvs2 = false;
2122 Datum *values2 = NULL;
2124 float4 *numbers2 = NULL;
2127 nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2128 nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2130 if (HeapTupleIsValid(vardata1->statsTuple))
2132 stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2133 have_mcvs1 = get_attstatsslot(vardata1->statsTuple,
2135 vardata1->atttypmod,
2139 &values1, &nvalues1,
2140 &numbers1, &nnumbers1);
2143 if (HeapTupleIsValid(vardata2->statsTuple))
2145 stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
2146 have_mcvs2 = get_attstatsslot(vardata2->statsTuple,
2148 vardata2->atttypmod,
2152 &values2, &nvalues2,
2153 &numbers2, &nnumbers2);
2156 if (have_mcvs1 && have_mcvs2)
2159 * We have most-common-value lists for both relations. Run through
2160 * the lists to see which MCVs actually join to each other with the
2161 * given operator. This allows us to determine the exact join
2162 * selectivity for the portion of the relations represented by the MCV
2163 * lists. We still have to estimate for the remaining population, but
2164 * in a skewed distribution this gives us a big leg up in accuracy.
2165 * For motivation see the analysis in Y. Ioannidis and S.
2166 * Christodoulakis, "On the propagation of errors in the size of join
2167 * results", Technical Report 1018, Computer Science Dept., University
2168 * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
2173 double nullfrac1 = stats1->stanullfrac;
2174 double nullfrac2 = stats2->stanullfrac;
2175 double matchprodfreq,
2187 fmgr_info(get_opcode(operator), &eqproc);
2188 hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
2189 hasmatch2 = (bool *) palloc0(nvalues2 * sizeof(bool));
2192 * Note we assume that each MCV will match at most one member of the
2193 * other MCV list. If the operator isn't really equality, there could
2194 * be multiple matches --- but we don't look for them, both for speed
2195 * and because the math wouldn't add up...
2197 matchprodfreq = 0.0;
2199 for (i = 0; i < nvalues1; i++)
2203 for (j = 0; j < nvalues2; j++)
2207 if (DatumGetBool(FunctionCall2Coll(&eqproc,
2208 DEFAULT_COLLATION_OID,
2212 hasmatch1[i] = hasmatch2[j] = true;
2213 matchprodfreq += numbers1[i] * numbers2[j];
2219 CLAMP_PROBABILITY(matchprodfreq);
2220 /* Sum up frequencies of matched and unmatched MCVs */
2221 matchfreq1 = unmatchfreq1 = 0.0;
2222 for (i = 0; i < nvalues1; i++)
2225 matchfreq1 += numbers1[i];
2227 unmatchfreq1 += numbers1[i];
2229 CLAMP_PROBABILITY(matchfreq1);
2230 CLAMP_PROBABILITY(unmatchfreq1);
2231 matchfreq2 = unmatchfreq2 = 0.0;
2232 for (i = 0; i < nvalues2; i++)
2235 matchfreq2 += numbers2[i];
2237 unmatchfreq2 += numbers2[i];
2239 CLAMP_PROBABILITY(matchfreq2);
2240 CLAMP_PROBABILITY(unmatchfreq2);
2245 * Compute total frequency of non-null values that are not in the MCV
2248 otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
2249 otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
2250 CLAMP_PROBABILITY(otherfreq1);
2251 CLAMP_PROBABILITY(otherfreq2);
2254 * We can estimate the total selectivity from the point of view of
2255 * relation 1 as: the known selectivity for matched MCVs, plus
2256 * unmatched MCVs that are assumed to match against random members of
2257 * relation 2's non-MCV population, plus non-MCV values that are
2258 * assumed to match against random members of relation 2's unmatched
2259 * MCVs plus non-MCV values.
2261 totalsel1 = matchprodfreq;
2263 totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - nvalues2);
2265 totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
2267 /* Same estimate from the point of view of relation 2. */
2268 totalsel2 = matchprodfreq;
2270 totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - nvalues1);
2272 totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
2276 * Use the smaller of the two estimates. This can be justified in
2277 * essentially the same terms as given below for the no-stats case: to
2278 * a first approximation, we are estimating from the point of view of
2279 * the relation with smaller nd.
2281 selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
2286 * We do not have MCV lists for both sides. Estimate the join
2287 * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
2288 * is plausible if we assume that the join operator is strict and the
2289 * non-null values are about equally distributed: a given non-null
2290 * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
2291 * of rel2, so total join rows are at most
2292 * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
2293 * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
2294 * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
2295 * with MIN() is an upper bound. Using the MIN() means we estimate
2296 * from the point of view of the relation with smaller nd (since the
2297 * larger nd is determining the MIN). It is reasonable to assume that
2298 * most tuples in this rel will have join partners, so the bound is
2299 * probably reasonably tight and should be taken as-is.
2301 * XXX Can we be smarter if we have an MCV list for just one side? It
2302 * seems that if we assume equal distribution for the other side, we
2303 * end up with the same answer anyway.
2305 double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2306 double nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
2308 selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
2316 free_attstatsslot(vardata1->atttype, values1, nvalues1,
2317 numbers1, nnumbers1);
2319 free_attstatsslot(vardata2->atttype, values2, nvalues2,
2320 numbers2, nnumbers2);
2326 * eqjoinsel_semi --- eqjoinsel for semi join
2328 * (Also used for anti join, which we are supposed to estimate the same way.)
2329 * Caller has ensured that vardata1 is the LHS variable.
2332 eqjoinsel_semi(Oid operator,
2333 VariableStatData *vardata1, VariableStatData *vardata2,
2334 RelOptInfo *inner_rel)
2341 Form_pg_statistic stats1 = NULL;
2342 bool have_mcvs1 = false;
2343 Datum *values1 = NULL;
2345 float4 *numbers1 = NULL;
2347 bool have_mcvs2 = false;
2348 Datum *values2 = NULL;
2350 float4 *numbers2 = NULL;
2353 nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2354 nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2357 * We clamp nd2 to be not more than what we estimate the inner relation's
2358 * size to be. This is intuitively somewhat reasonable since obviously
2359 * there can't be more than that many distinct values coming from the
2360 * inner rel. The reason for the asymmetry (ie, that we don't clamp nd1
2361 * likewise) is that this is the only pathway by which restriction clauses
2362 * applied to the inner rel will affect the join result size estimate,
2363 * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
2364 * only the outer rel's size. If we clamped nd1 we'd be double-counting
2365 * the selectivity of outer-rel restrictions.
2367 * We can apply this clamping both with respect to the base relation from
2368 * which the join variable comes (if there is just one), and to the
2369 * immediate inner input relation of the current join.
2372 nd2 = Min(nd2, vardata2->rel->rows);
2373 nd2 = Min(nd2, inner_rel->rows);
2375 if (HeapTupleIsValid(vardata1->statsTuple))
2377 stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2378 have_mcvs1 = get_attstatsslot(vardata1->statsTuple,
2380 vardata1->atttypmod,
2384 &values1, &nvalues1,
2385 &numbers1, &nnumbers1);
2388 if (HeapTupleIsValid(vardata2->statsTuple))
2390 have_mcvs2 = get_attstatsslot(vardata2->statsTuple,
2392 vardata2->atttypmod,
2396 &values2, &nvalues2,
2397 &numbers2, &nnumbers2);
2400 if (have_mcvs1 && have_mcvs2 && OidIsValid(operator))
2403 * We have most-common-value lists for both relations. Run through
2404 * the lists to see which MCVs actually join to each other with the
2405 * given operator. This allows us to determine the exact join
2406 * selectivity for the portion of the relations represented by the MCV
2407 * lists. We still have to estimate for the remaining population, but
2408 * in a skewed distribution this gives us a big leg up in accuracy.
2413 double nullfrac1 = stats1->stanullfrac;
2422 * The clamping above could have resulted in nd2 being less than
2423 * nvalues2; in which case, we assume that precisely the nd2 most
2424 * common values in the relation will appear in the join input, and so
2425 * compare to only the first nd2 members of the MCV list. Of course
2426 * this is frequently wrong, but it's the best bet we can make.
2428 clamped_nvalues2 = Min(nvalues2, nd2);
2430 fmgr_info(get_opcode(operator), &eqproc);
2431 hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
2432 hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool));
2435 * Note we assume that each MCV will match at most one member of the
2436 * other MCV list. If the operator isn't really equality, there could
2437 * be multiple matches --- but we don't look for them, both for speed
2438 * and because the math wouldn't add up...
2441 for (i = 0; i < nvalues1; i++)
2445 for (j = 0; j < clamped_nvalues2; j++)
2449 if (DatumGetBool(FunctionCall2Coll(&eqproc,
2450 DEFAULT_COLLATION_OID,
2454 hasmatch1[i] = hasmatch2[j] = true;
2460 /* Sum up frequencies of matched MCVs */
2462 for (i = 0; i < nvalues1; i++)
2465 matchfreq1 += numbers1[i];
2467 CLAMP_PROBABILITY(matchfreq1);
2472 * Now we need to estimate the fraction of relation 1 that has at
2473 * least one join partner. We know for certain that the matched MCVs
2474 * do, so that gives us a lower bound, but we're really in the dark
2475 * about everything else. Our crude approach is: if nd1 <= nd2 then
2476 * assume all non-null rel1 rows have join partners, else assume for
2477 * the uncertain rows that a fraction nd2/nd1 have join partners. We
2478 * can discount the known-matched MCVs from the distinct-values counts
2479 * before doing the division.
2481 * Crude as the above is, it's completely useless if we don't have
2482 * reliable ndistinct values for both sides. Hence, if either nd1 or
2483 * nd2 is default, punt and assume half of the uncertain rows have
2486 if (!isdefault1 && !isdefault2)
2490 if (nd1 <= nd2 || nd2 < 0)
2491 uncertainfrac = 1.0;
2493 uncertainfrac = nd2 / nd1;
2496 uncertainfrac = 0.5;
2497 uncertain = 1.0 - matchfreq1 - nullfrac1;
2498 CLAMP_PROBABILITY(uncertain);
2499 selec = matchfreq1 + uncertainfrac * uncertain;
2504 * Without MCV lists for both sides, we can only use the heuristic
2507 double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2509 if (!isdefault1 && !isdefault2)
2511 if (nd1 <= nd2 || nd2 < 0)
2512 selec = 1.0 - nullfrac1;
2514 selec = (nd2 / nd1) * (1.0 - nullfrac1);
2517 selec = 0.5 * (1.0 - nullfrac1);
2521 free_attstatsslot(vardata1->atttype, values1, nvalues1,
2522 numbers1, nnumbers1);
2524 free_attstatsslot(vardata2->atttype, values2, nvalues2,
2525 numbers2, nnumbers2);
2531 * neqjoinsel - Join selectivity of "!="
2534 neqjoinsel(PG_FUNCTION_ARGS)
2536 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2537 Oid operator = PG_GETARG_OID(1);
2538 List *args = (List *) PG_GETARG_POINTER(2);
2539 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2540 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
2545 * We want 1 - eqjoinsel() where the equality operator is the one
2546 * associated with this != operator, that is, its negator.
2548 eqop = get_negator(operator);
2551 result = DatumGetFloat8(DirectFunctionCall5(eqjoinsel,
2552 PointerGetDatum(root),
2553 ObjectIdGetDatum(eqop),
2554 PointerGetDatum(args),
2555 Int16GetDatum(jointype),
2556 PointerGetDatum(sjinfo)));
2560 /* Use default selectivity (should we raise an error instead?) */
2561 result = DEFAULT_EQ_SEL;
2563 result = 1.0 - result;
2564 PG_RETURN_FLOAT8(result);
2568 * scalarltjoinsel - Join selectivity of "<" and "<=" for scalars
2571 scalarltjoinsel(PG_FUNCTION_ARGS)
2573 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
2577 * scalargtjoinsel - Join selectivity of ">" and ">=" for scalars
2580 scalargtjoinsel(PG_FUNCTION_ARGS)
2582 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
2586 * patternjoinsel - Generic code for pattern-match join selectivity.
2589 patternjoinsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
2591 /* For the moment we just punt. */
2592 return negate ? (1.0 - DEFAULT_MATCH_SEL) : DEFAULT_MATCH_SEL;
2596 * regexeqjoinsel - Join selectivity of regular-expression pattern match.
2599 regexeqjoinsel(PG_FUNCTION_ARGS)
2601 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex, false));
2605 * icregexeqjoinsel - Join selectivity of case-insensitive regex match.
2608 icregexeqjoinsel(PG_FUNCTION_ARGS)
2610 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex_IC, false));
2614 * likejoinsel - Join selectivity of LIKE pattern match.
2617 likejoinsel(PG_FUNCTION_ARGS)
2619 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like, false));
2623 * iclikejoinsel - Join selectivity of ILIKE pattern match.
2626 iclikejoinsel(PG_FUNCTION_ARGS)
2628 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like_IC, false));
2632 * regexnejoinsel - Join selectivity of regex non-match.
2635 regexnejoinsel(PG_FUNCTION_ARGS)
2637 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex, true));
2641 * icregexnejoinsel - Join selectivity of case-insensitive regex non-match.
2644 icregexnejoinsel(PG_FUNCTION_ARGS)
2646 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex_IC, true));
2650 * nlikejoinsel - Join selectivity of LIKE pattern non-match.
2653 nlikejoinsel(PG_FUNCTION_ARGS)
2655 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like, true));
2659 * icnlikejoinsel - Join selectivity of ILIKE pattern non-match.
2662 icnlikejoinsel(PG_FUNCTION_ARGS)
2664 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like_IC, true));
2668 * mergejoinscansel - Scan selectivity of merge join.
2670 * A merge join will stop as soon as it exhausts either input stream.
2671 * Therefore, if we can estimate the ranges of both input variables,
2672 * we can estimate how much of the input will actually be read. This
2673 * can have a considerable impact on the cost when using indexscans.
2675 * Also, we can estimate how much of each input has to be read before the
2676 * first join pair is found, which will affect the join's startup time.
2678 * clause should be a clause already known to be mergejoinable. opfamily,
2679 * strategy, and nulls_first specify the sort ordering being used.
2682 * *leftstart is set to the fraction of the left-hand variable expected
2683 * to be scanned before the first join pair is found (0 to 1).
2684 * *leftend is set to the fraction of the left-hand variable expected
2685 * to be scanned before the join terminates (0 to 1).
2686 * *rightstart, *rightend similarly for the right-hand variable.
2689 mergejoinscansel(PlannerInfo *root, Node *clause,
2690 Oid opfamily, int strategy, bool nulls_first,
2691 Selectivity *leftstart, Selectivity *leftend,
2692 Selectivity *rightstart, Selectivity *rightend)
2696 VariableStatData leftvar,
2717 /* Set default results if we can't figure anything out. */
2718 /* XXX should default "start" fraction be a bit more than 0? */
2719 *leftstart = *rightstart = 0.0;
2720 *leftend = *rightend = 1.0;
2722 /* Deconstruct the merge clause */
2723 if (!is_opclause(clause))
2724 return; /* shouldn't happen */
2725 opno = ((OpExpr *) clause)->opno;
2726 left = get_leftop((Expr *) clause);
2727 right = get_rightop((Expr *) clause);
2729 return; /* shouldn't happen */
2731 /* Look for stats for the inputs */
2732 examine_variable(root, left, 0, &leftvar);
2733 examine_variable(root, right, 0, &rightvar);
2735 /* Extract the operator's declared left/right datatypes */
2736 get_op_opfamily_properties(opno, opfamily, false,
2740 Assert(op_strategy == BTEqualStrategyNumber);
2743 * Look up the various operators we need. If we don't find them all, it
2744 * probably means the opfamily is broken, but we just fail silently.
2746 * Note: we expect that pg_statistic histograms will be sorted by the '<'
2747 * operator, regardless of which sort direction we are considering.
2751 case BTLessStrategyNumber:
2753 if (op_lefttype == op_righttype)
2756 ltop = get_opfamily_member(opfamily,
2757 op_lefttype, op_righttype,
2758 BTLessStrategyNumber);
2759 leop = get_opfamily_member(opfamily,
2760 op_lefttype, op_righttype,
2761 BTLessEqualStrategyNumber);
2771 ltop = get_opfamily_member(opfamily,
2772 op_lefttype, op_righttype,
2773 BTLessStrategyNumber);
2774 leop = get_opfamily_member(opfamily,
2775 op_lefttype, op_righttype,
2776 BTLessEqualStrategyNumber);
2777 lsortop = get_opfamily_member(opfamily,
2778 op_lefttype, op_lefttype,
2779 BTLessStrategyNumber);
2780 rsortop = get_opfamily_member(opfamily,
2781 op_righttype, op_righttype,
2782 BTLessStrategyNumber);
2785 revltop = get_opfamily_member(opfamily,
2786 op_righttype, op_lefttype,
2787 BTLessStrategyNumber);
2788 revleop = get_opfamily_member(opfamily,
2789 op_righttype, op_lefttype,
2790 BTLessEqualStrategyNumber);
2793 case BTGreaterStrategyNumber:
2794 /* descending-order case */
2796 if (op_lefttype == op_righttype)
2799 ltop = get_opfamily_member(opfamily,
2800 op_lefttype, op_righttype,
2801 BTGreaterStrategyNumber);
2802 leop = get_opfamily_member(opfamily,
2803 op_lefttype, op_righttype,
2804 BTGreaterEqualStrategyNumber);
2807 lstatop = get_opfamily_member(opfamily,
2808 op_lefttype, op_lefttype,
2809 BTLessStrategyNumber);
2816 ltop = get_opfamily_member(opfamily,
2817 op_lefttype, op_righttype,
2818 BTGreaterStrategyNumber);
2819 leop = get_opfamily_member(opfamily,
2820 op_lefttype, op_righttype,
2821 BTGreaterEqualStrategyNumber);
2822 lsortop = get_opfamily_member(opfamily,
2823 op_lefttype, op_lefttype,
2824 BTGreaterStrategyNumber);
2825 rsortop = get_opfamily_member(opfamily,
2826 op_righttype, op_righttype,
2827 BTGreaterStrategyNumber);
2828 lstatop = get_opfamily_member(opfamily,
2829 op_lefttype, op_lefttype,
2830 BTLessStrategyNumber);
2831 rstatop = get_opfamily_member(opfamily,
2832 op_righttype, op_righttype,
2833 BTLessStrategyNumber);
2834 revltop = get_opfamily_member(opfamily,
2835 op_righttype, op_lefttype,
2836 BTGreaterStrategyNumber);
2837 revleop = get_opfamily_member(opfamily,
2838 op_righttype, op_lefttype,
2839 BTGreaterEqualStrategyNumber);
2843 goto fail; /* shouldn't get here */
2846 if (!OidIsValid(lsortop) ||
2847 !OidIsValid(rsortop) ||
2848 !OidIsValid(lstatop) ||
2849 !OidIsValid(rstatop) ||
2850 !OidIsValid(ltop) ||
2851 !OidIsValid(leop) ||
2852 !OidIsValid(revltop) ||
2853 !OidIsValid(revleop))
2854 goto fail; /* insufficient info in catalogs */
2856 /* Try to get ranges of both inputs */
2859 if (!get_variable_range(root, &leftvar, lstatop,
2860 &leftmin, &leftmax))
2861 goto fail; /* no range available from stats */
2862 if (!get_variable_range(root, &rightvar, rstatop,
2863 &rightmin, &rightmax))
2864 goto fail; /* no range available from stats */
2868 /* need to swap the max and min */
2869 if (!get_variable_range(root, &leftvar, lstatop,
2870 &leftmax, &leftmin))
2871 goto fail; /* no range available from stats */
2872 if (!get_variable_range(root, &rightvar, rstatop,
2873 &rightmax, &rightmin))
2874 goto fail; /* no range available from stats */
2878 * Now, the fraction of the left variable that will be scanned is the
2879 * fraction that's <= the right-side maximum value. But only believe
2880 * non-default estimates, else stick with our 1.0.
2882 selec = scalarineqsel(root, leop, isgt, &leftvar,
2883 rightmax, op_righttype);
2884 if (selec != DEFAULT_INEQ_SEL)
2887 /* And similarly for the right variable. */
2888 selec = scalarineqsel(root, revleop, isgt, &rightvar,
2889 leftmax, op_lefttype);
2890 if (selec != DEFAULT_INEQ_SEL)
2894 * Only one of the two "end" fractions can really be less than 1.0;
2895 * believe the smaller estimate and reset the other one to exactly 1.0. If
2896 * we get exactly equal estimates (as can easily happen with self-joins),
2899 if (*leftend > *rightend)
2901 else if (*leftend < *rightend)
2904 *leftend = *rightend = 1.0;
2907 * Also, the fraction of the left variable that will be scanned before the
2908 * first join pair is found is the fraction that's < the right-side
2909 * minimum value. But only believe non-default estimates, else stick with
2912 selec = scalarineqsel(root, ltop, isgt, &leftvar,
2913 rightmin, op_righttype);
2914 if (selec != DEFAULT_INEQ_SEL)
2917 /* And similarly for the right variable. */
2918 selec = scalarineqsel(root, revltop, isgt, &rightvar,
2919 leftmin, op_lefttype);
2920 if (selec != DEFAULT_INEQ_SEL)
2921 *rightstart = selec;
2924 * Only one of the two "start" fractions can really be more than zero;
2925 * believe the larger estimate and reset the other one to exactly 0.0. If
2926 * we get exactly equal estimates (as can easily happen with self-joins),
2929 if (*leftstart < *rightstart)
2931 else if (*leftstart > *rightstart)
2934 *leftstart = *rightstart = 0.0;
2937 * If the sort order is nulls-first, we're going to have to skip over any
2938 * nulls too. These would not have been counted by scalarineqsel, and we
2939 * can safely add in this fraction regardless of whether we believe
2940 * scalarineqsel's results or not. But be sure to clamp the sum to 1.0!
2944 Form_pg_statistic stats;
2946 if (HeapTupleIsValid(leftvar.statsTuple))
2948 stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
2949 *leftstart += stats->stanullfrac;
2950 CLAMP_PROBABILITY(*leftstart);
2951 *leftend += stats->stanullfrac;
2952 CLAMP_PROBABILITY(*leftend);
2954 if (HeapTupleIsValid(rightvar.statsTuple))
2956 stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
2957 *rightstart += stats->stanullfrac;
2958 CLAMP_PROBABILITY(*rightstart);
2959 *rightend += stats->stanullfrac;
2960 CLAMP_PROBABILITY(*rightend);
2964 /* Disbelieve start >= end, just in case that can happen */
2965 if (*leftstart >= *leftend)
2970 if (*rightstart >= *rightend)
2977 ReleaseVariableStats(leftvar);
2978 ReleaseVariableStats(rightvar);
2983 * Helper routine for estimate_num_groups: add an item to a list of
2984 * GroupVarInfos, but only if it's not known equal to any of the existing
2989 Node *var; /* might be an expression, not just a Var */
2990 RelOptInfo *rel; /* relation it belongs to */
2991 double ndistinct; /* # distinct values */
2995 add_unique_group_var(PlannerInfo *root, List *varinfos,
2996 Node *var, VariableStatData *vardata)
2998 GroupVarInfo *varinfo;
3003 ndistinct = get_variable_numdistinct(vardata, &isdefault);
3005 /* cannot use foreach here because of possible list_delete */
3006 lc = list_head(varinfos);
3009 varinfo = (GroupVarInfo *) lfirst(lc);
3011 /* must advance lc before list_delete possibly pfree's it */
3014 /* Drop exact duplicates */
3015 if (equal(var, varinfo->var))
3019 * Drop known-equal vars, but only if they belong to different
3020 * relations (see comments for estimate_num_groups)
3022 if (vardata->rel != varinfo->rel &&
3023 exprs_known_equal(root, var, varinfo->var))
3025 if (varinfo->ndistinct <= ndistinct)
3027 /* Keep older item, forget new one */
3032 /* Delete the older item */
3033 varinfos = list_delete_ptr(varinfos, varinfo);
3038 varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
3041 varinfo->rel = vardata->rel;
3042 varinfo->ndistinct = ndistinct;
3043 varinfos = lappend(varinfos, varinfo);
3048 * estimate_num_groups - Estimate number of groups in a grouped query
3050 * Given a query having a GROUP BY clause, estimate how many groups there
3051 * will be --- ie, the number of distinct combinations of the GROUP BY
3054 * This routine is also used to estimate the number of rows emitted by
3055 * a DISTINCT filtering step; that is an isomorphic problem. (Note:
3056 * actually, we only use it for DISTINCT when there's no grouping or
3057 * aggregation ahead of the DISTINCT.)
3061 * groupExprs - list of expressions being grouped by
3062 * input_rows - number of rows estimated to arrive at the group/unique
3065 * Given the lack of any cross-correlation statistics in the system, it's
3066 * impossible to do anything really trustworthy with GROUP BY conditions
3067 * involving multiple Vars. We should however avoid assuming the worst
3068 * case (all possible cross-product terms actually appear as groups) since
3069 * very often the grouped-by Vars are highly correlated. Our current approach
3071 * 1. Expressions yielding boolean are assumed to contribute two groups,
3072 * independently of their content, and are ignored in the subsequent
3073 * steps. This is mainly because tests like "col IS NULL" break the
3074 * heuristic used in step 2 especially badly.
3075 * 2. Reduce the given expressions to a list of unique Vars used. For
3076 * example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
3077 * It is clearly correct not to count the same Var more than once.
3078 * It is also reasonable to treat f(x) the same as x: f() cannot
3079 * increase the number of distinct values (unless it is volatile,
3080 * which we consider unlikely for grouping), but it probably won't
3081 * reduce the number of distinct values much either.
3082 * As a special case, if a GROUP BY expression can be matched to an
3083 * expressional index for which we have statistics, then we treat the
3084 * whole expression as though it were just a Var.
3085 * 3. If the list contains Vars of different relations that are known equal
3086 * due to equivalence classes, then drop all but one of the Vars from each
3087 * known-equal set, keeping the one with smallest estimated # of values
3088 * (since the extra values of the others can't appear in joined rows).
3089 * Note the reason we only consider Vars of different relations is that
3090 * if we considered ones of the same rel, we'd be double-counting the
3091 * restriction selectivity of the equality in the next step.
3092 * 4. For Vars within a single source rel, we multiply together the numbers
3093 * of values, clamp to the number of rows in the rel (divided by 10 if
3094 * more than one Var), and then multiply by the selectivity of the
3095 * restriction clauses for that rel. When there's more than one Var,
3096 * the initial product is probably too high (it's the worst case) but
3097 * clamping to a fraction of the rel's rows seems to be a helpful
3098 * heuristic for not letting the estimate get out of hand. (The factor
3099 * of 10 is derived from pre-Postgres-7.4 practice.) Multiplying
3100 * by the restriction selectivity is effectively assuming that the
3101 * restriction clauses are independent of the grouping, which is a crummy
3102 * assumption, but it's hard to do better.
3103 * 5. If there are Vars from multiple rels, we repeat step 4 for each such
3104 * rel, and multiply the results together.
3105 * Note that rels not containing grouped Vars are ignored completely, as are
3106 * join clauses. Such rels cannot increase the number of groups, and we
3107 * assume such clauses do not reduce the number either (somewhat bogus,
3108 * but we don't have the info to do better).
3111 estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows)
3113 List *varinfos = NIL;
3117 /* We should not be called unless query has GROUP BY (or DISTINCT) */
3118 Assert(groupExprs != NIL);
3121 * Count groups derived from boolean grouping expressions. For other
3122 * expressions, find the unique Vars used, treating an expression as a Var
3123 * if we can find stats for it. For each one, record the statistical
3124 * estimate of number of distinct values (total in its table, without
3125 * regard for filtering).
3129 foreach(l, groupExprs)
3131 Node *groupexpr = (Node *) lfirst(l);
3132 VariableStatData vardata;
3136 /* Short-circuit for expressions returning boolean */
3137 if (exprType(groupexpr) == BOOLOID)
3144 * If examine_variable is able to deduce anything about the GROUP BY
3145 * expression, treat it as a single variable even if it's really more
3148 examine_variable(root, groupexpr, 0, &vardata);
3149 if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
3151 varinfos = add_unique_group_var(root, varinfos,
3152 groupexpr, &vardata);
3153 ReleaseVariableStats(vardata);
3156 ReleaseVariableStats(vardata);
3159 * Else pull out the component Vars. Handle PlaceHolderVars by
3160 * recursing into their arguments (effectively assuming that the
3161 * PlaceHolderVar doesn't change the number of groups, which boils
3162 * down to ignoring the possible addition of nulls to the result set).
3164 varshere = pull_var_clause(groupexpr,
3165 PVC_RECURSE_AGGREGATES,
3166 PVC_RECURSE_PLACEHOLDERS);
3169 * If we find any variable-free GROUP BY item, then either it is a
3170 * constant (and we can ignore it) or it contains a volatile function;
3171 * in the latter case we punt and assume that each input row will
3172 * yield a distinct group.
3174 if (varshere == NIL)
3176 if (contain_volatile_functions(groupexpr))
3182 * Else add variables to varinfos list
3184 foreach(l2, varshere)
3186 Node *var = (Node *) lfirst(l2);
3188 examine_variable(root, var, 0, &vardata);
3189 varinfos = add_unique_group_var(root, varinfos, var, &vardata);
3190 ReleaseVariableStats(vardata);
3195 * If now no Vars, we must have an all-constant or all-boolean GROUP BY
3198 if (varinfos == NIL)
3200 /* Guard against out-of-range answers */
3201 if (numdistinct > input_rows)
3202 numdistinct = input_rows;
3207 * Group Vars by relation and estimate total numdistinct.
3209 * For each iteration of the outer loop, we process the frontmost Var in
3210 * varinfos, plus all other Vars in the same relation. We remove these
3211 * Vars from the newvarinfos list for the next iteration. This is the
3212 * easiest way to group Vars of same rel together.
3216 GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
3217 RelOptInfo *rel = varinfo1->rel;
3218 double reldistinct = varinfo1->ndistinct;
3219 double relmaxndistinct = reldistinct;
3220 int relvarcount = 1;
3221 List *newvarinfos = NIL;
3224 * Get the product of numdistinct estimates of the Vars for this rel.
3225 * Also, construct new varinfos list of remaining Vars.
3227 for_each_cell(l, lnext(list_head(varinfos)))
3229 GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3231 if (varinfo2->rel == varinfo1->rel)
3233 reldistinct *= varinfo2->ndistinct;
3234 if (relmaxndistinct < varinfo2->ndistinct)
3235 relmaxndistinct = varinfo2->ndistinct;
3240 /* not time to process varinfo2 yet */
3241 newvarinfos = lcons(varinfo2, newvarinfos);
3246 * Sanity check --- don't divide by zero if empty relation.
3248 Assert(rel->reloptkind == RELOPT_BASEREL);
3249 if (rel->tuples > 0)
3252 * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
3253 * fudge factor is because the Vars are probably correlated but we
3254 * don't know by how much. We should never clamp to less than the
3255 * largest ndistinct value for any of the Vars, though, since
3256 * there will surely be at least that many groups.
3258 double clamp = rel->tuples;
3260 if (relvarcount > 1)
3263 if (clamp < relmaxndistinct)
3265 clamp = relmaxndistinct;
3266 /* for sanity in case some ndistinct is too large: */
3267 if (clamp > rel->tuples)
3268 clamp = rel->tuples;
3271 if (reldistinct > clamp)
3272 reldistinct = clamp;
3275 * Multiply by restriction selectivity.
3277 reldistinct *= rel->rows / rel->tuples;
3280 * Update estimate of total distinct groups.
3282 numdistinct *= reldistinct;
3285 varinfos = newvarinfos;
3286 } while (varinfos != NIL);
3288 numdistinct = ceil(numdistinct);
3290 /* Guard against out-of-range answers */
3291 if (numdistinct > input_rows)
3292 numdistinct = input_rows;
3293 if (numdistinct < 1.0)
3300 * Estimate hash bucketsize fraction (ie, number of entries in a bucket
3301 * divided by total tuples in relation) if the specified expression is used
3304 * XXX This is really pretty bogus since we're effectively assuming that the
3305 * distribution of hash keys will be the same after applying restriction
3306 * clauses as it was in the underlying relation. However, we are not nearly
3307 * smart enough to figure out how the restrict clauses might change the
3308 * distribution, so this will have to do for now.
3310 * We are passed the number of buckets the executor will use for the given
3311 * input relation. If the data were perfectly distributed, with the same
3312 * number of tuples going into each available bucket, then the bucketsize
3313 * fraction would be 1/nbuckets. But this happy state of affairs will occur
3314 * only if (a) there are at least nbuckets distinct data values, and (b)
3315 * we have a not-too-skewed data distribution. Otherwise the buckets will
3316 * be nonuniformly occupied. If the other relation in the join has a key
3317 * distribution similar to this one's, then the most-loaded buckets are
3318 * exactly those that will be probed most often. Therefore, the "average"
3319 * bucket size for costing purposes should really be taken as something close
3320 * to the "worst case" bucket size. We try to estimate this by adjusting the
3321 * fraction if there are too few distinct data values, and then scaling up
3322 * by the ratio of the most common value's frequency to the average frequency.
3324 * If no statistics are available, use a default estimate of 0.1. This will
3325 * discourage use of a hash rather strongly if the inner relation is large,
3326 * which is what we want. We do not want to hash unless we know that the
3327 * inner rel is well-dispersed (or the alternatives seem much worse).
3330 estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
3332 VariableStatData vardata;
3342 examine_variable(root, hashkey, 0, &vardata);
3344 /* Get number of distinct values */
3345 ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3347 /* If ndistinct isn't real, punt and return 0.1, per comments above */
3350 ReleaseVariableStats(vardata);
3351 return (Selectivity) 0.1;
3354 /* Get fraction that are null */
3355 if (HeapTupleIsValid(vardata.statsTuple))
3357 Form_pg_statistic stats;
3359 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3360 stanullfrac = stats->stanullfrac;
3365 /* Compute avg freq of all distinct data values in raw relation */
3366 avgfreq = (1.0 - stanullfrac) / ndistinct;
3369 * Adjust ndistinct to account for restriction clauses. Observe we are
3370 * assuming that the data distribution is affected uniformly by the
3371 * restriction clauses!
3373 * XXX Possibly better way, but much more expensive: multiply by
3374 * selectivity of rel's restriction clauses that mention the target Var.
3377 ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3380 * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3381 * number of buckets is less than the expected number of distinct values;
3382 * otherwise it is 1/ndistinct.
3384 if (ndistinct > nbuckets)
3385 estfract = 1.0 / nbuckets;
3387 estfract = 1.0 / ndistinct;
3390 * Look up the frequency of the most common value, if available.
3394 if (HeapTupleIsValid(vardata.statsTuple))
3396 if (get_attstatsslot(vardata.statsTuple,
3397 vardata.atttype, vardata.atttypmod,
3398 STATISTIC_KIND_MCV, InvalidOid,
3401 &numbers, &nnumbers))
3404 * The first MCV stat is for the most common value.
3407 mcvfreq = numbers[0];
3408 free_attstatsslot(vardata.atttype, NULL, 0,
3414 * Adjust estimated bucketsize upward to account for skewed distribution.
3416 if (avgfreq > 0.0 && mcvfreq > avgfreq)
3417 estfract *= mcvfreq / avgfreq;
3420 * Clamp bucketsize to sane range (the above adjustment could easily
3421 * produce an out-of-range result). We set the lower bound a little above
3422 * zero, since zero isn't a very sane result.
3424 if (estfract < 1.0e-6)
3426 else if (estfract > 1.0)
3429 ReleaseVariableStats(vardata);
3431 return (Selectivity) estfract;
3435 /*-------------------------------------------------------------------------
3439 *-------------------------------------------------------------------------
3444 * Convert non-NULL values of the indicated types to the comparison
3445 * scale needed by scalarineqsel().
3446 * Returns "true" if successful.
3448 * XXX this routine is a hack: ideally we should look up the conversion
3449 * subroutines in pg_type.
3451 * All numeric datatypes are simply converted to their equivalent
3452 * "double" values. (NUMERIC values that are outside the range of "double"
3453 * are clamped to +/- HUGE_VAL.)
3455 * String datatypes are converted by convert_string_to_scalar(),
3456 * which is explained below. The reason why this routine deals with
3457 * three values at a time, not just one, is that we need it for strings.
3459 * The bytea datatype is just enough different from strings that it has
3460 * to be treated separately.
3462 * The several datatypes representing absolute times are all converted
3463 * to Timestamp, which is actually a double, and then we just use that
3464 * double value. Note this will give correct results even for the "special"
3465 * values of Timestamp, since those are chosen to compare correctly;
3466 * see timestamp_cmp.
3468 * The several datatypes representing relative times (intervals) are all
3469 * converted to measurements expressed in seconds.
3472 convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
3473 Datum lobound, Datum hibound, Oid boundstypid,
3474 double *scaledlobound, double *scaledhibound)
3477 * Both the valuetypid and the boundstypid should exactly match the
3478 * declared input type(s) of the operator we are invoked for, so we just
3479 * error out if either is not recognized.
3481 * XXX The histogram we are interpolating between points of could belong
3482 * to a column that's only binary-compatible with the declared type. In
3483 * essence we are assuming that the semantics of binary-compatible types
3484 * are enough alike that we can use a histogram generated with one type's
3485 * operators to estimate selectivity for the other's. This is outright
3486 * wrong in some cases --- in particular signed versus unsigned
3487 * interpretation could trip us up. But it's useful enough in the
3488 * majority of cases that we do it anyway. Should think about more
3489 * rigorous ways to do it.
3494 * Built-in numeric types
3505 case REGPROCEDUREOID:
3507 case REGOPERATOROID:
3511 case REGDICTIONARYOID:
3512 *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
3513 *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
3514 *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
3518 * Built-in string types
3526 char *valstr = convert_string_datum(value, valuetypid);
3527 char *lostr = convert_string_datum(lobound, boundstypid);
3528 char *histr = convert_string_datum(hibound, boundstypid);
3530 convert_string_to_scalar(valstr, scaledvalue,
3531 lostr, scaledlobound,
3532 histr, scaledhibound);
3540 * Built-in bytea type
3544 convert_bytea_to_scalar(value, scaledvalue,
3545 lobound, scaledlobound,
3546 hibound, scaledhibound);
3551 * Built-in time types
3554 case TIMESTAMPTZOID:
3562 *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
3563 *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
3564 *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
3568 * Built-in network types
3573 *scaledvalue = convert_network_to_scalar(value, valuetypid);
3574 *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
3575 *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
3578 /* Don't know how to convert */
3579 *scaledvalue = *scaledlobound = *scaledhibound = 0;
3584 * Do convert_to_scalar()'s work for any numeric data type.
3587 convert_numeric_to_scalar(Datum value, Oid typid)
3592 return (double) DatumGetBool(value);
3594 return (double) DatumGetInt16(value);
3596 return (double) DatumGetInt32(value);
3598 return (double) DatumGetInt64(value);
3600 return (double) DatumGetFloat4(value);
3602 return (double) DatumGetFloat8(value);
3604 /* Note: out-of-range values will be clamped to +-HUGE_VAL */
3606 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
3610 case REGPROCEDUREOID:
3612 case REGOPERATOROID:
3616 case REGDICTIONARYOID:
3617 /* we can treat OIDs as integers... */
3618 return (double) DatumGetObjectId(value);
3622 * Can't get here unless someone tries to use scalarltsel/scalargtsel on
3623 * an operator with one numeric and one non-numeric operand.
3625 elog(ERROR, "unsupported type: %u", typid);
3630 * Do convert_to_scalar()'s work for any character-string data type.
3632 * String datatypes are converted to a scale that ranges from 0 to 1,
3633 * where we visualize the bytes of the string as fractional digits.
3635 * We do not want the base to be 256, however, since that tends to
3636 * generate inflated selectivity estimates; few databases will have
3637 * occurrences of all 256 possible byte values at each position.
3638 * Instead, use the smallest and largest byte values seen in the bounds
3639 * as the estimated range for each byte, after some fudging to deal with
3640 * the fact that we probably aren't going to see the full range that way.
3642 * An additional refinement is that we discard any common prefix of the
3643 * three strings before computing the scaled values. This allows us to
3644 * "zoom in" when we encounter a narrow data range. An example is a phone
3645 * number database where all the values begin with the same area code.
3646 * (Actually, the bounds will be adjacent histogram-bin-boundary values,
3647 * so this is more likely to happen than you might think.)
3650 convert_string_to_scalar(char *value,
3651 double *scaledvalue,
3653 double *scaledlobound,
3655 double *scaledhibound)
3661 rangelo = rangehi = (unsigned char) hibound[0];
3662 for (sptr = lobound; *sptr; sptr++)
3664 if (rangelo > (unsigned char) *sptr)
3665 rangelo = (unsigned char) *sptr;
3666 if (rangehi < (unsigned char) *sptr)
3667 rangehi = (unsigned char) *sptr;
3669 for (sptr = hibound; *sptr; sptr++)
3671 if (rangelo > (unsigned char) *sptr)
3672 rangelo = (unsigned char) *sptr;
3673 if (rangehi < (unsigned char) *sptr)
3674 rangehi = (unsigned char) *sptr;
3676 /* If range includes any upper-case ASCII chars, make it include all */
3677 if (rangelo <= 'Z' && rangehi >= 'A')
3684 /* Ditto lower-case */
3685 if (rangelo <= 'z' && rangehi >= 'a')
3693 if (rangelo <= '9' && rangehi >= '0')
3702 * If range includes less than 10 chars, assume we have not got enough
3703 * data, and make it include regular ASCII set.
3705 if (rangehi - rangelo < 9)
3712 * Now strip any common prefix of the three strings.
3716 if (*lobound != *hibound || *lobound != *value)
3718 lobound++, hibound++, value++;
3722 * Now we can do the conversions.
3724 *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
3725 *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
3726 *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
3730 convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
3732 int slen = strlen(value);
3738 return 0.0; /* empty string has scalar value 0 */
3741 * Since base is at least 10, need not consider more than about 20 chars
3746 /* Convert initial characters to fraction */
3747 base = rangehi - rangelo + 1;
3752 int ch = (unsigned char) *value++;
3756 else if (ch > rangehi)
3758 num += ((double) (ch - rangelo)) / denom;
3766 * Convert a string-type Datum into a palloc'd, null-terminated string.
3768 * When using a non-C locale, we must pass the string through strxfrm()
3769 * before continuing, so as to generate correct locale-specific results.
3772 convert_string_datum(Datum value, Oid typid)
3779 val = (char *) palloc(2);
3780 val[0] = DatumGetChar(value);
3786 val = TextDatumGetCString(value);
3790 NameData *nm = (NameData *) DatumGetPointer(value);
3792 val = pstrdup(NameStr(*nm));
3798 * Can't get here unless someone tries to use scalarltsel on an
3799 * operator with one string and one non-string operand.
3801 elog(ERROR, "unsupported type: %u", typid);
3805 if (!lc_collate_is_c(DEFAULT_COLLATION_OID))
3812 * Note: originally we guessed at a suitable output buffer size, and
3813 * only needed to call strxfrm twice if our guess was too small.
3814 * However, it seems that some versions of Solaris have buggy strxfrm
3815 * that can write past the specified buffer length in that scenario.
3816 * So, do it the dumb way for portability.
3818 * Yet other systems (e.g., glibc) sometimes return a smaller value
3819 * from the second call than the first; thus the Assert must be <= not
3820 * == as you'd expect. Can't any of these people program their way
3821 * out of a paper bag?
3823 * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
3824 * bogus data or set an error. This is not really a problem unless it
3825 * crashes since it will only give an estimation error and nothing
3828 #if _MSC_VER == 1400 /* VS.Net 2005 */
3832 * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?
3833 * FeedbackID=99694 */
3837 xfrmlen = strxfrm(x, val, 0);
3840 xfrmlen = strxfrm(NULL, val, 0);
3845 * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
3846 * of trying to allocate this much memory (and fail), just return the
3847 * original string unmodified as if we were in the C locale.
3849 if (xfrmlen == INT_MAX)
3852 xfrmstr = (char *) palloc(xfrmlen + 1);
3853 xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
3854 Assert(xfrmlen2 <= xfrmlen);
3863 * Do convert_to_scalar()'s work for any bytea data type.
3865 * Very similar to convert_string_to_scalar except we can't assume
3866 * null-termination and therefore pass explicit lengths around.
3868 * Also, assumptions about likely "normal" ranges of characters have been
3869 * removed - a data range of 0..255 is always used, for now. (Perhaps
3870 * someday we will add information about actual byte data range to
3874 convert_bytea_to_scalar(Datum value,
3875 double *scaledvalue,
3877 double *scaledlobound,
3879 double *scaledhibound)
3883 valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
3884 loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
3885 hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
3888 unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
3889 *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
3890 *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
3893 * Assume bytea data is uniformly distributed across all byte values.
3899 * Now strip any common prefix of the three strings.
3901 minlen = Min(Min(valuelen, loboundlen), hiboundlen);
3902 for (i = 0; i < minlen; i++)
3904 if (*lostr != *histr || *lostr != *valstr)
3906 lostr++, histr++, valstr++;
3907 loboundlen--, hiboundlen--, valuelen--;
3911 * Now we can do the conversions.
3913 *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
3914 *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
3915 *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
3919 convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
3920 int rangelo, int rangehi)
3927 return 0.0; /* empty string has scalar value 0 */
3930 * Since base is 256, need not consider more than about 10 chars (even
3931 * this many seems like overkill)
3936 /* Convert initial characters to fraction */
3937 base = rangehi - rangelo + 1;
3940 while (valuelen-- > 0)
3946 else if (ch > rangehi)
3948 num += ((double) (ch - rangelo)) / denom;
3956 * Do convert_to_scalar()'s work for any timevalue data type.
3959 convert_timevalue_to_scalar(Datum value, Oid typid)
3964 return DatumGetTimestamp(value);
3965 case TIMESTAMPTZOID:
3966 return DatumGetTimestampTz(value);
3968 return DatumGetTimestamp(DirectFunctionCall1(abstime_timestamp,
3971 return date2timestamp_no_overflow(DatumGetDateADT(value));
3974 Interval *interval = DatumGetIntervalP(value);
3977 * Convert the month part of Interval to days using assumed
3978 * average month length of 365.25/12.0 days. Not too
3979 * accurate, but plenty good enough for our purposes.
3981 #ifdef HAVE_INT64_TIMESTAMP
3982 return interval->time + interval->day * (double) USECS_PER_DAY +
3983 interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
3985 return interval->time + interval->day * SECS_PER_DAY +
3986 interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * (double) SECS_PER_DAY);
3990 #ifdef HAVE_INT64_TIMESTAMP
3991 return (DatumGetRelativeTime(value) * 1000000.0);
3993 return DatumGetRelativeTime(value);
3997 TimeInterval tinterval = DatumGetTimeInterval(value);
3999 #ifdef HAVE_INT64_TIMESTAMP
4000 if (tinterval->status != 0)
4001 return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
4003 if (tinterval->status != 0)
4004 return tinterval->data[1] - tinterval->data[0];
4006 return 0; /* for lack of a better idea */
4009 return DatumGetTimeADT(value);
4012 TimeTzADT *timetz = DatumGetTimeTzADTP(value);
4014 /* use GMT-equivalent time */
4015 #ifdef HAVE_INT64_TIMESTAMP
4016 return (double) (timetz->time + (timetz->zone * 1000000.0));
4018 return (double) (timetz->time + timetz->zone);
4024 * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4025 * an operator with one timevalue and one non-timevalue operand.
4027 elog(ERROR, "unsupported type: %u", typid);
4033 * get_restriction_variable
4034 * Examine the args of a restriction clause to see if it's of the
4035 * form (variable op pseudoconstant) or (pseudoconstant op variable),
4036 * where "variable" could be either a Var or an expression in vars of a
4037 * single relation. If so, extract information about the variable,
4038 * and also indicate which side it was on and the other argument.
4041 * root: the planner info
4042 * args: clause argument list
4043 * varRelid: see specs for restriction selectivity functions
4045 * Outputs: (these are valid only if TRUE is returned)
4046 * *vardata: gets information about variable (see examine_variable)
4047 * *other: gets other clause argument, aggressively reduced to a constant
4048 * *varonleft: set TRUE if variable is on the left, FALSE if on the right
4050 * Returns TRUE if a variable is identified, otherwise FALSE.
4052 * Note: if there are Vars on both sides of the clause, we must fail, because
4053 * callers are expecting that the other side will act like a pseudoconstant.
4056 get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
4057 VariableStatData *vardata, Node **other,
4062 VariableStatData rdata;
4064 /* Fail if not a binary opclause (probably shouldn't happen) */
4065 if (list_length(args) != 2)
4068 left = (Node *) linitial(args);
4069 right = (Node *) lsecond(args);
4072 * Examine both sides. Note that when varRelid is nonzero, Vars of other
4073 * relations will be treated as pseudoconstants.
4075 examine_variable(root, left, varRelid, vardata);
4076 examine_variable(root, right, varRelid, &rdata);
4079 * If one side is a variable and the other not, we win.
4081 if (vardata->rel && rdata.rel == NULL)
4084 *other = estimate_expression_value(root, rdata.var);
4085 /* Assume we need no ReleaseVariableStats(rdata) here */
4089 if (vardata->rel == NULL && rdata.rel)
4092 *other = estimate_expression_value(root, vardata->var);
4093 /* Assume we need no ReleaseVariableStats(*vardata) here */
4098 /* Ooops, clause has wrong structure (probably var op var) */
4099 ReleaseVariableStats(*vardata);
4100 ReleaseVariableStats(rdata);
4106 * get_join_variables
4107 * Apply examine_variable() to each side of a join clause.
4108 * Also, attempt to identify whether the join clause has the same
4109 * or reversed sense compared to the SpecialJoinInfo.
4111 * We consider the join clause "normal" if it is "lhs_var OP rhs_var",
4112 * or "reversed" if it is "rhs_var OP lhs_var". In complicated cases
4113 * where we can't tell for sure, we default to assuming it's normal.
4116 get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
4117 VariableStatData *vardata1, VariableStatData *vardata2,
4118 bool *join_is_reversed)
4123 if (list_length(args) != 2)
4124 elog(ERROR, "join operator should take two arguments");
4126 left = (Node *) linitial(args);
4127 right = (Node *) lsecond(args);
4129 examine_variable(root, left, 0, vardata1);
4130 examine_variable(root, right, 0, vardata2);
4132 if (vardata1->rel &&
4133 bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4134 *join_is_reversed = true; /* var1 is on RHS */
4135 else if (vardata2->rel &&
4136 bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4137 *join_is_reversed = true; /* var2 is on LHS */
4139 *join_is_reversed = false;
4144 * Try to look up statistical data about an expression.
4145 * Fill in a VariableStatData struct to describe the expression.
4148 * root: the planner info
4149 * node: the expression tree to examine
4150 * varRelid: see specs for restriction selectivity functions
4152 * Outputs: *vardata is filled as follows:
4153 * var: the input expression (with any binary relabeling stripped, if
4154 * it is or contains a variable; but otherwise the type is preserved)
4155 * rel: RelOptInfo for relation containing variable; NULL if expression
4156 * contains no Vars (NOTE this could point to a RelOptInfo of a
4157 * subquery, not one in the current query).
4158 * statsTuple: the pg_statistic entry for the variable, if one exists;
4160 * freefunc: pointer to a function to release statsTuple with.
4161 * vartype: exposed type of the expression; this should always match
4162 * the declared input type of the operator we are estimating for.
4163 * atttype, atttypmod: type data to pass to get_attstatsslot(). This is
4164 * commonly the same as the exposed type of the variable argument,
4165 * but can be different in binary-compatible-type cases.
4166 * isunique: TRUE if we were able to match the var to a unique index or a
4167 * single-column DISTINCT clause, implying its values are unique for
4168 * this query. (Caution: this should be trusted for statistical
4169 * purposes only, since we do not check indimmediate nor verify that
4170 * the exact same definition of equality applies.)
4172 * Caller is responsible for doing ReleaseVariableStats() before exiting.
4175 examine_variable(PlannerInfo *root, Node *node, int varRelid,
4176 VariableStatData *vardata)
4182 /* Make sure we don't return dangling pointers in vardata */
4183 MemSet(vardata, 0, sizeof(VariableStatData));
4185 /* Save the exposed type of the expression */
4186 vardata->vartype = exprType(node);
4188 /* Look inside any binary-compatible relabeling */
4190 if (IsA(node, RelabelType))
4191 basenode = (Node *) ((RelabelType *) node)->arg;
4195 /* Fast path for a simple Var */
4197 if (IsA(basenode, Var) &&
4198 (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4200 Var *var = (Var *) basenode;
4202 /* Set up result fields other than the stats tuple */
4203 vardata->var = basenode; /* return Var without relabeling */
4204 vardata->rel = find_base_rel(root, var->varno);
4205 vardata->atttype = var->vartype;
4206 vardata->atttypmod = var->vartypmod;
4207 vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4209 /* Try to locate some stats */
4210 examine_simple_variable(root, var, vardata);
4216 * Okay, it's a more complicated expression. Determine variable
4217 * membership. Note that when varRelid isn't zero, only vars of that
4218 * relation are considered "real" vars.
4220 varnos = pull_varnos(basenode);
4224 switch (bms_membership(varnos))
4227 /* No Vars at all ... must be pseudo-constant clause */
4230 if (varRelid == 0 || bms_is_member(varRelid, varnos))
4232 onerel = find_base_rel(root,
4233 (varRelid ? varRelid : bms_singleton_member(varnos)));
4234 vardata->rel = onerel;
4235 node = basenode; /* strip any relabeling */
4237 /* else treat it as a constant */
4242 /* treat it as a variable of a join relation */
4243 vardata->rel = find_join_rel(root, varnos);
4244 node = basenode; /* strip any relabeling */
4246 else if (bms_is_member(varRelid, varnos))
4248 /* ignore the vars belonging to other relations */
4249 vardata->rel = find_base_rel(root, varRelid);
4250 node = basenode; /* strip any relabeling */
4251 /* note: no point in expressional-index search here */
4253 /* else treat it as a constant */
4259 vardata->var = node;
4260 vardata->atttype = exprType(node);
4261 vardata->atttypmod = exprTypmod(node);
4266 * We have an expression in vars of a single relation. Try to match
4267 * it to expressional index columns, in hopes of finding some
4270 * XXX it's conceivable that there are multiple matches with different
4271 * index opfamilies; if so, we need to pick one that matches the
4272 * operator we are estimating for. FIXME later.
4276 foreach(ilist, onerel->indexlist)
4278 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4279 ListCell *indexpr_item;
4282 indexpr_item = list_head(index->indexprs);
4283 if (indexpr_item == NULL)
4284 continue; /* no expressions here... */
4286 for (pos = 0; pos < index->ncolumns; pos++)
4288 if (index->indexkeys[pos] == 0)
4292 if (indexpr_item == NULL)
4293 elog(ERROR, "too few entries in indexprs list");
4294 indexkey = (Node *) lfirst(indexpr_item);
4295 if (indexkey && IsA(indexkey, RelabelType))
4296 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4297 if (equal(node, indexkey))
4300 * Found a match ... is it a unique index? Tests here
4301 * should match has_unique_index().
4303 if (index->unique &&
4304 index->ncolumns == 1 &&
4305 (index->indpred == NIL || index->predOK))
4306 vardata->isunique = true;
4309 * Has it got stats? We only consider stats for
4310 * non-partial indexes, since partial indexes probably
4311 * don't reflect whole-relation statistics; the above
4312 * check for uniqueness is the only info we take from
4315 * An index stats hook, however, must make its own
4316 * decisions about what to do with partial indexes.
4318 if (get_index_stats_hook &&
4319 (*get_index_stats_hook) (root, index->indexoid,
4323 * The hook took control of acquiring a stats
4324 * tuple. If it did supply a tuple, it'd better
4325 * have supplied a freefunc.
4327 if (HeapTupleIsValid(vardata->statsTuple) &&
4329 elog(ERROR, "no function provided to release variable stats with");
4331 else if (index->indpred == NIL)
4333 vardata->statsTuple =
4334 SearchSysCache3(STATRELATTINH,
4335 ObjectIdGetDatum(index->indexoid),
4336 Int16GetDatum(pos + 1),
4337 BoolGetDatum(false));
4338 vardata->freefunc = ReleaseSysCache;
4340 if (vardata->statsTuple)
4343 indexpr_item = lnext(indexpr_item);
4346 if (vardata->statsTuple)
4353 * examine_simple_variable
4354 * Handle a simple Var for examine_variable
4356 * This is split out as a subroutine so that we can recurse to deal with
4357 * Vars referencing subqueries.
4359 * We already filled in all the fields of *vardata except for the stats tuple.
4362 examine_simple_variable(PlannerInfo *root, Var *var,
4363 VariableStatData *vardata)
4365 RangeTblEntry *rte = root->simple_rte_array[var->varno];
4367 Assert(IsA(rte, RangeTblEntry));
4369 if (get_relation_stats_hook &&
4370 (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
4373 * The hook took control of acquiring a stats tuple. If it did supply
4374 * a tuple, it'd better have supplied a freefunc.
4376 if (HeapTupleIsValid(vardata->statsTuple) &&
4378 elog(ERROR, "no function provided to release variable stats with");
4380 else if (rte->rtekind == RTE_RELATION)
4383 * Plain table or parent of an inheritance appendrel, so look up the
4384 * column in pg_statistic
4386 vardata->statsTuple = SearchSysCache3(STATRELATTINH,
4387 ObjectIdGetDatum(rte->relid),
4388 Int16GetDatum(var->varattno),
4389 BoolGetDatum(rte->inh));
4390 vardata->freefunc = ReleaseSysCache;
4392 else if (rte->rtekind == RTE_SUBQUERY && !rte->inh)
4395 * Plain subquery (not one that was converted to an appendrel).
4397 Query *subquery = rte->subquery;
4402 * Punt if subquery uses set operations or GROUP BY, as these will
4403 * mash underlying columns' stats beyond recognition. (Set ops are
4404 * particularly nasty; if we forged ahead, we would return stats
4405 * relevant to only the leftmost subselect...) DISTINCT is also
4406 * problematic, but we check that later because there is a possibility
4407 * of learning something even with it.
4409 if (subquery->setOperations ||
4410 subquery->groupClause)
4414 * OK, fetch RelOptInfo for subquery. Note that we don't change the
4415 * rel returned in vardata, since caller expects it to be a rel of the
4416 * caller's query level. Because we might already be recursing, we
4417 * can't use that rel pointer either, but have to look up the Var's
4420 rel = find_base_rel(root, var->varno);
4422 /* Subquery should have been planned already */
4423 Assert(rel->subroot && IsA(rel->subroot, PlannerInfo));
4426 * Switch our attention to the subquery as mangled by the planner.
4427 * It was okay to look at the pre-planning version for the tests
4428 * above, but now we need a Var that will refer to the subroot's
4429 * live RelOptInfos. For instance, if any subquery pullup happened
4430 * during planning, Vars in the targetlist might have gotten replaced,
4431 * and we need to see the replacement expressions.
4433 subquery = rel->subroot->parse;
4434 Assert(IsA(subquery, Query));
4436 /* Get the subquery output expression referenced by the upper Var */
4437 ste = get_tle_by_resno(subquery->targetList, var->varattno);
4438 if (ste == NULL || ste->resjunk)
4439 elog(ERROR, "subquery %s does not have attribute %d",
4440 rte->eref->aliasname, var->varattno);
4441 var = (Var *) ste->expr;
4444 * If subquery uses DISTINCT, we can't make use of any stats for the
4445 * variable ... but, if it's the only DISTINCT column, we are entitled
4446 * to consider it unique. We do the test this way so that it works
4447 * for cases involving DISTINCT ON.
4449 if (subquery->distinctClause)
4451 if (list_length(subquery->distinctClause) == 1 &&
4452 targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
4453 vardata->isunique = true;
4454 /* cannot go further */
4459 * If the sub-query originated from a view with the security_barrier
4460 * attribute, we must not look at the variable's statistics, though
4461 * it seems all right to notice the existence of a DISTINCT clause.
4464 * This is probably a harsher restriction than necessary; it's
4465 * certainly OK for the selectivity estimator (which is a C function,
4466 * and therefore omnipotent anyway) to look at the statistics. But
4467 * many selectivity estimators will happily *invoke the operator
4468 * function* to try to work out a good estimate - and that's not OK.
4469 * So for now, don't dig down for stats.
4471 if (rte->security_barrier)
4474 /* Can only handle a simple Var of subquery's query level */
4475 if (var && IsA(var, Var) &&
4476 var->varlevelsup == 0)
4479 * OK, recurse into the subquery. Note that the original setting
4480 * of vardata->isunique (which will surely be false) is left
4481 * unchanged in this situation. That's what we want, since even
4482 * if the underlying column is unique, the subquery may have
4483 * joined to other tables in a way that creates duplicates.
4485 examine_simple_variable(rel->subroot, var, vardata);
4491 * Otherwise, the Var comes from a FUNCTION, VALUES, or CTE RTE. (We
4492 * won't see RTE_JOIN here because join alias Vars have already been
4493 * flattened.) There's not much we can do with function outputs, but
4494 * maybe someday try to be smarter about VALUES and/or CTEs.
4500 * get_variable_numdistinct
4501 * Estimate the number of distinct values of a variable.
4503 * vardata: results of examine_variable
4504 * *isdefault: set to TRUE if the result is a default rather than based on
4505 * anything meaningful.
4507 * NB: be careful to produce an integral result, since callers may compare
4508 * the result to exact integer counts.
4511 get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
4519 * Determine the stadistinct value to use. There are cases where we can
4520 * get an estimate even without a pg_statistic entry, or can get a better
4521 * value than is in pg_statistic.
4523 if (HeapTupleIsValid(vardata->statsTuple))
4525 /* Use the pg_statistic entry */
4526 Form_pg_statistic stats;
4528 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
4529 stadistinct = stats->stadistinct;
4531 else if (vardata->vartype == BOOLOID)
4534 * Special-case boolean columns: presumably, two distinct values.
4536 * Are there any other datatypes we should wire in special estimates
4544 * We don't keep statistics for system columns, but in some cases we
4545 * can infer distinctness anyway.
4547 if (vardata->var && IsA(vardata->var, Var))
4549 switch (((Var *) vardata->var)->varattno)
4551 case ObjectIdAttributeNumber:
4552 case SelfItemPointerAttributeNumber:
4553 stadistinct = -1.0; /* unique */
4555 case TableOidAttributeNumber:
4556 stadistinct = 1.0; /* only 1 value */
4559 stadistinct = 0.0; /* means "unknown" */
4564 stadistinct = 0.0; /* means "unknown" */
4567 * XXX consider using estimate_num_groups on expressions?
4572 * If there is a unique index or DISTINCT clause for the variable, assume
4573 * it is unique no matter what pg_statistic says; the statistics could be
4574 * out of date, or we might have found a partial unique index that proves
4575 * the var is unique for this query.
4577 if (vardata->isunique)
4581 * If we had an absolute estimate, use that.
4583 if (stadistinct > 0.0)
4587 * Otherwise we need to get the relation size; punt if not available.
4589 if (vardata->rel == NULL)
4592 return DEFAULT_NUM_DISTINCT;
4594 ntuples = vardata->rel->tuples;
4598 return DEFAULT_NUM_DISTINCT;
4602 * If we had a relative estimate, use that.
4604 if (stadistinct < 0.0)
4605 return floor((-stadistinct * ntuples) + 0.5);
4608 * With no data, estimate ndistinct = ntuples if the table is small, else
4609 * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small"
4610 * so that the behavior isn't discontinuous.
4612 if (ntuples < DEFAULT_NUM_DISTINCT)
4616 return DEFAULT_NUM_DISTINCT;
4620 * get_variable_range
4621 * Estimate the minimum and maximum value of the specified variable.
4622 * If successful, store values in *min and *max, and return TRUE.
4623 * If no data available, return FALSE.
4625 * sortop is the "<" comparison operator to use. This should generally
4626 * be "<" not ">", as only the former is likely to be found in pg_statistic.
4629 get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
4630 Datum *min, Datum *max)
4634 bool have_data = false;
4642 * XXX It's very tempting to try to use the actual column min and max, if
4643 * we can get them relatively-cheaply with an index probe. However, since
4644 * this function is called many times during join planning, that could
4645 * have unpleasant effects on planning speed. Need more investigation
4646 * before enabling this.
4649 if (get_actual_variable_range(root, vardata, sortop, min, max))
4653 if (!HeapTupleIsValid(vardata->statsTuple))
4655 /* no stats available, so default result */
4659 get_typlenbyval(vardata->atttype, &typLen, &typByVal);
4662 * If there is a histogram, grab the first and last values.
4664 * If there is a histogram that is sorted with some other operator than
4665 * the one we want, fail --- this suggests that there is data we can't
4668 if (get_attstatsslot(vardata->statsTuple,
4669 vardata->atttype, vardata->atttypmod,
4670 STATISTIC_KIND_HISTOGRAM, sortop,
4677 tmin = datumCopy(values[0], typByVal, typLen);
4678 tmax = datumCopy(values[nvalues - 1], typByVal, typLen);
4681 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
4683 else if (get_attstatsslot(vardata->statsTuple,
4684 vardata->atttype, vardata->atttypmod,
4685 STATISTIC_KIND_HISTOGRAM, InvalidOid,
4690 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
4695 * If we have most-common-values info, look for extreme MCVs. This is
4696 * needed even if we also have a histogram, since the histogram excludes
4697 * the MCVs. However, usually the MCVs will not be the extreme values, so
4698 * avoid unnecessary data copying.
4700 if (get_attstatsslot(vardata->statsTuple,
4701 vardata->atttype, vardata->atttypmod,
4702 STATISTIC_KIND_MCV, InvalidOid,
4707 bool tmin_is_mcv = false;
4708 bool tmax_is_mcv = false;
4711 fmgr_info(get_opcode(sortop), &opproc);
4713 for (i = 0; i < nvalues; i++)
4717 tmin = tmax = values[i];
4718 tmin_is_mcv = tmax_is_mcv = have_data = true;
4721 if (DatumGetBool(FunctionCall2Coll(&opproc,
4722 DEFAULT_COLLATION_OID,
4728 if (DatumGetBool(FunctionCall2Coll(&opproc,
4729 DEFAULT_COLLATION_OID,
4737 tmin = datumCopy(tmin, typByVal, typLen);
4739 tmax = datumCopy(tmax, typByVal, typLen);
4740 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
4750 * get_actual_variable_range
4751 * Attempt to identify the current *actual* minimum and/or maximum
4752 * of the specified variable, by looking for a suitable btree index
4753 * and fetching its low and/or high values.
4754 * If successful, store values in *min and *max, and return TRUE.
4755 * (Either pointer can be NULL if that endpoint isn't needed.)
4756 * If no data available, return FALSE.
4758 * sortop is the "<" comparison operator to use.
4761 get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata,
4763 Datum *min, Datum *max)
4765 bool have_data = false;
4766 RelOptInfo *rel = vardata->rel;
4770 /* No hope if no relation or it doesn't have indexes */
4771 if (rel == NULL || rel->indexlist == NIL)
4773 /* If it has indexes it must be a plain relation */
4774 rte = root->simple_rte_array[rel->relid];
4775 Assert(rte->rtekind == RTE_RELATION);
4777 /* Search through the indexes to see if any match our problem */
4778 foreach(lc, rel->indexlist)
4780 IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
4781 ScanDirection indexscandir;
4783 /* Ignore non-btree indexes */
4784 if (index->relam != BTREE_AM_OID)
4788 * Ignore partial indexes --- we only want stats that cover the entire
4791 if (index->indpred != NIL)
4795 * The index list might include hypothetical indexes inserted by a
4796 * get_relation_info hook --- don't try to access them.
4798 if (index->hypothetical)
4802 * The first index column must match the desired variable and sort
4803 * operator --- but we can use a descending-order index.
4805 if (!match_index_to_operand(vardata->var, 0, index))
4807 switch (get_op_opfamily_strategy(sortop, index->sortopfamily[0]))
4809 case BTLessStrategyNumber:
4810 if (index->reverse_sort[0])
4811 indexscandir = BackwardScanDirection;
4813 indexscandir = ForwardScanDirection;
4815 case BTGreaterStrategyNumber:
4816 if (index->reverse_sort[0])
4817 indexscandir = ForwardScanDirection;
4819 indexscandir = BackwardScanDirection;
4822 /* index doesn't match the sortop */
4827 * Found a suitable index to extract data from. We'll need an EState
4828 * and a bunch of other infrastructure.
4832 ExprContext *econtext;
4833 MemoryContext tmpcontext;
4834 MemoryContext oldcontext;
4837 IndexInfo *indexInfo;
4838 TupleTableSlot *slot;
4841 ScanKeyData scankeys[1];
4842 IndexScanDesc index_scan;
4844 Datum values[INDEX_MAX_KEYS];
4845 bool isnull[INDEX_MAX_KEYS];
4847 estate = CreateExecutorState();
4848 econtext = GetPerTupleExprContext(estate);
4849 /* Make sure any cruft is generated in the econtext's memory */
4850 tmpcontext = econtext->ecxt_per_tuple_memory;
4851 oldcontext = MemoryContextSwitchTo(tmpcontext);
4854 * Open the table and index so we can read from them. We should
4855 * already have at least AccessShareLock on the table, but not
4856 * necessarily on the index.
4858 heapRel = heap_open(rte->relid, NoLock);
4859 indexRel = index_open(index->indexoid, AccessShareLock);
4861 /* extract index key information from the index's pg_index info */
4862 indexInfo = BuildIndexInfo(indexRel);
4864 /* some other stuff */
4865 slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRel));
4866 econtext->ecxt_scantuple = slot;
4867 get_typlenbyval(vardata->atttype, &typLen, &typByVal);
4869 /* set up an IS NOT NULL scan key so that we ignore nulls */
4870 ScanKeyEntryInitialize(&scankeys[0],
4871 SK_ISNULL | SK_SEARCHNOTNULL,
4872 1, /* index col to scan */
4873 InvalidStrategy, /* no strategy */
4874 InvalidOid, /* no strategy subtype */
4875 InvalidOid, /* no collation */
4876 InvalidOid, /* no reg proc for this */
4877 (Datum) 0); /* constant */
4881 /* If min is requested ... */
4884 index_scan = index_beginscan(heapRel, indexRel, SnapshotNow,
4886 index_rescan(index_scan, scankeys, 1, NULL, 0);
4888 /* Fetch first tuple in sortop's direction */
4889 if ((tup = index_getnext(index_scan,
4890 indexscandir)) != NULL)
4892 /* Extract the index column values from the heap tuple */
4893 ExecStoreTuple(tup, slot, InvalidBuffer, false);
4894 FormIndexDatum(indexInfo, slot, estate,
4897 /* Shouldn't have got a null, but be careful */
4899 elog(ERROR, "found unexpected null value in index \"%s\"",
4900 RelationGetRelationName(indexRel));
4902 /* Copy the index column value out to caller's context */
4903 MemoryContextSwitchTo(oldcontext);
4904 *min = datumCopy(values[0], typByVal, typLen);
4905 MemoryContextSwitchTo(tmpcontext);
4910 index_endscan(index_scan);
4913 /* If max is requested, and we didn't find the index is empty */
4914 if (max && have_data)
4916 index_scan = index_beginscan(heapRel, indexRel, SnapshotNow,
4918 index_rescan(index_scan, scankeys, 1, NULL, 0);
4920 /* Fetch first tuple in reverse direction */
4921 if ((tup = index_getnext(index_scan,
4922 -indexscandir)) != NULL)
4924 /* Extract the index column values from the heap tuple */
4925 ExecStoreTuple(tup, slot, InvalidBuffer, false);
4926 FormIndexDatum(indexInfo, slot, estate,
4929 /* Shouldn't have got a null, but be careful */
4931 elog(ERROR, "found unexpected null value in index \"%s\"",
4932 RelationGetRelationName(indexRel));
4934 /* Copy the index column value out to caller's context */
4935 MemoryContextSwitchTo(oldcontext);
4936 *max = datumCopy(values[0], typByVal, typLen);
4937 MemoryContextSwitchTo(tmpcontext);
4942 index_endscan(index_scan);
4945 /* Clean everything up */
4946 ExecDropSingleTupleTableSlot(slot);
4948 index_close(indexRel, AccessShareLock);
4949 heap_close(heapRel, NoLock);
4951 MemoryContextSwitchTo(oldcontext);
4952 FreeExecutorState(estate);
4954 /* And we're done */
4963 * find_join_input_rel
4964 * Look up the input relation for a join.
4966 * We assume that the input relation's RelOptInfo must have been constructed
4970 find_join_input_rel(PlannerInfo *root, Relids relids)
4972 RelOptInfo *rel = NULL;
4974 switch (bms_membership(relids))
4977 /* should not happen */
4980 rel = find_base_rel(root, bms_singleton_member(relids));
4983 rel = find_join_rel(root, relids);
4988 elog(ERROR, "could not find RelOptInfo for given relids");
4994 /*-------------------------------------------------------------------------
4996 * Pattern analysis functions
4998 * These routines support analysis of LIKE and regular-expression patterns
4999 * by the planner/optimizer. It's important that they agree with the
5000 * regular-expression code in backend/regex/ and the LIKE code in
5001 * backend/utils/adt/like.c. Also, the computation of the fixed prefix
5002 * must be conservative: if we report a string longer than the true fixed
5003 * prefix, the query may produce actually wrong answers, rather than just
5004 * getting a bad selectivity estimate!
5006 * Note that the prefix-analysis functions are called from
5007 * backend/optimizer/path/indxpath.c as well as from routines in this file.
5009 *-------------------------------------------------------------------------
5013 * Check whether char is a letter (and, hence, subject to case-folding)
5015 * In multibyte character sets, we can't use isalpha, and it does not seem
5016 * worth trying to convert to wchar_t to use iswalpha. Instead, just assume
5017 * any multibyte char is potentially case-varying.
5020 pattern_char_isalpha(char c, bool is_multibyte,
5021 pg_locale_t locale, bool locale_is_c)
5024 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
5025 else if (is_multibyte && IS_HIGHBIT_SET(c))
5027 #ifdef HAVE_LOCALE_T
5029 return isalpha_l((unsigned char) c, locale);
5032 return isalpha((unsigned char) c);
5036 * Extract the fixed prefix, if any, for a pattern.
5038 * *prefix is set to a palloc'd prefix string (in the form of a Const node),
5039 * or to NULL if no fixed prefix exists for the pattern.
5040 * *rest is set to a palloc'd Const representing the remainder of the pattern
5041 * after the portion describing the fixed prefix.
5042 * Each of these has the same type (TEXT or BYTEA) as the given pattern Const.
5044 * The return value distinguishes no fixed prefix, a partial prefix,
5045 * or an exact-match-only pattern.
5048 static Pattern_Prefix_Status
5049 like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
5050 Const **prefix_const, Const **rest_const)
5056 Oid typeid = patt_const->consttype;
5059 bool is_multibyte = (pg_database_encoding_max_length() > 1);
5060 pg_locale_t locale = 0;
5061 bool locale_is_c = false;
5063 /* the right-hand const is type text or bytea */
5064 Assert(typeid == BYTEAOID || typeid == TEXTOID);
5066 if (case_insensitive)
5068 if (typeid == BYTEAOID)
5070 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5071 errmsg("case insensitive matching not supported on type bytea")));
5073 /* If case-insensitive, we need locale info */
5074 if (lc_ctype_is_c(collation))
5076 else if (collation != DEFAULT_COLLATION_OID)
5078 if (!OidIsValid(collation))
5081 * This typically means that the parser could not resolve a
5082 * conflict of implicit collations, so report it that way.
5085 (errcode(ERRCODE_INDETERMINATE_COLLATION),
5086 errmsg("could not determine which collation to use for ILIKE"),
5087 errhint("Use the COLLATE clause to set the collation explicitly.")));
5089 locale = pg_newlocale_from_collation(collation);
5093 if (typeid != BYTEAOID)
5095 patt = TextDatumGetCString(patt_const->constvalue);
5096 pattlen = strlen(patt);
5100 bytea *bstr = DatumGetByteaP(patt_const->constvalue);
5102 pattlen = VARSIZE(bstr) - VARHDRSZ;
5103 patt = (char *) palloc(pattlen);
5104 memcpy(patt, VARDATA(bstr), pattlen);
5105 if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
5109 match = palloc(pattlen + 1);
5111 for (pos = 0; pos < pattlen; pos++)
5113 /* % and _ are wildcard characters in LIKE */
5114 if (patt[pos] == '%' ||
5118 /* Backslash escapes the next character */
5119 if (patt[pos] == '\\')
5126 /* Stop if case-varying character (it's sort of a wildcard) */
5127 if (case_insensitive &&
5128 pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c))
5131 match[match_pos++] = patt[pos];
5134 match[match_pos] = '\0';
5137 if (typeid != BYTEAOID)
5139 *prefix_const = string_to_const(match, typeid);
5140 *rest_const = string_to_const(rest, typeid);
5144 *prefix_const = string_to_bytea_const(match, match_pos);
5145 *rest_const = string_to_bytea_const(rest, pattlen - pos);
5151 /* in LIKE, an empty pattern is an exact match! */
5153 return Pattern_Prefix_Exact; /* reached end of pattern, so exact */
5156 return Pattern_Prefix_Partial;
5158 return Pattern_Prefix_None;
5161 static Pattern_Prefix_Status
5162 regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
5163 Const **prefix_const, Const **rest_const)
5170 bool have_leading_paren;
5173 Oid typeid = patt_const->consttype;
5174 bool is_multibyte = (pg_database_encoding_max_length() > 1);
5175 pg_locale_t locale = 0;
5176 bool locale_is_c = false;
5179 * Should be unnecessary, there are no bytea regex operators defined. As
5180 * such, it should be noted that the rest of this function has *not* been
5181 * made safe for binary (possibly NULL containing) strings.
5183 if (typeid == BYTEAOID)
5185 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5186 errmsg("regular-expression matching not supported on type bytea")));
5188 if (case_insensitive)
5190 /* If case-insensitive, we need locale info */
5191 if (lc_ctype_is_c(collation))
5193 else if (collation != DEFAULT_COLLATION_OID)
5195 if (!OidIsValid(collation))
5198 * This typically means that the parser could not resolve a
5199 * conflict of implicit collations, so report it that way.
5202 (errcode(ERRCODE_INDETERMINATE_COLLATION),
5203 errmsg("could not determine which collation to use for regular expression"),
5204 errhint("Use the COLLATE clause to set the collation explicitly.")));
5206 locale = pg_newlocale_from_collation(collation);
5210 /* the right-hand const is type text for all of these */
5211 patt = TextDatumGetCString(patt_const->constvalue);
5214 * Check for ARE director prefix. It's worth our trouble to recognize
5215 * this because similar_escape() used to use it, and some other code might
5216 * still use it, to force ARE mode.
5219 if (strncmp(patt, "***:", 4) == 0)
5222 /* Pattern must be anchored left */
5223 if (patt[pos] != '^')
5227 *prefix_const = NULL;
5228 *rest_const = string_to_const(rest, typeid);
5230 return Pattern_Prefix_None;
5235 * If '|' is present in pattern, then there may be multiple alternatives
5236 * for the start of the string. (There are cases where this isn't so, for
5237 * instance if the '|' is inside parens, but detecting that reliably is
5240 if (strchr(patt + pos, '|') != NULL)
5244 *prefix_const = NULL;
5245 *rest_const = string_to_const(rest, typeid);
5247 return Pattern_Prefix_None;
5250 /* OK, allocate space for pattern */
5251 match = palloc(strlen(patt) + 1);
5252 prev_match_pos = match_pos = 0;
5255 * We special-case the syntax '^(...)$' because psql uses it. But beware:
5256 * sequences beginning "(?" are not what they seem, unless they're "(?:".
5257 * (We must recognize that because of similar_escape().)
5259 have_leading_paren = false;
5260 if (patt[pos] == '(' &&
5261 (patt[pos + 1] != '?' || patt[pos + 2] == ':'))
5263 have_leading_paren = true;
5264 pos += (patt[pos + 1] != '?' ? 1 : 3);
5267 /* Scan remainder of pattern */
5274 * Check for characters that indicate multiple possible matches here.
5275 * Also, drop out at ')' or '$' so the termination test works right.
5277 if (patt[pos] == '.' ||
5285 /* Stop if case-varying character (it's sort of a wildcard) */
5286 if (case_insensitive &&
5287 pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c))
5291 * Check for quantifiers. Except for +, this means the preceding
5292 * character is optional, so we must remove it from the prefix too!
5294 if (patt[pos] == '*' ||
5298 match_pos = prev_match_pos;
5302 if (patt[pos] == '+')
5309 * Normally, backslash quotes the next character. But in AREs,
5310 * backslash followed by alphanumeric is an escape, not a quoted
5311 * character. Must treat it as having multiple possible matches.
5312 * Note: since only ASCII alphanumerics are escapes, we don't have to
5313 * be paranoid about multibyte or collations here.
5315 if (patt[pos] == '\\')
5317 if (isalnum((unsigned char) patt[pos + 1]))
5320 if (patt[pos] == '\0')
5323 /* save position in case we need to back up on next loop cycle */
5324 prev_match_pos = match_pos;
5326 /* must use encoding-aware processing here */
5327 len = pg_mblen(&patt[pos]);
5328 memcpy(&match[match_pos], &patt[pos], len);
5333 match[match_pos] = '\0';
5336 if (have_leading_paren && patt[pos] == ')')
5339 if (patt[pos] == '$' && patt[pos + 1] == '\0')
5341 rest = &patt[pos + 1];
5343 *prefix_const = string_to_const(match, typeid);
5344 *rest_const = string_to_const(rest, typeid);
5349 return Pattern_Prefix_Exact; /* pattern specifies exact match */
5352 *prefix_const = string_to_const(match, typeid);
5353 *rest_const = string_to_const(rest, typeid);
5359 return Pattern_Prefix_Partial;
5361 return Pattern_Prefix_None;
5364 Pattern_Prefix_Status
5365 pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation,
5366 Const **prefix, Const **rest)
5368 Pattern_Prefix_Status result;
5372 case Pattern_Type_Like:
5373 result = like_fixed_prefix(patt, false, collation, prefix, rest);
5375 case Pattern_Type_Like_IC:
5376 result = like_fixed_prefix(patt, true, collation, prefix, rest);
5378 case Pattern_Type_Regex:
5379 result = regex_fixed_prefix(patt, false, collation, prefix, rest);
5381 case Pattern_Type_Regex_IC:
5382 result = regex_fixed_prefix(patt, true, collation, prefix, rest);
5385 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5386 result = Pattern_Prefix_None; /* keep compiler quiet */
5393 * Estimate the selectivity of a fixed prefix for a pattern match.
5395 * A fixed prefix "foo" is estimated as the selectivity of the expression
5396 * "variable >= 'foo' AND variable < 'fop'" (see also indxpath.c).
5398 * The selectivity estimate is with respect to the portion of the column
5399 * population represented by the histogram --- the caller must fold this
5400 * together with info about MCVs and NULLs.
5402 * We use the >= and < operators from the specified btree opfamily to do the
5403 * estimation. The given variable and Const must be of the associated
5406 * XXX Note: we make use of the upper bound to estimate operator selectivity
5407 * even if the locale is such that we cannot rely on the upper-bound string.
5408 * The selectivity only needs to be approximately right anyway, so it seems
5409 * more useful to use the upper-bound code than not.
5412 prefix_selectivity(PlannerInfo *root, VariableStatData *vardata,
5413 Oid vartype, Oid opfamily, Const *prefixcon)
5415 Selectivity prefixsel;
5418 Const *greaterstrcon;
5421 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5422 BTGreaterEqualStrategyNumber);
5423 if (cmpopr == InvalidOid)
5424 elog(ERROR, "no >= operator for opfamily %u", opfamily);
5425 fmgr_info(get_opcode(cmpopr), &opproc);
5427 prefixsel = ineq_histogram_selectivity(root, vardata, &opproc, true,
5428 prefixcon->constvalue,
5429 prefixcon->consttype);
5431 if (prefixsel < 0.0)
5433 /* No histogram is present ... return a suitable default estimate */
5434 return DEFAULT_MATCH_SEL;
5438 * If we can create a string larger than the prefix, say
5442 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5443 BTLessStrategyNumber);
5444 if (cmpopr == InvalidOid)
5445 elog(ERROR, "no < operator for opfamily %u", opfamily);
5446 fmgr_info(get_opcode(cmpopr), &opproc);
5447 greaterstrcon = make_greater_string(prefixcon, &opproc,
5448 DEFAULT_COLLATION_OID);
5453 topsel = ineq_histogram_selectivity(root, vardata, &opproc, false,
5454 greaterstrcon->constvalue,
5455 greaterstrcon->consttype);
5457 /* ineq_histogram_selectivity worked before, it shouldn't fail now */
5458 Assert(topsel >= 0.0);
5461 * Merge the two selectivities in the same way as for a range query
5462 * (see clauselist_selectivity()). Note that we don't need to worry
5463 * about double-exclusion of nulls, since ineq_histogram_selectivity
5464 * doesn't count those anyway.
5466 prefixsel = topsel + prefixsel - 1.0;
5470 * If the prefix is long then the two bounding values might be too close
5471 * together for the histogram to distinguish them usefully, resulting in a
5472 * zero estimate (plus or minus roundoff error). To avoid returning a
5473 * ridiculously small estimate, compute the estimated selectivity for
5474 * "variable = 'foo'", and clamp to that. (Obviously, the resultant
5475 * estimate should be at least that.)
5477 * We apply this even if we couldn't make a greater string. That case
5478 * suggests that the prefix is near the maximum possible, and thus
5479 * probably off the end of the histogram, and thus we probably got a very
5480 * small estimate from the >= condition; so we still need to clamp.
5482 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5483 BTEqualStrategyNumber);
5484 if (cmpopr == InvalidOid)
5485 elog(ERROR, "no = operator for opfamily %u", opfamily);
5486 eq_sel = var_eq_const(vardata, cmpopr, prefixcon->constvalue,
5489 prefixsel = Max(prefixsel, eq_sel);
5496 * Estimate the selectivity of a pattern of the specified type.
5497 * Note that any fixed prefix of the pattern will have been removed already.
5499 * For now, we use a very simplistic approach: fixed characters reduce the
5500 * selectivity a good deal, character ranges reduce it a little,
5501 * wildcards (such as % for LIKE or .* for regex) increase it.
5504 #define FIXED_CHAR_SEL 0.20 /* about 1/5 */
5505 #define CHAR_RANGE_SEL 0.25
5506 #define ANY_CHAR_SEL 0.9 /* not 1, since it won't match end-of-string */
5507 #define FULL_WILDCARD_SEL 5.0
5508 #define PARTIAL_WILDCARD_SEL 2.0
5511 like_selectivity(Const *patt_const, bool case_insensitive)
5513 Selectivity sel = 1.0;
5515 Oid typeid = patt_const->consttype;
5519 /* the right-hand const is type text or bytea */
5520 Assert(typeid == BYTEAOID || typeid == TEXTOID);
5522 if (typeid == BYTEAOID && case_insensitive)
5524 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5525 errmsg("case insensitive matching not supported on type bytea")));
5527 if (typeid != BYTEAOID)
5529 patt = TextDatumGetCString(patt_const->constvalue);
5530 pattlen = strlen(patt);
5534 bytea *bstr = DatumGetByteaP(patt_const->constvalue);
5536 pattlen = VARSIZE(bstr) - VARHDRSZ;
5537 patt = (char *) palloc(pattlen);
5538 memcpy(patt, VARDATA(bstr), pattlen);
5539 if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
5543 /* Skip any leading wildcard; it's already factored into initial sel */
5544 for (pos = 0; pos < pattlen; pos++)
5546 if (patt[pos] != '%' && patt[pos] != '_')
5550 for (; pos < pattlen; pos++)
5552 /* % and _ are wildcard characters in LIKE */
5553 if (patt[pos] == '%')
5554 sel *= FULL_WILDCARD_SEL;
5555 else if (patt[pos] == '_')
5556 sel *= ANY_CHAR_SEL;
5557 else if (patt[pos] == '\\')
5559 /* Backslash quotes the next character */
5563 sel *= FIXED_CHAR_SEL;
5566 sel *= FIXED_CHAR_SEL;
5568 /* Could get sel > 1 if multiple wildcards */
5577 regex_selectivity_sub(char *patt, int pattlen, bool case_insensitive)
5579 Selectivity sel = 1.0;
5580 int paren_depth = 0;
5581 int paren_pos = 0; /* dummy init to keep compiler quiet */
5584 for (pos = 0; pos < pattlen; pos++)
5586 if (patt[pos] == '(')
5588 if (paren_depth == 0)
5589 paren_pos = pos; /* remember start of parenthesized item */
5592 else if (patt[pos] == ')' && paren_depth > 0)
5595 if (paren_depth == 0)
5596 sel *= regex_selectivity_sub(patt + (paren_pos + 1),
5597 pos - (paren_pos + 1),
5600 else if (patt[pos] == '|' && paren_depth == 0)
5603 * If unquoted | is present at paren level 0 in pattern, we have
5604 * multiple alternatives; sum their probabilities.
5606 sel += regex_selectivity_sub(patt + (pos + 1),
5607 pattlen - (pos + 1),
5609 break; /* rest of pattern is now processed */
5611 else if (patt[pos] == '[')
5613 bool negclass = false;
5615 if (patt[++pos] == '^')
5620 if (patt[pos] == ']') /* ']' at start of class is not
5623 while (pos < pattlen && patt[pos] != ']')
5625 if (paren_depth == 0)
5626 sel *= (negclass ? (1.0 - CHAR_RANGE_SEL) : CHAR_RANGE_SEL);
5628 else if (patt[pos] == '.')
5630 if (paren_depth == 0)
5631 sel *= ANY_CHAR_SEL;
5633 else if (patt[pos] == '*' ||
5637 /* Ought to be smarter about quantifiers... */
5638 if (paren_depth == 0)
5639 sel *= PARTIAL_WILDCARD_SEL;
5641 else if (patt[pos] == '{')
5643 while (pos < pattlen && patt[pos] != '}')
5645 if (paren_depth == 0)
5646 sel *= PARTIAL_WILDCARD_SEL;
5648 else if (patt[pos] == '\\')
5650 /* backslash quotes the next character */
5654 if (paren_depth == 0)
5655 sel *= FIXED_CHAR_SEL;
5659 if (paren_depth == 0)
5660 sel *= FIXED_CHAR_SEL;
5663 /* Could get sel > 1 if multiple wildcards */
5670 regex_selectivity(Const *patt_const, bool case_insensitive)
5675 Oid typeid = patt_const->consttype;
5678 * Should be unnecessary, there are no bytea regex operators defined. As
5679 * such, it should be noted that the rest of this function has *not* been
5680 * made safe for binary (possibly NULL containing) strings.
5682 if (typeid == BYTEAOID)
5684 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5685 errmsg("regular-expression matching not supported on type bytea")));
5687 /* the right-hand const is type text for all of these */
5688 patt = TextDatumGetCString(patt_const->constvalue);
5689 pattlen = strlen(patt);
5691 /* If patt doesn't end with $, consider it to have a trailing wildcard */
5692 if (pattlen > 0 && patt[pattlen - 1] == '$' &&
5693 (pattlen == 1 || patt[pattlen - 2] != '\\'))
5695 /* has trailing $ */
5696 sel = regex_selectivity_sub(patt, pattlen - 1, case_insensitive);
5701 sel = regex_selectivity_sub(patt, pattlen, case_insensitive);
5702 sel *= FULL_WILDCARD_SEL;
5710 pattern_selectivity(Const *patt, Pattern_Type ptype)
5716 case Pattern_Type_Like:
5717 result = like_selectivity(patt, false);
5719 case Pattern_Type_Like_IC:
5720 result = like_selectivity(patt, true);
5722 case Pattern_Type_Regex:
5723 result = regex_selectivity(patt, false);
5725 case Pattern_Type_Regex_IC:
5726 result = regex_selectivity(patt, true);
5729 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5730 result = 1.0; /* keep compiler quiet */
5738 * For bytea, the increment function need only increment the current byte
5739 * (there are no multibyte characters to worry about).
5742 byte_increment(unsigned char *ptr, int len)
5751 * Try to generate a string greater than the given string or any
5752 * string it is a prefix of. If successful, return a palloc'd string
5753 * in the form of a Const node; else return NULL.
5755 * The caller must provide the appropriate "less than" comparison function
5756 * for testing the strings, along with the collation to use.
5758 * The key requirement here is that given a prefix string, say "foo",
5759 * we must be able to generate another string "fop" that is greater than
5760 * all strings "foobar" starting with "foo". We can test that we have
5761 * generated a string greater than the prefix string, but in non-C collations
5762 * that is not a bulletproof guarantee that an extension of the string might
5763 * not sort after it; an example is that "foo " is less than "foo!", but it
5764 * is not clear that a "dictionary" sort ordering will consider "foo!" less
5765 * than "foo bar". CAUTION: Therefore, this function should be used only for
5766 * estimation purposes when working in a non-C collation.
5768 * To try to catch most cases where an extended string might otherwise sort
5769 * before the result value, we determine which of the strings "Z", "z", "y",
5770 * and "9" is seen as largest by the collation, and append that to the given
5771 * prefix before trying to find a string that compares as larger.
5773 * To search for a greater string, we repeatedly "increment" the rightmost
5774 * character, using an encoding-specific character incrementer function.
5775 * When it's no longer possible to increment the last character, we truncate
5776 * off that character and start incrementing the next-to-rightmost.
5777 * For example, if "z" were the last character in the sort order, then we
5778 * could produce "foo" as a string greater than "fonz".
5780 * This could be rather slow in the worst case, but in most cases we
5781 * won't have to try more than one or two strings before succeeding.
5783 * Note that it's important for the character incrementer not to be too anal
5784 * about producing every possible character code, since in some cases the only
5785 * way to get a larger string is to increment a previous character position.
5786 * So we don't want to spend too much time trying every possible character
5787 * code at the last position. A good rule of thumb is to be sure that we
5788 * don't try more than 256*K values for a K-byte character (and definitely
5789 * not 256^K, which is what an exhaustive search would approach).
5792 make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
5794 Oid datatype = str_const->consttype;
5798 text *cmptxt = NULL;
5799 mbcharacter_incrementer charinc;
5802 * Get a modifiable copy of the prefix string in C-string format, and set
5803 * up the string we will compare to as a Datum. In C locale this can just
5804 * be the given prefix string, otherwise we need to add a suffix. Types
5805 * NAME and BYTEA sort bytewise so they don't need a suffix either.
5807 if (datatype == NAMEOID)
5809 workstr = DatumGetCString(DirectFunctionCall1(nameout,
5810 str_const->constvalue));
5811 len = strlen(workstr);
5812 cmpstr = str_const->constvalue;
5814 else if (datatype == BYTEAOID)
5816 bytea *bstr = DatumGetByteaP(str_const->constvalue);
5818 len = VARSIZE(bstr) - VARHDRSZ;
5819 workstr = (char *) palloc(len);
5820 memcpy(workstr, VARDATA(bstr), len);
5821 if ((Pointer) bstr != DatumGetPointer(str_const->constvalue))
5823 cmpstr = str_const->constvalue;
5827 workstr = TextDatumGetCString(str_const->constvalue);
5828 len = strlen(workstr);
5829 if (lc_collate_is_c(collation) || len == 0)
5830 cmpstr = str_const->constvalue;
5833 /* If first time through, determine the suffix to use */
5834 static char suffixchar = 0;
5835 static Oid suffixcollation = 0;
5837 if (!suffixchar || suffixcollation != collation)
5842 if (varstr_cmp(best, 1, "z", 1, collation) < 0)
5844 if (varstr_cmp(best, 1, "y", 1, collation) < 0)
5846 if (varstr_cmp(best, 1, "9", 1, collation) < 0)
5849 suffixcollation = collation;
5852 /* And build the string to compare to */
5853 cmptxt = (text *) palloc(VARHDRSZ + len + 1);
5854 SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
5855 memcpy(VARDATA(cmptxt), workstr, len);
5856 *(VARDATA(cmptxt) + len) = suffixchar;
5857 cmpstr = PointerGetDatum(cmptxt);
5861 /* Select appropriate character-incrementer function */
5862 if (datatype == BYTEAOID)
5863 charinc = byte_increment;
5865 charinc = pg_database_encoding_character_incrementer();
5867 /* And search ... */
5871 unsigned char *lastchar;
5873 /* Identify the last character --- for bytea, just the last byte */
5874 if (datatype == BYTEAOID)
5877 charlen = len - pg_mbcliplen(workstr, len, len - 1);
5878 lastchar = (unsigned char *) (workstr + len - charlen);
5881 * Try to generate a larger string by incrementing the last character
5882 * (for BYTEA, we treat each byte as a character).
5884 * Note: the incrementer function is expected to return true if it's
5885 * generated a valid-per-the-encoding new character, otherwise false.
5886 * The contents of the character on false return are unspecified.
5888 while (charinc(lastchar, charlen))
5890 Const *workstr_const;
5892 if (datatype == BYTEAOID)
5893 workstr_const = string_to_bytea_const(workstr, len);
5895 workstr_const = string_to_const(workstr, datatype);
5897 if (DatumGetBool(FunctionCall2Coll(ltproc,
5900 workstr_const->constvalue)))
5902 /* Successfully made a string larger than cmpstr */
5906 return workstr_const;
5909 /* No good, release unusable value and try again */
5910 pfree(DatumGetPointer(workstr_const->constvalue));
5911 pfree(workstr_const);
5915 * No luck here, so truncate off the last character and try to
5916 * increment the next one.
5919 workstr[len] = '\0';
5931 * Generate a Datum of the appropriate type from a C string.
5932 * Note that all of the supported types are pass-by-ref, so the
5933 * returned value should be pfree'd if no longer needed.
5936 string_to_datum(const char *str, Oid datatype)
5938 Assert(str != NULL);
5941 * We cheat a little by assuming that CStringGetTextDatum() will do for
5942 * bpchar and varchar constants too...
5944 if (datatype == NAMEOID)
5945 return DirectFunctionCall1(namein, CStringGetDatum(str));
5946 else if (datatype == BYTEAOID)
5947 return DirectFunctionCall1(byteain, CStringGetDatum(str));
5949 return CStringGetTextDatum(str);
5953 * Generate a Const node of the appropriate type from a C string.
5956 string_to_const(const char *str, Oid datatype)
5958 Datum conval = string_to_datum(str, datatype);
5963 * We only need to support a few datatypes here, so hard-wire properties
5964 * instead of incurring the expense of catalog lookups.
5971 collation = DEFAULT_COLLATION_OID;
5976 collation = InvalidOid;
5977 constlen = NAMEDATALEN;
5981 collation = InvalidOid;
5986 elog(ERROR, "unexpected datatype in string_to_const: %u",
5991 return makeConst(datatype, -1, collation, constlen,
5992 conval, false, false);
5996 * Generate a Const node of bytea type from a binary C string and a length.
5999 string_to_bytea_const(const char *str, size_t str_len)
6001 bytea *bstr = palloc(VARHDRSZ + str_len);
6004 memcpy(VARDATA(bstr), str, str_len);
6005 SET_VARSIZE(bstr, VARHDRSZ + str_len);
6006 conval = PointerGetDatum(bstr);
6008 return makeConst(BYTEAOID, -1, InvalidOid, -1, conval, false, false);
6011 /*-------------------------------------------------------------------------
6013 * Index cost estimation functions
6015 *-------------------------------------------------------------------------
6019 * If the index is partial, add its predicate to the given qual list.
6021 * ANDing the index predicate with the explicitly given indexquals produces
6022 * a more accurate idea of the index's selectivity. However, we need to be
6023 * careful not to insert redundant clauses, because clauselist_selectivity()
6024 * is easily fooled into computing a too-low selectivity estimate. Our
6025 * approach is to add only the predicate clause(s) that cannot be proven to
6026 * be implied by the given indexquals. This successfully handles cases such
6027 * as a qual "x = 42" used with a partial index "WHERE x >= 40 AND x < 50".
6028 * There are many other cases where we won't detect redundancy, leading to a
6029 * too-low selectivity estimate, which will bias the system in favor of using
6030 * partial indexes where possible. That is not necessarily bad though.
6032 * Note that indexQuals contains RestrictInfo nodes while the indpred
6033 * does not, so the output list will be mixed. This is OK for both
6034 * predicate_implied_by() and clauselist_selectivity(), but might be
6035 * problematic if the result were passed to other things.
6038 add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
6040 List *predExtraQuals = NIL;
6043 if (index->indpred == NIL)
6046 foreach(lc, index->indpred)
6048 Node *predQual = (Node *) lfirst(lc);
6049 List *oneQual = list_make1(predQual);
6051 if (!predicate_implied_by(oneQual, indexQuals))
6052 predExtraQuals = list_concat(predExtraQuals, oneQual);
6054 /* list_concat avoids modifying the passed-in indexQuals list */
6055 return list_concat(predExtraQuals, indexQuals);
6059 * genericcostestimate is a general-purpose estimator for use when we
6060 * don't have any better idea about how to estimate. Index-type-specific
6061 * knowledge can be incorporated in the type-specific routines.
6063 * One bit of index-type-specific knowledge we can relatively easily use
6064 * in genericcostestimate is the estimate of the number of index tuples
6065 * visited. If numIndexTuples is not 0 then it is used as the estimate,
6066 * otherwise we compute a generic estimate.
6069 genericcostestimate(PlannerInfo *root,
6072 double numIndexTuples,
6073 Cost *indexStartupCost,
6074 Cost *indexTotalCost,
6075 Selectivity *indexSelectivity,
6076 double *indexCorrelation)
6078 IndexOptInfo *index = path->indexinfo;
6079 List *indexQuals = path->indexquals;
6080 List *indexOrderBys = path->indexorderbys;
6081 double numIndexPages;
6082 double num_sa_scans;
6083 double num_outer_scans;
6085 QualCost index_qual_cost;
6086 double qual_op_cost;
6087 double qual_arg_cost;
6088 double spc_random_page_cost;
6089 List *selectivityQuals;
6093 * If the index is partial, AND the index predicate with the explicitly
6094 * given indexquals to produce a more accurate idea of the index
6097 selectivityQuals = add_predicate_to_quals(index, indexQuals);
6100 * Check for ScalarArrayOpExpr index quals, and estimate the number of
6101 * index scans that will be performed.
6104 foreach(l, indexQuals)
6106 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6108 if (IsA(rinfo->clause, ScalarArrayOpExpr))
6110 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6111 int alength = estimate_array_length(lsecond(saop->args));
6114 num_sa_scans *= alength;
6118 /* Estimate the fraction of main-table tuples that will be visited */
6119 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6125 * If caller didn't give us an estimate, estimate the number of index
6126 * tuples that will be visited. We do it in this rather peculiar-looking
6127 * way in order to get the right answer for partial indexes.
6129 if (numIndexTuples <= 0.0)
6131 numIndexTuples = *indexSelectivity * index->rel->tuples;
6134 * The above calculation counts all the tuples visited across all
6135 * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6136 * average per-indexscan number, so adjust. This is a handy place to
6137 * round to integer, too. (If caller supplied tuple estimate, it's
6138 * responsible for handling these considerations.)
6140 numIndexTuples = rint(numIndexTuples / num_sa_scans);
6144 * We can bound the number of tuples by the index size in any case. Also,
6145 * always estimate at least one tuple is touched, even when
6146 * indexSelectivity estimate is tiny.
6148 if (numIndexTuples > index->tuples)
6149 numIndexTuples = index->tuples;
6150 if (numIndexTuples < 1.0)
6151 numIndexTuples = 1.0;
6154 * Estimate the number of index pages that will be retrieved.
6156 * We use the simplistic method of taking a pro-rata fraction of the total
6157 * number of index pages. In effect, this counts only leaf pages and not
6158 * any overhead such as index metapage or upper tree levels. In practice
6159 * this seems a better approximation than charging for access to the upper
6160 * levels, perhaps because those tend to stay in cache under load.
6162 if (index->pages > 1 && index->tuples > 1)
6163 numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6165 numIndexPages = 1.0;
6167 /* fetch estimated page cost for schema containing index */
6168 get_tablespace_page_costs(index->reltablespace,
6169 &spc_random_page_cost,
6173 * Now compute the disk access costs.
6175 * The above calculations are all per-index-scan. However, if we are in a
6176 * nestloop inner scan, we can expect the scan to be repeated (with
6177 * different search keys) for each row of the outer relation. Likewise,
6178 * ScalarArrayOpExpr quals result in multiple index scans. This creates
6179 * the potential for cache effects to reduce the number of disk page
6180 * fetches needed. We want to estimate the average per-scan I/O cost in
6181 * the presence of caching.
6183 * We use the Mackert-Lohman formula (see costsize.c for details) to
6184 * estimate the total number of page fetches that occur. While this
6185 * wasn't what it was designed for, it seems a reasonable model anyway.
6186 * Note that we are counting pages not tuples anymore, so we take N = T =
6187 * index size, as if there were one "tuple" per page.
6189 num_outer_scans = loop_count;
6190 num_scans = num_sa_scans * num_outer_scans;
6194 double pages_fetched;
6196 /* total page fetches ignoring cache effects */
6197 pages_fetched = numIndexPages * num_scans;
6199 /* use Mackert and Lohman formula to adjust for cache effects */
6200 pages_fetched = index_pages_fetched(pages_fetched,
6202 (double) index->pages,
6206 * Now compute the total disk access cost, and then report a pro-rated
6207 * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6208 * since that's internal to the indexscan.)
6210 *indexTotalCost = (pages_fetched * spc_random_page_cost)
6216 * For a single index scan, we just charge spc_random_page_cost per
6219 *indexTotalCost = numIndexPages * spc_random_page_cost;
6223 * A difficulty with the leaf-pages-only cost approach is that for small
6224 * selectivities (eg, single index tuple fetched) all indexes will look
6225 * equally attractive because we will estimate exactly 1 leaf page to be
6226 * fetched. All else being equal, we should prefer physically smaller
6227 * indexes over larger ones. (An index might be smaller because it is
6228 * partial or because it contains fewer columns; presumably the other
6229 * columns in the larger index aren't useful to the query, or the larger
6230 * index would have better selectivity.)
6232 * We can deal with this by adding a very small "fudge factor" that
6233 * depends on the index size. The fudge factor used here is one
6234 * spc_random_page_cost per 10000 index pages, which should be small
6235 * enough to not alter index-vs-seqscan decisions, but will prevent
6236 * indexes of different sizes from looking exactly equally attractive.
6238 *indexTotalCost += index->pages * spc_random_page_cost / 10000.0;
6241 * CPU cost: any complex expressions in the indexquals will need to be
6242 * evaluated once at the start of the scan to reduce them to runtime keys
6243 * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6244 * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6245 * indexqual operator. Because we have numIndexTuples as a per-scan
6246 * number, we have to multiply by num_sa_scans to get the correct result
6247 * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6248 * ORDER BY expressions.
6250 * Note: this neglects the possible costs of rechecking lossy operators
6251 * and OR-clause expressions. Detecting that that might be needed seems
6252 * more expensive than it's worth, though, considering all the other
6253 * inaccuracies here ...
6255 cost_qual_eval(&index_qual_cost, indexQuals, root);
6256 qual_arg_cost = index_qual_cost.startup + index_qual_cost.per_tuple;
6257 cost_qual_eval(&index_qual_cost, indexOrderBys, root);
6258 qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
6259 qual_op_cost = cpu_operator_cost *
6260 (list_length(indexQuals) + list_length(indexOrderBys));
6261 qual_arg_cost -= qual_op_cost;
6262 if (qual_arg_cost < 0) /* just in case... */
6265 *indexStartupCost = qual_arg_cost;
6266 *indexTotalCost += qual_arg_cost;
6267 *indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6270 * We also add a CPU-cost component to represent the general costs of
6271 * starting an indexscan, such as analysis of btree index keys and initial
6272 * tree descent. This is estimated at 100x cpu_operator_cost, which is a
6273 * bit arbitrary but seems the right order of magnitude. (As noted above,
6274 * we don't charge any I/O for touching upper tree levels, but charging
6275 * nothing at all has been found too optimistic.)
6277 * Although this is startup cost with respect to any one scan, we add it
6278 * to the "total" cost component because it's only very interesting in the
6279 * many-ScalarArrayOpExpr-scan case, and there it will be paid over the
6280 * life of the scan node.
6282 *indexTotalCost += num_sa_scans * 100.0 * cpu_operator_cost;
6285 * Generic assumption about index correlation: there isn't any.
6287 *indexCorrelation = 0.0;
6292 btcostestimate(PG_FUNCTION_ARGS)
6294 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
6295 IndexPath *path = (IndexPath *) PG_GETARG_POINTER(1);
6296 double loop_count = PG_GETARG_FLOAT8(2);
6297 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
6298 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
6299 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
6300 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
6301 IndexOptInfo *index = path->indexinfo;
6304 VariableStatData vardata;
6305 double numIndexTuples;
6306 List *indexBoundQuals;
6310 bool found_is_null_op;
6311 double num_sa_scans;
6316 * For a btree scan, only leading '=' quals plus inequality quals for the
6317 * immediately next attribute contribute to index selectivity (these are
6318 * the "boundary quals" that determine the starting and stopping points of
6319 * the index scan). Additional quals can suppress visits to the heap, so
6320 * it's OK to count them in indexSelectivity, but they should not count
6321 * for estimating numIndexTuples. So we must examine the given indexquals
6322 * to find out which ones count as boundary quals. We rely on the
6323 * knowledge that they are given in index column order.
6325 * For a RowCompareExpr, we consider only the first column, just as
6326 * rowcomparesel() does.
6328 * If there's a ScalarArrayOpExpr in the quals, we'll actually perform N
6329 * index scans not one, but the ScalarArrayOpExpr's operator can be
6330 * considered to act the same as it normally does.
6332 indexBoundQuals = NIL;
6336 found_is_null_op = false;
6338 forboth(lcc, path->indexquals, lci, path->indexqualcols)
6340 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
6346 bool is_null_op = false;
6348 if (indexcol != lfirst_int(lci))
6350 /* Beginning of a new column's quals */
6352 break; /* done if no '=' qual for indexcol */
6355 if (indexcol != lfirst_int(lci))
6356 break; /* no quals at all for indexcol */
6359 Assert(IsA(rinfo, RestrictInfo));
6360 clause = rinfo->clause;
6362 if (IsA(clause, OpExpr))
6364 leftop = get_leftop(clause);
6365 rightop = get_rightop(clause);
6366 clause_op = ((OpExpr *) clause)->opno;
6368 else if (IsA(clause, RowCompareExpr))
6370 RowCompareExpr *rc = (RowCompareExpr *) clause;
6372 leftop = (Node *) linitial(rc->largs);
6373 rightop = (Node *) linitial(rc->rargs);
6374 clause_op = linitial_oid(rc->opnos);
6376 else if (IsA(clause, ScalarArrayOpExpr))
6378 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6380 leftop = (Node *) linitial(saop->args);
6381 rightop = (Node *) lsecond(saop->args);
6382 clause_op = saop->opno;
6385 else if (IsA(clause, NullTest))
6387 NullTest *nt = (NullTest *) clause;
6389 leftop = (Node *) nt->arg;
6391 clause_op = InvalidOid;
6392 if (nt->nulltesttype == IS_NULL)
6394 found_is_null_op = true;
6400 elog(ERROR, "unsupported indexqual type: %d",
6401 (int) nodeTag(clause));
6402 continue; /* keep compiler quiet */
6405 if (match_index_to_operand(leftop, indexcol, index))
6407 /* clause_op is correct */
6411 Assert(match_index_to_operand(rightop, indexcol, index));
6412 /* Must flip operator to get the opfamily member */
6413 clause_op = get_commutator(clause_op);
6416 /* check for equality operator */
6417 if (OidIsValid(clause_op))
6419 op_strategy = get_op_opfamily_strategy(clause_op,
6420 index->opfamily[indexcol]);
6421 Assert(op_strategy != 0); /* not a member of opfamily?? */
6422 if (op_strategy == BTEqualStrategyNumber)
6425 else if (is_null_op)
6427 /* IS NULL is like = for purposes of selectivity determination */
6430 /* count up number of SA scans induced by indexBoundQuals only */
6431 if (IsA(clause, ScalarArrayOpExpr))
6433 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6434 int alength = estimate_array_length(lsecond(saop->args));
6437 num_sa_scans *= alength;
6439 indexBoundQuals = lappend(indexBoundQuals, rinfo);
6443 * If index is unique and we found an '=' clause for each column, we can
6444 * just assume numIndexTuples = 1 and skip the expensive
6445 * clauselist_selectivity calculations. However, a ScalarArrayOp or
6446 * NullTest invalidates that theory, even though it sets eqQualHere.
6448 if (index->unique &&
6449 indexcol == index->ncolumns - 1 &&
6453 numIndexTuples = 1.0;
6456 List *selectivityQuals;
6457 Selectivity btreeSelectivity;
6460 * If the index is partial, AND the index predicate with the
6461 * index-bound quals to produce a more accurate idea of the number
6462 * of rows covered by the bound conditions.
6464 selectivityQuals = add_predicate_to_quals(index, indexBoundQuals);
6466 btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
6470 numIndexTuples = btreeSelectivity * index->rel->tuples;
6473 * As in genericcostestimate(), we have to adjust for any
6474 * ScalarArrayOpExpr quals included in indexBoundQuals, and then round
6477 numIndexTuples = rint(numIndexTuples / num_sa_scans);
6480 genericcostestimate(root, path, loop_count,
6482 indexStartupCost, indexTotalCost,
6483 indexSelectivity, indexCorrelation);
6486 * If we can get an estimate of the first column's ordering correlation C
6487 * from pg_statistic, estimate the index correlation as C for a
6488 * single-column index, or C * 0.75 for multiple columns. (The idea here
6489 * is that multiple columns dilute the importance of the first column's
6490 * ordering, but don't negate it entirely. Before 8.0 we divided the
6491 * correlation by the number of columns, but that seems too strong.)
6493 MemSet(&vardata, 0, sizeof(vardata));
6495 if (index->indexkeys[0] != 0)
6497 /* Simple variable --- look to stats for the underlying table */
6498 RangeTblEntry *rte = planner_rt_fetch(index->rel->relid, root);
6500 Assert(rte->rtekind == RTE_RELATION);
6502 Assert(relid != InvalidOid);
6503 colnum = index->indexkeys[0];
6505 if (get_relation_stats_hook &&
6506 (*get_relation_stats_hook) (root, rte, colnum, &vardata))
6509 * The hook took control of acquiring a stats tuple. If it did
6510 * supply a tuple, it'd better have supplied a freefunc.
6512 if (HeapTupleIsValid(vardata.statsTuple) &&
6514 elog(ERROR, "no function provided to release variable stats with");
6518 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
6519 ObjectIdGetDatum(relid),
6520 Int16GetDatum(colnum),
6521 BoolGetDatum(rte->inh));
6522 vardata.freefunc = ReleaseSysCache;
6527 /* Expression --- maybe there are stats for the index itself */
6528 relid = index->indexoid;
6531 if (get_index_stats_hook &&
6532 (*get_index_stats_hook) (root, relid, colnum, &vardata))
6535 * The hook took control of acquiring a stats tuple. If it did
6536 * supply a tuple, it'd better have supplied a freefunc.
6538 if (HeapTupleIsValid(vardata.statsTuple) &&
6540 elog(ERROR, "no function provided to release variable stats with");
6544 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
6545 ObjectIdGetDatum(relid),
6546 Int16GetDatum(colnum),
6547 BoolGetDatum(false));
6548 vardata.freefunc = ReleaseSysCache;
6552 if (HeapTupleIsValid(vardata.statsTuple))
6558 sortop = get_opfamily_member(index->opfamily[0],
6559 index->opcintype[0],
6560 index->opcintype[0],
6561 BTLessStrategyNumber);
6562 if (OidIsValid(sortop) &&
6563 get_attstatsslot(vardata.statsTuple, InvalidOid, 0,
6564 STATISTIC_KIND_CORRELATION,
6568 &numbers, &nnumbers))
6570 double varCorrelation;
6572 Assert(nnumbers == 1);
6573 varCorrelation = numbers[0];
6575 if (index->reverse_sort[0])
6576 varCorrelation = -varCorrelation;
6578 if (index->ncolumns > 1)
6579 *indexCorrelation = varCorrelation * 0.75;
6581 *indexCorrelation = varCorrelation;
6583 free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers);
6587 ReleaseVariableStats(vardata);
6593 hashcostestimate(PG_FUNCTION_ARGS)
6595 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
6596 IndexPath *path = (IndexPath *) PG_GETARG_POINTER(1);
6597 double loop_count = PG_GETARG_FLOAT8(2);
6598 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
6599 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
6600 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
6601 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
6603 genericcostestimate(root, path, loop_count, 0.0,
6604 indexStartupCost, indexTotalCost,
6605 indexSelectivity, indexCorrelation);
6611 gistcostestimate(PG_FUNCTION_ARGS)
6613 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
6614 IndexPath *path = (IndexPath *) PG_GETARG_POINTER(1);
6615 double loop_count = PG_GETARG_FLOAT8(2);
6616 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
6617 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
6618 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
6619 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
6621 genericcostestimate(root, path, loop_count, 0.0,
6622 indexStartupCost, indexTotalCost,
6623 indexSelectivity, indexCorrelation);
6629 spgcostestimate(PG_FUNCTION_ARGS)
6631 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
6632 IndexPath *path = (IndexPath *) PG_GETARG_POINTER(1);
6633 double loop_count = PG_GETARG_FLOAT8(2);
6634 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
6635 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
6636 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
6637 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
6639 genericcostestimate(root, path, loop_count, 0.0,
6640 indexStartupCost, indexTotalCost,
6641 indexSelectivity, indexCorrelation);
6648 * Support routines for gincostestimate
6654 double partialEntries;
6655 double exactEntries;
6656 double searchEntries;
6660 /* Find the index column matching "op"; return its index, or -1 if no match */
6662 find_index_column(Node *op, IndexOptInfo *index)
6666 for (i = 0; i < index->ncolumns; i++)
6668 if (match_index_to_operand(op, i, index))
6676 * Estimate the number of index terms that need to be searched for while
6677 * testing the given GIN query, and increment the counts in *counts
6678 * appropriately. If the query is unsatisfiable, return false.
6681 gincost_pattern(IndexOptInfo *index, int indexcol,
6682 Oid clause_op, Datum query,
6683 GinQualCounts *counts)
6690 bool *partial_matches = NULL;
6691 Pointer *extra_data = NULL;
6692 bool *nullFlags = NULL;
6693 int32 searchMode = GIN_SEARCH_MODE_DEFAULT;
6697 * Get the operator's strategy number and declared input data types
6698 * within the index opfamily. (We don't need the latter, but we use
6699 * get_op_opfamily_properties because it will throw error if it fails
6700 * to find a matching pg_amop entry.)
6702 get_op_opfamily_properties(clause_op, index->opfamily[indexcol], false,
6703 &strategy_op, &lefttype, &righttype);
6706 * GIN always uses the "default" support functions, which are those
6707 * with lefttype == righttype == the opclass' opcintype (see
6708 * IndexSupportInitialize in relcache.c).
6710 extractProcOid = get_opfamily_proc(index->opfamily[indexcol],
6711 index->opcintype[indexcol],
6712 index->opcintype[indexcol],
6713 GIN_EXTRACTQUERY_PROC);
6715 if (!OidIsValid(extractProcOid))
6717 /* should not happen; throw same error as index_getprocinfo */
6718 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
6719 GIN_EXTRACTQUERY_PROC, indexcol + 1,
6720 get_rel_name(index->indexoid));
6723 OidFunctionCall7(extractProcOid,
6725 PointerGetDatum(&nentries),
6726 UInt16GetDatum(strategy_op),
6727 PointerGetDatum(&partial_matches),
6728 PointerGetDatum(&extra_data),
6729 PointerGetDatum(&nullFlags),
6730 PointerGetDatum(&searchMode));
6732 if (nentries <= 0 && searchMode == GIN_SEARCH_MODE_DEFAULT)
6734 /* No match is possible */
6738 for (i = 0; i < nentries; i++)
6741 * For partial match we haven't any information to estimate number of
6742 * matched entries in index, so, we just estimate it as 100
6744 if (partial_matches && partial_matches[i])
6745 counts->partialEntries += 100;
6747 counts->exactEntries++;
6749 counts->searchEntries++;
6752 if (searchMode == GIN_SEARCH_MODE_INCLUDE_EMPTY)
6754 /* Treat "include empty" like an exact-match item */
6755 counts->exactEntries++;
6756 counts->searchEntries++;
6758 else if (searchMode != GIN_SEARCH_MODE_DEFAULT)
6760 /* It's GIN_SEARCH_MODE_ALL */
6761 counts->haveFullScan = true;
6768 * Estimate the number of index terms that need to be searched for while
6769 * testing the given GIN index clause, and increment the counts in *counts
6770 * appropriately. If the query is unsatisfiable, return false.
6773 gincost_opexpr(IndexOptInfo *index, OpExpr *clause, GinQualCounts *counts)
6775 Node *leftop = get_leftop((Expr *) clause);
6776 Node *rightop = get_rightop((Expr *) clause);
6777 Oid clause_op = clause->opno;
6781 /* Locate the operand being compared to the index column */
6782 if ((indexcol = find_index_column(leftop, index)) >= 0)
6786 else if ((indexcol = find_index_column(rightop, index)) >= 0)
6789 clause_op = get_commutator(clause_op);
6793 elog(ERROR, "could not match index to operand");
6794 operand = NULL; /* keep compiler quiet */
6797 if (IsA(operand, RelabelType))
6798 operand = (Node *) ((RelabelType *) operand)->arg;
6801 * It's impossible to call extractQuery method for unknown operand. So
6802 * unless operand is a Const we can't do much; just assume there will
6803 * be one ordinary search entry from the operand at runtime.
6805 if (!IsA(operand, Const))
6807 counts->exactEntries++;
6808 counts->searchEntries++;
6812 /* If Const is null, there can be no matches */
6813 if (((Const *) operand)->constisnull)
6816 /* Otherwise, apply extractQuery and get the actual term counts */
6817 return gincost_pattern(index, indexcol, clause_op,
6818 ((Const *) operand)->constvalue,
6823 * Estimate the number of index terms that need to be searched for while
6824 * testing the given GIN index clause, and increment the counts in *counts
6825 * appropriately. If the query is unsatisfiable, return false.
6827 * A ScalarArrayOpExpr will give rise to N separate indexscans at runtime,
6828 * each of which involves one value from the RHS array, plus all the
6829 * non-array quals (if any). To model this, we average the counts across
6830 * the RHS elements, and add the averages to the counts in *counts (which
6831 * correspond to per-indexscan costs). We also multiply counts->arrayScans
6832 * by N, causing gincostestimate to scale up its estimates accordingly.
6835 gincost_scalararrayopexpr(IndexOptInfo *index, ScalarArrayOpExpr *clause,
6836 double numIndexEntries,
6837 GinQualCounts *counts)
6839 Node *leftop = (Node *) linitial(clause->args);
6840 Node *rightop = (Node *) lsecond(clause->args);
6841 Oid clause_op = clause->opno;
6843 ArrayType *arrayval;
6850 GinQualCounts arraycounts;
6851 int numPossible = 0;
6854 Assert(clause->useOr);
6856 /* index column must be on the left */
6857 if ((indexcol = find_index_column(leftop, index)) < 0)
6858 elog(ERROR, "could not match index to operand");
6860 if (IsA(rightop, RelabelType))
6861 rightop = (Node *) ((RelabelType *) rightop)->arg;
6864 * It's impossible to call extractQuery method for unknown operand. So
6865 * unless operand is a Const we can't do much; just assume there will
6866 * be one ordinary search entry from each array entry at runtime, and
6867 * fall back on a probably-bad estimate of the number of array entries.
6869 if (!IsA(rightop, Const))
6871 counts->exactEntries++;
6872 counts->searchEntries++;
6873 counts->arrayScans *= estimate_array_length(rightop);
6877 /* If Const is null, there can be no matches */
6878 if (((Const *) rightop)->constisnull)
6881 /* Otherwise, extract the array elements and iterate over them */
6882 arrayval = DatumGetArrayTypeP(((Const *) rightop)->constvalue);
6883 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
6884 &elmlen, &elmbyval, &elmalign);
6885 deconstruct_array(arrayval,
6886 ARR_ELEMTYPE(arrayval),
6887 elmlen, elmbyval, elmalign,
6888 &elemValues, &elemNulls, &numElems);
6890 memset(&arraycounts, 0, sizeof(arraycounts));
6892 for (i = 0; i < numElems; i++)
6894 GinQualCounts elemcounts;
6896 /* NULL can't match anything, so ignore, as the executor will */
6900 /* Otherwise, apply extractQuery and get the actual term counts */
6901 memset(&elemcounts, 0, sizeof(elemcounts));
6903 if (gincost_pattern(index, indexcol, clause_op, elemValues[i],
6906 /* We ignore array elements that are unsatisfiable patterns */
6909 if (elemcounts.haveFullScan)
6912 * Full index scan will be required. We treat this as if
6913 * every key in the index had been listed in the query; is
6916 elemcounts.partialEntries = 0;
6917 elemcounts.exactEntries = numIndexEntries;
6918 elemcounts.searchEntries = numIndexEntries;
6920 arraycounts.partialEntries += elemcounts.partialEntries;
6921 arraycounts.exactEntries += elemcounts.exactEntries;
6922 arraycounts.searchEntries += elemcounts.searchEntries;
6926 if (numPossible == 0)
6928 /* No satisfiable patterns in the array */
6933 * Now add the averages to the global counts. This will give us an
6934 * estimate of the average number of terms searched for in each indexscan,
6935 * including contributions from both array and non-array quals.
6937 counts->partialEntries += arraycounts.partialEntries / numPossible;
6938 counts->exactEntries += arraycounts.exactEntries / numPossible;
6939 counts->searchEntries += arraycounts.searchEntries / numPossible;
6941 counts->arrayScans *= numPossible;
6947 * GIN has search behavior completely different from other index types
6950 gincostestimate(PG_FUNCTION_ARGS)
6952 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
6953 IndexPath *path = (IndexPath *) PG_GETARG_POINTER(1);
6954 double loop_count = PG_GETARG_FLOAT8(2);
6955 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
6956 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
6957 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
6958 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
6959 IndexOptInfo *index = path->indexinfo;
6960 List *indexQuals = path->indexquals;
6961 List *indexOrderBys = path->indexorderbys;
6963 List *selectivityQuals;
6964 double numPages = index->pages,
6965 numTuples = index->tuples;
6966 double numEntryPages,
6970 GinQualCounts counts;
6972 double entryPagesFetched,
6974 dataPagesFetchedBySel;
6975 double qual_op_cost,
6977 spc_random_page_cost,
6979 QualCost index_qual_cost;
6981 GinStatsData ginStats;
6984 * Obtain statistic information from the meta page
6986 indexRel = index_open(index->indexoid, AccessShareLock);
6987 ginGetStats(indexRel, &ginStats);
6988 index_close(indexRel, AccessShareLock);
6990 numEntryPages = ginStats.nEntryPages;
6991 numDataPages = ginStats.nDataPages;
6992 numPendingPages = ginStats.nPendingPages;
6993 numEntries = ginStats.nEntries;
6996 * nPendingPages can be trusted, but the other fields are as of the last
6997 * VACUUM. Scale them by the ratio numPages / nTotalPages to account for
6998 * growth since then. If the fields are zero (implying no VACUUM at all,
6999 * and an index created pre-9.1), assume all pages are entry pages.
7001 if (ginStats.nTotalPages == 0 || ginStats.nEntryPages == 0)
7003 numEntryPages = numPages;
7005 numEntries = numTuples; /* bogus, but no other info available */
7009 double scale = numPages / ginStats.nTotalPages;
7011 numEntryPages = ceil(numEntryPages * scale);
7012 numDataPages = ceil(numDataPages * scale);
7013 numEntries = ceil(numEntries * scale);
7014 /* ensure we didn't round up too much */
7015 numEntryPages = Min(numEntryPages, numPages);
7016 numDataPages = Min(numDataPages, numPages - numEntryPages);
7019 /* In an empty index, numEntries could be zero. Avoid divide-by-zero */
7024 * Include predicate in selectivityQuals (should match
7025 * genericcostestimate)
7027 if (index->indpred != NIL)
7029 List *predExtraQuals = NIL;
7031 foreach(l, index->indpred)
7033 Node *predQual = (Node *) lfirst(l);
7034 List *oneQual = list_make1(predQual);
7036 if (!predicate_implied_by(oneQual, indexQuals))
7037 predExtraQuals = list_concat(predExtraQuals, oneQual);
7039 /* list_concat avoids modifying the passed-in indexQuals list */
7040 selectivityQuals = list_concat(predExtraQuals, indexQuals);
7043 selectivityQuals = indexQuals;
7045 /* Estimate the fraction of main-table tuples that will be visited */
7046 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
7051 /* fetch estimated page cost for schema containing index */
7052 get_tablespace_page_costs(index->reltablespace,
7053 &spc_random_page_cost,
7057 * Generic assumption about index correlation: there isn't any.
7059 *indexCorrelation = 0.0;
7062 * Examine quals to estimate number of search entries & partial matches
7064 memset(&counts, 0, sizeof(counts));
7065 counts.arrayScans = 1;
7066 matchPossible = true;
7068 foreach(l, indexQuals)
7070 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
7073 Assert(IsA(rinfo, RestrictInfo));
7074 clause = rinfo->clause;
7075 if (IsA(clause, OpExpr))
7077 matchPossible = gincost_opexpr(index,
7083 else if (IsA(clause, ScalarArrayOpExpr))
7085 matchPossible = gincost_scalararrayopexpr(index,
7086 (ScalarArrayOpExpr *) clause,
7094 /* shouldn't be anything else for a GIN index */
7095 elog(ERROR, "unsupported GIN indexqual type: %d",
7096 (int) nodeTag(clause));
7100 /* Fall out if there were any provably-unsatisfiable quals */
7103 *indexStartupCost = 0;
7104 *indexTotalCost = 0;
7105 *indexSelectivity = 0;
7109 if (counts.haveFullScan || indexQuals == NIL)
7112 * Full index scan will be required. We treat this as if every key in
7113 * the index had been listed in the query; is that reasonable?
7115 counts.partialEntries = 0;
7116 counts.exactEntries = numEntries;
7117 counts.searchEntries = numEntries;
7120 /* Will we have more than one iteration of a nestloop scan? */
7121 outer_scans = loop_count;
7124 * Compute cost to begin scan, first of all, pay attention to pending list.
7126 entryPagesFetched = numPendingPages;
7129 * Estimate number of entry pages read. We need to do
7130 * counts.searchEntries searches. Use a power function as it should be,
7131 * but tuples on leaf pages usually is much greater. Here we include all
7132 * searches in entry tree, including search of first entry in partial
7135 entryPagesFetched += ceil(counts.searchEntries * rint(pow(numEntryPages, 0.15)));
7138 * Add an estimate of entry pages read by partial match algorithm. It's a
7139 * scan over leaf pages in entry tree. We haven't any useful stats here,
7140 * so estimate it as proportion.
7142 entryPagesFetched += ceil(numEntryPages * counts.partialEntries / numEntries);
7145 * Partial match algorithm reads all data pages before doing actual scan,
7146 * so it's a startup cost. Again, we haven't any useful stats here, so,
7147 * estimate it as proportion
7149 dataPagesFetched = ceil(numDataPages * counts.partialEntries / numEntries);
7152 * Calculate cache effects if more than one scan due to nestloops or array
7153 * quals. The result is pro-rated per nestloop scan, but the array qual
7154 * factor shouldn't be pro-rated (compare genericcostestimate).
7156 if (outer_scans > 1 || counts.arrayScans > 1)
7158 entryPagesFetched *= outer_scans * counts.arrayScans;
7159 entryPagesFetched = index_pages_fetched(entryPagesFetched,
7160 (BlockNumber) numEntryPages,
7161 numEntryPages, root);
7162 entryPagesFetched /= outer_scans;
7163 dataPagesFetched *= outer_scans * counts.arrayScans;
7164 dataPagesFetched = index_pages_fetched(dataPagesFetched,
7165 (BlockNumber) numDataPages,
7166 numDataPages, root);
7167 dataPagesFetched /= outer_scans;
7171 * Here we use random page cost because logically-close pages could be far
7174 *indexStartupCost = (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
7177 * Now we compute the number of data pages fetched while the scan proceeds.
7180 /* data pages scanned for each exact (non-partial) matched entry */
7181 dataPagesFetched = ceil(numDataPages * counts.exactEntries / numEntries);
7184 * Estimate number of data pages read, using selectivity estimation and
7185 * capacity of data page.
7187 dataPagesFetchedBySel = ceil(*indexSelectivity *
7188 (numTuples / (BLCKSZ / SizeOfIptrData)));
7190 if (dataPagesFetchedBySel > dataPagesFetched)
7193 * At least one of entries is very frequent and, unfortunately, we
7194 * couldn't get statistic about entries (only tsvector has such
7195 * statistics). So, we obviously have too small estimation of pages
7196 * fetched from data tree. Re-estimate it from known capacity of data
7199 dataPagesFetched = dataPagesFetchedBySel;
7202 /* Account for cache effects, the same as above */
7203 if (outer_scans > 1 || counts.arrayScans > 1)
7205 dataPagesFetched *= outer_scans * counts.arrayScans;
7206 dataPagesFetched = index_pages_fetched(dataPagesFetched,
7207 (BlockNumber) numDataPages,
7208 numDataPages, root);
7209 dataPagesFetched /= outer_scans;
7212 /* And apply random_page_cost as the cost per page */
7213 *indexTotalCost = *indexStartupCost +
7214 dataPagesFetched * spc_random_page_cost;
7217 * Add on index qual eval costs, much as in genericcostestimate
7219 cost_qual_eval(&index_qual_cost, indexQuals, root);
7220 qual_arg_cost = index_qual_cost.startup + index_qual_cost.per_tuple;
7221 cost_qual_eval(&index_qual_cost, indexOrderBys, root);
7222 qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
7223 qual_op_cost = cpu_operator_cost *
7224 (list_length(indexQuals) + list_length(indexOrderBys));
7225 qual_arg_cost -= qual_op_cost;
7226 if (qual_arg_cost < 0) /* just in case... */
7229 *indexStartupCost += qual_arg_cost;
7230 *indexTotalCost += qual_arg_cost;
7231 *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost + qual_op_cost);