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 located via the index AM's API struct,
11 * which is obtained from the handler function registered in pg_am.
13 * Portions Copyright (c) 1996-2017, 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.
87 * For both oprrest and oprjoin functions, the operator's input collation OID
88 * (if any) is passed using the standard fmgr mechanism, so that the estimator
89 * function can fetch it with PG_GET_COLLATION(). Note, however, that all
90 * statistics in pg_statistic are currently built using the database's default
91 * collation. Thus, in most cases where we are looking at statistics, we
92 * should ignore the actual operator collation and use DEFAULT_COLLATION_OID.
93 * We expect that the error induced by doing this is usually not large enough
94 * to justify complicating matters.
104 #include "access/brin.h"
105 #include "access/gin.h"
106 #include "access/htup_details.h"
107 #include "access/sysattr.h"
108 #include "catalog/index.h"
109 #include "catalog/pg_am.h"
110 #include "catalog/pg_collation.h"
111 #include "catalog/pg_operator.h"
112 #include "catalog/pg_opfamily.h"
113 #include "catalog/pg_statistic.h"
114 #include "catalog/pg_statistic_ext.h"
115 #include "catalog/pg_type.h"
116 #include "executor/executor.h"
117 #include "mb/pg_wchar.h"
118 #include "nodes/makefuncs.h"
119 #include "nodes/nodeFuncs.h"
120 #include "optimizer/clauses.h"
121 #include "optimizer/cost.h"
122 #include "optimizer/pathnode.h"
123 #include "optimizer/paths.h"
124 #include "optimizer/plancat.h"
125 #include "optimizer/predtest.h"
126 #include "optimizer/restrictinfo.h"
127 #include "optimizer/var.h"
128 #include "parser/parse_clause.h"
129 #include "parser/parse_coerce.h"
130 #include "parser/parsetree.h"
131 #include "statistics/statistics.h"
132 #include "utils/builtins.h"
133 #include "utils/bytea.h"
134 #include "utils/date.h"
135 #include "utils/datum.h"
136 #include "utils/fmgroids.h"
137 #include "utils/index_selfuncs.h"
138 #include "utils/lsyscache.h"
139 #include "utils/nabstime.h"
140 #include "utils/pg_locale.h"
141 #include "utils/rel.h"
142 #include "utils/selfuncs.h"
143 #include "utils/spccache.h"
144 #include "utils/syscache.h"
145 #include "utils/timestamp.h"
146 #include "utils/tqual.h"
147 #include "utils/typcache.h"
148 #include "utils/varlena.h"
151 /* Hooks for plugins to get control when we ask for stats */
152 get_relation_stats_hook_type get_relation_stats_hook = NULL;
153 get_index_stats_hook_type get_index_stats_hook = NULL;
155 static double var_eq_const(VariableStatData *vardata, Oid operator,
156 Datum constval, bool constisnull,
158 static double var_eq_non_const(VariableStatData *vardata, Oid operator,
161 static double ineq_histogram_selectivity(PlannerInfo *root,
162 VariableStatData *vardata,
163 FmgrInfo *opproc, bool isgt,
164 Datum constval, Oid consttype);
165 static double eqjoinsel_inner(Oid operator,
166 VariableStatData *vardata1, VariableStatData *vardata2);
167 static double eqjoinsel_semi(Oid operator,
168 VariableStatData *vardata1, VariableStatData *vardata2,
169 RelOptInfo *inner_rel);
170 static bool estimate_multivariate_ndistinct(PlannerInfo *root,
171 RelOptInfo *rel, List **varinfos, double *ndistinct);
172 static bool convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
173 Datum lobound, Datum hibound, Oid boundstypid,
174 double *scaledlobound, double *scaledhibound);
175 static double convert_numeric_to_scalar(Datum value, Oid typid);
176 static void convert_string_to_scalar(char *value,
179 double *scaledlobound,
181 double *scaledhibound);
182 static void convert_bytea_to_scalar(Datum value,
185 double *scaledlobound,
187 double *scaledhibound);
188 static double convert_one_string_to_scalar(char *value,
189 int rangelo, int rangehi);
190 static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
191 int rangelo, int rangehi);
192 static char *convert_string_datum(Datum value, Oid typid);
193 static double convert_timevalue_to_scalar(Datum value, Oid typid);
194 static void examine_simple_variable(PlannerInfo *root, Var *var,
195 VariableStatData *vardata);
196 static bool get_variable_range(PlannerInfo *root, VariableStatData *vardata,
197 Oid sortop, Datum *min, Datum *max);
198 static bool get_actual_variable_range(PlannerInfo *root,
199 VariableStatData *vardata,
201 Datum *min, Datum *max);
202 static RelOptInfo *find_join_input_rel(PlannerInfo *root, Relids relids);
203 static Selectivity prefix_selectivity(PlannerInfo *root,
204 VariableStatData *vardata,
205 Oid vartype, Oid opfamily, Const *prefixcon);
206 static Selectivity like_selectivity(const char *patt, int pattlen,
207 bool case_insensitive);
208 static Selectivity regex_selectivity(const char *patt, int pattlen,
209 bool case_insensitive,
210 int fixed_prefix_len);
211 static Datum string_to_datum(const char *str, Oid datatype);
212 static Const *string_to_const(const char *str, Oid datatype);
213 static Const *string_to_bytea_const(const char *str, size_t str_len);
214 static List *add_predicate_to_quals(IndexOptInfo *index, List *indexQuals);
218 * eqsel - Selectivity of "=" for any data types.
220 * Note: this routine is also used to estimate selectivity for some
221 * operators that are not "=" but have comparable selectivity behavior,
222 * such as "~=" (geometric approximate-match). Even for "=", we must
223 * keep in mind that the left and right datatypes may differ.
226 eqsel(PG_FUNCTION_ARGS)
228 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
229 Oid operator = PG_GETARG_OID(1);
230 List *args = (List *) PG_GETARG_POINTER(2);
231 int varRelid = PG_GETARG_INT32(3);
232 VariableStatData vardata;
238 * If expression is not variable = something or something = variable, then
239 * punt and return a default estimate.
241 if (!get_restriction_variable(root, args, varRelid,
242 &vardata, &other, &varonleft))
243 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
246 * We can do a lot better if the something is a constant. (Note: the
247 * Const might result from estimation rather than being a simple constant
250 if (IsA(other, Const))
251 selec = var_eq_const(&vardata, operator,
252 ((Const *) other)->constvalue,
253 ((Const *) other)->constisnull,
256 selec = var_eq_non_const(&vardata, operator, other,
259 ReleaseVariableStats(vardata);
261 PG_RETURN_FLOAT8((float8) selec);
265 * var_eq_const --- eqsel for var = const case
267 * This is split out so that some other estimation functions can use it.
270 var_eq_const(VariableStatData *vardata, Oid operator,
271 Datum constval, bool constisnull,
278 * If the constant is NULL, assume operator is strict and return zero, ie,
279 * operator will never return TRUE.
285 * If we matched the var to a unique index or DISTINCT clause, assume
286 * there is exactly one match regardless of anything else. (This is
287 * slightly bogus, since the index or clause's equality operator might be
288 * different from ours, but it's much more likely to be right than
289 * ignoring the information.)
291 if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
292 return 1.0 / vardata->rel->tuples;
294 if (HeapTupleIsValid(vardata->statsTuple))
296 Form_pg_statistic stats;
304 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
307 * Is the constant "=" to any of the column's most common values?
308 * (Although the given operator may not really be "=", we will assume
309 * that seeing whether it returns TRUE is an appropriate test. If you
310 * don't like this, maybe you shouldn't be using eqsel for your
313 if (get_attstatsslot(vardata->statsTuple,
314 vardata->atttype, vardata->atttypmod,
315 STATISTIC_KIND_MCV, InvalidOid,
318 &numbers, &nnumbers))
322 fmgr_info(get_opcode(operator), &eqproc);
324 for (i = 0; i < nvalues; i++)
326 /* be careful to apply operator right way 'round */
328 match = DatumGetBool(FunctionCall2Coll(&eqproc,
329 DEFAULT_COLLATION_OID,
333 match = DatumGetBool(FunctionCall2Coll(&eqproc,
334 DEFAULT_COLLATION_OID,
343 /* no most-common-value info available */
346 i = nvalues = nnumbers = 0;
352 * Constant is "=" to this common value. We know selectivity
353 * exactly (or as exactly as ANALYZE could calculate it, anyway).
360 * Comparison is against a constant that is neither NULL nor any
361 * of the common values. Its selectivity cannot be more than
364 double sumcommon = 0.0;
365 double otherdistinct;
367 for (i = 0; i < nnumbers; i++)
368 sumcommon += numbers[i];
369 selec = 1.0 - sumcommon - stats->stanullfrac;
370 CLAMP_PROBABILITY(selec);
373 * and in fact it's probably a good deal less. We approximate that
374 * all the not-common values share this remaining fraction
375 * equally, so we divide by the number of other distinct values.
377 otherdistinct = get_variable_numdistinct(vardata, &isdefault) - nnumbers;
378 if (otherdistinct > 1)
379 selec /= otherdistinct;
382 * Another cross-check: selectivity shouldn't be estimated as more
383 * than the least common "most common value".
385 if (nnumbers > 0 && selec > numbers[nnumbers - 1])
386 selec = numbers[nnumbers - 1];
389 free_attstatsslot(vardata->atttype, values, nvalues,
395 * No ANALYZE stats available, so make a guess using estimated number
396 * of distinct values and assuming they are equally common. (The guess
397 * is unlikely to be very good, but we do know a few special cases.)
399 selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
402 /* result should be in range, but make sure... */
403 CLAMP_PROBABILITY(selec);
409 * var_eq_non_const --- eqsel for var = something-other-than-const case
412 var_eq_non_const(VariableStatData *vardata, Oid operator,
420 * If we matched the var to a unique index or DISTINCT clause, assume
421 * there is exactly one match regardless of anything else. (This is
422 * slightly bogus, since the index or clause's equality operator might be
423 * different from ours, but it's much more likely to be right than
424 * ignoring the information.)
426 if (vardata->isunique && vardata->rel && vardata->rel->tuples >= 1.0)
427 return 1.0 / vardata->rel->tuples;
429 if (HeapTupleIsValid(vardata->statsTuple))
431 Form_pg_statistic stats;
436 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
439 * Search is for a value that we do not know a priori, but we will
440 * assume it is not NULL. Estimate the selectivity as non-null
441 * fraction divided by number of distinct values, so that we get a
442 * result averaged over all possible values whether common or
443 * uncommon. (Essentially, we are assuming that the not-yet-known
444 * comparison value is equally likely to be any of the possible
445 * values, regardless of their frequency in the table. Is that a good
448 selec = 1.0 - stats->stanullfrac;
449 ndistinct = get_variable_numdistinct(vardata, &isdefault);
454 * Cross-check: selectivity should never be estimated as more than the
455 * most common value's.
457 if (get_attstatsslot(vardata->statsTuple,
458 vardata->atttype, vardata->atttypmod,
459 STATISTIC_KIND_MCV, InvalidOid,
462 &numbers, &nnumbers))
464 if (nnumbers > 0 && selec > numbers[0])
466 free_attstatsslot(vardata->atttype, NULL, 0, numbers, nnumbers);
472 * No ANALYZE stats available, so make a guess using estimated number
473 * of distinct values and assuming they are equally common. (The guess
474 * is unlikely to be very good, but we do know a few special cases.)
476 selec = 1.0 / get_variable_numdistinct(vardata, &isdefault);
479 /* result should be in range, but make sure... */
480 CLAMP_PROBABILITY(selec);
486 * neqsel - Selectivity of "!=" for any data types.
488 * This routine is also used for some operators that are not "!="
489 * but have comparable selectivity behavior. See above comments
493 neqsel(PG_FUNCTION_ARGS)
495 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
496 Oid operator = PG_GETARG_OID(1);
497 List *args = (List *) PG_GETARG_POINTER(2);
498 int varRelid = PG_GETARG_INT32(3);
503 * We want 1 - eqsel() where the equality operator is the one associated
504 * with this != operator, that is, its negator.
506 eqop = get_negator(operator);
509 result = DatumGetFloat8(DirectFunctionCall4(eqsel,
510 PointerGetDatum(root),
511 ObjectIdGetDatum(eqop),
512 PointerGetDatum(args),
513 Int32GetDatum(varRelid)));
517 /* Use default selectivity (should we raise an error instead?) */
518 result = DEFAULT_EQ_SEL;
520 result = 1.0 - result;
521 PG_RETURN_FLOAT8(result);
525 * scalarineqsel - Selectivity of "<", "<=", ">", ">=" for scalars.
527 * This is the guts of both scalarltsel and scalargtsel. The caller has
528 * commuted the clause, if necessary, so that we can treat the variable as
529 * being on the left. The caller must also make sure that the other side
530 * of the clause is a non-null Const, and dissect same into a value and
533 * This routine works for any datatype (or pair of datatypes) known to
534 * convert_to_scalar(). If it is applied to some other datatype,
535 * it will return a default estimate.
538 scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
539 VariableStatData *vardata, Datum constval, Oid consttype)
541 Form_pg_statistic stats;
548 if (!HeapTupleIsValid(vardata->statsTuple))
550 /* no stats available, so default result */
551 return DEFAULT_INEQ_SEL;
553 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
555 fmgr_info(get_opcode(operator), &opproc);
558 * If we have most-common-values info, add up the fractions of the MCV
559 * entries that satisfy MCV OP CONST. These fractions contribute directly
560 * to the result selectivity. Also add up the total fraction represented
563 mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
567 * If there is a histogram, determine which bin the constant falls in, and
568 * compute the resulting contribution to selectivity.
570 hist_selec = ineq_histogram_selectivity(root, vardata, &opproc, isgt,
571 constval, consttype);
574 * Now merge the results from the MCV and histogram calculations,
575 * realizing that the histogram covers only the non-null values that are
578 selec = 1.0 - stats->stanullfrac - sumcommon;
580 if (hist_selec >= 0.0)
585 * If no histogram but there are values not accounted for by MCV,
586 * arbitrarily assume half of them will match.
593 /* result should be in range, but make sure... */
594 CLAMP_PROBABILITY(selec);
600 * mcv_selectivity - Examine the MCV list for selectivity estimates
602 * Determine the fraction of the variable's MCV population that satisfies
603 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
604 * compute the fraction of the total column population represented by the MCV
605 * list. This code will work for any boolean-returning predicate operator.
607 * The function result is the MCV selectivity, and the fraction of the
608 * total population is returned into *sumcommonp. Zeroes are returned
609 * if there is no MCV list.
612 mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
613 Datum constval, bool varonleft,
627 if (HeapTupleIsValid(vardata->statsTuple) &&
628 get_attstatsslot(vardata->statsTuple,
629 vardata->atttype, vardata->atttypmod,
630 STATISTIC_KIND_MCV, InvalidOid,
633 &numbers, &nnumbers))
635 for (i = 0; i < nvalues; i++)
638 DatumGetBool(FunctionCall2Coll(opproc,
639 DEFAULT_COLLATION_OID,
642 DatumGetBool(FunctionCall2Coll(opproc,
643 DEFAULT_COLLATION_OID,
646 mcv_selec += numbers[i];
647 sumcommon += numbers[i];
649 free_attstatsslot(vardata->atttype, values, nvalues,
653 *sumcommonp = sumcommon;
658 * histogram_selectivity - Examine the histogram for selectivity estimates
660 * Determine the fraction of the variable's histogram entries that satisfy
661 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
663 * This code will work for any boolean-returning predicate operator, whether
664 * or not it has anything to do with the histogram sort operator. We are
665 * essentially using the histogram just as a representative sample. However,
666 * small histograms are unlikely to be all that representative, so the caller
667 * should be prepared to fall back on some other estimation approach when the
668 * histogram is missing or very small. It may also be prudent to combine this
669 * approach with another one when the histogram is small.
671 * If the actual histogram size is not at least min_hist_size, we won't bother
672 * to do the calculation at all. Also, if the n_skip parameter is > 0, we
673 * ignore the first and last n_skip histogram elements, on the grounds that
674 * they are outliers and hence not very representative. Typical values for
675 * these parameters are 10 and 1.
677 * The function result is the selectivity, or -1 if there is no histogram
678 * or it's smaller than min_hist_size.
680 * The output parameter *hist_size receives the actual histogram size,
681 * or zero if no histogram. Callers may use this number to decide how
682 * much faith to put in the function result.
684 * Note that the result disregards both the most-common-values (if any) and
685 * null entries. The caller is expected to combine this result with
686 * statistics for those portions of the column population. It may also be
687 * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
690 histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
691 Datum constval, bool varonleft,
692 int min_hist_size, int n_skip,
699 /* check sanity of parameters */
701 Assert(min_hist_size > 2 * n_skip);
703 if (HeapTupleIsValid(vardata->statsTuple) &&
704 get_attstatsslot(vardata->statsTuple,
705 vardata->atttype, vardata->atttypmod,
706 STATISTIC_KIND_HISTOGRAM, InvalidOid,
711 *hist_size = nvalues;
712 if (nvalues >= min_hist_size)
717 for (i = n_skip; i < nvalues - n_skip; i++)
720 DatumGetBool(FunctionCall2Coll(opproc,
721 DEFAULT_COLLATION_OID,
724 DatumGetBool(FunctionCall2Coll(opproc,
725 DEFAULT_COLLATION_OID,
730 result = ((double) nmatch) / ((double) (nvalues - 2 * n_skip));
734 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
746 * ineq_histogram_selectivity - Examine the histogram for scalarineqsel
748 * Determine the fraction of the variable's histogram population that
749 * satisfies the inequality condition, ie, VAR < CONST or VAR > CONST.
751 * Returns -1 if there is no histogram (valid results will always be >= 0).
753 * Note that the result disregards both the most-common-values (if any) and
754 * null entries. The caller is expected to combine this result with
755 * statistics for those portions of the column population.
758 ineq_histogram_selectivity(PlannerInfo *root,
759 VariableStatData *vardata,
760 FmgrInfo *opproc, bool isgt,
761 Datum constval, Oid consttype)
771 * Someday, ANALYZE might store more than one histogram per rel/att,
772 * corresponding to more than one possible sort ordering defined for the
773 * column type. However, to make that work we will need to figure out
774 * which staop to search for --- it's not necessarily the one we have at
775 * hand! (For example, we might have a '<=' operator rather than the '<'
776 * operator that will appear in staop.) For now, assume that whatever
777 * appears in pg_statistic is sorted the same way our operator sorts, or
778 * the reverse way if isgt is TRUE.
780 if (HeapTupleIsValid(vardata->statsTuple) &&
781 get_attstatsslot(vardata->statsTuple,
782 vardata->atttype, vardata->atttypmod,
783 STATISTIC_KIND_HISTOGRAM, InvalidOid,
791 * Use binary search to find proper location, ie, the first slot
792 * at which the comparison fails. (If the given operator isn't
793 * actually sort-compatible with the histogram, you'll get garbage
794 * results ... but probably not any more garbage-y than you would
795 * from the old linear search.)
797 * If the binary search accesses the first or last histogram
798 * entry, we try to replace that endpoint with the true column min
799 * or max as found by get_actual_variable_range(). This
800 * ameliorates misestimates when the min or max is moving as a
801 * result of changes since the last ANALYZE. Note that this could
802 * result in effectively including MCVs into the histogram that
803 * weren't there before, but we don't try to correct for that.
806 int lobound = 0; /* first possible slot to search */
807 int hibound = nvalues; /* last+1 slot to search */
808 bool have_end = false;
811 * If there are only two histogram entries, we'll want up-to-date
812 * values for both. (If there are more than two, we need at most
813 * one of them to be updated, so we deal with that within the
817 have_end = get_actual_variable_range(root,
823 while (lobound < hibound)
825 int probe = (lobound + hibound) / 2;
829 * If we find ourselves about to compare to the first or last
830 * histogram entry, first try to replace it with the actual
831 * current min or max (unless we already did so above).
833 if (probe == 0 && nvalues > 2)
834 have_end = get_actual_variable_range(root,
839 else if (probe == nvalues - 1 && nvalues > 2)
840 have_end = get_actual_variable_range(root,
846 ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
847 DEFAULT_COLLATION_OID,
860 /* Constant is below lower histogram boundary. */
863 else if (lobound >= nvalues)
865 /* Constant is above upper histogram boundary. */
877 * We have values[i-1] <= constant <= values[i].
879 * Convert the constant and the two nearest bin boundary
880 * values to a uniform comparison scale, and do a linear
881 * interpolation within this bin.
883 if (convert_to_scalar(constval, consttype, &val,
884 values[i - 1], values[i],
890 /* cope if bin boundaries appear identical */
895 else if (val >= high)
899 binfrac = (val - low) / (high - low);
902 * Watch out for the possibility that we got a NaN or
903 * Infinity from the division. This can happen
904 * despite the previous checks, if for example "low"
907 if (isnan(binfrac) ||
908 binfrac < 0.0 || binfrac > 1.0)
915 * Ideally we'd produce an error here, on the grounds that
916 * the given operator shouldn't have scalarXXsel
917 * registered as its selectivity func unless we can deal
918 * with its operand types. But currently, all manner of
919 * stuff is invoking scalarXXsel, so give a default
920 * estimate until that can be fixed.
926 * Now, compute the overall selectivity across the values
927 * represented by the histogram. We have i-1 full bins and
928 * binfrac partial bin below the constant.
930 histfrac = (double) (i - 1) + binfrac;
931 histfrac /= (double) (nvalues - 1);
935 * Now histfrac = fraction of histogram entries below the
938 * Account for "<" vs ">"
940 hist_selec = isgt ? (1.0 - histfrac) : histfrac;
943 * The histogram boundaries are only approximate to begin with,
944 * and may well be out of date anyway. Therefore, don't believe
945 * extremely small or large selectivity estimates --- unless we
946 * got actual current endpoint values from the table.
949 CLAMP_PROBABILITY(hist_selec);
952 if (hist_selec < 0.0001)
954 else if (hist_selec > 0.9999)
959 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
966 * scalarltsel - Selectivity of "<" (also "<=") for scalars.
969 scalarltsel(PG_FUNCTION_ARGS)
971 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
972 Oid operator = PG_GETARG_OID(1);
973 List *args = (List *) PG_GETARG_POINTER(2);
974 int varRelid = PG_GETARG_INT32(3);
975 VariableStatData vardata;
984 * If expression is not variable op something or something op variable,
985 * then punt and return a default estimate.
987 if (!get_restriction_variable(root, args, varRelid,
988 &vardata, &other, &varonleft))
989 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
992 * Can't do anything useful if the something is not a constant, either.
994 if (!IsA(other, Const))
996 ReleaseVariableStats(vardata);
997 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1001 * If the constant is NULL, assume operator is strict and return zero, ie,
1002 * operator will never return TRUE.
1004 if (((Const *) other)->constisnull)
1006 ReleaseVariableStats(vardata);
1007 PG_RETURN_FLOAT8(0.0);
1009 constval = ((Const *) other)->constvalue;
1010 consttype = ((Const *) other)->consttype;
1013 * Force the var to be on the left to simplify logic in scalarineqsel.
1017 /* we have var < other */
1022 /* we have other < var, commute to make var > other */
1023 operator = get_commutator(operator);
1026 /* Use default selectivity (should we raise an error instead?) */
1027 ReleaseVariableStats(vardata);
1028 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1033 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
1035 ReleaseVariableStats(vardata);
1037 PG_RETURN_FLOAT8((float8) selec);
1041 * scalargtsel - Selectivity of ">" (also ">=") for integers.
1044 scalargtsel(PG_FUNCTION_ARGS)
1046 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1047 Oid operator = PG_GETARG_OID(1);
1048 List *args = (List *) PG_GETARG_POINTER(2);
1049 int varRelid = PG_GETARG_INT32(3);
1050 VariableStatData vardata;
1059 * If expression is not variable op something or something op variable,
1060 * then punt and return a default estimate.
1062 if (!get_restriction_variable(root, args, varRelid,
1063 &vardata, &other, &varonleft))
1064 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1067 * Can't do anything useful if the something is not a constant, either.
1069 if (!IsA(other, Const))
1071 ReleaseVariableStats(vardata);
1072 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1076 * If the constant is NULL, assume operator is strict and return zero, ie,
1077 * operator will never return TRUE.
1079 if (((Const *) other)->constisnull)
1081 ReleaseVariableStats(vardata);
1082 PG_RETURN_FLOAT8(0.0);
1084 constval = ((Const *) other)->constvalue;
1085 consttype = ((Const *) other)->consttype;
1088 * Force the var to be on the left to simplify logic in scalarineqsel.
1092 /* we have var > other */
1097 /* we have other > var, commute to make var < other */
1098 operator = get_commutator(operator);
1101 /* Use default selectivity (should we raise an error instead?) */
1102 ReleaseVariableStats(vardata);
1103 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1108 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
1110 ReleaseVariableStats(vardata);
1112 PG_RETURN_FLOAT8((float8) selec);
1116 * patternsel - Generic code for pattern-match selectivity.
1119 patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
1121 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1122 Oid operator = PG_GETARG_OID(1);
1123 List *args = (List *) PG_GETARG_POINTER(2);
1124 int varRelid = PG_GETARG_INT32(3);
1125 Oid collation = PG_GET_COLLATION();
1126 VariableStatData vardata;
1133 Pattern_Prefix_Status pstatus;
1135 Const *prefix = NULL;
1136 Selectivity rest_selec = 0;
1140 * If this is for a NOT LIKE or similar operator, get the corresponding
1141 * positive-match operator and work with that. Set result to the correct
1142 * default estimate, too.
1146 operator = get_negator(operator);
1147 if (!OidIsValid(operator))
1148 elog(ERROR, "patternsel called for operator without a negator");
1149 result = 1.0 - DEFAULT_MATCH_SEL;
1153 result = DEFAULT_MATCH_SEL;
1157 * If expression is not variable op constant, then punt and return a
1160 if (!get_restriction_variable(root, args, varRelid,
1161 &vardata, &other, &varonleft))
1163 if (!varonleft || !IsA(other, Const))
1165 ReleaseVariableStats(vardata);
1170 * If the constant is NULL, assume operator is strict and return zero, ie,
1171 * operator will never return TRUE. (It's zero even for a negator op.)
1173 if (((Const *) other)->constisnull)
1175 ReleaseVariableStats(vardata);
1178 constval = ((Const *) other)->constvalue;
1179 consttype = ((Const *) other)->consttype;
1182 * The right-hand const is type text or bytea for all supported operators.
1183 * We do not expect to see binary-compatible types here, since
1184 * const-folding should have relabeled the const to exactly match the
1185 * operator's declared type.
1187 if (consttype != TEXTOID && consttype != BYTEAOID)
1189 ReleaseVariableStats(vardata);
1194 * Similarly, the exposed type of the left-hand side should be one of
1195 * those we know. (Do not look at vardata.atttype, which might be
1196 * something binary-compatible but different.) We can use it to choose
1197 * the index opfamily from which we must draw the comparison operators.
1199 * NOTE: It would be more correct to use the PATTERN opfamilies than the
1200 * simple ones, but at the moment ANALYZE will not generate statistics for
1201 * the PATTERN operators. But our results are so approximate anyway that
1202 * it probably hardly matters.
1204 vartype = vardata.vartype;
1209 opfamily = TEXT_BTREE_FAM_OID;
1212 opfamily = BPCHAR_BTREE_FAM_OID;
1215 opfamily = NAME_BTREE_FAM_OID;
1218 opfamily = BYTEA_BTREE_FAM_OID;
1221 ReleaseVariableStats(vardata);
1226 * Pull out any fixed prefix implied by the pattern, and estimate the
1227 * fractional selectivity of the remainder of the pattern. Unlike many of
1228 * the other functions in this file, we use the pattern operator's actual
1229 * collation for this step. This is not because we expect the collation
1230 * to make a big difference in the selectivity estimate (it seldom would),
1231 * but because we want to be sure we cache compiled regexps under the
1232 * right cache key, so that they can be re-used at runtime.
1234 patt = (Const *) other;
1235 pstatus = pattern_fixed_prefix(patt, ptype, collation,
1236 &prefix, &rest_selec);
1239 * If necessary, coerce the prefix constant to the right type.
1241 if (prefix && prefix->consttype != vartype)
1245 switch (prefix->consttype)
1248 prefixstr = TextDatumGetCString(prefix->constvalue);
1251 prefixstr = DatumGetCString(DirectFunctionCall1(byteaout,
1252 prefix->constvalue));
1255 elog(ERROR, "unrecognized consttype: %u",
1257 ReleaseVariableStats(vardata);
1260 prefix = string_to_const(prefixstr, vartype);
1264 if (pstatus == Pattern_Prefix_Exact)
1267 * Pattern specifies an exact match, so pretend operator is '='
1269 Oid eqopr = get_opfamily_member(opfamily, vartype, vartype,
1270 BTEqualStrategyNumber);
1272 if (eqopr == InvalidOid)
1273 elog(ERROR, "no = operator for opfamily %u", opfamily);
1274 result = var_eq_const(&vardata, eqopr, prefix->constvalue,
1280 * Not exact-match pattern. If we have a sufficiently large
1281 * histogram, estimate selectivity for the histogram part of the
1282 * population by counting matches in the histogram. If not, estimate
1283 * selectivity of the fixed prefix and remainder of pattern
1284 * separately, then combine the two to get an estimate of the
1285 * selectivity for the part of the column population represented by
1286 * the histogram. (For small histograms, we combine these
1289 * We then add up data for any most-common-values values; these are
1290 * not in the histogram population, and we can get exact answers for
1291 * them by applying the pattern operator, so there's no reason to
1292 * approximate. (If the MCVs cover a significant part of the total
1293 * population, this gives us a big leg up in accuracy.)
1302 /* Try to use the histogram entries to get selectivity */
1303 fmgr_info(get_opcode(operator), &opproc);
1305 selec = histogram_selectivity(&vardata, &opproc, constval, true,
1308 /* If not at least 100 entries, use the heuristic method */
1309 if (hist_size < 100)
1311 Selectivity heursel;
1312 Selectivity prefixsel;
1314 if (pstatus == Pattern_Prefix_Partial)
1315 prefixsel = prefix_selectivity(root, &vardata, vartype,
1319 heursel = prefixsel * rest_selec;
1321 if (selec < 0) /* fewer than 10 histogram entries? */
1326 * For histogram sizes from 10 to 100, we combine the
1327 * histogram and heuristic selectivities, putting increasingly
1328 * more trust in the histogram for larger sizes.
1330 double hist_weight = hist_size / 100.0;
1332 selec = selec * hist_weight + heursel * (1.0 - hist_weight);
1336 /* In any case, don't believe extremely small or large estimates. */
1339 else if (selec > 0.9999)
1343 * If we have most-common-values info, add up the fractions of the MCV
1344 * entries that satisfy MCV OP PATTERN. These fractions contribute
1345 * directly to the result selectivity. Also add up the total fraction
1346 * represented by MCV entries.
1348 mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
1351 if (HeapTupleIsValid(vardata.statsTuple))
1352 nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
1357 * Now merge the results from the MCV and histogram calculations,
1358 * realizing that the histogram covers only the non-null values that
1359 * are not listed in MCV.
1361 selec *= 1.0 - nullfrac - sumcommon;
1364 /* result should be in range, but make sure... */
1365 CLAMP_PROBABILITY(selec);
1371 pfree(DatumGetPointer(prefix->constvalue));
1375 ReleaseVariableStats(vardata);
1377 return negate ? (1.0 - result) : result;
1381 * regexeqsel - Selectivity of regular-expression pattern match.
1384 regexeqsel(PG_FUNCTION_ARGS)
1386 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex, false));
1390 * icregexeqsel - Selectivity of case-insensitive regex match.
1393 icregexeqsel(PG_FUNCTION_ARGS)
1395 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC, false));
1399 * likesel - Selectivity of LIKE pattern match.
1402 likesel(PG_FUNCTION_ARGS)
1404 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like, false));
1408 * iclikesel - Selectivity of ILIKE pattern match.
1411 iclikesel(PG_FUNCTION_ARGS)
1413 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC, false));
1417 * regexnesel - Selectivity of regular-expression pattern non-match.
1420 regexnesel(PG_FUNCTION_ARGS)
1422 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex, true));
1426 * icregexnesel - Selectivity of case-insensitive regex non-match.
1429 icregexnesel(PG_FUNCTION_ARGS)
1431 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC, true));
1435 * nlikesel - Selectivity of LIKE pattern non-match.
1438 nlikesel(PG_FUNCTION_ARGS)
1440 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like, true));
1444 * icnlikesel - Selectivity of ILIKE pattern non-match.
1447 icnlikesel(PG_FUNCTION_ARGS)
1449 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC, true));
1453 * boolvarsel - Selectivity of Boolean variable.
1455 * This can actually be called on any boolean-valued expression. If it
1456 * involves only Vars of the specified relation, and if there are statistics
1457 * about the Var or expression (the latter is possible if it's indexed) then
1458 * we'll produce a real estimate; otherwise it's just a default.
1461 boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
1463 VariableStatData vardata;
1466 examine_variable(root, arg, varRelid, &vardata);
1467 if (HeapTupleIsValid(vardata.statsTuple))
1470 * A boolean variable V is equivalent to the clause V = 't', so we
1471 * compute the selectivity as if that is what we have.
1473 selec = var_eq_const(&vardata, BooleanEqualOperator,
1474 BoolGetDatum(true), false, true);
1476 else if (is_funcclause(arg))
1479 * If we have no stats and it's a function call, estimate 0.3333333.
1480 * This seems a pretty unprincipled choice, but Postgres has been
1481 * using that estimate for function calls since 1992. The hoariness
1482 * of this behavior suggests that we should not be in too much hurry
1483 * to use another value.
1489 /* Otherwise, the default estimate is 0.5 */
1492 ReleaseVariableStats(vardata);
1497 * booltestsel - Selectivity of BooleanTest Node.
1500 booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
1501 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1503 VariableStatData vardata;
1506 examine_variable(root, arg, varRelid, &vardata);
1508 if (HeapTupleIsValid(vardata.statsTuple))
1510 Form_pg_statistic stats;
1517 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1518 freq_null = stats->stanullfrac;
1520 if (get_attstatsslot(vardata.statsTuple,
1521 vardata.atttype, vardata.atttypmod,
1522 STATISTIC_KIND_MCV, InvalidOid,
1525 &numbers, &nnumbers)
1532 * Get first MCV frequency and derive frequency for true.
1534 if (DatumGetBool(values[0]))
1535 freq_true = numbers[0];
1537 freq_true = 1.0 - numbers[0] - freq_null;
1540 * Next derive frequency for false. Then use these as appropriate
1541 * to derive frequency for each case.
1543 freq_false = 1.0 - freq_true - freq_null;
1545 switch (booltesttype)
1548 /* select only NULL values */
1551 case IS_NOT_UNKNOWN:
1552 /* select non-NULL values */
1553 selec = 1.0 - freq_null;
1556 /* select only TRUE values */
1560 /* select non-TRUE values */
1561 selec = 1.0 - freq_true;
1564 /* select only FALSE values */
1568 /* select non-FALSE values */
1569 selec = 1.0 - freq_false;
1572 elog(ERROR, "unrecognized booltesttype: %d",
1573 (int) booltesttype);
1574 selec = 0.0; /* Keep compiler quiet */
1578 free_attstatsslot(vardata.atttype, values, nvalues,
1584 * No most-common-value info available. Still have null fraction
1585 * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1586 * for null fraction and assume a 50-50 split of TRUE and FALSE.
1588 switch (booltesttype)
1591 /* select only NULL values */
1594 case IS_NOT_UNKNOWN:
1595 /* select non-NULL values */
1596 selec = 1.0 - freq_null;
1600 /* Assume we select half of the non-NULL values */
1601 selec = (1.0 - freq_null) / 2.0;
1605 /* Assume we select NULLs plus half of the non-NULLs */
1606 /* equiv. to freq_null + (1.0 - freq_null) / 2.0 */
1607 selec = (freq_null + 1.0) / 2.0;
1610 elog(ERROR, "unrecognized booltesttype: %d",
1611 (int) booltesttype);
1612 selec = 0.0; /* Keep compiler quiet */
1620 * If we can't get variable statistics for the argument, perhaps
1621 * clause_selectivity can do something with it. We ignore the
1622 * possibility of a NULL value when using clause_selectivity, and just
1623 * assume the value is either TRUE or FALSE.
1625 switch (booltesttype)
1628 selec = DEFAULT_UNK_SEL;
1630 case IS_NOT_UNKNOWN:
1631 selec = DEFAULT_NOT_UNK_SEL;
1635 selec = (double) clause_selectivity(root, arg,
1641 selec = 1.0 - (double) clause_selectivity(root, arg,
1646 elog(ERROR, "unrecognized booltesttype: %d",
1647 (int) booltesttype);
1648 selec = 0.0; /* Keep compiler quiet */
1653 ReleaseVariableStats(vardata);
1655 /* result should be in range, but make sure... */
1656 CLAMP_PROBABILITY(selec);
1658 return (Selectivity) selec;
1662 * nulltestsel - Selectivity of NullTest Node.
1665 nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg,
1666 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
1668 VariableStatData vardata;
1671 examine_variable(root, arg, varRelid, &vardata);
1673 if (HeapTupleIsValid(vardata.statsTuple))
1675 Form_pg_statistic stats;
1678 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1679 freq_null = stats->stanullfrac;
1681 switch (nulltesttype)
1686 * Use freq_null directly.
1693 * Select not unknown (not null) values. Calculate from
1696 selec = 1.0 - freq_null;
1699 elog(ERROR, "unrecognized nulltesttype: %d",
1700 (int) nulltesttype);
1701 return (Selectivity) 0; /* keep compiler quiet */
1707 * No ANALYZE stats available, so make a guess
1709 switch (nulltesttype)
1712 selec = DEFAULT_UNK_SEL;
1715 selec = DEFAULT_NOT_UNK_SEL;
1718 elog(ERROR, "unrecognized nulltesttype: %d",
1719 (int) nulltesttype);
1720 return (Selectivity) 0; /* keep compiler quiet */
1724 ReleaseVariableStats(vardata);
1726 /* result should be in range, but make sure... */
1727 CLAMP_PROBABILITY(selec);
1729 return (Selectivity) selec;
1733 * strip_array_coercion - strip binary-compatible relabeling from an array expr
1735 * For array values, the parser normally generates ArrayCoerceExpr conversions,
1736 * but it seems possible that RelabelType might show up. Also, the planner
1737 * is not currently tense about collapsing stacked ArrayCoerceExpr nodes,
1738 * so we need to be ready to deal with more than one level.
1741 strip_array_coercion(Node *node)
1745 if (node && IsA(node, ArrayCoerceExpr) &&
1746 ((ArrayCoerceExpr *) node)->elemfuncid == InvalidOid)
1748 node = (Node *) ((ArrayCoerceExpr *) node)->arg;
1750 else if (node && IsA(node, RelabelType))
1752 /* We don't really expect this case, but may as well cope */
1753 node = (Node *) ((RelabelType *) node)->arg;
1762 * scalararraysel - Selectivity of ScalarArrayOpExpr Node.
1765 scalararraysel(PlannerInfo *root,
1766 ScalarArrayOpExpr *clause,
1767 bool is_join_clause,
1770 SpecialJoinInfo *sjinfo)
1772 Oid operator = clause->opno;
1773 bool useOr = clause->useOr;
1774 bool isEquality = false;
1775 bool isInequality = false;
1778 Oid nominal_element_type;
1779 Oid nominal_element_collation;
1780 TypeCacheEntry *typentry;
1781 RegProcedure oprsel;
1782 FmgrInfo oprselproc;
1784 Selectivity s1disjoint;
1786 /* First, deconstruct the expression */
1787 Assert(list_length(clause->args) == 2);
1788 leftop = (Node *) linitial(clause->args);
1789 rightop = (Node *) lsecond(clause->args);
1791 /* aggressively reduce both sides to constants */
1792 leftop = estimate_expression_value(root, leftop);
1793 rightop = estimate_expression_value(root, rightop);
1795 /* get nominal (after relabeling) element type of rightop */
1796 nominal_element_type = get_base_element_type(exprType(rightop));
1797 if (!OidIsValid(nominal_element_type))
1798 return (Selectivity) 0.5; /* probably shouldn't happen */
1799 /* get nominal collation, too, for generating constants */
1800 nominal_element_collation = exprCollation(rightop);
1802 /* look through any binary-compatible relabeling of rightop */
1803 rightop = strip_array_coercion(rightop);
1806 * Detect whether the operator is the default equality or inequality
1807 * operator of the array element type.
1809 typentry = lookup_type_cache(nominal_element_type, TYPECACHE_EQ_OPR);
1810 if (OidIsValid(typentry->eq_opr))
1812 if (operator == typentry->eq_opr)
1814 else if (get_negator(operator) == typentry->eq_opr)
1815 isInequality = true;
1819 * If it is equality or inequality, we might be able to estimate this as a
1820 * form of array containment; for instance "const = ANY(column)" can be
1821 * treated as "ARRAY[const] <@ column". scalararraysel_containment tries
1822 * that, and returns the selectivity estimate if successful, or -1 if not.
1824 if ((isEquality || isInequality) && !is_join_clause)
1826 s1 = scalararraysel_containment(root, leftop, rightop,
1827 nominal_element_type,
1828 isEquality, useOr, varRelid);
1834 * Look up the underlying operator's selectivity estimator. Punt if it
1838 oprsel = get_oprjoin(operator);
1840 oprsel = get_oprrest(operator);
1842 return (Selectivity) 0.5;
1843 fmgr_info(oprsel, &oprselproc);
1846 * In the array-containment check above, we must only believe that an
1847 * operator is equality or inequality if it is the default btree equality
1848 * operator (or its negator) for the element type, since those are the
1849 * operators that array containment will use. But in what follows, we can
1850 * be a little laxer, and also believe that any operators using eqsel() or
1851 * neqsel() as selectivity estimator act like equality or inequality.
1853 if (oprsel == F_EQSEL || oprsel == F_EQJOINSEL)
1855 else if (oprsel == F_NEQSEL || oprsel == F_NEQJOINSEL)
1856 isInequality = true;
1859 * We consider three cases:
1861 * 1. rightop is an Array constant: deconstruct the array, apply the
1862 * operator's selectivity function for each array element, and merge the
1863 * results in the same way that clausesel.c does for AND/OR combinations.
1865 * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1866 * function for each element of the ARRAY[] construct, and merge.
1868 * 3. otherwise, make a guess ...
1870 if (rightop && IsA(rightop, Const))
1872 Datum arraydatum = ((Const *) rightop)->constvalue;
1873 bool arrayisnull = ((Const *) rightop)->constisnull;
1874 ArrayType *arrayval;
1883 if (arrayisnull) /* qual can't succeed if null array */
1884 return (Selectivity) 0.0;
1885 arrayval = DatumGetArrayTypeP(arraydatum);
1886 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
1887 &elmlen, &elmbyval, &elmalign);
1888 deconstruct_array(arrayval,
1889 ARR_ELEMTYPE(arrayval),
1890 elmlen, elmbyval, elmalign,
1891 &elem_values, &elem_nulls, &num_elems);
1894 * For generic operators, we assume the probability of success is
1895 * independent for each array element. But for "= ANY" or "<> ALL",
1896 * if the array elements are distinct (which'd typically be the case)
1897 * then the probabilities are disjoint, and we should just sum them.
1899 * If we were being really tense we would try to confirm that the
1900 * elements are all distinct, but that would be expensive and it
1901 * doesn't seem to be worth the cycles; it would amount to penalizing
1902 * well-written queries in favor of poorly-written ones. However, we
1903 * do protect ourselves a little bit by checking whether the
1904 * disjointness assumption leads to an impossible (out of range)
1905 * probability; if so, we fall back to the normal calculation.
1907 s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1909 for (i = 0; i < num_elems; i++)
1914 args = list_make2(leftop,
1915 makeConst(nominal_element_type,
1917 nominal_element_collation,
1923 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1924 clause->inputcollid,
1925 PointerGetDatum(root),
1926 ObjectIdGetDatum(operator),
1927 PointerGetDatum(args),
1928 Int16GetDatum(jointype),
1929 PointerGetDatum(sjinfo)));
1931 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1932 clause->inputcollid,
1933 PointerGetDatum(root),
1934 ObjectIdGetDatum(operator),
1935 PointerGetDatum(args),
1936 Int32GetDatum(varRelid)));
1940 s1 = s1 + s2 - s1 * s2;
1948 s1disjoint += s2 - 1.0;
1952 /* accept disjoint-probability estimate if in range */
1953 if ((useOr ? isEquality : isInequality) &&
1954 s1disjoint >= 0.0 && s1disjoint <= 1.0)
1957 else if (rightop && IsA(rightop, ArrayExpr) &&
1958 !((ArrayExpr *) rightop)->multidims)
1960 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1965 get_typlenbyval(arrayexpr->element_typeid,
1966 &elmlen, &elmbyval);
1969 * We use the assumption of disjoint probabilities here too, although
1970 * the odds of equal array elements are rather higher if the elements
1971 * are not all constants (which they won't be, else constant folding
1972 * would have reduced the ArrayExpr to a Const). In this path it's
1973 * critical to have the sanity check on the s1disjoint estimate.
1975 s1 = s1disjoint = (useOr ? 0.0 : 1.0);
1977 foreach(l, arrayexpr->elements)
1979 Node *elem = (Node *) lfirst(l);
1984 * Theoretically, if elem isn't of nominal_element_type we should
1985 * insert a RelabelType, but it seems unlikely that any operator
1986 * estimation function would really care ...
1988 args = list_make2(leftop, elem);
1990 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
1991 clause->inputcollid,
1992 PointerGetDatum(root),
1993 ObjectIdGetDatum(operator),
1994 PointerGetDatum(args),
1995 Int16GetDatum(jointype),
1996 PointerGetDatum(sjinfo)));
1998 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
1999 clause->inputcollid,
2000 PointerGetDatum(root),
2001 ObjectIdGetDatum(operator),
2002 PointerGetDatum(args),
2003 Int32GetDatum(varRelid)));
2007 s1 = s1 + s2 - s1 * s2;
2015 s1disjoint += s2 - 1.0;
2019 /* accept disjoint-probability estimate if in range */
2020 if ((useOr ? isEquality : isInequality) &&
2021 s1disjoint >= 0.0 && s1disjoint <= 1.0)
2026 CaseTestExpr *dummyexpr;
2032 * We need a dummy rightop to pass to the operator selectivity
2033 * routine. It can be pretty much anything that doesn't look like a
2034 * constant; CaseTestExpr is a convenient choice.
2036 dummyexpr = makeNode(CaseTestExpr);
2037 dummyexpr->typeId = nominal_element_type;
2038 dummyexpr->typeMod = -1;
2039 dummyexpr->collation = clause->inputcollid;
2040 args = list_make2(leftop, dummyexpr);
2042 s2 = DatumGetFloat8(FunctionCall5Coll(&oprselproc,
2043 clause->inputcollid,
2044 PointerGetDatum(root),
2045 ObjectIdGetDatum(operator),
2046 PointerGetDatum(args),
2047 Int16GetDatum(jointype),
2048 PointerGetDatum(sjinfo)));
2050 s2 = DatumGetFloat8(FunctionCall4Coll(&oprselproc,
2051 clause->inputcollid,
2052 PointerGetDatum(root),
2053 ObjectIdGetDatum(operator),
2054 PointerGetDatum(args),
2055 Int32GetDatum(varRelid)));
2056 s1 = useOr ? 0.0 : 1.0;
2059 * Arbitrarily assume 10 elements in the eventual array value (see
2060 * also estimate_array_length). We don't risk an assumption of
2061 * disjoint probabilities here.
2063 for (i = 0; i < 10; i++)
2066 s1 = s1 + s2 - s1 * s2;
2072 /* result should be in range, but make sure... */
2073 CLAMP_PROBABILITY(s1);
2079 * Estimate number of elements in the array yielded by an expression.
2081 * It's important that this agree with scalararraysel.
2084 estimate_array_length(Node *arrayexpr)
2086 /* look through any binary-compatible relabeling of arrayexpr */
2087 arrayexpr = strip_array_coercion(arrayexpr);
2089 if (arrayexpr && IsA(arrayexpr, Const))
2091 Datum arraydatum = ((Const *) arrayexpr)->constvalue;
2092 bool arrayisnull = ((Const *) arrayexpr)->constisnull;
2093 ArrayType *arrayval;
2097 arrayval = DatumGetArrayTypeP(arraydatum);
2098 return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2100 else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
2101 !((ArrayExpr *) arrayexpr)->multidims)
2103 return list_length(((ArrayExpr *) arrayexpr)->elements);
2107 /* default guess --- see also scalararraysel */
2113 * rowcomparesel - Selectivity of RowCompareExpr Node.
2115 * We estimate RowCompare selectivity by considering just the first (high
2116 * order) columns, which makes it equivalent to an ordinary OpExpr. While
2117 * this estimate could be refined by considering additional columns, it
2118 * seems unlikely that we could do a lot better without multi-column
2122 rowcomparesel(PlannerInfo *root,
2123 RowCompareExpr *clause,
2124 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
2127 Oid opno = linitial_oid(clause->opnos);
2128 Oid inputcollid = linitial_oid(clause->inputcollids);
2130 bool is_join_clause;
2132 /* Build equivalent arg list for single operator */
2133 opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
2136 * Decide if it's a join clause. This should match clausesel.c's
2137 * treat_as_join_clause(), except that we intentionally consider only the
2138 * leading columns and not the rest of the clause.
2143 * Caller is forcing restriction mode (eg, because we are examining an
2144 * inner indexscan qual).
2146 is_join_clause = false;
2148 else if (sjinfo == NULL)
2151 * It must be a restriction clause, since it's being evaluated at a
2154 is_join_clause = false;
2159 * Otherwise, it's a join if there's more than one relation used.
2161 is_join_clause = (NumRelids((Node *) opargs) > 1);
2166 /* Estimate selectivity for a join clause. */
2167 s1 = join_selectivity(root, opno,
2175 /* Estimate selectivity for a restriction clause. */
2176 s1 = restriction_selectivity(root, opno,
2186 * eqjoinsel - Join selectivity of "="
2189 eqjoinsel(PG_FUNCTION_ARGS)
2191 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2192 Oid operator = PG_GETARG_OID(1);
2193 List *args = (List *) PG_GETARG_POINTER(2);
2196 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2198 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
2200 VariableStatData vardata1;
2201 VariableStatData vardata2;
2202 bool join_is_reversed;
2203 RelOptInfo *inner_rel;
2205 get_join_variables(root, args, sjinfo,
2206 &vardata1, &vardata2, &join_is_reversed);
2208 switch (sjinfo->jointype)
2213 selec = eqjoinsel_inner(operator, &vardata1, &vardata2);
2219 * Look up the join's inner relation. min_righthand is sufficient
2220 * information because neither SEMI nor ANTI joins permit any
2221 * reassociation into or out of their RHS, so the righthand will
2222 * always be exactly that set of rels.
2224 inner_rel = find_join_input_rel(root, sjinfo->min_righthand);
2226 if (!join_is_reversed)
2227 selec = eqjoinsel_semi(operator, &vardata1, &vardata2,
2230 selec = eqjoinsel_semi(get_commutator(operator),
2231 &vardata2, &vardata1,
2235 /* other values not expected here */
2236 elog(ERROR, "unrecognized join type: %d",
2237 (int) sjinfo->jointype);
2238 selec = 0; /* keep compiler quiet */
2242 ReleaseVariableStats(vardata1);
2243 ReleaseVariableStats(vardata2);
2245 CLAMP_PROBABILITY(selec);
2247 PG_RETURN_FLOAT8((float8) selec);
2251 * eqjoinsel_inner --- eqjoinsel for normal inner join
2253 * We also use this for LEFT/FULL outer joins; it's not presently clear
2254 * that it's worth trying to distinguish them here.
2257 eqjoinsel_inner(Oid operator,
2258 VariableStatData *vardata1, VariableStatData *vardata2)
2265 Form_pg_statistic stats1 = NULL;
2266 Form_pg_statistic stats2 = NULL;
2267 bool have_mcvs1 = false;
2268 Datum *values1 = NULL;
2270 float4 *numbers1 = NULL;
2272 bool have_mcvs2 = false;
2273 Datum *values2 = NULL;
2275 float4 *numbers2 = NULL;
2278 nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2279 nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2281 if (HeapTupleIsValid(vardata1->statsTuple))
2283 stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2284 have_mcvs1 = get_attstatsslot(vardata1->statsTuple,
2286 vardata1->atttypmod,
2290 &values1, &nvalues1,
2291 &numbers1, &nnumbers1);
2294 if (HeapTupleIsValid(vardata2->statsTuple))
2296 stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
2297 have_mcvs2 = get_attstatsslot(vardata2->statsTuple,
2299 vardata2->atttypmod,
2303 &values2, &nvalues2,
2304 &numbers2, &nnumbers2);
2307 if (have_mcvs1 && have_mcvs2)
2310 * We have most-common-value lists for both relations. Run through
2311 * the lists to see which MCVs actually join to each other with the
2312 * given operator. This allows us to determine the exact join
2313 * selectivity for the portion of the relations represented by the MCV
2314 * lists. We still have to estimate for the remaining population, but
2315 * in a skewed distribution this gives us a big leg up in accuracy.
2316 * For motivation see the analysis in Y. Ioannidis and S.
2317 * Christodoulakis, "On the propagation of errors in the size of join
2318 * results", Technical Report 1018, Computer Science Dept., University
2319 * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
2324 double nullfrac1 = stats1->stanullfrac;
2325 double nullfrac2 = stats2->stanullfrac;
2326 double matchprodfreq,
2338 fmgr_info(get_opcode(operator), &eqproc);
2339 hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
2340 hasmatch2 = (bool *) palloc0(nvalues2 * sizeof(bool));
2343 * Note we assume that each MCV will match at most one member of the
2344 * other MCV list. If the operator isn't really equality, there could
2345 * be multiple matches --- but we don't look for them, both for speed
2346 * and because the math wouldn't add up...
2348 matchprodfreq = 0.0;
2350 for (i = 0; i < nvalues1; i++)
2354 for (j = 0; j < nvalues2; j++)
2358 if (DatumGetBool(FunctionCall2Coll(&eqproc,
2359 DEFAULT_COLLATION_OID,
2363 hasmatch1[i] = hasmatch2[j] = true;
2364 matchprodfreq += numbers1[i] * numbers2[j];
2370 CLAMP_PROBABILITY(matchprodfreq);
2371 /* Sum up frequencies of matched and unmatched MCVs */
2372 matchfreq1 = unmatchfreq1 = 0.0;
2373 for (i = 0; i < nvalues1; i++)
2376 matchfreq1 += numbers1[i];
2378 unmatchfreq1 += numbers1[i];
2380 CLAMP_PROBABILITY(matchfreq1);
2381 CLAMP_PROBABILITY(unmatchfreq1);
2382 matchfreq2 = unmatchfreq2 = 0.0;
2383 for (i = 0; i < nvalues2; i++)
2386 matchfreq2 += numbers2[i];
2388 unmatchfreq2 += numbers2[i];
2390 CLAMP_PROBABILITY(matchfreq2);
2391 CLAMP_PROBABILITY(unmatchfreq2);
2396 * Compute total frequency of non-null values that are not in the MCV
2399 otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
2400 otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
2401 CLAMP_PROBABILITY(otherfreq1);
2402 CLAMP_PROBABILITY(otherfreq2);
2405 * We can estimate the total selectivity from the point of view of
2406 * relation 1 as: the known selectivity for matched MCVs, plus
2407 * unmatched MCVs that are assumed to match against random members of
2408 * relation 2's non-MCV population, plus non-MCV values that are
2409 * assumed to match against random members of relation 2's unmatched
2410 * MCVs plus non-MCV values.
2412 totalsel1 = matchprodfreq;
2414 totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - nvalues2);
2416 totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
2418 /* Same estimate from the point of view of relation 2. */
2419 totalsel2 = matchprodfreq;
2421 totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - nvalues1);
2423 totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
2427 * Use the smaller of the two estimates. This can be justified in
2428 * essentially the same terms as given below for the no-stats case: to
2429 * a first approximation, we are estimating from the point of view of
2430 * the relation with smaller nd.
2432 selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
2437 * We do not have MCV lists for both sides. Estimate the join
2438 * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
2439 * is plausible if we assume that the join operator is strict and the
2440 * non-null values are about equally distributed: a given non-null
2441 * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
2442 * of rel2, so total join rows are at most
2443 * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
2444 * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
2445 * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
2446 * with MIN() is an upper bound. Using the MIN() means we estimate
2447 * from the point of view of the relation with smaller nd (since the
2448 * larger nd is determining the MIN). It is reasonable to assume that
2449 * most tuples in this rel will have join partners, so the bound is
2450 * probably reasonably tight and should be taken as-is.
2452 * XXX Can we be smarter if we have an MCV list for just one side? It
2453 * seems that if we assume equal distribution for the other side, we
2454 * end up with the same answer anyway.
2456 double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2457 double nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
2459 selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
2467 free_attstatsslot(vardata1->atttype, values1, nvalues1,
2468 numbers1, nnumbers1);
2470 free_attstatsslot(vardata2->atttype, values2, nvalues2,
2471 numbers2, nnumbers2);
2477 * eqjoinsel_semi --- eqjoinsel for semi join
2479 * (Also used for anti join, which we are supposed to estimate the same way.)
2480 * Caller has ensured that vardata1 is the LHS variable.
2483 eqjoinsel_semi(Oid operator,
2484 VariableStatData *vardata1, VariableStatData *vardata2,
2485 RelOptInfo *inner_rel)
2492 Form_pg_statistic stats1 = NULL;
2493 bool have_mcvs1 = false;
2494 Datum *values1 = NULL;
2496 float4 *numbers1 = NULL;
2498 bool have_mcvs2 = false;
2499 Datum *values2 = NULL;
2501 float4 *numbers2 = NULL;
2504 nd1 = get_variable_numdistinct(vardata1, &isdefault1);
2505 nd2 = get_variable_numdistinct(vardata2, &isdefault2);
2508 * We clamp nd2 to be not more than what we estimate the inner relation's
2509 * size to be. This is intuitively somewhat reasonable since obviously
2510 * there can't be more than that many distinct values coming from the
2511 * inner rel. The reason for the asymmetry (ie, that we don't clamp nd1
2512 * likewise) is that this is the only pathway by which restriction clauses
2513 * applied to the inner rel will affect the join result size estimate,
2514 * since set_joinrel_size_estimates will multiply SEMI/ANTI selectivity by
2515 * only the outer rel's size. If we clamped nd1 we'd be double-counting
2516 * the selectivity of outer-rel restrictions.
2518 * We can apply this clamping both with respect to the base relation from
2519 * which the join variable comes (if there is just one), and to the
2520 * immediate inner input relation of the current join.
2522 * If we clamp, we can treat nd2 as being a non-default estimate; it's not
2523 * great, maybe, but it didn't come out of nowhere either. This is most
2524 * helpful when the inner relation is empty and consequently has no stats.
2528 if (nd2 >= vardata2->rel->rows)
2530 nd2 = vardata2->rel->rows;
2534 if (nd2 >= inner_rel->rows)
2536 nd2 = inner_rel->rows;
2540 if (HeapTupleIsValid(vardata1->statsTuple))
2542 stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
2543 have_mcvs1 = get_attstatsslot(vardata1->statsTuple,
2545 vardata1->atttypmod,
2549 &values1, &nvalues1,
2550 &numbers1, &nnumbers1);
2553 if (HeapTupleIsValid(vardata2->statsTuple))
2555 have_mcvs2 = get_attstatsslot(vardata2->statsTuple,
2557 vardata2->atttypmod,
2561 &values2, &nvalues2,
2562 &numbers2, &nnumbers2);
2565 if (have_mcvs1 && have_mcvs2 && OidIsValid(operator))
2568 * We have most-common-value lists for both relations. Run through
2569 * the lists to see which MCVs actually join to each other with the
2570 * given operator. This allows us to determine the exact join
2571 * selectivity for the portion of the relations represented by the MCV
2572 * lists. We still have to estimate for the remaining population, but
2573 * in a skewed distribution this gives us a big leg up in accuracy.
2578 double nullfrac1 = stats1->stanullfrac;
2587 * The clamping above could have resulted in nd2 being less than
2588 * nvalues2; in which case, we assume that precisely the nd2 most
2589 * common values in the relation will appear in the join input, and so
2590 * compare to only the first nd2 members of the MCV list. Of course
2591 * this is frequently wrong, but it's the best bet we can make.
2593 clamped_nvalues2 = Min(nvalues2, nd2);
2595 fmgr_info(get_opcode(operator), &eqproc);
2596 hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
2597 hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool));
2600 * Note we assume that each MCV will match at most one member of the
2601 * other MCV list. If the operator isn't really equality, there could
2602 * be multiple matches --- but we don't look for them, both for speed
2603 * and because the math wouldn't add up...
2606 for (i = 0; i < nvalues1; i++)
2610 for (j = 0; j < clamped_nvalues2; j++)
2614 if (DatumGetBool(FunctionCall2Coll(&eqproc,
2615 DEFAULT_COLLATION_OID,
2619 hasmatch1[i] = hasmatch2[j] = true;
2625 /* Sum up frequencies of matched MCVs */
2627 for (i = 0; i < nvalues1; i++)
2630 matchfreq1 += numbers1[i];
2632 CLAMP_PROBABILITY(matchfreq1);
2637 * Now we need to estimate the fraction of relation 1 that has at
2638 * least one join partner. We know for certain that the matched MCVs
2639 * do, so that gives us a lower bound, but we're really in the dark
2640 * about everything else. Our crude approach is: if nd1 <= nd2 then
2641 * assume all non-null rel1 rows have join partners, else assume for
2642 * the uncertain rows that a fraction nd2/nd1 have join partners. We
2643 * can discount the known-matched MCVs from the distinct-values counts
2644 * before doing the division.
2646 * Crude as the above is, it's completely useless if we don't have
2647 * reliable ndistinct values for both sides. Hence, if either nd1 or
2648 * nd2 is default, punt and assume half of the uncertain rows have
2651 if (!isdefault1 && !isdefault2)
2655 if (nd1 <= nd2 || nd2 < 0)
2656 uncertainfrac = 1.0;
2658 uncertainfrac = nd2 / nd1;
2661 uncertainfrac = 0.5;
2662 uncertain = 1.0 - matchfreq1 - nullfrac1;
2663 CLAMP_PROBABILITY(uncertain);
2664 selec = matchfreq1 + uncertainfrac * uncertain;
2669 * Without MCV lists for both sides, we can only use the heuristic
2672 double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2674 if (!isdefault1 && !isdefault2)
2676 if (nd1 <= nd2 || nd2 < 0)
2677 selec = 1.0 - nullfrac1;
2679 selec = (nd2 / nd1) * (1.0 - nullfrac1);
2682 selec = 0.5 * (1.0 - nullfrac1);
2686 free_attstatsslot(vardata1->atttype, values1, nvalues1,
2687 numbers1, nnumbers1);
2689 free_attstatsslot(vardata2->atttype, values2, nvalues2,
2690 numbers2, nnumbers2);
2696 * neqjoinsel - Join selectivity of "!="
2699 neqjoinsel(PG_FUNCTION_ARGS)
2701 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2702 Oid operator = PG_GETARG_OID(1);
2703 List *args = (List *) PG_GETARG_POINTER(2);
2704 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2705 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) PG_GETARG_POINTER(4);
2710 * We want 1 - eqjoinsel() where the equality operator is the one
2711 * associated with this != operator, that is, its negator.
2713 eqop = get_negator(operator);
2716 result = DatumGetFloat8(DirectFunctionCall5(eqjoinsel,
2717 PointerGetDatum(root),
2718 ObjectIdGetDatum(eqop),
2719 PointerGetDatum(args),
2720 Int16GetDatum(jointype),
2721 PointerGetDatum(sjinfo)));
2725 /* Use default selectivity (should we raise an error instead?) */
2726 result = DEFAULT_EQ_SEL;
2728 result = 1.0 - result;
2729 PG_RETURN_FLOAT8(result);
2733 * scalarltjoinsel - Join selectivity of "<" and "<=" for scalars
2736 scalarltjoinsel(PG_FUNCTION_ARGS)
2738 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
2742 * scalargtjoinsel - Join selectivity of ">" and ">=" for scalars
2745 scalargtjoinsel(PG_FUNCTION_ARGS)
2747 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
2751 * patternjoinsel - Generic code for pattern-match join selectivity.
2754 patternjoinsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
2756 /* For the moment we just punt. */
2757 return negate ? (1.0 - DEFAULT_MATCH_SEL) : DEFAULT_MATCH_SEL;
2761 * regexeqjoinsel - Join selectivity of regular-expression pattern match.
2764 regexeqjoinsel(PG_FUNCTION_ARGS)
2766 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex, false));
2770 * icregexeqjoinsel - Join selectivity of case-insensitive regex match.
2773 icregexeqjoinsel(PG_FUNCTION_ARGS)
2775 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex_IC, false));
2779 * likejoinsel - Join selectivity of LIKE pattern match.
2782 likejoinsel(PG_FUNCTION_ARGS)
2784 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like, false));
2788 * iclikejoinsel - Join selectivity of ILIKE pattern match.
2791 iclikejoinsel(PG_FUNCTION_ARGS)
2793 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like_IC, false));
2797 * regexnejoinsel - Join selectivity of regex non-match.
2800 regexnejoinsel(PG_FUNCTION_ARGS)
2802 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex, true));
2806 * icregexnejoinsel - Join selectivity of case-insensitive regex non-match.
2809 icregexnejoinsel(PG_FUNCTION_ARGS)
2811 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Regex_IC, true));
2815 * nlikejoinsel - Join selectivity of LIKE pattern non-match.
2818 nlikejoinsel(PG_FUNCTION_ARGS)
2820 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like, true));
2824 * icnlikejoinsel - Join selectivity of ILIKE pattern non-match.
2827 icnlikejoinsel(PG_FUNCTION_ARGS)
2829 PG_RETURN_FLOAT8(patternjoinsel(fcinfo, Pattern_Type_Like_IC, true));
2833 * mergejoinscansel - Scan selectivity of merge join.
2835 * A merge join will stop as soon as it exhausts either input stream.
2836 * Therefore, if we can estimate the ranges of both input variables,
2837 * we can estimate how much of the input will actually be read. This
2838 * can have a considerable impact on the cost when using indexscans.
2840 * Also, we can estimate how much of each input has to be read before the
2841 * first join pair is found, which will affect the join's startup time.
2843 * clause should be a clause already known to be mergejoinable. opfamily,
2844 * strategy, and nulls_first specify the sort ordering being used.
2847 * *leftstart is set to the fraction of the left-hand variable expected
2848 * to be scanned before the first join pair is found (0 to 1).
2849 * *leftend is set to the fraction of the left-hand variable expected
2850 * to be scanned before the join terminates (0 to 1).
2851 * *rightstart, *rightend similarly for the right-hand variable.
2854 mergejoinscansel(PlannerInfo *root, Node *clause,
2855 Oid opfamily, int strategy, bool nulls_first,
2856 Selectivity *leftstart, Selectivity *leftend,
2857 Selectivity *rightstart, Selectivity *rightend)
2861 VariableStatData leftvar,
2882 /* Set default results if we can't figure anything out. */
2883 /* XXX should default "start" fraction be a bit more than 0? */
2884 *leftstart = *rightstart = 0.0;
2885 *leftend = *rightend = 1.0;
2887 /* Deconstruct the merge clause */
2888 if (!is_opclause(clause))
2889 return; /* shouldn't happen */
2890 opno = ((OpExpr *) clause)->opno;
2891 left = get_leftop((Expr *) clause);
2892 right = get_rightop((Expr *) clause);
2894 return; /* shouldn't happen */
2896 /* Look for stats for the inputs */
2897 examine_variable(root, left, 0, &leftvar);
2898 examine_variable(root, right, 0, &rightvar);
2900 /* Extract the operator's declared left/right datatypes */
2901 get_op_opfamily_properties(opno, opfamily, false,
2905 Assert(op_strategy == BTEqualStrategyNumber);
2908 * Look up the various operators we need. If we don't find them all, it
2909 * probably means the opfamily is broken, but we just fail silently.
2911 * Note: we expect that pg_statistic histograms will be sorted by the '<'
2912 * operator, regardless of which sort direction we are considering.
2916 case BTLessStrategyNumber:
2918 if (op_lefttype == op_righttype)
2921 ltop = get_opfamily_member(opfamily,
2922 op_lefttype, op_righttype,
2923 BTLessStrategyNumber);
2924 leop = get_opfamily_member(opfamily,
2925 op_lefttype, op_righttype,
2926 BTLessEqualStrategyNumber);
2936 ltop = get_opfamily_member(opfamily,
2937 op_lefttype, op_righttype,
2938 BTLessStrategyNumber);
2939 leop = get_opfamily_member(opfamily,
2940 op_lefttype, op_righttype,
2941 BTLessEqualStrategyNumber);
2942 lsortop = get_opfamily_member(opfamily,
2943 op_lefttype, op_lefttype,
2944 BTLessStrategyNumber);
2945 rsortop = get_opfamily_member(opfamily,
2946 op_righttype, op_righttype,
2947 BTLessStrategyNumber);
2950 revltop = get_opfamily_member(opfamily,
2951 op_righttype, op_lefttype,
2952 BTLessStrategyNumber);
2953 revleop = get_opfamily_member(opfamily,
2954 op_righttype, op_lefttype,
2955 BTLessEqualStrategyNumber);
2958 case BTGreaterStrategyNumber:
2959 /* descending-order case */
2961 if (op_lefttype == op_righttype)
2964 ltop = get_opfamily_member(opfamily,
2965 op_lefttype, op_righttype,
2966 BTGreaterStrategyNumber);
2967 leop = get_opfamily_member(opfamily,
2968 op_lefttype, op_righttype,
2969 BTGreaterEqualStrategyNumber);
2972 lstatop = get_opfamily_member(opfamily,
2973 op_lefttype, op_lefttype,
2974 BTLessStrategyNumber);
2981 ltop = get_opfamily_member(opfamily,
2982 op_lefttype, op_righttype,
2983 BTGreaterStrategyNumber);
2984 leop = get_opfamily_member(opfamily,
2985 op_lefttype, op_righttype,
2986 BTGreaterEqualStrategyNumber);
2987 lsortop = get_opfamily_member(opfamily,
2988 op_lefttype, op_lefttype,
2989 BTGreaterStrategyNumber);
2990 rsortop = get_opfamily_member(opfamily,
2991 op_righttype, op_righttype,
2992 BTGreaterStrategyNumber);
2993 lstatop = get_opfamily_member(opfamily,
2994 op_lefttype, op_lefttype,
2995 BTLessStrategyNumber);
2996 rstatop = get_opfamily_member(opfamily,
2997 op_righttype, op_righttype,
2998 BTLessStrategyNumber);
2999 revltop = get_opfamily_member(opfamily,
3000 op_righttype, op_lefttype,
3001 BTGreaterStrategyNumber);
3002 revleop = get_opfamily_member(opfamily,
3003 op_righttype, op_lefttype,
3004 BTGreaterEqualStrategyNumber);
3008 goto fail; /* shouldn't get here */
3011 if (!OidIsValid(lsortop) ||
3012 !OidIsValid(rsortop) ||
3013 !OidIsValid(lstatop) ||
3014 !OidIsValid(rstatop) ||
3015 !OidIsValid(ltop) ||
3016 !OidIsValid(leop) ||
3017 !OidIsValid(revltop) ||
3018 !OidIsValid(revleop))
3019 goto fail; /* insufficient info in catalogs */
3021 /* Try to get ranges of both inputs */
3024 if (!get_variable_range(root, &leftvar, lstatop,
3025 &leftmin, &leftmax))
3026 goto fail; /* no range available from stats */
3027 if (!get_variable_range(root, &rightvar, rstatop,
3028 &rightmin, &rightmax))
3029 goto fail; /* no range available from stats */
3033 /* need to swap the max and min */
3034 if (!get_variable_range(root, &leftvar, lstatop,
3035 &leftmax, &leftmin))
3036 goto fail; /* no range available from stats */
3037 if (!get_variable_range(root, &rightvar, rstatop,
3038 &rightmax, &rightmin))
3039 goto fail; /* no range available from stats */
3043 * Now, the fraction of the left variable that will be scanned is the
3044 * fraction that's <= the right-side maximum value. But only believe
3045 * non-default estimates, else stick with our 1.0.
3047 selec = scalarineqsel(root, leop, isgt, &leftvar,
3048 rightmax, op_righttype);
3049 if (selec != DEFAULT_INEQ_SEL)
3052 /* And similarly for the right variable. */
3053 selec = scalarineqsel(root, revleop, isgt, &rightvar,
3054 leftmax, op_lefttype);
3055 if (selec != DEFAULT_INEQ_SEL)
3059 * Only one of the two "end" fractions can really be less than 1.0;
3060 * believe the smaller estimate and reset the other one to exactly 1.0. If
3061 * we get exactly equal estimates (as can easily happen with self-joins),
3064 if (*leftend > *rightend)
3066 else if (*leftend < *rightend)
3069 *leftend = *rightend = 1.0;
3072 * Also, the fraction of the left variable that will be scanned before the
3073 * first join pair is found is the fraction that's < the right-side
3074 * minimum value. But only believe non-default estimates, else stick with
3077 selec = scalarineqsel(root, ltop, isgt, &leftvar,
3078 rightmin, op_righttype);
3079 if (selec != DEFAULT_INEQ_SEL)
3082 /* And similarly for the right variable. */
3083 selec = scalarineqsel(root, revltop, isgt, &rightvar,
3084 leftmin, op_lefttype);
3085 if (selec != DEFAULT_INEQ_SEL)
3086 *rightstart = selec;
3089 * Only one of the two "start" fractions can really be more than zero;
3090 * believe the larger estimate and reset the other one to exactly 0.0. If
3091 * we get exactly equal estimates (as can easily happen with self-joins),
3094 if (*leftstart < *rightstart)
3096 else if (*leftstart > *rightstart)
3099 *leftstart = *rightstart = 0.0;
3102 * If the sort order is nulls-first, we're going to have to skip over any
3103 * nulls too. These would not have been counted by scalarineqsel, and we
3104 * can safely add in this fraction regardless of whether we believe
3105 * scalarineqsel's results or not. But be sure to clamp the sum to 1.0!
3109 Form_pg_statistic stats;
3111 if (HeapTupleIsValid(leftvar.statsTuple))
3113 stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
3114 *leftstart += stats->stanullfrac;
3115 CLAMP_PROBABILITY(*leftstart);
3116 *leftend += stats->stanullfrac;
3117 CLAMP_PROBABILITY(*leftend);
3119 if (HeapTupleIsValid(rightvar.statsTuple))
3121 stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
3122 *rightstart += stats->stanullfrac;
3123 CLAMP_PROBABILITY(*rightstart);
3124 *rightend += stats->stanullfrac;
3125 CLAMP_PROBABILITY(*rightend);
3129 /* Disbelieve start >= end, just in case that can happen */
3130 if (*leftstart >= *leftend)
3135 if (*rightstart >= *rightend)
3142 ReleaseVariableStats(leftvar);
3143 ReleaseVariableStats(rightvar);
3148 * Helper routine for estimate_num_groups: add an item to a list of
3149 * GroupVarInfos, but only if it's not known equal to any of the existing
3154 Node *var; /* might be an expression, not just a Var */
3155 RelOptInfo *rel; /* relation it belongs to */
3156 double ndistinct; /* # distinct values */
3160 add_unique_group_var(PlannerInfo *root, List *varinfos,
3161 Node *var, VariableStatData *vardata)
3163 GroupVarInfo *varinfo;
3168 ndistinct = get_variable_numdistinct(vardata, &isdefault);
3170 /* cannot use foreach here because of possible list_delete */
3171 lc = list_head(varinfos);
3174 varinfo = (GroupVarInfo *) lfirst(lc);
3176 /* must advance lc before list_delete possibly pfree's it */
3179 /* Drop exact duplicates */
3180 if (equal(var, varinfo->var))
3184 * Drop known-equal vars, but only if they belong to different
3185 * relations (see comments for estimate_num_groups)
3187 if (vardata->rel != varinfo->rel &&
3188 exprs_known_equal(root, var, varinfo->var))
3190 if (varinfo->ndistinct <= ndistinct)
3192 /* Keep older item, forget new one */
3197 /* Delete the older item */
3198 varinfos = list_delete_ptr(varinfos, varinfo);
3203 varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
3206 varinfo->rel = vardata->rel;
3207 varinfo->ndistinct = ndistinct;
3208 varinfos = lappend(varinfos, varinfo);
3213 * estimate_num_groups - Estimate number of groups in a grouped query
3215 * Given a query having a GROUP BY clause, estimate how many groups there
3216 * will be --- ie, the number of distinct combinations of the GROUP BY
3219 * This routine is also used to estimate the number of rows emitted by
3220 * a DISTINCT filtering step; that is an isomorphic problem. (Note:
3221 * actually, we only use it for DISTINCT when there's no grouping or
3222 * aggregation ahead of the DISTINCT.)
3226 * groupExprs - list of expressions being grouped by
3227 * input_rows - number of rows estimated to arrive at the group/unique
3229 * pgset - NULL, or a List** pointing to a grouping set to filter the
3230 * groupExprs against
3232 * Given the lack of any cross-correlation statistics in the system, it's
3233 * impossible to do anything really trustworthy with GROUP BY conditions
3234 * involving multiple Vars. We should however avoid assuming the worst
3235 * case (all possible cross-product terms actually appear as groups) since
3236 * very often the grouped-by Vars are highly correlated. Our current approach
3238 * 1. Expressions yielding boolean are assumed to contribute two groups,
3239 * independently of their content, and are ignored in the subsequent
3240 * steps. This is mainly because tests like "col IS NULL" break the
3241 * heuristic used in step 2 especially badly.
3242 * 2. Reduce the given expressions to a list of unique Vars used. For
3243 * example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
3244 * It is clearly correct not to count the same Var more than once.
3245 * It is also reasonable to treat f(x) the same as x: f() cannot
3246 * increase the number of distinct values (unless it is volatile,
3247 * which we consider unlikely for grouping), but it probably won't
3248 * reduce the number of distinct values much either.
3249 * As a special case, if a GROUP BY expression can be matched to an
3250 * expressional index for which we have statistics, then we treat the
3251 * whole expression as though it were just a Var.
3252 * 3. If the list contains Vars of different relations that are known equal
3253 * due to equivalence classes, then drop all but one of the Vars from each
3254 * known-equal set, keeping the one with smallest estimated # of values
3255 * (since the extra values of the others can't appear in joined rows).
3256 * Note the reason we only consider Vars of different relations is that
3257 * if we considered ones of the same rel, we'd be double-counting the
3258 * restriction selectivity of the equality in the next step.
3259 * 4. For Vars within a single source rel, we multiply together the numbers
3260 * of values, clamp to the number of rows in the rel (divided by 10 if
3261 * more than one Var), and then multiply by a factor based on the
3262 * selectivity of the restriction clauses for that rel. When there's
3263 * more than one Var, the initial product is probably too high (it's the
3264 * worst case) but clamping to a fraction of the rel's rows seems to be a
3265 * helpful heuristic for not letting the estimate get out of hand. (The
3266 * factor of 10 is derived from pre-Postgres-7.4 practice.) The factor
3267 * we multiply by to adjust for the restriction selectivity assumes that
3268 * the restriction clauses are independent of the grouping, which may not
3269 * be a valid assumption, but it's hard to do better.
3270 * 5. If there are Vars from multiple rels, we repeat step 4 for each such
3271 * rel, and multiply the results together.
3272 * Note that rels not containing grouped Vars are ignored completely, as are
3273 * join clauses. Such rels cannot increase the number of groups, and we
3274 * assume such clauses do not reduce the number either (somewhat bogus,
3275 * but we don't have the info to do better).
3278 estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows,
3281 List *varinfos = NIL;
3287 * We don't ever want to return an estimate of zero groups, as that tends
3288 * to lead to division-by-zero and other unpleasantness. The input_rows
3289 * estimate is usually already at least 1, but clamp it just in case it
3292 input_rows = clamp_row_est(input_rows);
3295 * If no grouping columns, there's exactly one group. (This can't happen
3296 * for normal cases with GROUP BY or DISTINCT, but it is possible for
3297 * corner cases with set operations.)
3299 if (groupExprs == NIL || (pgset && list_length(*pgset) < 1))
3303 * Count groups derived from boolean grouping expressions. For other
3304 * expressions, find the unique Vars used, treating an expression as a Var
3305 * if we can find stats for it. For each one, record the statistical
3306 * estimate of number of distinct values (total in its table, without
3307 * regard for filtering).
3312 foreach(l, groupExprs)
3314 Node *groupexpr = (Node *) lfirst(l);
3315 VariableStatData vardata;
3319 /* is expression in this grouping set? */
3320 if (pgset && !list_member_int(*pgset, i++))
3323 /* Short-circuit for expressions returning boolean */
3324 if (exprType(groupexpr) == BOOLOID)
3331 * If examine_variable is able to deduce anything about the GROUP BY
3332 * expression, treat it as a single variable even if it's really more
3335 examine_variable(root, groupexpr, 0, &vardata);
3336 if (HeapTupleIsValid(vardata.statsTuple) || vardata.isunique)
3338 varinfos = add_unique_group_var(root, varinfos,
3339 groupexpr, &vardata);
3340 ReleaseVariableStats(vardata);
3343 ReleaseVariableStats(vardata);
3346 * Else pull out the component Vars. Handle PlaceHolderVars by
3347 * recursing into their arguments (effectively assuming that the
3348 * PlaceHolderVar doesn't change the number of groups, which boils
3349 * down to ignoring the possible addition of nulls to the result set).
3351 varshere = pull_var_clause(groupexpr,
3352 PVC_RECURSE_AGGREGATES |
3353 PVC_RECURSE_WINDOWFUNCS |
3354 PVC_RECURSE_PLACEHOLDERS);
3357 * If we find any variable-free GROUP BY item, then either it is a
3358 * constant (and we can ignore it) or it contains a volatile function;
3359 * in the latter case we punt and assume that each input row will
3360 * yield a distinct group.
3362 if (varshere == NIL)
3364 if (contain_volatile_functions(groupexpr))
3370 * Else add variables to varinfos list
3372 foreach(l2, varshere)
3374 Node *var = (Node *) lfirst(l2);
3376 examine_variable(root, var, 0, &vardata);
3377 varinfos = add_unique_group_var(root, varinfos, var, &vardata);
3378 ReleaseVariableStats(vardata);
3383 * If now no Vars, we must have an all-constant or all-boolean GROUP BY
3386 if (varinfos == NIL)
3388 /* Guard against out-of-range answers */
3389 if (numdistinct > input_rows)
3390 numdistinct = input_rows;
3395 * Group Vars by relation and estimate total numdistinct.
3397 * For each iteration of the outer loop, we process the frontmost Var in
3398 * varinfos, plus all other Vars in the same relation. We remove these
3399 * Vars from the newvarinfos list for the next iteration. This is the
3400 * easiest way to group Vars of same rel together.
3404 GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
3405 RelOptInfo *rel = varinfo1->rel;
3406 double reldistinct = 1;
3407 double relmaxndistinct = reldistinct;
3408 int relvarcount = 0;
3409 List *newvarinfos = NIL;
3410 List *relvarinfos = NIL;
3413 * Split the list of varinfos in two - one for the current rel,
3414 * one for remaining Vars on other rels.
3416 relvarinfos = lcons(varinfo1, relvarinfos);
3417 for_each_cell(l, lnext(list_head(varinfos)))
3419 GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3421 if (varinfo2->rel == varinfo1->rel)
3423 /* varinfos on current rel */
3424 relvarinfos = lcons(varinfo2, relvarinfos);
3428 /* not time to process varinfo2 yet */
3429 newvarinfos = lcons(varinfo2, newvarinfos);
3434 * Get the numdistinct estimate for the Vars of this rel. We
3435 * iteratively search for multivariate n-distinct with maximum number
3436 * of vars; assuming that each var group is independent of the others,
3437 * we multiply them together. Any remaining relvarinfos after
3438 * no more multivariate matches are found are assumed independent too,
3439 * so their individual ndistinct estimates are multiplied also.
3441 * While iterating, count how many separate numdistinct values we
3442 * apply. We apply a fudge factor below, but only if we multiplied
3443 * more than one such values.
3449 if (estimate_multivariate_ndistinct(root, rel, &relvarinfos,
3452 reldistinct *= mvndistinct;
3453 if (relmaxndistinct < mvndistinct)
3454 relmaxndistinct = mvndistinct;
3459 foreach (l, relvarinfos)
3461 GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
3463 reldistinct *= varinfo2->ndistinct;
3464 if (relmaxndistinct < varinfo2->ndistinct)
3465 relmaxndistinct = varinfo2->ndistinct;
3469 /* we're done with this relation */
3475 * Sanity check --- don't divide by zero if empty relation.
3477 Assert(IS_SIMPLE_REL(rel));
3478 if (rel->tuples > 0)
3481 * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
3482 * fudge factor is because the Vars are probably correlated but we
3483 * don't know by how much. We should never clamp to less than the
3484 * largest ndistinct value for any of the Vars, though, since
3485 * there will surely be at least that many groups.
3487 double clamp = rel->tuples;
3489 if (relvarcount > 1)
3492 if (clamp < relmaxndistinct)
3494 clamp = relmaxndistinct;
3495 /* for sanity in case some ndistinct is too large: */
3496 if (clamp > rel->tuples)
3497 clamp = rel->tuples;
3500 if (reldistinct > clamp)
3501 reldistinct = clamp;
3504 * Update the estimate based on the restriction selectivity,
3505 * guarding against division by zero when reldistinct is zero.
3506 * Also skip this if we know that we are returning all rows.
3508 if (reldistinct > 0 && rel->rows < rel->tuples)
3511 * Given a table containing N rows with n distinct values in a
3512 * uniform distribution, if we select p rows at random then
3513 * the expected number of distinct values selected is
3515 * n * (1 - product((N-N/n-i)/(N-i), i=0..p-1))
3517 * = n * (1 - (N-N/n)! / (N-N/n-p)! * (N-p)! / N!)
3519 * See "Approximating block accesses in database
3520 * organizations", S. B. Yao, Communications of the ACM,
3521 * Volume 20 Issue 4, April 1977 Pages 260-261.
3523 * Alternatively, re-arranging the terms from the factorials,
3524 * this may be written as
3526 * n * (1 - product((N-p-i)/(N-i), i=0..N/n-1))
3528 * This form of the formula is more efficient to compute in
3529 * the common case where p is larger than N/n. Additionally,
3530 * as pointed out by Dell'Era, if i << N for all terms in the
3531 * product, it can be approximated by
3533 * n * (1 - ((N-p)/N)^(N/n))
3535 * See "Expected distinct values when selecting from a bag
3536 * without replacement", Alberto Dell'Era,
3537 * http://www.adellera.it/investigations/distinct_balls/.
3539 * The condition i << N is equivalent to n >> 1, so this is a
3540 * good approximation when the number of distinct values in
3541 * the table is large. It turns out that this formula also
3542 * works well even when n is small.
3545 (1 - pow((rel->tuples - rel->rows) / rel->tuples,
3546 rel->tuples / reldistinct));
3548 reldistinct = clamp_row_est(reldistinct);
3551 * Update estimate of total distinct groups.
3553 numdistinct *= reldistinct;
3556 varinfos = newvarinfos;
3557 } while (varinfos != NIL);
3559 numdistinct = ceil(numdistinct);
3561 /* Guard against out-of-range answers */
3562 if (numdistinct > input_rows)
3563 numdistinct = input_rows;
3564 if (numdistinct < 1.0)
3571 * Estimate hash bucketsize fraction (ie, number of entries in a bucket
3572 * divided by total tuples in relation) if the specified expression is used
3575 * XXX This is really pretty bogus since we're effectively assuming that the
3576 * distribution of hash keys will be the same after applying restriction
3577 * clauses as it was in the underlying relation. However, we are not nearly
3578 * smart enough to figure out how the restrict clauses might change the
3579 * distribution, so this will have to do for now.
3581 * We are passed the number of buckets the executor will use for the given
3582 * input relation. If the data were perfectly distributed, with the same
3583 * number of tuples going into each available bucket, then the bucketsize
3584 * fraction would be 1/nbuckets. But this happy state of affairs will occur
3585 * only if (a) there are at least nbuckets distinct data values, and (b)
3586 * we have a not-too-skewed data distribution. Otherwise the buckets will
3587 * be nonuniformly occupied. If the other relation in the join has a key
3588 * distribution similar to this one's, then the most-loaded buckets are
3589 * exactly those that will be probed most often. Therefore, the "average"
3590 * bucket size for costing purposes should really be taken as something close
3591 * to the "worst case" bucket size. We try to estimate this by adjusting the
3592 * fraction if there are too few distinct data values, and then scaling up
3593 * by the ratio of the most common value's frequency to the average frequency.
3595 * If no statistics are available, use a default estimate of 0.1. This will
3596 * discourage use of a hash rather strongly if the inner relation is large,
3597 * which is what we want. We do not want to hash unless we know that the
3598 * inner rel is well-dispersed (or the alternatives seem much worse).
3601 estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
3603 VariableStatData vardata;
3613 examine_variable(root, hashkey, 0, &vardata);
3615 /* Get number of distinct values */
3616 ndistinct = get_variable_numdistinct(&vardata, &isdefault);
3618 /* If ndistinct isn't real, punt and return 0.1, per comments above */
3621 ReleaseVariableStats(vardata);
3622 return (Selectivity) 0.1;
3625 /* Get fraction that are null */
3626 if (HeapTupleIsValid(vardata.statsTuple))
3628 Form_pg_statistic stats;
3630 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
3631 stanullfrac = stats->stanullfrac;
3636 /* Compute avg freq of all distinct data values in raw relation */
3637 avgfreq = (1.0 - stanullfrac) / ndistinct;
3640 * Adjust ndistinct to account for restriction clauses. Observe we are
3641 * assuming that the data distribution is affected uniformly by the
3642 * restriction clauses!
3644 * XXX Possibly better way, but much more expensive: multiply by
3645 * selectivity of rel's restriction clauses that mention the target Var.
3647 if (vardata.rel && vardata.rel->tuples > 0)
3649 ndistinct *= vardata.rel->rows / vardata.rel->tuples;
3650 ndistinct = clamp_row_est(ndistinct);
3654 * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
3655 * number of buckets is less than the expected number of distinct values;
3656 * otherwise it is 1/ndistinct.
3658 if (ndistinct > nbuckets)
3659 estfract = 1.0 / nbuckets;
3661 estfract = 1.0 / ndistinct;
3664 * Look up the frequency of the most common value, if available.
3668 if (HeapTupleIsValid(vardata.statsTuple))
3670 if (get_attstatsslot(vardata.statsTuple,
3671 vardata.atttype, vardata.atttypmod,
3672 STATISTIC_KIND_MCV, InvalidOid,
3675 &numbers, &nnumbers))
3678 * The first MCV stat is for the most common value.
3681 mcvfreq = numbers[0];
3682 free_attstatsslot(vardata.atttype, NULL, 0,
3688 * Adjust estimated bucketsize upward to account for skewed distribution.
3690 if (avgfreq > 0.0 && mcvfreq > avgfreq)
3691 estfract *= mcvfreq / avgfreq;
3694 * Clamp bucketsize to sane range (the above adjustment could easily
3695 * produce an out-of-range result). We set the lower bound a little above
3696 * zero, since zero isn't a very sane result.
3698 if (estfract < 1.0e-6)
3700 else if (estfract > 1.0)
3703 ReleaseVariableStats(vardata);
3705 return (Selectivity) estfract;
3709 /*-------------------------------------------------------------------------
3713 *-------------------------------------------------------------------------
3717 * Find applicable ndistinct statistics for the given list of VarInfos (which
3718 * must all belong to the given rel), and update *ndistinct to the estimate of
3719 * the MVNDistinctItem that best matches. If a match it found, *varinfos is
3720 * updated to remove the list of matched varinfos.
3722 * Varinfos that aren't for simple Vars are ignored.
3724 * Return TRUE if we're able to find a match, FALSE otherwise.
3727 estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
3728 List **varinfos, double *ndistinct)
3731 Bitmapset *attnums = NULL;
3733 Oid statOid = InvalidOid;
3735 Bitmapset *matched = NULL;
3737 /* bail out immediately if the table has no extended statistics */
3741 /* Determine the attnums we're looking for */
3742 foreach(lc, *varinfos)
3744 GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3746 Assert(varinfo->rel == rel);
3748 if (IsA(varinfo->var, Var))
3750 attnums = bms_add_member(attnums,
3751 ((Var *) varinfo->var)->varattno);
3755 /* look for the ndistinct statistics matching the most vars */
3756 nmatches = 1; /* we require at least two matches */
3757 foreach(lc, rel->statlist)
3759 StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
3762 /* skip statistics of other kinds */
3763 if (info->kind != STATS_EXT_NDISTINCT)
3766 /* compute attnums shared by the vars and the statistic */
3767 shared = bms_intersect(info->keys, attnums);
3770 * Does this statistics matches more columns than the currently
3771 * best statistic? If so, use this one instead.
3773 * XXX This should break ties using name of the statistic, or
3774 * something like that, to make the outcome stable.
3776 if (bms_num_members(shared) > nmatches)
3778 statOid = info->statOid;
3779 nmatches = bms_num_members(shared);
3785 if (statOid == InvalidOid)
3787 Assert(nmatches > 1 && matched != NULL);
3789 stats = statext_ndistinct_load(statOid);
3792 * If we have a match, search it for the specific item that matches (there
3793 * must be one), and construct the output values.
3798 List *newlist = NIL;
3799 MVNDistinctItem *item = NULL;
3801 /* Find the specific item that exactly matches the combination */
3802 for (i = 0; i < stats->nitems; i++)
3804 MVNDistinctItem *tmpitem = &stats->items[i];
3806 if (bms_subset_compare(tmpitem->attrs, matched) == BMS_EQUAL)
3813 /* make sure we found an item */
3815 elog(ERROR, "corrupt MVNDistinct entry");
3817 /* Form the output varinfo list, keeping only unmatched ones */
3818 foreach(lc, *varinfos)
3820 GroupVarInfo *varinfo = (GroupVarInfo *) lfirst(lc);
3823 if (!IsA(varinfo->var, Var))
3825 newlist = lappend(newlist, varinfo);
3829 attnum = ((Var *) varinfo->var)->varattno;
3830 if (!bms_is_member(attnum, matched))
3831 newlist = lappend(newlist, varinfo);
3834 *varinfos = newlist;
3835 *ndistinct = item->ndistinct;
3844 * Convert non-NULL values of the indicated types to the comparison
3845 * scale needed by scalarineqsel().
3846 * Returns "true" if successful.
3848 * XXX this routine is a hack: ideally we should look up the conversion
3849 * subroutines in pg_type.
3851 * All numeric datatypes are simply converted to their equivalent
3852 * "double" values. (NUMERIC values that are outside the range of "double"
3853 * are clamped to +/- HUGE_VAL.)
3855 * String datatypes are converted by convert_string_to_scalar(),
3856 * which is explained below. The reason why this routine deals with
3857 * three values at a time, not just one, is that we need it for strings.
3859 * The bytea datatype is just enough different from strings that it has
3860 * to be treated separately.
3862 * The several datatypes representing absolute times are all converted
3863 * to Timestamp, which is actually a double, and then we just use that
3864 * double value. Note this will give correct results even for the "special"
3865 * values of Timestamp, since those are chosen to compare correctly;
3866 * see timestamp_cmp.
3868 * The several datatypes representing relative times (intervals) are all
3869 * converted to measurements expressed in seconds.
3872 convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
3873 Datum lobound, Datum hibound, Oid boundstypid,
3874 double *scaledlobound, double *scaledhibound)
3877 * Both the valuetypid and the boundstypid should exactly match the
3878 * declared input type(s) of the operator we are invoked for, so we just
3879 * error out if either is not recognized.
3881 * XXX The histogram we are interpolating between points of could belong
3882 * to a column that's only binary-compatible with the declared type. In
3883 * essence we are assuming that the semantics of binary-compatible types
3884 * are enough alike that we can use a histogram generated with one type's
3885 * operators to estimate selectivity for the other's. This is outright
3886 * wrong in some cases --- in particular signed versus unsigned
3887 * interpretation could trip us up. But it's useful enough in the
3888 * majority of cases that we do it anyway. Should think about more
3889 * rigorous ways to do it.
3894 * Built-in numeric types
3905 case REGPROCEDUREOID:
3907 case REGOPERATOROID:
3911 case REGDICTIONARYOID:
3913 case REGNAMESPACEOID:
3914 *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
3915 *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
3916 *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
3920 * Built-in string types
3928 char *valstr = convert_string_datum(value, valuetypid);
3929 char *lostr = convert_string_datum(lobound, boundstypid);
3930 char *histr = convert_string_datum(hibound, boundstypid);
3932 convert_string_to_scalar(valstr, scaledvalue,
3933 lostr, scaledlobound,
3934 histr, scaledhibound);
3942 * Built-in bytea type
3946 convert_bytea_to_scalar(value, scaledvalue,
3947 lobound, scaledlobound,
3948 hibound, scaledhibound);
3953 * Built-in time types
3956 case TIMESTAMPTZOID:
3964 *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
3965 *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
3966 *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
3970 * Built-in network types
3976 *scaledvalue = convert_network_to_scalar(value, valuetypid);
3977 *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
3978 *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
3981 /* Don't know how to convert */
3982 *scaledvalue = *scaledlobound = *scaledhibound = 0;
3987 * Do convert_to_scalar()'s work for any numeric data type.
3990 convert_numeric_to_scalar(Datum value, Oid typid)
3995 return (double) DatumGetBool(value);
3997 return (double) DatumGetInt16(value);
3999 return (double) DatumGetInt32(value);
4001 return (double) DatumGetInt64(value);
4003 return (double) DatumGetFloat4(value);
4005 return (double) DatumGetFloat8(value);
4007 /* Note: out-of-range values will be clamped to +-HUGE_VAL */
4009 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
4013 case REGPROCEDUREOID:
4015 case REGOPERATOROID:
4019 case REGDICTIONARYOID:
4021 case REGNAMESPACEOID:
4022 /* we can treat OIDs as integers... */
4023 return (double) DatumGetObjectId(value);
4027 * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4028 * an operator with one numeric and one non-numeric operand.
4030 elog(ERROR, "unsupported type: %u", typid);
4035 * Do convert_to_scalar()'s work for any character-string data type.
4037 * String datatypes are converted to a scale that ranges from 0 to 1,
4038 * where we visualize the bytes of the string as fractional digits.
4040 * We do not want the base to be 256, however, since that tends to
4041 * generate inflated selectivity estimates; few databases will have
4042 * occurrences of all 256 possible byte values at each position.
4043 * Instead, use the smallest and largest byte values seen in the bounds
4044 * as the estimated range for each byte, after some fudging to deal with
4045 * the fact that we probably aren't going to see the full range that way.
4047 * An additional refinement is that we discard any common prefix of the
4048 * three strings before computing the scaled values. This allows us to
4049 * "zoom in" when we encounter a narrow data range. An example is a phone
4050 * number database where all the values begin with the same area code.
4051 * (Actually, the bounds will be adjacent histogram-bin-boundary values,
4052 * so this is more likely to happen than you might think.)
4055 convert_string_to_scalar(char *value,
4056 double *scaledvalue,
4058 double *scaledlobound,
4060 double *scaledhibound)
4066 rangelo = rangehi = (unsigned char) hibound[0];
4067 for (sptr = lobound; *sptr; sptr++)
4069 if (rangelo > (unsigned char) *sptr)
4070 rangelo = (unsigned char) *sptr;
4071 if (rangehi < (unsigned char) *sptr)
4072 rangehi = (unsigned char) *sptr;
4074 for (sptr = hibound; *sptr; sptr++)
4076 if (rangelo > (unsigned char) *sptr)
4077 rangelo = (unsigned char) *sptr;
4078 if (rangehi < (unsigned char) *sptr)
4079 rangehi = (unsigned char) *sptr;
4081 /* If range includes any upper-case ASCII chars, make it include all */
4082 if (rangelo <= 'Z' && rangehi >= 'A')
4089 /* Ditto lower-case */
4090 if (rangelo <= 'z' && rangehi >= 'a')
4098 if (rangelo <= '9' && rangehi >= '0')
4107 * If range includes less than 10 chars, assume we have not got enough
4108 * data, and make it include regular ASCII set.
4110 if (rangehi - rangelo < 9)
4117 * Now strip any common prefix of the three strings.
4121 if (*lobound != *hibound || *lobound != *value)
4123 lobound++, hibound++, value++;
4127 * Now we can do the conversions.
4129 *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
4130 *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
4131 *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
4135 convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
4137 int slen = strlen(value);
4143 return 0.0; /* empty string has scalar value 0 */
4146 * There seems little point in considering more than a dozen bytes from
4147 * the string. Since base is at least 10, that will give us nominal
4148 * resolution of at least 12 decimal digits, which is surely far more
4149 * precision than this estimation technique has got anyway (especially in
4150 * non-C locales). Also, even with the maximum possible base of 256, this
4151 * ensures denom cannot grow larger than 256^13 = 2.03e31, which will not
4152 * overflow on any known machine.
4157 /* Convert initial characters to fraction */
4158 base = rangehi - rangelo + 1;
4163 int ch = (unsigned char) *value++;
4167 else if (ch > rangehi)
4169 num += ((double) (ch - rangelo)) / denom;
4177 * Convert a string-type Datum into a palloc'd, null-terminated string.
4179 * When using a non-C locale, we must pass the string through strxfrm()
4180 * before continuing, so as to generate correct locale-specific results.
4183 convert_string_datum(Datum value, Oid typid)
4190 val = (char *) palloc(2);
4191 val[0] = DatumGetChar(value);
4197 val = TextDatumGetCString(value);
4201 NameData *nm = (NameData *) DatumGetPointer(value);
4203 val = pstrdup(NameStr(*nm));
4209 * Can't get here unless someone tries to use scalarltsel on an
4210 * operator with one string and one non-string operand.
4212 elog(ERROR, "unsupported type: %u", typid);
4216 if (!lc_collate_is_c(DEFAULT_COLLATION_OID))
4220 size_t xfrmlen2 PG_USED_FOR_ASSERTS_ONLY;
4223 * XXX: We could guess at a suitable output buffer size and only call
4224 * strxfrm twice if our guess is too small.
4226 * XXX: strxfrm doesn't support UTF-8 encoding on Win32, it can return
4227 * bogus data or set an error. This is not really a problem unless it
4228 * crashes since it will only give an estimation error and nothing
4231 #if _MSC_VER == 1400 /* VS.Net 2005 */
4235 * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?
4236 * FeedbackID=99694 */
4240 xfrmlen = strxfrm(x, val, 0);
4243 xfrmlen = strxfrm(NULL, val, 0);
4248 * On Windows, strxfrm returns INT_MAX when an error occurs. Instead
4249 * of trying to allocate this much memory (and fail), just return the
4250 * original string unmodified as if we were in the C locale.
4252 if (xfrmlen == INT_MAX)
4255 xfrmstr = (char *) palloc(xfrmlen + 1);
4256 xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
4259 * Some systems (e.g., glibc) can return a smaller value from the
4260 * second call than the first; thus the Assert must be <= not ==.
4262 Assert(xfrmlen2 <= xfrmlen);
4271 * Do convert_to_scalar()'s work for any bytea data type.
4273 * Very similar to convert_string_to_scalar except we can't assume
4274 * null-termination and therefore pass explicit lengths around.
4276 * Also, assumptions about likely "normal" ranges of characters have been
4277 * removed - a data range of 0..255 is always used, for now. (Perhaps
4278 * someday we will add information about actual byte data range to
4282 convert_bytea_to_scalar(Datum value,
4283 double *scaledvalue,
4285 double *scaledlobound,
4287 double *scaledhibound)
4291 valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
4292 loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
4293 hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
4296 unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
4297 *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
4298 *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
4301 * Assume bytea data is uniformly distributed across all byte values.
4307 * Now strip any common prefix of the three strings.
4309 minlen = Min(Min(valuelen, loboundlen), hiboundlen);
4310 for (i = 0; i < minlen; i++)
4312 if (*lostr != *histr || *lostr != *valstr)
4314 lostr++, histr++, valstr++;
4315 loboundlen--, hiboundlen--, valuelen--;
4319 * Now we can do the conversions.
4321 *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
4322 *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
4323 *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
4327 convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
4328 int rangelo, int rangehi)
4335 return 0.0; /* empty string has scalar value 0 */
4338 * Since base is 256, need not consider more than about 10 chars (even
4339 * this many seems like overkill)
4344 /* Convert initial characters to fraction */
4345 base = rangehi - rangelo + 1;
4348 while (valuelen-- > 0)
4354 else if (ch > rangehi)
4356 num += ((double) (ch - rangelo)) / denom;
4364 * Do convert_to_scalar()'s work for any timevalue data type.
4367 convert_timevalue_to_scalar(Datum value, Oid typid)
4372 return DatumGetTimestamp(value);
4373 case TIMESTAMPTZOID:
4374 return DatumGetTimestampTz(value);
4376 return DatumGetTimestamp(DirectFunctionCall1(abstime_timestamp,
4379 return date2timestamp_no_overflow(DatumGetDateADT(value));
4382 Interval *interval = DatumGetIntervalP(value);
4385 * Convert the month part of Interval to days using assumed
4386 * average month length of 365.25/12.0 days. Not too
4387 * accurate, but plenty good enough for our purposes.
4389 return interval->time + interval->day * (double) USECS_PER_DAY +
4390 interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
4393 return (DatumGetRelativeTime(value) * 1000000.0);
4396 TimeInterval tinterval = DatumGetTimeInterval(value);
4398 if (tinterval->status != 0)
4399 return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
4400 return 0; /* for lack of a better idea */
4403 return DatumGetTimeADT(value);
4406 TimeTzADT *timetz = DatumGetTimeTzADTP(value);
4408 /* use GMT-equivalent time */
4409 return (double) (timetz->time + (timetz->zone * 1000000.0));
4414 * Can't get here unless someone tries to use scalarltsel/scalargtsel on
4415 * an operator with one timevalue and one non-timevalue operand.
4417 elog(ERROR, "unsupported type: %u", typid);
4423 * get_restriction_variable
4424 * Examine the args of a restriction clause to see if it's of the
4425 * form (variable op pseudoconstant) or (pseudoconstant op variable),
4426 * where "variable" could be either a Var or an expression in vars of a
4427 * single relation. If so, extract information about the variable,
4428 * and also indicate which side it was on and the other argument.
4431 * root: the planner info
4432 * args: clause argument list
4433 * varRelid: see specs for restriction selectivity functions
4435 * Outputs: (these are valid only if TRUE is returned)
4436 * *vardata: gets information about variable (see examine_variable)
4437 * *other: gets other clause argument, aggressively reduced to a constant
4438 * *varonleft: set TRUE if variable is on the left, FALSE if on the right
4440 * Returns TRUE if a variable is identified, otherwise FALSE.
4442 * Note: if there are Vars on both sides of the clause, we must fail, because
4443 * callers are expecting that the other side will act like a pseudoconstant.
4446 get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
4447 VariableStatData *vardata, Node **other,
4452 VariableStatData rdata;
4454 /* Fail if not a binary opclause (probably shouldn't happen) */
4455 if (list_length(args) != 2)
4458 left = (Node *) linitial(args);
4459 right = (Node *) lsecond(args);
4462 * Examine both sides. Note that when varRelid is nonzero, Vars of other
4463 * relations will be treated as pseudoconstants.
4465 examine_variable(root, left, varRelid, vardata);
4466 examine_variable(root, right, varRelid, &rdata);
4469 * If one side is a variable and the other not, we win.
4471 if (vardata->rel && rdata.rel == NULL)
4474 *other = estimate_expression_value(root, rdata.var);
4475 /* Assume we need no ReleaseVariableStats(rdata) here */
4479 if (vardata->rel == NULL && rdata.rel)
4482 *other = estimate_expression_value(root, vardata->var);
4483 /* Assume we need no ReleaseVariableStats(*vardata) here */
4488 /* Oops, clause has wrong structure (probably var op var) */
4489 ReleaseVariableStats(*vardata);
4490 ReleaseVariableStats(rdata);
4496 * get_join_variables
4497 * Apply examine_variable() to each side of a join clause.
4498 * Also, attempt to identify whether the join clause has the same
4499 * or reversed sense compared to the SpecialJoinInfo.
4501 * We consider the join clause "normal" if it is "lhs_var OP rhs_var",
4502 * or "reversed" if it is "rhs_var OP lhs_var". In complicated cases
4503 * where we can't tell for sure, we default to assuming it's normal.
4506 get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
4507 VariableStatData *vardata1, VariableStatData *vardata2,
4508 bool *join_is_reversed)
4513 if (list_length(args) != 2)
4514 elog(ERROR, "join operator should take two arguments");
4516 left = (Node *) linitial(args);
4517 right = (Node *) lsecond(args);
4519 examine_variable(root, left, 0, vardata1);
4520 examine_variable(root, right, 0, vardata2);
4522 if (vardata1->rel &&
4523 bms_is_subset(vardata1->rel->relids, sjinfo->syn_righthand))
4524 *join_is_reversed = true; /* var1 is on RHS */
4525 else if (vardata2->rel &&
4526 bms_is_subset(vardata2->rel->relids, sjinfo->syn_lefthand))
4527 *join_is_reversed = true; /* var2 is on LHS */
4529 *join_is_reversed = false;
4534 * Try to look up statistical data about an expression.
4535 * Fill in a VariableStatData struct to describe the expression.
4538 * root: the planner info
4539 * node: the expression tree to examine
4540 * varRelid: see specs for restriction selectivity functions
4542 * Outputs: *vardata is filled as follows:
4543 * var: the input expression (with any binary relabeling stripped, if
4544 * it is or contains a variable; but otherwise the type is preserved)
4545 * rel: RelOptInfo for relation containing variable; NULL if expression
4546 * contains no Vars (NOTE this could point to a RelOptInfo of a
4547 * subquery, not one in the current query).
4548 * statsTuple: the pg_statistic entry for the variable, if one exists;
4550 * freefunc: pointer to a function to release statsTuple with.
4551 * vartype: exposed type of the expression; this should always match
4552 * the declared input type of the operator we are estimating for.
4553 * atttype, atttypmod: type data to pass to get_attstatsslot(). This is
4554 * commonly the same as the exposed type of the variable argument,
4555 * but can be different in binary-compatible-type cases.
4556 * isunique: TRUE if we were able to match the var to a unique index or a
4557 * single-column DISTINCT clause, implying its values are unique for
4558 * this query. (Caution: this should be trusted for statistical
4559 * purposes only, since we do not check indimmediate nor verify that
4560 * the exact same definition of equality applies.)
4562 * Caller is responsible for doing ReleaseVariableStats() before exiting.
4565 examine_variable(PlannerInfo *root, Node *node, int varRelid,
4566 VariableStatData *vardata)
4572 /* Make sure we don't return dangling pointers in vardata */
4573 MemSet(vardata, 0, sizeof(VariableStatData));
4575 /* Save the exposed type of the expression */
4576 vardata->vartype = exprType(node);
4578 /* Look inside any binary-compatible relabeling */
4580 if (IsA(node, RelabelType))
4581 basenode = (Node *) ((RelabelType *) node)->arg;
4585 /* Fast path for a simple Var */
4587 if (IsA(basenode, Var) &&
4588 (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
4590 Var *var = (Var *) basenode;
4592 /* Set up result fields other than the stats tuple */
4593 vardata->var = basenode; /* return Var without relabeling */
4594 vardata->rel = find_base_rel(root, var->varno);
4595 vardata->atttype = var->vartype;
4596 vardata->atttypmod = var->vartypmod;
4597 vardata->isunique = has_unique_index(vardata->rel, var->varattno);
4599 /* Try to locate some stats */
4600 examine_simple_variable(root, var, vardata);
4606 * Okay, it's a more complicated expression. Determine variable
4607 * membership. Note that when varRelid isn't zero, only vars of that
4608 * relation are considered "real" vars.
4610 varnos = pull_varnos(basenode);
4614 switch (bms_membership(varnos))
4617 /* No Vars at all ... must be pseudo-constant clause */
4620 if (varRelid == 0 || bms_is_member(varRelid, varnos))
4622 onerel = find_base_rel(root,
4623 (varRelid ? varRelid : bms_singleton_member(varnos)));
4624 vardata->rel = onerel;
4625 node = basenode; /* strip any relabeling */
4627 /* else treat it as a constant */
4632 /* treat it as a variable of a join relation */
4633 vardata->rel = find_join_rel(root, varnos);
4634 node = basenode; /* strip any relabeling */
4636 else if (bms_is_member(varRelid, varnos))
4638 /* ignore the vars belonging to other relations */
4639 vardata->rel = find_base_rel(root, varRelid);
4640 node = basenode; /* strip any relabeling */
4641 /* note: no point in expressional-index search here */
4643 /* else treat it as a constant */
4649 vardata->var = node;
4650 vardata->atttype = exprType(node);
4651 vardata->atttypmod = exprTypmod(node);
4656 * We have an expression in vars of a single relation. Try to match
4657 * it to expressional index columns, in hopes of finding some
4660 * XXX it's conceivable that there are multiple matches with different
4661 * index opfamilies; if so, we need to pick one that matches the
4662 * operator we are estimating for. FIXME later.
4666 foreach(ilist, onerel->indexlist)
4668 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
4669 ListCell *indexpr_item;
4672 indexpr_item = list_head(index->indexprs);
4673 if (indexpr_item == NULL)
4674 continue; /* no expressions here... */
4676 for (pos = 0; pos < index->ncolumns; pos++)
4678 if (index->indexkeys[pos] == 0)
4682 if (indexpr_item == NULL)
4683 elog(ERROR, "too few entries in indexprs list");
4684 indexkey = (Node *) lfirst(indexpr_item);
4685 if (indexkey && IsA(indexkey, RelabelType))
4686 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4687 if (equal(node, indexkey))
4690 * Found a match ... is it a unique index? Tests here
4691 * should match has_unique_index().
4693 if (index->unique &&
4694 index->ncolumns == 1 &&
4695 (index->indpred == NIL || index->predOK))
4696 vardata->isunique = true;
4699 * Has it got stats? We only consider stats for
4700 * non-partial indexes, since partial indexes probably
4701 * don't reflect whole-relation statistics; the above
4702 * check for uniqueness is the only info we take from
4705 * An index stats hook, however, must make its own
4706 * decisions about what to do with partial indexes.
4708 if (get_index_stats_hook &&
4709 (*get_index_stats_hook) (root, index->indexoid,
4713 * The hook took control of acquiring a stats
4714 * tuple. If it did supply a tuple, it'd better
4715 * have supplied a freefunc.
4717 if (HeapTupleIsValid(vardata->statsTuple) &&
4719 elog(ERROR, "no function provided to release variable stats with");
4721 else if (index->indpred == NIL)
4723 vardata->statsTuple =
4724 SearchSysCache3(STATRELATTINH,
4725 ObjectIdGetDatum(index->indexoid),
4726 Int16GetDatum(pos + 1),
4727 BoolGetDatum(false));
4728 vardata->freefunc = ReleaseSysCache;
4730 if (vardata->statsTuple)
4733 indexpr_item = lnext(indexpr_item);
4736 if (vardata->statsTuple)
4743 * examine_simple_variable
4744 * Handle a simple Var for examine_variable
4746 * This is split out as a subroutine so that we can recurse to deal with
4747 * Vars referencing subqueries.
4749 * We already filled in all the fields of *vardata except for the stats tuple.
4752 examine_simple_variable(PlannerInfo *root, Var *var,
4753 VariableStatData *vardata)
4755 RangeTblEntry *rte = root->simple_rte_array[var->varno];
4757 Assert(IsA(rte, RangeTblEntry));
4759 if (get_relation_stats_hook &&
4760 (*get_relation_stats_hook) (root, rte, var->varattno, vardata))
4763 * The hook took control of acquiring a stats tuple. If it did supply
4764 * a tuple, it'd better have supplied a freefunc.
4766 if (HeapTupleIsValid(vardata->statsTuple) &&
4768 elog(ERROR, "no function provided to release variable stats with");
4770 else if (rte->rtekind == RTE_RELATION)
4773 * Plain table or parent of an inheritance appendrel, so look up the
4774 * column in pg_statistic
4776 vardata->statsTuple = SearchSysCache3(STATRELATTINH,
4777 ObjectIdGetDatum(rte->relid),
4778 Int16GetDatum(var->varattno),
4779 BoolGetDatum(rte->inh));
4780 vardata->freefunc = ReleaseSysCache;
4782 else if (rte->rtekind == RTE_SUBQUERY && !rte->inh)
4785 * Plain subquery (not one that was converted to an appendrel).
4787 Query *subquery = rte->subquery;
4792 * Punt if it's a whole-row var rather than a plain column reference.
4794 if (var->varattno == InvalidAttrNumber)
4798 * Punt if subquery uses set operations or GROUP BY, as these will
4799 * mash underlying columns' stats beyond recognition. (Set ops are
4800 * particularly nasty; if we forged ahead, we would return stats
4801 * relevant to only the leftmost subselect...) DISTINCT is also
4802 * problematic, but we check that later because there is a possibility
4803 * of learning something even with it.
4805 if (subquery->setOperations ||
4806 subquery->groupClause)
4810 * OK, fetch RelOptInfo for subquery. Note that we don't change the
4811 * rel returned in vardata, since caller expects it to be a rel of the
4812 * caller's query level. Because we might already be recursing, we
4813 * can't use that rel pointer either, but have to look up the Var's
4816 rel = find_base_rel(root, var->varno);
4818 /* If the subquery hasn't been planned yet, we have to punt */
4819 if (rel->subroot == NULL)
4821 Assert(IsA(rel->subroot, PlannerInfo));
4824 * Switch our attention to the subquery as mangled by the planner. It
4825 * was okay to look at the pre-planning version for the tests above,
4826 * but now we need a Var that will refer to the subroot's live
4827 * RelOptInfos. For instance, if any subquery pullup happened during
4828 * planning, Vars in the targetlist might have gotten replaced, and we
4829 * need to see the replacement expressions.
4831 subquery = rel->subroot->parse;
4832 Assert(IsA(subquery, Query));
4834 /* Get the subquery output expression referenced by the upper Var */
4835 ste = get_tle_by_resno(subquery->targetList, var->varattno);
4836 if (ste == NULL || ste->resjunk)
4837 elog(ERROR, "subquery %s does not have attribute %d",
4838 rte->eref->aliasname, var->varattno);
4839 var = (Var *) ste->expr;
4842 * If subquery uses DISTINCT, we can't make use of any stats for the
4843 * variable ... but, if it's the only DISTINCT column, we are entitled
4844 * to consider it unique. We do the test this way so that it works
4845 * for cases involving DISTINCT ON.
4847 if (subquery->distinctClause)
4849 if (list_length(subquery->distinctClause) == 1 &&
4850 targetIsInSortList(ste, InvalidOid, subquery->distinctClause))
4851 vardata->isunique = true;
4852 /* cannot go further */
4857 * If the sub-query originated from a view with the security_barrier
4858 * attribute, we must not look at the variable's statistics, though it
4859 * seems all right to notice the existence of a DISTINCT clause. So
4862 * This is probably a harsher restriction than necessary; it's
4863 * certainly OK for the selectivity estimator (which is a C function,
4864 * and therefore omnipotent anyway) to look at the statistics. But
4865 * many selectivity estimators will happily *invoke the operator
4866 * function* to try to work out a good estimate - and that's not OK.
4867 * So for now, don't dig down for stats.
4869 if (rte->security_barrier)
4872 /* Can only handle a simple Var of subquery's query level */
4873 if (var && IsA(var, Var) &&
4874 var->varlevelsup == 0)
4877 * OK, recurse into the subquery. Note that the original setting
4878 * of vardata->isunique (which will surely be false) is left
4879 * unchanged in this situation. That's what we want, since even
4880 * if the underlying column is unique, the subquery may have
4881 * joined to other tables in a way that creates duplicates.
4883 examine_simple_variable(rel->subroot, var, vardata);
4889 * Otherwise, the Var comes from a FUNCTION, VALUES, or CTE RTE. (We
4890 * won't see RTE_JOIN here because join alias Vars have already been
4891 * flattened.) There's not much we can do with function outputs, but
4892 * maybe someday try to be smarter about VALUES and/or CTEs.
4898 * get_variable_numdistinct
4899 * Estimate the number of distinct values of a variable.
4901 * vardata: results of examine_variable
4902 * *isdefault: set to TRUE if the result is a default rather than based on
4903 * anything meaningful.
4905 * NB: be careful to produce a positive integral result, since callers may
4906 * compare the result to exact integer counts, or might divide by it.
4909 get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
4912 double stanullfrac = 0.0;
4918 * Determine the stadistinct value to use. There are cases where we can
4919 * get an estimate even without a pg_statistic entry, or can get a better
4920 * value than is in pg_statistic. Grab stanullfrac too if we can find it
4921 * (otherwise, assume no nulls, for lack of any better idea).
4923 if (HeapTupleIsValid(vardata->statsTuple))
4925 /* Use the pg_statistic entry */
4926 Form_pg_statistic stats;
4928 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
4929 stadistinct = stats->stadistinct;
4930 stanullfrac = stats->stanullfrac;
4932 else if (vardata->vartype == BOOLOID)
4935 * Special-case boolean columns: presumably, two distinct values.
4937 * Are there any other datatypes we should wire in special estimates
4945 * We don't keep statistics for system columns, but in some cases we
4946 * can infer distinctness anyway.
4948 if (vardata->var && IsA(vardata->var, Var))
4950 switch (((Var *) vardata->var)->varattno)
4952 case ObjectIdAttributeNumber:
4953 case SelfItemPointerAttributeNumber:
4954 stadistinct = -1.0; /* unique (and all non null) */
4956 case TableOidAttributeNumber:
4957 stadistinct = 1.0; /* only 1 value */
4960 stadistinct = 0.0; /* means "unknown" */
4965 stadistinct = 0.0; /* means "unknown" */
4968 * XXX consider using estimate_num_groups on expressions?
4973 * If there is a unique index or DISTINCT clause for the variable, assume
4974 * it is unique no matter what pg_statistic says; the statistics could be
4975 * out of date, or we might have found a partial unique index that proves
4976 * the var is unique for this query. However, we'd better still believe
4977 * the null-fraction statistic.
4979 if (vardata->isunique)
4980 stadistinct = -1.0 * (1.0 - stanullfrac);
4983 * If we had an absolute estimate, use that.
4985 if (stadistinct > 0.0)
4986 return clamp_row_est(stadistinct);
4989 * Otherwise we need to get the relation size; punt if not available.
4991 if (vardata->rel == NULL)
4994 return DEFAULT_NUM_DISTINCT;
4996 ntuples = vardata->rel->tuples;
5000 return DEFAULT_NUM_DISTINCT;
5004 * If we had a relative estimate, use that.
5006 if (stadistinct < 0.0)
5007 return clamp_row_est(-stadistinct * ntuples);
5010 * With no data, estimate ndistinct = ntuples if the table is small, else
5011 * use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
5012 * that the behavior isn't discontinuous.
5014 if (ntuples < DEFAULT_NUM_DISTINCT)
5015 return clamp_row_est(ntuples);
5018 return DEFAULT_NUM_DISTINCT;
5022 * get_variable_range
5023 * Estimate the minimum and maximum value of the specified variable.
5024 * If successful, store values in *min and *max, and return TRUE.
5025 * If no data available, return FALSE.
5027 * sortop is the "<" comparison operator to use. This should generally
5028 * be "<" not ">", as only the former is likely to be found in pg_statistic.
5031 get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
5032 Datum *min, Datum *max)
5036 bool have_data = false;
5044 * XXX It's very tempting to try to use the actual column min and max, if
5045 * we can get them relatively-cheaply with an index probe. However, since
5046 * this function is called many times during join planning, that could
5047 * have unpleasant effects on planning speed. Need more investigation
5048 * before enabling this.
5051 if (get_actual_variable_range(root, vardata, sortop, min, max))
5055 if (!HeapTupleIsValid(vardata->statsTuple))
5057 /* no stats available, so default result */
5061 get_typlenbyval(vardata->atttype, &typLen, &typByVal);
5064 * If there is a histogram, grab the first and last values.
5066 * If there is a histogram that is sorted with some other operator than
5067 * the one we want, fail --- this suggests that there is data we can't
5070 if (get_attstatsslot(vardata->statsTuple,
5071 vardata->atttype, vardata->atttypmod,
5072 STATISTIC_KIND_HISTOGRAM, sortop,
5079 tmin = datumCopy(values[0], typByVal, typLen);
5080 tmax = datumCopy(values[nvalues - 1], typByVal, typLen);
5083 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
5085 else if (get_attstatsslot(vardata->statsTuple,
5086 vardata->atttype, vardata->atttypmod,
5087 STATISTIC_KIND_HISTOGRAM, InvalidOid,
5092 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
5097 * If we have most-common-values info, look for extreme MCVs. This is
5098 * needed even if we also have a histogram, since the histogram excludes
5099 * the MCVs. However, usually the MCVs will not be the extreme values, so
5100 * avoid unnecessary data copying.
5102 if (get_attstatsslot(vardata->statsTuple,
5103 vardata->atttype, vardata->atttypmod,
5104 STATISTIC_KIND_MCV, InvalidOid,
5109 bool tmin_is_mcv = false;
5110 bool tmax_is_mcv = false;
5113 fmgr_info(get_opcode(sortop), &opproc);
5115 for (i = 0; i < nvalues; i++)
5119 tmin = tmax = values[i];
5120 tmin_is_mcv = tmax_is_mcv = have_data = true;
5123 if (DatumGetBool(FunctionCall2Coll(&opproc,
5124 DEFAULT_COLLATION_OID,
5130 if (DatumGetBool(FunctionCall2Coll(&opproc,
5131 DEFAULT_COLLATION_OID,
5139 tmin = datumCopy(tmin, typByVal, typLen);
5141 tmax = datumCopy(tmax, typByVal, typLen);
5142 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
5152 * get_actual_variable_range
5153 * Attempt to identify the current *actual* minimum and/or maximum
5154 * of the specified variable, by looking for a suitable btree index
5155 * and fetching its low and/or high values.
5156 * If successful, store values in *min and *max, and return TRUE.
5157 * (Either pointer can be NULL if that endpoint isn't needed.)
5158 * If no data available, return FALSE.
5160 * sortop is the "<" comparison operator to use.
5163 get_actual_variable_range(PlannerInfo *root, VariableStatData *vardata,
5165 Datum *min, Datum *max)
5167 bool have_data = false;
5168 RelOptInfo *rel = vardata->rel;
5172 /* No hope if no relation or it doesn't have indexes */
5173 if (rel == NULL || rel->indexlist == NIL)
5175 /* If it has indexes it must be a plain relation */
5176 rte = root->simple_rte_array[rel->relid];
5177 Assert(rte->rtekind == RTE_RELATION);
5179 /* Search through the indexes to see if any match our problem */
5180 foreach(lc, rel->indexlist)
5182 IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
5183 ScanDirection indexscandir;
5185 /* Ignore non-btree indexes */
5186 if (index->relam != BTREE_AM_OID)
5190 * Ignore partial indexes --- we only want stats that cover the entire
5193 if (index->indpred != NIL)
5197 * The index list might include hypothetical indexes inserted by a
5198 * get_relation_info hook --- don't try to access them.
5200 if (index->hypothetical)
5204 * The first index column must match the desired variable and sort
5205 * operator --- but we can use a descending-order index.
5207 if (!match_index_to_operand(vardata->var, 0, index))
5209 switch (get_op_opfamily_strategy(sortop, index->sortopfamily[0]))
5211 case BTLessStrategyNumber:
5212 if (index->reverse_sort[0])
5213 indexscandir = BackwardScanDirection;
5215 indexscandir = ForwardScanDirection;
5217 case BTGreaterStrategyNumber:
5218 if (index->reverse_sort[0])
5219 indexscandir = ForwardScanDirection;
5221 indexscandir = BackwardScanDirection;
5224 /* index doesn't match the sortop */
5229 * Found a suitable index to extract data from. We'll need an EState
5230 * and a bunch of other infrastructure.
5234 ExprContext *econtext;
5235 MemoryContext tmpcontext;
5236 MemoryContext oldcontext;
5239 IndexInfo *indexInfo;
5240 TupleTableSlot *slot;
5243 ScanKeyData scankeys[1];
5244 IndexScanDesc index_scan;
5246 Datum values[INDEX_MAX_KEYS];
5247 bool isnull[INDEX_MAX_KEYS];
5248 SnapshotData SnapshotDirty;
5250 estate = CreateExecutorState();
5251 econtext = GetPerTupleExprContext(estate);
5252 /* Make sure any cruft is generated in the econtext's memory */
5253 tmpcontext = econtext->ecxt_per_tuple_memory;
5254 oldcontext = MemoryContextSwitchTo(tmpcontext);
5257 * Open the table and index so we can read from them. We should
5258 * already have at least AccessShareLock on the table, but not
5259 * necessarily on the index.
5261 heapRel = heap_open(rte->relid, NoLock);
5262 indexRel = index_open(index->indexoid, AccessShareLock);
5264 /* extract index key information from the index's pg_index info */
5265 indexInfo = BuildIndexInfo(indexRel);
5267 /* some other stuff */
5268 slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRel));
5269 econtext->ecxt_scantuple = slot;
5270 get_typlenbyval(vardata->atttype, &typLen, &typByVal);
5271 InitDirtySnapshot(SnapshotDirty);
5273 /* set up an IS NOT NULL scan key so that we ignore nulls */
5274 ScanKeyEntryInitialize(&scankeys[0],
5275 SK_ISNULL | SK_SEARCHNOTNULL,
5276 1, /* index col to scan */
5277 InvalidStrategy, /* no strategy */
5278 InvalidOid, /* no strategy subtype */
5279 InvalidOid, /* no collation */
5280 InvalidOid, /* no reg proc for this */
5281 (Datum) 0); /* constant */
5285 /* If min is requested ... */
5289 * In principle, we should scan the index with our current
5290 * active snapshot, which is the best approximation we've got
5291 * to what the query will see when executed. But that won't
5292 * be exact if a new snap is taken before running the query,
5293 * and it can be very expensive if a lot of uncommitted rows
5294 * exist at the end of the index (because we'll laboriously
5295 * fetch each one and reject it). What seems like a good
5296 * compromise is to use SnapshotDirty. That will accept
5297 * uncommitted rows, and thus avoid fetching multiple heap
5298 * tuples in this scenario. On the other hand, it will reject
5299 * known-dead rows, and thus not give a bogus answer when the
5300 * extreme value has been deleted; that case motivates not
5301 * using SnapshotAny here.
5303 index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
5305 index_rescan(index_scan, scankeys, 1, NULL, 0);
5307 /* Fetch first tuple in sortop's direction */
5308 if ((tup = index_getnext(index_scan,
5309 indexscandir)) != NULL)
5311 /* Extract the index column values from the heap tuple */
5312 ExecStoreTuple(tup, slot, InvalidBuffer, false);
5313 FormIndexDatum(indexInfo, slot, estate,
5316 /* Shouldn't have got a null, but be careful */
5318 elog(ERROR, "found unexpected null value in index \"%s\"",
5319 RelationGetRelationName(indexRel));
5321 /* Copy the index column value out to caller's context */
5322 MemoryContextSwitchTo(oldcontext);
5323 *min = datumCopy(values[0], typByVal, typLen);
5324 MemoryContextSwitchTo(tmpcontext);
5329 index_endscan(index_scan);
5332 /* If max is requested, and we didn't find the index is empty */
5333 if (max && have_data)
5335 index_scan = index_beginscan(heapRel, indexRel, &SnapshotDirty,
5337 index_rescan(index_scan, scankeys, 1, NULL, 0);
5339 /* Fetch first tuple in reverse direction */
5340 if ((tup = index_getnext(index_scan,
5341 -indexscandir)) != NULL)
5343 /* Extract the index column values from the heap tuple */
5344 ExecStoreTuple(tup, slot, InvalidBuffer, false);
5345 FormIndexDatum(indexInfo, slot, estate,
5348 /* Shouldn't have got a null, but be careful */
5350 elog(ERROR, "found unexpected null value in index \"%s\"",
5351 RelationGetRelationName(indexRel));
5353 /* Copy the index column value out to caller's context */
5354 MemoryContextSwitchTo(oldcontext);
5355 *max = datumCopy(values[0], typByVal, typLen);
5356 MemoryContextSwitchTo(tmpcontext);
5361 index_endscan(index_scan);
5364 /* Clean everything up */
5365 ExecDropSingleTupleTableSlot(slot);
5367 index_close(indexRel, AccessShareLock);
5368 heap_close(heapRel, NoLock);
5370 MemoryContextSwitchTo(oldcontext);
5371 FreeExecutorState(estate);
5373 /* And we're done */
5382 * find_join_input_rel
5383 * Look up the input relation for a join.
5385 * We assume that the input relation's RelOptInfo must have been constructed
5389 find_join_input_rel(PlannerInfo *root, Relids relids)
5391 RelOptInfo *rel = NULL;
5393 switch (bms_membership(relids))
5396 /* should not happen */
5399 rel = find_base_rel(root, bms_singleton_member(relids));
5402 rel = find_join_rel(root, relids);
5407 elog(ERROR, "could not find RelOptInfo for given relids");
5413 /*-------------------------------------------------------------------------
5415 * Pattern analysis functions
5417 * These routines support analysis of LIKE and regular-expression patterns
5418 * by the planner/optimizer. It's important that they agree with the
5419 * regular-expression code in backend/regex/ and the LIKE code in
5420 * backend/utils/adt/like.c. Also, the computation of the fixed prefix
5421 * must be conservative: if we report a string longer than the true fixed
5422 * prefix, the query may produce actually wrong answers, rather than just
5423 * getting a bad selectivity estimate!
5425 * Note that the prefix-analysis functions are called from
5426 * backend/optimizer/path/indxpath.c as well as from routines in this file.
5428 *-------------------------------------------------------------------------
5432 * Check whether char is a letter (and, hence, subject to case-folding)
5434 * In multibyte character sets or with ICU, we can't use isalpha, and it does not seem
5435 * worth trying to convert to wchar_t to use iswalpha. Instead, just assume
5436 * any multibyte char is potentially case-varying.
5439 pattern_char_isalpha(char c, bool is_multibyte,
5440 pg_locale_t locale, bool locale_is_c)
5443 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
5444 else if (is_multibyte && IS_HIGHBIT_SET(c))
5446 else if (locale && locale->provider == COLLPROVIDER_ICU)
5447 return IS_HIGHBIT_SET(c) ? true : false;
5448 #ifdef HAVE_LOCALE_T
5449 else if (locale && locale->provider == COLLPROVIDER_LIBC)
5450 return isalpha_l((unsigned char) c, locale->info.lt);
5453 return isalpha((unsigned char) c);
5457 * Extract the fixed prefix, if any, for a pattern.
5459 * *prefix is set to a palloc'd prefix string (in the form of a Const node),
5460 * or to NULL if no fixed prefix exists for the pattern.
5461 * If rest_selec is not NULL, *rest_selec is set to an estimate of the
5462 * selectivity of the remainder of the pattern (without any fixed prefix).
5463 * The prefix Const has the same type (TEXT or BYTEA) as the input pattern.
5465 * The return value distinguishes no fixed prefix, a partial prefix,
5466 * or an exact-match-only pattern.
5469 static Pattern_Prefix_Status
5470 like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
5471 Const **prefix_const, Selectivity *rest_selec)
5476 Oid typeid = patt_const->consttype;
5479 bool is_multibyte = (pg_database_encoding_max_length() > 1);
5480 pg_locale_t locale = 0;
5481 bool locale_is_c = false;
5483 /* the right-hand const is type text or bytea */
5484 Assert(typeid == BYTEAOID || typeid == TEXTOID);
5486 if (case_insensitive)
5488 if (typeid == BYTEAOID)
5490 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5491 errmsg("case insensitive matching not supported on type bytea")));
5493 /* If case-insensitive, we need locale info */
5494 if (lc_ctype_is_c(collation))
5496 else if (collation != DEFAULT_COLLATION_OID)
5498 if (!OidIsValid(collation))
5501 * This typically means that the parser could not resolve a
5502 * conflict of implicit collations, so report it that way.
5505 (errcode(ERRCODE_INDETERMINATE_COLLATION),
5506 errmsg("could not determine which collation to use for ILIKE"),
5507 errhint("Use the COLLATE clause to set the collation explicitly.")));
5509 locale = pg_newlocale_from_collation(collation);
5513 if (typeid != BYTEAOID)
5515 patt = TextDatumGetCString(patt_const->constvalue);
5516 pattlen = strlen(patt);
5520 bytea *bstr = DatumGetByteaPP(patt_const->constvalue);
5522 pattlen = VARSIZE_ANY_EXHDR(bstr);
5523 patt = (char *) palloc(pattlen);
5524 memcpy(patt, VARDATA_ANY(bstr), pattlen);
5525 Assert((Pointer) bstr == DatumGetPointer(patt_const->constvalue));
5528 match = palloc(pattlen + 1);
5530 for (pos = 0; pos < pattlen; pos++)
5532 /* % and _ are wildcard characters in LIKE */
5533 if (patt[pos] == '%' ||
5537 /* Backslash escapes the next character */
5538 if (patt[pos] == '\\')
5545 /* Stop if case-varying character (it's sort of a wildcard) */
5546 if (case_insensitive &&
5547 pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c))
5550 match[match_pos++] = patt[pos];
5553 match[match_pos] = '\0';
5555 if (typeid != BYTEAOID)
5556 *prefix_const = string_to_const(match, typeid);
5558 *prefix_const = string_to_bytea_const(match, match_pos);
5560 if (rest_selec != NULL)
5561 *rest_selec = like_selectivity(&patt[pos], pattlen - pos,
5567 /* in LIKE, an empty pattern is an exact match! */
5569 return Pattern_Prefix_Exact; /* reached end of pattern, so exact */
5572 return Pattern_Prefix_Partial;
5574 return Pattern_Prefix_None;
5577 static Pattern_Prefix_Status
5578 regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation,
5579 Const **prefix_const, Selectivity *rest_selec)
5581 Oid typeid = patt_const->consttype;
5586 * Should be unnecessary, there are no bytea regex operators defined. As
5587 * such, it should be noted that the rest of this function has *not* been
5588 * made safe for binary (possibly NULL containing) strings.
5590 if (typeid == BYTEAOID)
5592 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5593 errmsg("regular-expression matching not supported on type bytea")));
5595 /* Use the regexp machinery to extract the prefix, if any */
5596 prefix = regexp_fixed_prefix(DatumGetTextPP(patt_const->constvalue),
5597 case_insensitive, collation,
5602 *prefix_const = NULL;
5604 if (rest_selec != NULL)
5606 char *patt = TextDatumGetCString(patt_const->constvalue);
5608 *rest_selec = regex_selectivity(patt, strlen(patt),
5614 return Pattern_Prefix_None;
5617 *prefix_const = string_to_const(prefix, typeid);
5619 if (rest_selec != NULL)
5623 /* Exact match, so there's no additional selectivity */
5628 char *patt = TextDatumGetCString(patt_const->constvalue);
5630 *rest_selec = regex_selectivity(patt, strlen(patt),
5640 return Pattern_Prefix_Exact; /* pattern specifies exact match */
5642 return Pattern_Prefix_Partial;
5645 Pattern_Prefix_Status
5646 pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation,
5647 Const **prefix, Selectivity *rest_selec)
5649 Pattern_Prefix_Status result;
5653 case Pattern_Type_Like:
5654 result = like_fixed_prefix(patt, false, collation,
5655 prefix, rest_selec);
5657 case Pattern_Type_Like_IC:
5658 result = like_fixed_prefix(patt, true, collation,
5659 prefix, rest_selec);
5661 case Pattern_Type_Regex:
5662 result = regex_fixed_prefix(patt, false, collation,
5663 prefix, rest_selec);
5665 case Pattern_Type_Regex_IC:
5666 result = regex_fixed_prefix(patt, true, collation,
5667 prefix, rest_selec);
5670 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
5671 result = Pattern_Prefix_None; /* keep compiler quiet */
5678 * Estimate the selectivity of a fixed prefix for a pattern match.
5680 * A fixed prefix "foo" is estimated as the selectivity of the expression
5681 * "variable >= 'foo' AND variable < 'fop'" (see also indxpath.c).
5683 * The selectivity estimate is with respect to the portion of the column
5684 * population represented by the histogram --- the caller must fold this
5685 * together with info about MCVs and NULLs.
5687 * We use the >= and < operators from the specified btree opfamily to do the
5688 * estimation. The given variable and Const must be of the associated
5691 * XXX Note: we make use of the upper bound to estimate operator selectivity
5692 * even if the locale is such that we cannot rely on the upper-bound string.
5693 * The selectivity only needs to be approximately right anyway, so it seems
5694 * more useful to use the upper-bound code than not.
5697 prefix_selectivity(PlannerInfo *root, VariableStatData *vardata,
5698 Oid vartype, Oid opfamily, Const *prefixcon)
5700 Selectivity prefixsel;
5703 Const *greaterstrcon;
5706 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5707 BTGreaterEqualStrategyNumber);
5708 if (cmpopr == InvalidOid)
5709 elog(ERROR, "no >= operator for opfamily %u", opfamily);
5710 fmgr_info(get_opcode(cmpopr), &opproc);
5712 prefixsel = ineq_histogram_selectivity(root, vardata, &opproc, true,
5713 prefixcon->constvalue,
5714 prefixcon->consttype);
5716 if (prefixsel < 0.0)
5718 /* No histogram is present ... return a suitable default estimate */
5719 return DEFAULT_MATCH_SEL;
5723 * If we can create a string larger than the prefix, say
5727 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5728 BTLessStrategyNumber);
5729 if (cmpopr == InvalidOid)
5730 elog(ERROR, "no < operator for opfamily %u", opfamily);
5731 fmgr_info(get_opcode(cmpopr), &opproc);
5732 greaterstrcon = make_greater_string(prefixcon, &opproc,
5733 DEFAULT_COLLATION_OID);
5738 topsel = ineq_histogram_selectivity(root, vardata, &opproc, false,
5739 greaterstrcon->constvalue,
5740 greaterstrcon->consttype);
5742 /* ineq_histogram_selectivity worked before, it shouldn't fail now */
5743 Assert(topsel >= 0.0);
5746 * Merge the two selectivities in the same way as for a range query
5747 * (see clauselist_selectivity()). Note that we don't need to worry
5748 * about double-exclusion of nulls, since ineq_histogram_selectivity
5749 * doesn't count those anyway.
5751 prefixsel = topsel + prefixsel - 1.0;
5755 * If the prefix is long then the two bounding values might be too close
5756 * together for the histogram to distinguish them usefully, resulting in a
5757 * zero estimate (plus or minus roundoff error). To avoid returning a
5758 * ridiculously small estimate, compute the estimated selectivity for
5759 * "variable = 'foo'", and clamp to that. (Obviously, the resultant
5760 * estimate should be at least that.)
5762 * We apply this even if we couldn't make a greater string. That case
5763 * suggests that the prefix is near the maximum possible, and thus
5764 * probably off the end of the histogram, and thus we probably got a very
5765 * small estimate from the >= condition; so we still need to clamp.
5767 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
5768 BTEqualStrategyNumber);
5769 if (cmpopr == InvalidOid)
5770 elog(ERROR, "no = operator for opfamily %u", opfamily);
5771 eq_sel = var_eq_const(vardata, cmpopr, prefixcon->constvalue,
5774 prefixsel = Max(prefixsel, eq_sel);
5781 * Estimate the selectivity of a pattern of the specified type.
5782 * Note that any fixed prefix of the pattern will have been removed already,
5783 * so actually we may be looking at just a fragment of the pattern.
5785 * For now, we use a very simplistic approach: fixed characters reduce the
5786 * selectivity a good deal, character ranges reduce it a little,
5787 * wildcards (such as % for LIKE or .* for regex) increase it.
5790 #define FIXED_CHAR_SEL 0.20 /* about 1/5 */
5791 #define CHAR_RANGE_SEL 0.25
5792 #define ANY_CHAR_SEL 0.9 /* not 1, since it won't match end-of-string */
5793 #define FULL_WILDCARD_SEL 5.0
5794 #define PARTIAL_WILDCARD_SEL 2.0
5797 like_selectivity(const char *patt, int pattlen, bool case_insensitive)
5799 Selectivity sel = 1.0;
5802 /* Skip any leading wildcard; it's already factored into initial sel */
5803 for (pos = 0; pos < pattlen; pos++)
5805 if (patt[pos] != '%' && patt[pos] != '_')
5809 for (; pos < pattlen; pos++)
5811 /* % and _ are wildcard characters in LIKE */
5812 if (patt[pos] == '%')
5813 sel *= FULL_WILDCARD_SEL;
5814 else if (patt[pos] == '_')
5815 sel *= ANY_CHAR_SEL;
5816 else if (patt[pos] == '\\')
5818 /* Backslash quotes the next character */
5822 sel *= FIXED_CHAR_SEL;
5825 sel *= FIXED_CHAR_SEL;
5827 /* Could get sel > 1 if multiple wildcards */
5834 regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive)
5836 Selectivity sel = 1.0;
5837 int paren_depth = 0;
5838 int paren_pos = 0; /* dummy init to keep compiler quiet */
5841 for (pos = 0; pos < pattlen; pos++)
5843 if (patt[pos] == '(')
5845 if (paren_depth == 0)
5846 paren_pos = pos; /* remember start of parenthesized item */
5849 else if (patt[pos] == ')' && paren_depth > 0)
5852 if (paren_depth == 0)
5853 sel *= regex_selectivity_sub(patt + (paren_pos + 1),
5854 pos - (paren_pos + 1),
5857 else if (patt[pos] == '|' && paren_depth == 0)
5860 * If unquoted | is present at paren level 0 in pattern, we have
5861 * multiple alternatives; sum their probabilities.
5863 sel += regex_selectivity_sub(patt + (pos + 1),
5864 pattlen - (pos + 1),
5866 break; /* rest of pattern is now processed */
5868 else if (patt[pos] == '[')
5870 bool negclass = false;
5872 if (patt[++pos] == '^')
5877 if (patt[pos] == ']') /* ']' at start of class is not
5880 while (pos < pattlen && patt[pos] != ']')
5882 if (paren_depth == 0)
5883 sel *= (negclass ? (1.0 - CHAR_RANGE_SEL) : CHAR_RANGE_SEL);
5885 else if (patt[pos] == '.')
5887 if (paren_depth == 0)
5888 sel *= ANY_CHAR_SEL;
5890 else if (patt[pos] == '*' ||
5894 /* Ought to be smarter about quantifiers... */
5895 if (paren_depth == 0)
5896 sel *= PARTIAL_WILDCARD_SEL;
5898 else if (patt[pos] == '{')
5900 while (pos < pattlen && patt[pos] != '}')
5902 if (paren_depth == 0)
5903 sel *= PARTIAL_WILDCARD_SEL;
5905 else if (patt[pos] == '\\')
5907 /* backslash quotes the next character */
5911 if (paren_depth == 0)
5912 sel *= FIXED_CHAR_SEL;
5916 if (paren_depth == 0)
5917 sel *= FIXED_CHAR_SEL;
5920 /* Could get sel > 1 if multiple wildcards */
5927 regex_selectivity(const char *patt, int pattlen, bool case_insensitive,
5928 int fixed_prefix_len)
5932 /* If patt doesn't end with $, consider it to have a trailing wildcard */
5933 if (pattlen > 0 && patt[pattlen - 1] == '$' &&
5934 (pattlen == 1 || patt[pattlen - 2] != '\\'))
5936 /* has trailing $ */
5937 sel = regex_selectivity_sub(patt, pattlen - 1, case_insensitive);
5942 sel = regex_selectivity_sub(patt, pattlen, case_insensitive);
5943 sel *= FULL_WILDCARD_SEL;
5946 /* If there's a fixed prefix, discount its selectivity */
5947 if (fixed_prefix_len > 0)
5948 sel /= pow(FIXED_CHAR_SEL, fixed_prefix_len);
5950 /* Make sure result stays in range */
5951 CLAMP_PROBABILITY(sel);
5957 * For bytea, the increment function need only increment the current byte
5958 * (there are no multibyte characters to worry about).
5961 byte_increment(unsigned char *ptr, int len)
5970 * Try to generate a string greater than the given string or any
5971 * string it is a prefix of. If successful, return a palloc'd string
5972 * in the form of a Const node; else return NULL.
5974 * The caller must provide the appropriate "less than" comparison function
5975 * for testing the strings, along with the collation to use.
5977 * The key requirement here is that given a prefix string, say "foo",
5978 * we must be able to generate another string "fop" that is greater than
5979 * all strings "foobar" starting with "foo". We can test that we have
5980 * generated a string greater than the prefix string, but in non-C collations
5981 * that is not a bulletproof guarantee that an extension of the string might
5982 * not sort after it; an example is that "foo " is less than "foo!", but it
5983 * is not clear that a "dictionary" sort ordering will consider "foo!" less
5984 * than "foo bar". CAUTION: Therefore, this function should be used only for
5985 * estimation purposes when working in a non-C collation.
5987 * To try to catch most cases where an extended string might otherwise sort
5988 * before the result value, we determine which of the strings "Z", "z", "y",
5989 * and "9" is seen as largest by the collation, and append that to the given
5990 * prefix before trying to find a string that compares as larger.
5992 * To search for a greater string, we repeatedly "increment" the rightmost
5993 * character, using an encoding-specific character incrementer function.
5994 * When it's no longer possible to increment the last character, we truncate
5995 * off that character and start incrementing the next-to-rightmost.
5996 * For example, if "z" were the last character in the sort order, then we
5997 * could produce "foo" as a string greater than "fonz".
5999 * This could be rather slow in the worst case, but in most cases we
6000 * won't have to try more than one or two strings before succeeding.
6002 * Note that it's important for the character incrementer not to be too anal
6003 * about producing every possible character code, since in some cases the only
6004 * way to get a larger string is to increment a previous character position.
6005 * So we don't want to spend too much time trying every possible character
6006 * code at the last position. A good rule of thumb is to be sure that we
6007 * don't try more than 256*K values for a K-byte character (and definitely
6008 * not 256^K, which is what an exhaustive search would approach).
6011 make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
6013 Oid datatype = str_const->consttype;
6017 text *cmptxt = NULL;
6018 mbcharacter_incrementer charinc;
6021 * Get a modifiable copy of the prefix string in C-string format, and set
6022 * up the string we will compare to as a Datum. In C locale this can just
6023 * be the given prefix string, otherwise we need to add a suffix. Types
6024 * NAME and BYTEA sort bytewise so they don't need a suffix either.
6026 if (datatype == NAMEOID)
6028 workstr = DatumGetCString(DirectFunctionCall1(nameout,
6029 str_const->constvalue));
6030 len = strlen(workstr);
6031 cmpstr = str_const->constvalue;
6033 else if (datatype == BYTEAOID)
6035 bytea *bstr = DatumGetByteaPP(str_const->constvalue);
6037 len = VARSIZE_ANY_EXHDR(bstr);
6038 workstr = (char *) palloc(len);
6039 memcpy(workstr, VARDATA_ANY(bstr), len);
6040 Assert((Pointer) bstr == DatumGetPointer(str_const->constvalue));
6041 cmpstr = str_const->constvalue;
6045 workstr = TextDatumGetCString(str_const->constvalue);
6046 len = strlen(workstr);
6047 if (lc_collate_is_c(collation) || len == 0)
6048 cmpstr = str_const->constvalue;
6051 /* If first time through, determine the suffix to use */
6052 static char suffixchar = 0;
6053 static Oid suffixcollation = 0;
6055 if (!suffixchar || suffixcollation != collation)
6060 if (varstr_cmp(best, 1, "z", 1, collation) < 0)
6062 if (varstr_cmp(best, 1, "y", 1, collation) < 0)
6064 if (varstr_cmp(best, 1, "9", 1, collation) < 0)
6067 suffixcollation = collation;
6070 /* And build the string to compare to */
6071 cmptxt = (text *) palloc(VARHDRSZ + len + 1);
6072 SET_VARSIZE(cmptxt, VARHDRSZ + len + 1);
6073 memcpy(VARDATA(cmptxt), workstr, len);
6074 *(VARDATA(cmptxt) + len) = suffixchar;
6075 cmpstr = PointerGetDatum(cmptxt);
6079 /* Select appropriate character-incrementer function */
6080 if (datatype == BYTEAOID)
6081 charinc = byte_increment;
6083 charinc = pg_database_encoding_character_incrementer();
6085 /* And search ... */
6089 unsigned char *lastchar;
6091 /* Identify the last character --- for bytea, just the last byte */
6092 if (datatype == BYTEAOID)
6095 charlen = len - pg_mbcliplen(workstr, len, len - 1);
6096 lastchar = (unsigned char *) (workstr + len - charlen);
6099 * Try to generate a larger string by incrementing the last character
6100 * (for BYTEA, we treat each byte as a character).
6102 * Note: the incrementer function is expected to return true if it's
6103 * generated a valid-per-the-encoding new character, otherwise false.
6104 * The contents of the character on false return are unspecified.
6106 while (charinc(lastchar, charlen))
6108 Const *workstr_const;
6110 if (datatype == BYTEAOID)
6111 workstr_const = string_to_bytea_const(workstr, len);
6113 workstr_const = string_to_const(workstr, datatype);
6115 if (DatumGetBool(FunctionCall2Coll(ltproc,
6118 workstr_const->constvalue)))
6120 /* Successfully made a string larger than cmpstr */
6124 return workstr_const;
6127 /* No good, release unusable value and try again */
6128 pfree(DatumGetPointer(workstr_const->constvalue));
6129 pfree(workstr_const);
6133 * No luck here, so truncate off the last character and try to
6134 * increment the next one.
6137 workstr[len] = '\0';
6149 * Generate a Datum of the appropriate type from a C string.
6150 * Note that all of the supported types are pass-by-ref, so the
6151 * returned value should be pfree'd if no longer needed.
6154 string_to_datum(const char *str, Oid datatype)
6156 Assert(str != NULL);
6159 * We cheat a little by assuming that CStringGetTextDatum() will do for
6160 * bpchar and varchar constants too...
6162 if (datatype == NAMEOID)
6163 return DirectFunctionCall1(namein, CStringGetDatum(str));
6164 else if (datatype == BYTEAOID)
6165 return DirectFunctionCall1(byteain, CStringGetDatum(str));
6167 return CStringGetTextDatum(str);
6171 * Generate a Const node of the appropriate type from a C string.
6174 string_to_const(const char *str, Oid datatype)
6176 Datum conval = string_to_datum(str, datatype);
6181 * We only need to support a few datatypes here, so hard-wire properties
6182 * instead of incurring the expense of catalog lookups.
6189 collation = DEFAULT_COLLATION_OID;
6194 collation = InvalidOid;
6195 constlen = NAMEDATALEN;
6199 collation = InvalidOid;
6204 elog(ERROR, "unexpected datatype in string_to_const: %u",
6209 return makeConst(datatype, -1, collation, constlen,
6210 conval, false, false);
6214 * Generate a Const node of bytea type from a binary C string and a length.
6217 string_to_bytea_const(const char *str, size_t str_len)
6219 bytea *bstr = palloc(VARHDRSZ + str_len);
6222 memcpy(VARDATA(bstr), str, str_len);
6223 SET_VARSIZE(bstr, VARHDRSZ + str_len);
6224 conval = PointerGetDatum(bstr);
6226 return makeConst(BYTEAOID, -1, InvalidOid, -1, conval, false, false);
6229 /*-------------------------------------------------------------------------
6231 * Index cost estimation functions
6233 *-------------------------------------------------------------------------
6237 deconstruct_indexquals(IndexPath *path)
6240 IndexOptInfo *index = path->indexinfo;
6244 forboth(lcc, path->indexquals, lci, path->indexqualcols)
6246 RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lcc));
6247 int indexcol = lfirst_int(lci);
6251 IndexQualInfo *qinfo;
6253 clause = rinfo->clause;
6255 qinfo = (IndexQualInfo *) palloc(sizeof(IndexQualInfo));
6256 qinfo->rinfo = rinfo;
6257 qinfo->indexcol = indexcol;
6259 if (IsA(clause, OpExpr))
6261 qinfo->clause_op = ((OpExpr *) clause)->opno;
6262 leftop = get_leftop(clause);
6263 rightop = get_rightop(clause);
6264 if (match_index_to_operand(leftop, indexcol, index))
6266 qinfo->varonleft = true;
6267 qinfo->other_operand = rightop;
6271 Assert(match_index_to_operand(rightop, indexcol, index));
6272 qinfo->varonleft = false;
6273 qinfo->other_operand = leftop;
6276 else if (IsA(clause, RowCompareExpr))
6278 RowCompareExpr *rc = (RowCompareExpr *) clause;
6280 qinfo->clause_op = linitial_oid(rc->opnos);
6281 /* Examine only first columns to determine left/right sides */
6282 if (match_index_to_operand((Node *) linitial(rc->largs),
6285 qinfo->varonleft = true;
6286 qinfo->other_operand = (Node *) rc->rargs;
6290 Assert(match_index_to_operand((Node *) linitial(rc->rargs),
6292 qinfo->varonleft = false;
6293 qinfo->other_operand = (Node *) rc->largs;
6296 else if (IsA(clause, ScalarArrayOpExpr))
6298 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
6300 qinfo->clause_op = saop->opno;
6301 /* index column is always on the left in this case */
6302 Assert(match_index_to_operand((Node *) linitial(saop->args),
6304 qinfo->varonleft = true;
6305 qinfo->other_operand = (Node *) lsecond(saop->args);
6307 else if (IsA(clause, NullTest))
6309 qinfo->clause_op = InvalidOid;
6310 Assert(match_index_to_operand((Node *) ((NullTest *) clause)->arg,
6312 qinfo->varonleft = true;
6313 qinfo->other_operand = NULL;
6317 elog(ERROR, "unsupported indexqual type: %d",
6318 (int) nodeTag(clause));
6321 result = lappend(result, qinfo);
6327 * Simple function to compute the total eval cost of the "other operands"
6328 * in an IndexQualInfo list. Since we know these will be evaluated just
6329 * once per scan, there's no need to distinguish startup from per-row cost.
6332 other_operands_eval_cost(PlannerInfo *root, List *qinfos)
6334 Cost qual_arg_cost = 0;
6339 IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(lc);
6340 QualCost index_qual_cost;
6342 cost_qual_eval_node(&index_qual_cost, qinfo->other_operand, root);
6343 qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
6345 return qual_arg_cost;
6349 * Get other-operand eval cost for an index orderby list.
6351 * Index orderby expressions aren't represented as RestrictInfos (since they
6352 * aren't boolean, usually). So we can't apply deconstruct_indexquals to
6353 * them. However, they are much simpler to deal with since they are always
6354 * OpExprs and the index column is always on the left.
6357 orderby_operands_eval_cost(PlannerInfo *root, IndexPath *path)
6359 Cost qual_arg_cost = 0;
6362 foreach(lc, path->indexorderbys)
6364 Expr *clause = (Expr *) lfirst(lc);
6365 Node *other_operand;
6366 QualCost index_qual_cost;
6368 if (IsA(clause, OpExpr))
6370 other_operand = get_rightop(clause);
6374 elog(ERROR, "unsupported indexorderby type: %d",
6375 (int) nodeTag(clause));
6376 other_operand = NULL; /* keep compiler quiet */
6379 cost_qual_eval_node(&index_qual_cost, other_operand, root);
6380 qual_arg_cost += index_qual_cost.startup + index_qual_cost.per_tuple;
6382 return qual_arg_cost;
6386 genericcostestimate(PlannerInfo *root,
6390 GenericCosts *costs)
6392 IndexOptInfo *index = path->indexinfo;
6393 List *indexQuals = path->indexquals;
6394 List *indexOrderBys = path->indexorderbys;
6395 Cost indexStartupCost;
6396 Cost indexTotalCost;
6397 Selectivity indexSelectivity;
6398 double indexCorrelation;
6399 double numIndexPages;
6400 double numIndexTuples;
6401 double spc_random_page_cost;
6402 double num_sa_scans;
6403 double num_outer_scans;
6405 double qual_op_cost;
6406 double qual_arg_cost;
6407 List *selectivityQuals;
6411 * If the index is partial, AND the index predicate with the explicitly
6412 * given indexquals to produce a more accurate idea of the index
6415 selectivityQuals = add_predicate_to_quals(index, indexQuals);
6418 * Check for ScalarArrayOpExpr index quals, and estimate the number of
6419 * index scans that will be performed.
6422 foreach(l, indexQuals)
6424 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
6426 if (IsA(rinfo->clause, ScalarArrayOpExpr))
6428 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
6429 int alength = estimate_array_length(lsecond(saop->args));
6432 num_sa_scans *= alength;
6436 /* Estimate the fraction of main-table tuples that will be visited */
6437 indexSelectivity = clauselist_selectivity(root, selectivityQuals,
6443 * If caller didn't give us an estimate, estimate the number of index
6444 * tuples that will be visited. We do it in this rather peculiar-looking
6445 * way in order to get the right answer for partial indexes.
6447 numIndexTuples = costs->numIndexTuples;
6448 if (numIndexTuples <= 0.0)
6450 numIndexTuples = indexSelectivity * index->rel->tuples;
6453 * The above calculation counts all the tuples visited across all
6454 * scans induced by ScalarArrayOpExpr nodes. We want to consider the
6455 * average per-indexscan number, so adjust. This is a handy place to
6456 * round to integer, too. (If caller supplied tuple estimate, it's
6457 * responsible for handling these considerations.)
6459 numIndexTuples = rint(numIndexTuples / num_sa_scans);
6463 * We can bound the number of tuples by the index size in any case. Also,
6464 * always estimate at least one tuple is touched, even when
6465 * indexSelectivity estimate is tiny.
6467 if (numIndexTuples > index->tuples)
6468 numIndexTuples = index->tuples;
6469 if (numIndexTuples < 1.0)
6470 numIndexTuples = 1.0;
6473 * Estimate the number of index pages that will be retrieved.
6475 * We use the simplistic method of taking a pro-rata fraction of the total
6476 * number of index pages. In effect, this counts only leaf pages and not
6477 * any overhead such as index metapage or upper tree levels.
6479 * In practice access to upper index levels is often nearly free because
6480 * those tend to stay in cache under load; moreover, the cost involved is
6481 * highly dependent on index type. We therefore ignore such costs here
6482 * and leave it to the caller to add a suitable charge if needed.
6484 if (index->pages > 1 && index->tuples > 1)
6485 numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
6487 numIndexPages = 1.0;
6489 /* fetch estimated page cost for tablespace containing index */
6490 get_tablespace_page_costs(index->reltablespace,
6491 &spc_random_page_cost,
6495 * Now compute the disk access costs.
6497 * The above calculations are all per-index-scan. However, if we are in a
6498 * nestloop inner scan, we can expect the scan to be repeated (with
6499 * different search keys) for each row of the outer relation. Likewise,
6500 * ScalarArrayOpExpr quals result in multiple index scans. This creates
6501 * the potential for cache effects to reduce the number of disk page
6502 * fetches needed. We want to estimate the average per-scan I/O cost in
6503 * the presence of caching.
6505 * We use the Mackert-Lohman formula (see costsize.c for details) to
6506 * estimate the total number of page fetches that occur. While this
6507 * wasn't what it was designed for, it seems a reasonable model anyway.
6508 * Note that we are counting pages not tuples anymore, so we take N = T =
6509 * index size, as if there were one "tuple" per page.
6511 num_outer_scans = loop_count;
6512 num_scans = num_sa_scans * num_outer_scans;
6516 double pages_fetched;
6518 /* total page fetches ignoring cache effects */
6519 pages_fetched = numIndexPages * num_scans;
6521 /* use Mackert and Lohman formula to adjust for cache effects */
6522 pages_fetched = index_pages_fetched(pages_fetched,
6524 (double) index->pages,
6528 * Now compute the total disk access cost, and then report a pro-rated
6529 * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
6530 * since that's internal to the indexscan.)
6532 indexTotalCost = (pages_fetched * spc_random_page_cost)
6538 * For a single index scan, we just charge spc_random_page_cost per
6541 indexTotalCost = numIndexPages * spc_random_page_cost;
6545 * CPU cost: any complex expressions in the indexquals will need to be
6546 * evaluated once at the start of the scan to reduce them to runtime keys
6547 * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
6548 * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
6549 * indexqual operator. Because we have numIndexTuples as a per-scan
6550 * number, we have to multiply by num_sa_scans to get the correct result
6551 * for ScalarArrayOpExpr cases. Similarly add in costs for any index
6552 * ORDER BY expressions.
6554 * Note: this neglects the possible costs of rechecking lossy operators.
6555 * Detecting that that might be needed seems more expensive than it's
6556 * worth, though, considering all the other inaccuracies here ...
6558 qual_arg_cost = other_operands_eval_cost(root, qinfos) +
6559 orderby_operands_eval_cost(root, path);
6560 qual_op_cost = cpu_operator_cost *
6561 (list_length(indexQuals) + list_length(indexOrderBys));
6563 indexStartupCost = qual_arg_cost;
6564 indexTotalCost += qual_arg_cost;
6565 indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
6568 * Generic assumption about index correlation: there isn't any.
6570 indexCorrelation = 0.0;
6573 * Return everything to caller.
6575 costs->indexStartupCost = indexStartupCost;
6576 costs->indexTotalCost = indexTotalCost;
6577 costs->indexSelectivity = indexSelectivity;
6578 costs->indexCorrelation = indexCorrelation;
6579 costs->numIndexPages = numIndexPages;
6580 costs->numIndexTuples = numIndexTuples;
6581 costs->spc_random_page_cost = spc_random_page_cost;
6582 costs->num_sa_scans = num_sa_scans;
6586 * If the index is partial, add its predicate to the given qual list.
6588 * ANDing the index predicate with the explicitly given indexquals produces
6589 * a more accurate idea of the index's selectivity. However, we need to be
6590 * careful not to insert redundant clauses, because clauselist_selectivity()
6591 * is easily fooled into computing a too-low selectivity estimate. Our
6592 * approach is to add only the predicate clause(s) that cannot be proven to
6593 * be implied by the given indexquals. This successfully handles cases such
6594 * as a qual "x = 42" used with a partial index "WHERE x >= 40 AND x < 50".
6595 * There are many other cases where we won't detect redundancy, leading to a
6596 * too-low selectivity estimate, which will bias the system in favor of using
6597 * partial indexes where possible. That is not necessarily bad though.
6599 * Note that indexQuals contains RestrictInfo nodes while the indpred
6600 * does not, so the output list will be mixed. This is OK for both
6601 * predicate_implied_by() and clauselist_selectivity(), but might be
6602 * problematic if the result were passed to other things.
6605 add_predicate_to_quals(IndexOptInfo *index, List *indexQuals)
6607 List *predExtraQuals = NIL;
6610 if (index->indpred == NIL)
6613 foreach(lc, index->indpred)
6615 Node *predQual = (Node *) lfirst(lc);
6616 List *oneQual = list_make1(predQual);
6618 if (!predicate_implied_by(oneQual, indexQuals))
6619 predExtraQuals = list_concat(predExtraQuals, oneQual);
6621 /* list_concat avoids modifying the passed-in indexQuals list */
6622 return list_concat(predExtraQuals, indexQuals);
6627 btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
6628 Cost *indexStartupCost, Cost *indexTotalCost,
6629 Selectivity *indexSelectivity, double *indexCorrelation,
6632 IndexOptInfo *index = path->indexinfo;
6637 VariableStatData vardata;
6638 double numIndexTuples;
6640 List *indexBoundQuals;
6644 bool found_is_null_op;
6645 double num_sa_scans;
6648 /* Do preliminary analysis of indexquals */
6649 qinfos = deconstruct_indexquals(path);
6652 * For a btree scan, only leading '=' quals plus inequality quals for the
6653 * immediately next attribute contribute to index selectivity (these are
6654 * the "boundary quals" that determine the starting and stopping points of
6655 * the index scan). Additional quals can suppress visits to the heap, so
6656 * it's OK to count them in indexSelectivity, but they should not count
6657 * for estimating numIndexTuples. So we must examine the given indexquals
6658 * to find out which ones count as boundary quals. We rely on the
6659 * knowledge that they are given in index column order.
6661 * For a RowCompareExpr, we consider only the first column, just as
6662 * rowcomparesel() does.
6664 * If there's a ScalarArrayOpExpr in the quals, we'll actually perform N
6665 * index scans not one, but the ScalarArrayOpExpr's operator can be
6666 * considered to act the same as it normally does.
6668 indexBoundQuals = NIL;
6672 found_is_null_op = false;
6676 IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(lc);
6677 RestrictInfo *rinfo = qinfo->rinfo;
6678 Expr *clause = rinfo->clause;
6682 if (indexcol != qinfo->indexcol)
6684 /* Beginning of a new column's quals */
6686 break; /* done if no '=' qual for indexcol */
6689 if (indexcol != qinfo->indexcol)
6690 break; /* no quals at all for indexcol */
6693 if (IsA(clause, ScalarArrayOpExpr))
6695 int alength = estimate_array_length(qinfo->other_operand);
6698 /* count up number of SA scans induced by indexBoundQuals only */
6700 num_sa_scans *= alength;
6702 else if (IsA(clause, NullTest))
6704 NullTest *nt = (NullTest *) clause;
6706 if (nt->nulltesttype == IS_NULL)
6708 found_is_null_op = true;
6709 /* IS NULL is like = for selectivity determination purposes */
6715 * We would need to commute the clause_op if not varonleft, except
6716 * that we only care if it's equality or not, so that refinement is
6719 clause_op = qinfo->clause_op;
6721 /* check for equality operator */
6722 if (OidIsValid(clause_op))
6724 op_strategy = get_op_opfamily_strategy(clause_op,
6725 index->opfamily[indexcol]);
6726 Assert(op_strategy != 0); /* not a member of opfamily?? */
6727 if (op_strategy == BTEqualStrategyNumber)
6731 indexBoundQuals = lappend(indexBoundQuals, rinfo);
6735 * If index is unique and we found an '=' clause for each column, we can
6736 * just assume numIndexTuples = 1 and skip the expensive
6737 * clauselist_selectivity calculations. However, a ScalarArrayOp or
6738 * NullTest invalidates that theory, even though it sets eqQualHere.
6740 if (index->unique &&
6741 indexcol == index->ncolumns - 1 &&
6745 numIndexTuples = 1.0;
6748 List *selectivityQuals;
6749 Selectivity btreeSelectivity;
6752 * If the index is partial, AND the index predicate with the
6753 * index-bound quals to produce a more accurate idea of the number of
6754 * rows covered by the bound conditions.
6756 selectivityQuals = add_predicate_to_quals(index, indexBoundQuals);
6758 btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
6762 numIndexTuples = btreeSelectivity * index->rel->tuples;
6765 * As in genericcostestimate(), we have to adjust for any
6766 * ScalarArrayOpExpr quals included in indexBoundQuals, and then round
6769 numIndexTuples = rint(numIndexTuples / num_sa_scans);
6773 * Now do generic index cost estimation.
6775 MemSet(&costs, 0, sizeof(costs));
6776 costs.numIndexTuples = numIndexTuples;
6778 genericcostestimate(root, path, loop_count, qinfos, &costs);
6781 * Add a CPU-cost component to represent the costs of initial btree
6782 * descent. We don't charge any I/O cost for touching upper btree levels,
6783 * since they tend to stay in cache, but we still have to do about log2(N)
6784 * comparisons to descend a btree of N leaf tuples. We charge one
6785 * cpu_operator_cost per comparison.
6787 * If there are ScalarArrayOpExprs, charge this once per SA scan. The
6788 * ones after the first one are not startup cost so far as the overall
6789 * plan is concerned, so add them only to "total" cost.
6791 if (index->tuples > 1) /* avoid computing log(0) */
6793 descentCost = ceil(log(index->tuples) / log(2.0)) * cpu_operator_cost;
6794 costs.indexStartupCost += descentCost;
6795 costs.indexTotalCost += costs.num_sa_scans * descentCost;
6799 * Even though we're not charging I/O cost for touching upper btree pages,
6800 * it's still reasonable to charge some CPU cost per page descended
6801 * through. Moreover, if we had no such charge at all, bloated indexes
6802 * would appear to have the same search cost as unbloated ones, at least
6803 * in cases where only a single leaf page is expected to be visited. This
6804 * cost is somewhat arbitrarily set at 50x cpu_operator_cost per page
6805 * touched. The number of such pages is btree tree height plus one (ie,
6806 * we charge for the leaf page too). As above, charge once per SA scan.
6808 descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
6809 costs.indexStartupCost += descentCost;
6810 costs.indexTotalCost += costs.num_sa_scans * descentCost;
6813 * If we can get an estimate of the first column's ordering correlation C
6814 * from pg_statistic, estimate the index correlation as C for a
6815 * single-column index, or C * 0.75 for multiple columns. (The idea here
6816 * is that multiple columns dilute the importance of the first column's
6817 * ordering, but don't negate it entirely. Before 8.0 we divided the
6818 * correlation by the number of columns, but that seems too strong.)
6820 MemSet(&vardata, 0, sizeof(vardata));
6822 if (index->indexkeys[0] != 0)
6824 /* Simple variable --- look to stats for the underlying table */
6825 RangeTblEntry *rte = planner_rt_fetch(index->rel->relid, root);
6827 Assert(rte->rtekind == RTE_RELATION);
6829 Assert(relid != InvalidOid);
6830 colnum = index->indexkeys[0];
6832 if (get_relation_stats_hook &&
6833 (*get_relation_stats_hook) (root, rte, colnum, &vardata))
6836 * The hook took control of acquiring a stats tuple. If it did
6837 * supply a tuple, it'd better have supplied a freefunc.
6839 if (HeapTupleIsValid(vardata.statsTuple) &&
6841 elog(ERROR, "no function provided to release variable stats with");
6845 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
6846 ObjectIdGetDatum(relid),
6847 Int16GetDatum(colnum),
6848 BoolGetDatum(rte->inh));
6849 vardata.freefunc = ReleaseSysCache;
6854 /* Expression --- maybe there are stats for the index itself */
6855 relid = index->indexoid;
6858 if (get_index_stats_hook &&
6859 (*get_index_stats_hook) (root, relid, colnum, &vardata))
6862 * The hook took control of acquiring a stats tuple. If it did
6863 * supply a tuple, it'd better have supplied a freefunc.
6865 if (HeapTupleIsValid(vardata.statsTuple) &&
6867 elog(ERROR, "no function provided to release variable stats with");
6871 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
6872 ObjectIdGetDatum(relid),
6873 Int16GetDatum(colnum),
6874 BoolGetDatum(false));
6875 vardata.freefunc = ReleaseSysCache;
6879 if (HeapTupleIsValid(vardata.statsTuple))
6885 sortop = get_opfamily_member(index->opfamily[0],
6886 index->opcintype[0],
6887 index->opcintype[0],
6888 BTLessStrategyNumber);
6889 if (OidIsValid(sortop) &&
6890 get_attstatsslot(vardata.statsTuple, InvalidOid, 0,
6891 STATISTIC_KIND_CORRELATION,
6895 &numbers, &nnumbers))
6897 double varCorrelation;
6899 Assert(nnumbers == 1);
6900 varCorrelation = numbers[0];
6902 if (index->reverse_sort[0])
6903 varCorrelation = -varCorrelation;
6905 if (index->ncolumns > 1)
6906 costs.indexCorrelation = varCorrelation * 0.75;
6908 costs.indexCorrelation = varCorrelation;
6910 free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers);
6914 ReleaseVariableStats(vardata);
6916 *indexStartupCost = costs.indexStartupCost;
6917 *indexTotalCost = costs.indexTotalCost;
6918 *indexSelectivity = costs.indexSelectivity;
6919 *indexCorrelation = costs.indexCorrelation;
6920 *indexPages = costs.numIndexPages;
6924 hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
6925 Cost *indexStartupCost, Cost *indexTotalCost,
6926 Selectivity *indexSelectivity, double *indexCorrelation,
6932 /* Do preliminary analysis of indexquals */
6933 qinfos = deconstruct_indexquals(path);
6935 MemSet(&costs, 0, sizeof(costs));
6937 genericcostestimate(root, path, loop_count, qinfos, &costs);
6940 * A hash index has no descent costs as such, since the index AM can go
6941 * directly to the target bucket after computing the hash value. There
6942 * are a couple of other hash-specific costs that we could conceivably add
6945 * Ideally we'd charge spc_random_page_cost for each page in the target
6946 * bucket, not just the numIndexPages pages that genericcostestimate
6947 * thought we'd visit. However in most cases we don't know which bucket
6948 * that will be. There's no point in considering the average bucket size
6949 * because the hash AM makes sure that's always one page.
6951 * Likewise, we could consider charging some CPU for each index tuple in
6952 * the bucket, if we knew how many there were. But the per-tuple cost is
6953 * just a hash value comparison, not a general datatype-dependent
6954 * comparison, so any such charge ought to be quite a bit less than
6955 * cpu_operator_cost; which makes it probably not worth worrying about.
6957 * A bigger issue is that chance hash-value collisions will result in
6958 * wasted probes into the heap. We don't currently attempt to model this
6959 * cost on the grounds that it's rare, but maybe it's not rare enough.
6960 * (Any fix for this ought to consider the generic lossy-operator problem,
6961 * though; it's not entirely hash-specific.)
6964 *indexStartupCost = costs.indexStartupCost;
6965 *indexTotalCost = costs.indexTotalCost;
6966 *indexSelectivity = costs.indexSelectivity;
6967 *indexCorrelation = costs.indexCorrelation;
6968 *indexPages = costs.numIndexPages;
6972 gistcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
6973 Cost *indexStartupCost, Cost *indexTotalCost,
6974 Selectivity *indexSelectivity, double *indexCorrelation,
6977 IndexOptInfo *index = path->indexinfo;
6982 /* Do preliminary analysis of indexquals */
6983 qinfos = deconstruct_indexquals(path);
6985 MemSet(&costs, 0, sizeof(costs));
6987 genericcostestimate(root, path, loop_count, qinfos, &costs);
6990 * We model index descent costs similarly to those for btree, but to do
6991 * that we first need an idea of the tree height. We somewhat arbitrarily
6992 * assume that the fanout is 100, meaning the tree height is at most
6993 * log100(index->pages).
6995 * Although this computation isn't really expensive enough to require
6996 * caching, we might as well use index->tree_height to cache it.
6998 if (index->tree_height < 0) /* unknown? */
7000 if (index->pages > 1) /* avoid computing log(0) */
7001 index->tree_height = (int) (log(index->pages) / log(100.0));
7003 index->tree_height = 0;
7007 * Add a CPU-cost component to represent the costs of initial descent. We
7008 * just use log(N) here not log2(N) since the branching factor isn't
7009 * necessarily two anyway. As for btree, charge once per SA scan.
7011 if (index->tuples > 1) /* avoid computing log(0) */
7013 descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
7014 costs.indexStartupCost += descentCost;
7015 costs.indexTotalCost += costs.num_sa_scans * descentCost;
7019 * Likewise add a per-page charge, calculated the same as for btrees.
7021 descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
7022 costs.indexStartupCost += descentCost;
7023 costs.indexTotalCost += costs.num_sa_scans * descentCost;
7025 *indexStartupCost = costs.indexStartupCost;
7026 *indexTotalCost = costs.indexTotalCost;
7027 *indexSelectivity = costs.indexSelectivity;
7028 *indexCorrelation = costs.indexCorrelation;
7029 *indexPages = costs.numIndexPages;
7033 spgcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
7034 Cost *indexStartupCost, Cost *indexTotalCost,
7035 Selectivity *indexSelectivity, double *indexCorrelation,
7038 IndexOptInfo *index = path->indexinfo;
7043 /* Do preliminary analysis of indexquals */
7044 qinfos = deconstruct_indexquals(path);
7046 MemSet(&costs, 0, sizeof(costs));
7048 genericcostestimate(root, path, loop_count, qinfos, &costs);
7051 * We model index descent costs similarly to those for btree, but to do
7052 * that we first need an idea of the tree height. We somewhat arbitrarily
7053 * assume that the fanout is 100, meaning the tree height is at most
7054 * log100(index->pages).
7056 * Although this computation isn't really expensive enough to require
7057 * caching, we might as well use index->tree_height to cache it.
7059 if (index->tree_height < 0) /* unknown? */
7061 if (index->pages > 1) /* avoid computing log(0) */
7062 index->tree_height = (int) (log(index->pages) / log(100.0));
7064 index->tree_height = 0;
7068 * Add a CPU-cost component to represent the costs of initial descent. We
7069 * just use log(N) here not log2(N) since the branching factor isn't
7070 * necessarily two anyway. As for btree, charge once per SA scan.
7072 if (index->tuples > 1) /* avoid computing log(0) */
7074 descentCost = ceil(log(index->tuples)) * cpu_operator_cost;
7075 costs.indexStartupCost += descentCost;
7076 costs.indexTotalCost += costs.num_sa_scans * descentCost;
7080 * Likewise add a per-page charge, calculated the same as for btrees.
7082 descentCost = (index->tree_height + 1) * 50.0 * cpu_operator_cost;
7083 costs.indexStartupCost += descentCost;
7084 costs.indexTotalCost += costs.num_sa_scans * descentCost;
7086 *indexStartupCost = costs.indexStartupCost;
7087 *indexTotalCost = costs.indexTotalCost;
7088 *indexSelectivity = costs.indexSelectivity;
7089 *indexCorrelation = costs.indexCorrelation;
7090 *indexPages = costs.numIndexPages;
7095 * Support routines for gincostestimate
7101 double partialEntries;
7102 double exactEntries;
7103 double searchEntries;
7108 * Estimate the number of index terms that need to be searched for while
7109 * testing the given GIN query, and increment the counts in *counts
7110 * appropriately. If the query is unsatisfiable, return false.
7113 gincost_pattern(IndexOptInfo *index, int indexcol,
7114 Oid clause_op, Datum query,
7115 GinQualCounts *counts)
7123 bool *partial_matches = NULL;
7124 Pointer *extra_data = NULL;
7125 bool *nullFlags = NULL;
7126 int32 searchMode = GIN_SEARCH_MODE_DEFAULT;
7130 * Get the operator's strategy number and declared input data types within
7131 * the index opfamily. (We don't need the latter, but we use
7132 * get_op_opfamily_properties because it will throw error if it fails to
7133 * find a matching pg_amop entry.)
7135 get_op_opfamily_properties(clause_op, index->opfamily[indexcol], false,
7136 &strategy_op, &lefttype, &righttype);
7139 * GIN always uses the "default" support functions, which are those with
7140 * lefttype == righttype == the opclass' opcintype (see
7141 * IndexSupportInitialize in relcache.c).
7143 extractProcOid = get_opfamily_proc(index->opfamily[indexcol],
7144 index->opcintype[indexcol],
7145 index->opcintype[indexcol],
7146 GIN_EXTRACTQUERY_PROC);
7148 if (!OidIsValid(extractProcOid))
7150 /* should not happen; throw same error as index_getprocinfo */
7151 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
7152 GIN_EXTRACTQUERY_PROC, indexcol + 1,
7153 get_rel_name(index->indexoid));
7157 * Choose collation to pass to extractProc (should match initGinState).
7159 if (OidIsValid(index->indexcollations[indexcol]))
7160 collation = index->indexcollations[indexcol];
7162 collation = DEFAULT_COLLATION_OID;
7164 OidFunctionCall7Coll(extractProcOid,
7167 PointerGetDatum(&nentries),
7168 UInt16GetDatum(strategy_op),
7169 PointerGetDatum(&partial_matches),
7170 PointerGetDatum(&extra_data),
7171 PointerGetDatum(&nullFlags),
7172 PointerGetDatum(&searchMode));
7174 if (nentries <= 0 && searchMode == GIN_SEARCH_MODE_DEFAULT)
7176 /* No match is possible */
7180 for (i = 0; i < nentries; i++)
7183 * For partial match we haven't any information to estimate number of
7184 * matched entries in index, so, we just estimate it as 100
7186 if (partial_matches && partial_matches[i])
7187 counts->partialEntries += 100;
7189 counts->exactEntries++;
7191 counts->searchEntries++;
7194 if (searchMode == GIN_SEARCH_MODE_INCLUDE_EMPTY)
7196 /* Treat "include empty" like an exact-match item */
7197 counts->exactEntries++;
7198 counts->searchEntries++;
7200 else if (searchMode != GIN_SEARCH_MODE_DEFAULT)
7202 /* It's GIN_SEARCH_MODE_ALL */
7203 counts->haveFullScan = true;
7210 * Estimate the number of index terms that need to be searched for while
7211 * testing the given GIN index clause, and increment the counts in *counts
7212 * appropriately. If the query is unsatisfiable, return false.
7215 gincost_opexpr(PlannerInfo *root,
7216 IndexOptInfo *index,
7217 IndexQualInfo *qinfo,
7218 GinQualCounts *counts)
7220 int indexcol = qinfo->indexcol;
7221 Oid clause_op = qinfo->clause_op;
7222 Node *operand = qinfo->other_operand;
7224 if (!qinfo->varonleft)
7226 /* must commute the operator */
7227 clause_op = get_commutator(clause_op);
7230 /* aggressively reduce to a constant, and look through relabeling */
7231 operand = estimate_expression_value(root, operand);
7233 if (IsA(operand, RelabelType))
7234 operand = (Node *) ((RelabelType *) operand)->arg;
7237 * It's impossible to call extractQuery method for unknown operand. So
7238 * unless operand is a Const we can't do much; just assume there will be
7239 * one ordinary search entry from the operand at runtime.
7241 if (!IsA(operand, Const))
7243 counts->exactEntries++;
7244 counts->searchEntries++;
7248 /* If Const is null, there can be no matches */
7249 if (((Const *) operand)->constisnull)
7252 /* Otherwise, apply extractQuery and get the actual term counts */
7253 return gincost_pattern(index, indexcol, clause_op,
7254 ((Const *) operand)->constvalue,
7259 * Estimate the number of index terms that need to be searched for while
7260 * testing the given GIN index clause, and increment the counts in *counts
7261 * appropriately. If the query is unsatisfiable, return false.
7263 * A ScalarArrayOpExpr will give rise to N separate indexscans at runtime,
7264 * each of which involves one value from the RHS array, plus all the
7265 * non-array quals (if any). To model this, we average the counts across
7266 * the RHS elements, and add the averages to the counts in *counts (which
7267 * correspond to per-indexscan costs). We also multiply counts->arrayScans
7268 * by N, causing gincostestimate to scale up its estimates accordingly.
7271 gincost_scalararrayopexpr(PlannerInfo *root,
7272 IndexOptInfo *index,
7273 IndexQualInfo *qinfo,
7274 double numIndexEntries,
7275 GinQualCounts *counts)
7277 int indexcol = qinfo->indexcol;
7278 Oid clause_op = qinfo->clause_op;
7279 Node *rightop = qinfo->other_operand;
7280 ArrayType *arrayval;
7287 GinQualCounts arraycounts;
7288 int numPossible = 0;
7291 Assert(((ScalarArrayOpExpr *) qinfo->rinfo->clause)->useOr);
7293 /* aggressively reduce to a constant, and look through relabeling */
7294 rightop = estimate_expression_value(root, rightop);
7296 if (IsA(rightop, RelabelType))
7297 rightop = (Node *) ((RelabelType *) rightop)->arg;
7300 * It's impossible to call extractQuery method for unknown operand. So
7301 * unless operand is a Const we can't do much; just assume there will be
7302 * one ordinary search entry from each array entry at runtime, and fall
7303 * back on a probably-bad estimate of the number of array entries.
7305 if (!IsA(rightop, Const))
7307 counts->exactEntries++;
7308 counts->searchEntries++;
7309 counts->arrayScans *= estimate_array_length(rightop);
7313 /* If Const is null, there can be no matches */
7314 if (((Const *) rightop)->constisnull)
7317 /* Otherwise, extract the array elements and iterate over them */
7318 arrayval = DatumGetArrayTypeP(((Const *) rightop)->constvalue);
7319 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
7320 &elmlen, &elmbyval, &elmalign);
7321 deconstruct_array(arrayval,
7322 ARR_ELEMTYPE(arrayval),
7323 elmlen, elmbyval, elmalign,
7324 &elemValues, &elemNulls, &numElems);
7326 memset(&arraycounts, 0, sizeof(arraycounts));
7328 for (i = 0; i < numElems; i++)
7330 GinQualCounts elemcounts;
7332 /* NULL can't match anything, so ignore, as the executor will */
7336 /* Otherwise, apply extractQuery and get the actual term counts */
7337 memset(&elemcounts, 0, sizeof(elemcounts));
7339 if (gincost_pattern(index, indexcol, clause_op, elemValues[i],
7342 /* We ignore array elements that are unsatisfiable patterns */
7345 if (elemcounts.haveFullScan)
7348 * Full index scan will be required. We treat this as if
7349 * every key in the index had been listed in the query; is
7352 elemcounts.partialEntries = 0;
7353 elemcounts.exactEntries = numIndexEntries;
7354 elemcounts.searchEntries = numIndexEntries;
7356 arraycounts.partialEntries += elemcounts.partialEntries;
7357 arraycounts.exactEntries += elemcounts.exactEntries;
7358 arraycounts.searchEntries += elemcounts.searchEntries;
7362 if (numPossible == 0)
7364 /* No satisfiable patterns in the array */
7369 * Now add the averages to the global counts. This will give us an
7370 * estimate of the average number of terms searched for in each indexscan,
7371 * including contributions from both array and non-array quals.
7373 counts->partialEntries += arraycounts.partialEntries / numPossible;
7374 counts->exactEntries += arraycounts.exactEntries / numPossible;
7375 counts->searchEntries += arraycounts.searchEntries / numPossible;
7377 counts->arrayScans *= numPossible;
7383 * GIN has search behavior completely different from other index types
7386 gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
7387 Cost *indexStartupCost, Cost *indexTotalCost,
7388 Selectivity *indexSelectivity, double *indexCorrelation,
7391 IndexOptInfo *index = path->indexinfo;
7392 List *indexQuals = path->indexquals;
7393 List *indexOrderBys = path->indexorderbys;
7396 List *selectivityQuals;
7397 double numPages = index->pages,
7398 numTuples = index->tuples;
7399 double numEntryPages,
7403 GinQualCounts counts;
7405 double partialScale;
7406 double entryPagesFetched,
7408 dataPagesFetchedBySel;
7409 double qual_op_cost,
7411 spc_random_page_cost,
7414 GinStatsData ginStats;
7416 /* Do preliminary analysis of indexquals */
7417 qinfos = deconstruct_indexquals(path);
7420 * Obtain statistical information from the meta page, if possible. Else
7421 * set ginStats to zeroes, and we'll cope below.
7423 if (!index->hypothetical)
7425 indexRel = index_open(index->indexoid, AccessShareLock);
7426 ginGetStats(indexRel, &ginStats);
7427 index_close(indexRel, AccessShareLock);
7431 memset(&ginStats, 0, sizeof(ginStats));
7435 * Assuming we got valid (nonzero) stats at all, nPendingPages can be
7436 * trusted, but the other fields are data as of the last VACUUM. We can
7437 * scale them up to account for growth since then, but that method only
7438 * goes so far; in the worst case, the stats might be for a completely
7439 * empty index, and scaling them will produce pretty bogus numbers.
7440 * Somewhat arbitrarily, set the cutoff for doing scaling at 4X growth; if
7441 * it's grown more than that, fall back to estimating things only from the
7442 * assumed-accurate index size. But we'll trust nPendingPages in any case
7443 * so long as it's not clearly insane, ie, more than the index size.
7445 if (ginStats.nPendingPages < numPages)
7446 numPendingPages = ginStats.nPendingPages;
7448 numPendingPages = 0;
7450 if (numPages > 0 && ginStats.nTotalPages <= numPages &&
7451 ginStats.nTotalPages > numPages / 4 &&
7452 ginStats.nEntryPages > 0 && ginStats.nEntries > 0)
7455 * OK, the stats seem close enough to sane to be trusted. But we
7456 * still need to scale them by the ratio numPages / nTotalPages to
7457 * account for growth since the last VACUUM.
7459 double scale = numPages / ginStats.nTotalPages;
7461 numEntryPages = ceil(ginStats.nEntryPages * scale);
7462 numDataPages = ceil(ginStats.nDataPages * scale);
7463 numEntries = ceil(ginStats.nEntries * scale);
7464 /* ensure we didn't round up too much */
7465 numEntryPages = Min(numEntryPages, numPages - numPendingPages);
7466 numDataPages = Min(numDataPages,
7467 numPages - numPendingPages - numEntryPages);
7472 * We might get here because it's a hypothetical index, or an index
7473 * created pre-9.1 and never vacuumed since upgrading (in which case
7474 * its stats would read as zeroes), or just because it's grown too
7475 * much since the last VACUUM for us to put our faith in scaling.
7477 * Invent some plausible internal statistics based on the index page
7478 * count (and clamp that to at least 10 pages, just in case). We
7479 * estimate that 90% of the index is entry pages, and the rest is data
7480 * pages. Estimate 100 entries per entry page; this is rather bogus
7481 * since it'll depend on the size of the keys, but it's more robust
7482 * than trying to predict the number of entries per heap tuple.
7484 numPages = Max(numPages, 10);
7485 numEntryPages = floor((numPages - numPendingPages) * 0.90);
7486 numDataPages = numPages - numPendingPages - numEntryPages;
7487 numEntries = floor(numEntryPages * 100);
7490 /* In an empty index, numEntries could be zero. Avoid divide-by-zero */
7495 * Include predicate in selectivityQuals (should match
7496 * genericcostestimate)
7498 if (index->indpred != NIL)
7500 List *predExtraQuals = NIL;
7502 foreach(l, index->indpred)
7504 Node *predQual = (Node *) lfirst(l);
7505 List *oneQual = list_make1(predQual);
7507 if (!predicate_implied_by(oneQual, indexQuals))
7508 predExtraQuals = list_concat(predExtraQuals, oneQual);
7510 /* list_concat avoids modifying the passed-in indexQuals list */
7511 selectivityQuals = list_concat(predExtraQuals, indexQuals);
7514 selectivityQuals = indexQuals;
7516 /* Estimate the fraction of main-table tuples that will be visited */
7517 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
7522 /* fetch estimated page cost for tablespace containing index */
7523 get_tablespace_page_costs(index->reltablespace,
7524 &spc_random_page_cost,
7528 * Generic assumption about index correlation: there isn't any.
7530 *indexCorrelation = 0.0;
7533 * Examine quals to estimate number of search entries & partial matches
7535 memset(&counts, 0, sizeof(counts));
7536 counts.arrayScans = 1;
7537 matchPossible = true;
7541 IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(l);
7542 Expr *clause = qinfo->rinfo->clause;
7544 if (IsA(clause, OpExpr))
7546 matchPossible = gincost_opexpr(root,
7553 else if (IsA(clause, ScalarArrayOpExpr))
7555 matchPossible = gincost_scalararrayopexpr(root,
7565 /* shouldn't be anything else for a GIN index */
7566 elog(ERROR, "unsupported GIN indexqual type: %d",
7567 (int) nodeTag(clause));
7571 /* Fall out if there were any provably-unsatisfiable quals */
7574 *indexStartupCost = 0;
7575 *indexTotalCost = 0;
7576 *indexSelectivity = 0;
7580 if (counts.haveFullScan || indexQuals == NIL)
7583 * Full index scan will be required. We treat this as if every key in
7584 * the index had been listed in the query; is that reasonable?
7586 counts.partialEntries = 0;
7587 counts.exactEntries = numEntries;
7588 counts.searchEntries = numEntries;
7591 /* Will we have more than one iteration of a nestloop scan? */
7592 outer_scans = loop_count;
7595 * Compute cost to begin scan, first of all, pay attention to pending
7598 entryPagesFetched = numPendingPages;
7601 * Estimate number of entry pages read. We need to do
7602 * counts.searchEntries searches. Use a power function as it should be,
7603 * but tuples on leaf pages usually is much greater. Here we include all
7604 * searches in entry tree, including search of first entry in partial
7607 entryPagesFetched += ceil(counts.searchEntries * rint(pow(numEntryPages, 0.15)));
7610 * Add an estimate of entry pages read by partial match algorithm. It's a
7611 * scan over leaf pages in entry tree. We haven't any useful stats here,
7612 * so estimate it as proportion. Because counts.partialEntries is really
7613 * pretty bogus (see code above), it's possible that it is more than
7614 * numEntries; clamp the proportion to ensure sanity.
7616 partialScale = counts.partialEntries / numEntries;
7617 partialScale = Min(partialScale, 1.0);
7619 entryPagesFetched += ceil(numEntryPages * partialScale);
7622 * Partial match algorithm reads all data pages before doing actual scan,
7623 * so it's a startup cost. Again, we haven't any useful stats here, so
7624 * estimate it as proportion.
7626 dataPagesFetched = ceil(numDataPages * partialScale);
7629 * Calculate cache effects if more than one scan due to nestloops or array
7630 * quals. The result is pro-rated per nestloop scan, but the array qual
7631 * factor shouldn't be pro-rated (compare genericcostestimate).
7633 if (outer_scans > 1 || counts.arrayScans > 1)
7635 entryPagesFetched *= outer_scans * counts.arrayScans;
7636 entryPagesFetched = index_pages_fetched(entryPagesFetched,
7637 (BlockNumber) numEntryPages,
7638 numEntryPages, root);
7639 entryPagesFetched /= outer_scans;
7640 dataPagesFetched *= outer_scans * counts.arrayScans;
7641 dataPagesFetched = index_pages_fetched(dataPagesFetched,
7642 (BlockNumber) numDataPages,
7643 numDataPages, root);
7644 dataPagesFetched /= outer_scans;
7648 * Here we use random page cost because logically-close pages could be far
7651 *indexStartupCost = (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
7654 * Now compute the number of data pages fetched during the scan.
7656 * We assume every entry to have the same number of items, and that there
7657 * is no overlap between them. (XXX: tsvector and array opclasses collect
7658 * statistics on the frequency of individual keys; it would be nice to use
7661 dataPagesFetched = ceil(numDataPages * counts.exactEntries / numEntries);
7664 * If there is a lot of overlap among the entries, in particular if one of
7665 * the entries is very frequent, the above calculation can grossly
7666 * under-estimate. As a simple cross-check, calculate a lower bound based
7667 * on the overall selectivity of the quals. At a minimum, we must read
7668 * one item pointer for each matching entry.
7670 * The width of each item pointer varies, based on the level of
7671 * compression. We don't have statistics on that, but an average of
7672 * around 3 bytes per item is fairly typical.
7674 dataPagesFetchedBySel = ceil(*indexSelectivity *
7675 (numTuples / (BLCKSZ / 3)));
7676 if (dataPagesFetchedBySel > dataPagesFetched)
7677 dataPagesFetched = dataPagesFetchedBySel;
7679 /* Account for cache effects, the same as above */
7680 if (outer_scans > 1 || counts.arrayScans > 1)
7682 dataPagesFetched *= outer_scans * counts.arrayScans;
7683 dataPagesFetched = index_pages_fetched(dataPagesFetched,
7684 (BlockNumber) numDataPages,
7685 numDataPages, root);
7686 dataPagesFetched /= outer_scans;
7689 /* And apply random_page_cost as the cost per page */
7690 *indexTotalCost = *indexStartupCost +
7691 dataPagesFetched * spc_random_page_cost;
7694 * Add on index qual eval costs, much as in genericcostestimate
7696 qual_arg_cost = other_operands_eval_cost(root, qinfos) +
7697 orderby_operands_eval_cost(root, path);
7698 qual_op_cost = cpu_operator_cost *
7699 (list_length(indexQuals) + list_length(indexOrderBys));
7701 *indexStartupCost += qual_arg_cost;
7702 *indexTotalCost += qual_arg_cost;
7703 *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost + qual_op_cost);
7704 *indexPages = dataPagesFetched;
7708 * BRIN has search behavior completely different from other index types
7711 brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
7712 Cost *indexStartupCost, Cost *indexTotalCost,
7713 Selectivity *indexSelectivity, double *indexCorrelation,
7716 IndexOptInfo *index = path->indexinfo;
7717 List *indexQuals = path->indexquals;
7718 double numPages = index->pages;
7719 RelOptInfo *baserel = index->rel;
7720 RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
7722 Cost spc_seq_page_cost;
7723 Cost spc_random_page_cost;
7724 double qual_arg_cost;
7725 double qualSelectivity;
7726 BrinStatsData statsData;
7728 double minimalRanges;
7729 double estimatedRanges;
7733 VariableStatData vardata;
7735 Assert(rte->rtekind == RTE_RELATION);
7737 /* fetch estimated page cost for the tablespace containing the index */
7738 get_tablespace_page_costs(index->reltablespace,
7739 &spc_random_page_cost,
7740 &spc_seq_page_cost);
7743 * Obtain some data from the index itself.
7745 indexRel = index_open(index->indexoid, AccessShareLock);
7746 brinGetStats(indexRel, &statsData);
7747 index_close(indexRel, AccessShareLock);
7750 * Compute index correlation
7752 * Because we can use all index quals equally when scanning, we can use
7753 * the largest correlation (in absolute value) among columns used by the
7754 * query. Start at zero, the worst possible case. If we cannot find
7755 * any correlation statistics, we will keep it as 0.
7757 *indexCorrelation = 0;
7759 qinfos = deconstruct_indexquals(path);
7762 IndexQualInfo *qinfo = (IndexQualInfo *) lfirst(l);
7763 AttrNumber attnum = index->indexkeys[qinfo->indexcol];
7765 /* attempt to lookup stats in relation for this index column */
7768 /* Simple variable -- look to stats for the underlying table */
7769 if (get_relation_stats_hook &&
7770 (*get_relation_stats_hook) (root, rte, attnum, &vardata))
7773 * The hook took control of acquiring a stats tuple. If it
7774 * did supply a tuple, it'd better have supplied a freefunc.
7776 if (HeapTupleIsValid(vardata.statsTuple) && !vardata.freefunc)
7778 "no function provided to release variable stats with");
7782 vardata.statsTuple =
7783 SearchSysCache3(STATRELATTINH,
7784 ObjectIdGetDatum(rte->relid),
7785 Int16GetDatum(attnum),
7786 BoolGetDatum(false));
7787 vardata.freefunc = ReleaseSysCache;
7793 * Looks like we've found an expression column in the index. Let's
7794 * see if there's any stats for it.
7797 /* get the attnum from the 0-based index. */
7798 attnum = qinfo->indexcol + 1;
7800 if (get_index_stats_hook &&
7801 (*get_index_stats_hook) (root, index->indexoid, attnum, &vardata))
7804 * The hook took control of acquiring a stats tuple. If it did
7805 * supply a tuple, it'd better have supplied a freefunc.
7807 if (HeapTupleIsValid(vardata.statsTuple) &&
7809 elog(ERROR, "no function provided to release variable stats with");
7813 vardata.statsTuple = SearchSysCache3(STATRELATTINH,
7814 ObjectIdGetDatum(index->indexoid),
7815 Int16GetDatum(attnum),
7816 BoolGetDatum(false));
7817 vardata.freefunc = ReleaseSysCache;
7821 if (HeapTupleIsValid(vardata.statsTuple))
7826 if (get_attstatsslot(vardata.statsTuple, InvalidOid, 0,
7827 STATISTIC_KIND_CORRELATION,
7831 &numbers, &nnumbers))
7833 double varCorrelation = 0.0;
7836 varCorrelation = Abs(numbers[0]);
7838 if (varCorrelation > *indexCorrelation)
7839 *indexCorrelation = varCorrelation;
7841 free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers);
7845 ReleaseVariableStats(vardata);
7848 qualSelectivity = clauselist_selectivity(root, indexQuals,
7852 /* work out the actual number of ranges in the index */
7853 indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),
7857 * Now calculate the minimum possible ranges we could match with if all of
7858 * the rows were in the perfect order in the table's heap.
7860 minimalRanges = ceil(indexRanges * qualSelectivity);
7863 * Now estimate the number of ranges that we'll touch by using the
7864 * indexCorrelation from the stats. Careful not to divide by zero
7865 * (note we're using the absolute value of the correlation).
7867 if (*indexCorrelation < 1.0e-10)
7868 estimatedRanges = indexRanges;
7870 estimatedRanges = Min(minimalRanges / *indexCorrelation, indexRanges);
7872 /* we expect to visit this portion of the table */
7873 selec = estimatedRanges / indexRanges;
7875 CLAMP_PROBABILITY(selec);
7877 *indexSelectivity = selec;
7880 * Compute the index qual costs, much as in genericcostestimate, to add
7881 * to the index costs.
7883 qual_arg_cost = other_operands_eval_cost(root, qinfos) +
7884 orderby_operands_eval_cost(root, path);
7887 * Compute the startup cost as the cost to read the whole revmap
7888 * sequentially, including the cost to execute the index quals.
7891 spc_seq_page_cost * statsData.revmapNumPages * loop_count;
7892 *indexStartupCost += qual_arg_cost;
7895 * To read a BRIN index there might be a bit of back and forth over
7896 * regular pages, as revmap might point to them out of sequential order;
7897 * calculate the total cost as reading the whole index in random order.
7899 *indexTotalCost = *indexStartupCost +
7900 spc_random_page_cost * (numPages - statsData.revmapNumPages) * loop_count;
7903 * Charge a small amount per range tuple which we expect to match to. This
7904 * is meant to reflect the costs of manipulating the bitmap. The BRIN scan
7905 * will set a bit for each page in the range when we find a matching
7906 * range, so we must multiply the charge by the number of pages in the
7909 *indexTotalCost += 0.1 * cpu_operator_cost * estimatedRanges *
7910 statsData.pagesPerRange;
7912 *indexPages = index->pages;