1 /*-------------------------------------------------------------------------
4 * Selectivity functions and index cost estimation functions for
5 * standard operators and index access methods.
7 * Selectivity routines are registered in the pg_operator catalog
8 * in the "oprrest" and "oprjoin" attributes.
10 * Index cost functions are registered in the pg_am catalog
11 * in the "amcostestimate" attribute.
13 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
18 * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.226 2007/02/19 07:03:31 tgl Exp $
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 call convention for a join estimator (oprjoin function) is similar
59 * except that varRelid is not needed, and instead the join type is
62 * Selectivity oprjoin (PlannerInfo *root,
67 * float8 oprjoin (internal, oid, internal, int2);
69 * (We deliberately make the SQL signature different to facilitate
79 #include "catalog/pg_opfamily.h"
80 #include "catalog/pg_statistic.h"
81 #include "catalog/pg_type.h"
82 #include "mb/pg_wchar.h"
83 #include "nodes/makefuncs.h"
84 #include "optimizer/clauses.h"
85 #include "optimizer/cost.h"
86 #include "optimizer/pathnode.h"
87 #include "optimizer/paths.h"
88 #include "optimizer/plancat.h"
89 #include "optimizer/restrictinfo.h"
90 #include "optimizer/var.h"
91 #include "parser/parse_coerce.h"
92 #include "parser/parse_expr.h"
93 #include "parser/parsetree.h"
94 #include "utils/builtins.h"
95 #include "utils/date.h"
96 #include "utils/datum.h"
97 #include "utils/fmgroids.h"
98 #include "utils/lsyscache.h"
99 #include "utils/nabstime.h"
100 #include "utils/pg_locale.h"
101 #include "utils/selfuncs.h"
102 #include "utils/syscache.h"
105 static double ineq_histogram_selectivity(VariableStatData *vardata,
106 FmgrInfo *opproc, bool isgt,
107 Datum constval, Oid consttype);
108 static bool convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
109 Datum lobound, Datum hibound, Oid boundstypid,
110 double *scaledlobound, double *scaledhibound);
111 static double convert_numeric_to_scalar(Datum value, Oid typid);
112 static void convert_string_to_scalar(char *value,
115 double *scaledlobound,
117 double *scaledhibound);
118 static void convert_bytea_to_scalar(Datum value,
121 double *scaledlobound,
123 double *scaledhibound);
124 static double convert_one_string_to_scalar(char *value,
125 int rangelo, int rangehi);
126 static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
127 int rangelo, int rangehi);
128 static char *convert_string_datum(Datum value, Oid typid);
129 static double convert_timevalue_to_scalar(Datum value, Oid typid);
130 static bool get_variable_maximum(PlannerInfo *root, VariableStatData *vardata,
131 Oid sortop, Datum *max);
132 static Selectivity prefix_selectivity(VariableStatData *vardata,
133 Oid vartype, Oid opfamily, Const *prefixcon);
134 static Selectivity pattern_selectivity(Const *patt, Pattern_Type ptype);
135 static Datum string_to_datum(const char *str, Oid datatype);
136 static Const *string_to_const(const char *str, Oid datatype);
137 static Const *string_to_bytea_const(const char *str, size_t str_len);
141 * eqsel - Selectivity of "=" for any data types.
143 * Note: this routine is also used to estimate selectivity for some
144 * operators that are not "=" but have comparable selectivity behavior,
145 * such as "~=" (geometric approximate-match). Even for "=", we must
146 * keep in mind that the left and right datatypes may differ.
149 eqsel(PG_FUNCTION_ARGS)
151 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
152 Oid operator = PG_GETARG_OID(1);
153 List *args = (List *) PG_GETARG_POINTER(2);
154 int varRelid = PG_GETARG_INT32(3);
155 VariableStatData vardata;
165 * If expression is not variable = something or something = variable, then
166 * punt and return a default estimate.
168 if (!get_restriction_variable(root, args, varRelid,
169 &vardata, &other, &varonleft))
170 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
173 * If the something is a NULL constant, assume operator is strict and
174 * return zero, ie, operator will never return TRUE.
176 if (IsA(other, Const) &&
177 ((Const *) other)->constisnull)
179 ReleaseVariableStats(vardata);
180 PG_RETURN_FLOAT8(0.0);
183 if (HeapTupleIsValid(vardata.statsTuple))
185 Form_pg_statistic stats;
187 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
189 if (IsA(other, Const))
191 /* Variable is being compared to a known non-null constant */
192 Datum constval = ((Const *) other)->constvalue;
197 * Is the constant "=" to any of the column's most common values?
198 * (Although the given operator may not really be "=", we will
199 * assume that seeing whether it returns TRUE is an appropriate
200 * test. If you don't like this, maybe you shouldn't be using
201 * eqsel for your operator...)
203 if (get_attstatsslot(vardata.statsTuple,
204 vardata.atttype, vardata.atttypmod,
205 STATISTIC_KIND_MCV, InvalidOid,
207 &numbers, &nnumbers))
211 fmgr_info(get_opcode(operator), &eqproc);
213 for (i = 0; i < nvalues; i++)
215 /* be careful to apply operator right way 'round */
217 match = DatumGetBool(FunctionCall2(&eqproc,
221 match = DatumGetBool(FunctionCall2(&eqproc,
230 /* no most-common-value info available */
233 i = nvalues = nnumbers = 0;
239 * Constant is "=" to this common value. We know selectivity
240 * exactly (or as exactly as ANALYZE could calculate it,
248 * Comparison is against a constant that is neither NULL nor
249 * any of the common values. Its selectivity cannot be more
252 double sumcommon = 0.0;
253 double otherdistinct;
255 for (i = 0; i < nnumbers; i++)
256 sumcommon += numbers[i];
257 selec = 1.0 - sumcommon - stats->stanullfrac;
258 CLAMP_PROBABILITY(selec);
261 * and in fact it's probably a good deal less. We approximate
262 * that all the not-common values share this remaining
263 * fraction equally, so we divide by the number of other
266 otherdistinct = get_variable_numdistinct(&vardata)
268 if (otherdistinct > 1)
269 selec /= otherdistinct;
272 * Another cross-check: selectivity shouldn't be estimated as
273 * more than the least common "most common value".
275 if (nnumbers > 0 && selec > numbers[nnumbers - 1])
276 selec = numbers[nnumbers - 1];
279 free_attstatsslot(vardata.atttype, values, nvalues,
287 * Search is for a value that we do not know a priori, but we will
288 * assume it is not NULL. Estimate the selectivity as non-null
289 * fraction divided by number of distinct values, so that we get a
290 * result averaged over all possible values whether common or
291 * uncommon. (Essentially, we are assuming that the not-yet-known
292 * comparison value is equally likely to be any of the possible
293 * values, regardless of their frequency in the table. Is that a
296 selec = 1.0 - stats->stanullfrac;
297 ndistinct = get_variable_numdistinct(&vardata);
302 * Cross-check: selectivity should never be estimated as more than
303 * the most common value's.
305 if (get_attstatsslot(vardata.statsTuple,
306 vardata.atttype, vardata.atttypmod,
307 STATISTIC_KIND_MCV, InvalidOid,
309 &numbers, &nnumbers))
311 if (nnumbers > 0 && selec > numbers[0])
313 free_attstatsslot(vardata.atttype, NULL, 0, numbers, nnumbers);
320 * No ANALYZE stats available, so make a guess using estimated number
321 * of distinct values and assuming they are equally common. (The guess
322 * is unlikely to be very good, but we do know a few special cases.)
324 selec = 1.0 / get_variable_numdistinct(&vardata);
327 ReleaseVariableStats(vardata);
329 /* result should be in range, but make sure... */
330 CLAMP_PROBABILITY(selec);
332 PG_RETURN_FLOAT8((float8) selec);
336 * neqsel - Selectivity of "!=" for any data types.
338 * This routine is also used for some operators that are not "!="
339 * but have comparable selectivity behavior. See above comments
343 neqsel(PG_FUNCTION_ARGS)
345 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
346 Oid operator = PG_GETARG_OID(1);
347 List *args = (List *) PG_GETARG_POINTER(2);
348 int varRelid = PG_GETARG_INT32(3);
353 * We want 1 - eqsel() where the equality operator is the one associated
354 * with this != operator, that is, its negator.
356 eqop = get_negator(operator);
359 result = DatumGetFloat8(DirectFunctionCall4(eqsel,
360 PointerGetDatum(root),
361 ObjectIdGetDatum(eqop),
362 PointerGetDatum(args),
363 Int32GetDatum(varRelid)));
367 /* Use default selectivity (should we raise an error instead?) */
368 result = DEFAULT_EQ_SEL;
370 result = 1.0 - result;
371 PG_RETURN_FLOAT8(result);
375 * scalarineqsel - Selectivity of "<", "<=", ">", ">=" for scalars.
377 * This is the guts of both scalarltsel and scalargtsel. The caller has
378 * commuted the clause, if necessary, so that we can treat the variable as
379 * being on the left. The caller must also make sure that the other side
380 * of the clause is a non-null Const, and dissect same into a value and
383 * This routine works for any datatype (or pair of datatypes) known to
384 * convert_to_scalar(). If it is applied to some other datatype,
385 * it will return a default estimate.
388 scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
389 VariableStatData *vardata, Datum constval, Oid consttype)
391 Form_pg_statistic stats;
398 if (!HeapTupleIsValid(vardata->statsTuple))
400 /* no stats available, so default result */
401 return DEFAULT_INEQ_SEL;
403 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
405 fmgr_info(get_opcode(operator), &opproc);
408 * If we have most-common-values info, add up the fractions of the MCV
409 * entries that satisfy MCV OP CONST. These fractions contribute directly
410 * to the result selectivity. Also add up the total fraction represented
413 mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
417 * If there is a histogram, determine which bin the constant falls in, and
418 * compute the resulting contribution to selectivity.
420 hist_selec = ineq_histogram_selectivity(vardata, &opproc, isgt,
421 constval, consttype);
424 * Now merge the results from the MCV and histogram calculations,
425 * realizing that the histogram covers only the non-null values that are
428 selec = 1.0 - stats->stanullfrac - sumcommon;
430 if (hist_selec > 0.0)
435 * If no histogram but there are values not accounted for by MCV,
436 * arbitrarily assume half of them will match.
443 /* result should be in range, but make sure... */
444 CLAMP_PROBABILITY(selec);
450 * mcv_selectivity - Examine the MCV list for selectivity estimates
452 * Determine the fraction of the variable's MCV population that satisfies
453 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
454 * compute the fraction of the total column population represented by the MCV
455 * list. This code will work for any boolean-returning predicate operator.
457 * The function result is the MCV selectivity, and the fraction of the
458 * total population is returned into *sumcommonp. Zeroes are returned
459 * if there is no MCV list.
462 mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
463 Datum constval, bool varonleft,
477 if (HeapTupleIsValid(vardata->statsTuple) &&
478 get_attstatsslot(vardata->statsTuple,
479 vardata->atttype, vardata->atttypmod,
480 STATISTIC_KIND_MCV, InvalidOid,
482 &numbers, &nnumbers))
484 for (i = 0; i < nvalues; i++)
487 DatumGetBool(FunctionCall2(opproc,
490 DatumGetBool(FunctionCall2(opproc,
493 mcv_selec += numbers[i];
494 sumcommon += numbers[i];
496 free_attstatsslot(vardata->atttype, values, nvalues,
500 *sumcommonp = sumcommon;
505 * histogram_selectivity - Examine the histogram for selectivity estimates
507 * Determine the fraction of the variable's histogram entries that satisfy
508 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft.
510 * This code will work for any boolean-returning predicate operator, whether
511 * or not it has anything to do with the histogram sort operator. We are
512 * essentially using the histogram just as a representative sample. However,
513 * small histograms are unlikely to be all that representative, so the caller
514 * should specify a minimum histogram size to use, and fall back on some
515 * other approach if this routine fails.
517 * The caller also specifies n_skip, which causes us to ignore the first and
518 * last n_skip histogram elements, on the grounds that they are outliers and
519 * hence not very representative. If in doubt, min_hist_size = 100 and
520 * n_skip = 1 are reasonable values.
522 * The function result is the selectivity, or -1 if there is no histogram
523 * or it's smaller than min_hist_size.
525 * Note that the result disregards both the most-common-values (if any) and
526 * null entries. The caller is expected to combine this result with
527 * statistics for those portions of the column population. It may also be
528 * prudent to clamp the result range, ie, disbelieve exact 0 or 1 outputs.
531 histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
532 Datum constval, bool varonleft,
533 int min_hist_size, int n_skip)
539 /* check sanity of parameters */
541 Assert(min_hist_size > 2 * n_skip);
543 if (HeapTupleIsValid(vardata->statsTuple) &&
544 get_attstatsslot(vardata->statsTuple,
545 vardata->atttype, vardata->atttypmod,
546 STATISTIC_KIND_HISTOGRAM, InvalidOid,
550 if (nvalues >= min_hist_size)
555 for (i = n_skip; i < nvalues - n_skip; i++)
558 DatumGetBool(FunctionCall2(opproc,
561 DatumGetBool(FunctionCall2(opproc,
566 result = ((double) nmatch) / ((double) (nvalues - 2 * n_skip));
570 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
579 * ineq_histogram_selectivity - Examine the histogram for scalarineqsel
581 * Determine the fraction of the variable's histogram population that
582 * satisfies the inequality condition, ie, VAR < CONST or VAR > CONST.
584 * Returns zero if there is no histogram (valid results will always be
585 * greater than zero).
587 * Note that the result disregards both the most-common-values (if any) and
588 * null entries. The caller is expected to combine this result with
589 * statistics for those portions of the column population.
592 ineq_histogram_selectivity(VariableStatData *vardata,
593 FmgrInfo *opproc, bool isgt,
594 Datum constval, Oid consttype)
603 * Someday, ANALYZE might store more than one histogram per rel/att,
604 * corresponding to more than one possible sort ordering defined for the
605 * column type. However, to make that work we will need to figure out
606 * which staop to search for --- it's not necessarily the one we have at
607 * hand! (For example, we might have a '<=' operator rather than the '<'
608 * operator that will appear in staop.) For now, assume that whatever
609 * appears in pg_statistic is sorted the same way our operator sorts, or
610 * the reverse way if isgt is TRUE.
612 if (HeapTupleIsValid(vardata->statsTuple) &&
613 get_attstatsslot(vardata->statsTuple,
614 vardata->atttype, vardata->atttypmod,
615 STATISTIC_KIND_HISTOGRAM, InvalidOid,
622 * Use binary search to find proper location, ie, the first slot
623 * at which the comparison fails. (If the given operator isn't
624 * actually sort-compatible with the histogram, you'll get garbage
625 * results ... but probably not any more garbage-y than you would
626 * from the old linear search.)
629 int lobound = 0; /* first possible slot to search */
630 int hibound = nvalues; /* last+1 slot to search */
632 while (lobound < hibound)
634 int probe = (lobound + hibound) / 2;
637 ltcmp = DatumGetBool(FunctionCall2(opproc,
650 /* Constant is below lower histogram boundary. */
653 else if (lobound >= nvalues)
655 /* Constant is above upper histogram boundary. */
667 * We have values[i-1] < constant < values[i].
669 * Convert the constant and the two nearest bin boundary
670 * values to a uniform comparison scale, and do a linear
671 * interpolation within this bin.
673 if (convert_to_scalar(constval, consttype, &val,
674 values[i - 1], values[i],
680 /* cope if bin boundaries appear identical */
685 else if (val >= high)
689 binfrac = (val - low) / (high - low);
692 * Watch out for the possibility that we got a NaN or
693 * Infinity from the division. This can happen
694 * despite the previous checks, if for example "low"
697 if (isnan(binfrac) ||
698 binfrac < 0.0 || binfrac > 1.0)
705 * Ideally we'd produce an error here, on the grounds that
706 * the given operator shouldn't have scalarXXsel
707 * registered as its selectivity func unless we can deal
708 * with its operand types. But currently, all manner of
709 * stuff is invoking scalarXXsel, so give a default
710 * estimate until that can be fixed.
716 * Now, compute the overall selectivity across the values
717 * represented by the histogram. We have i-1 full bins and
718 * binfrac partial bin below the constant.
720 histfrac = (double) (i - 1) + binfrac;
721 histfrac /= (double) (nvalues - 1);
725 * Now histfrac = fraction of histogram entries below the
728 * Account for "<" vs ">"
730 hist_selec = isgt ? (1.0 - histfrac) : histfrac;
733 * The histogram boundaries are only approximate to begin with,
734 * and may well be out of date anyway. Therefore, don't believe
735 * extremely small or large selectivity estimates.
737 if (hist_selec < 0.0001)
739 else if (hist_selec > 0.9999)
743 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
750 * scalarltsel - Selectivity of "<" (also "<=") for scalars.
753 scalarltsel(PG_FUNCTION_ARGS)
755 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
756 Oid operator = PG_GETARG_OID(1);
757 List *args = (List *) PG_GETARG_POINTER(2);
758 int varRelid = PG_GETARG_INT32(3);
759 VariableStatData vardata;
768 * If expression is not variable op something or something op variable,
769 * then punt and return a default estimate.
771 if (!get_restriction_variable(root, args, varRelid,
772 &vardata, &other, &varonleft))
773 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
776 * Can't do anything useful if the something is not a constant, either.
778 if (!IsA(other, Const))
780 ReleaseVariableStats(vardata);
781 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
785 * If the constant is NULL, assume operator is strict and return zero, ie,
786 * operator will never return TRUE.
788 if (((Const *) other)->constisnull)
790 ReleaseVariableStats(vardata);
791 PG_RETURN_FLOAT8(0.0);
793 constval = ((Const *) other)->constvalue;
794 consttype = ((Const *) other)->consttype;
797 * Force the var to be on the left to simplify logic in scalarineqsel.
801 /* we have var < other */
806 /* we have other < var, commute to make var > other */
807 operator = get_commutator(operator);
810 /* Use default selectivity (should we raise an error instead?) */
811 ReleaseVariableStats(vardata);
812 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
817 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
819 ReleaseVariableStats(vardata);
821 PG_RETURN_FLOAT8((float8) selec);
825 * scalargtsel - Selectivity of ">" (also ">=") for integers.
828 scalargtsel(PG_FUNCTION_ARGS)
830 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
831 Oid operator = PG_GETARG_OID(1);
832 List *args = (List *) PG_GETARG_POINTER(2);
833 int varRelid = PG_GETARG_INT32(3);
834 VariableStatData vardata;
843 * If expression is not variable op something or something op variable,
844 * then punt and return a default estimate.
846 if (!get_restriction_variable(root, args, varRelid,
847 &vardata, &other, &varonleft))
848 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
851 * Can't do anything useful if the something is not a constant, either.
853 if (!IsA(other, Const))
855 ReleaseVariableStats(vardata);
856 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
860 * If the constant is NULL, assume operator is strict and return zero, ie,
861 * operator will never return TRUE.
863 if (((Const *) other)->constisnull)
865 ReleaseVariableStats(vardata);
866 PG_RETURN_FLOAT8(0.0);
868 constval = ((Const *) other)->constvalue;
869 consttype = ((Const *) other)->consttype;
872 * Force the var to be on the left to simplify logic in scalarineqsel.
876 /* we have var > other */
881 /* we have other > var, commute to make var < other */
882 operator = get_commutator(operator);
885 /* Use default selectivity (should we raise an error instead?) */
886 ReleaseVariableStats(vardata);
887 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
892 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
894 ReleaseVariableStats(vardata);
896 PG_RETURN_FLOAT8((float8) selec);
900 * patternsel - Generic code for pattern-match selectivity.
903 patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
905 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
906 Oid operator = PG_GETARG_OID(1);
907 List *args = (List *) PG_GETARG_POINTER(2);
908 int varRelid = PG_GETARG_INT32(3);
909 VariableStatData vardata;
917 Pattern_Prefix_Status pstatus;
919 Const *prefix = NULL;
924 * If expression is not variable op constant, then punt and return a
927 if (!get_restriction_variable(root, args, varRelid,
928 &vardata, &other, &varonleft))
929 return DEFAULT_MATCH_SEL;
930 if (!varonleft || !IsA(other, Const))
932 ReleaseVariableStats(vardata);
933 return DEFAULT_MATCH_SEL;
935 variable = (Node *) linitial(args);
938 * If the constant is NULL, assume operator is strict and return zero, ie,
939 * operator will never return TRUE.
941 if (((Const *) other)->constisnull)
943 ReleaseVariableStats(vardata);
946 constval = ((Const *) other)->constvalue;
947 consttype = ((Const *) other)->consttype;
950 * The right-hand const is type text or bytea for all supported operators.
951 * We do not expect to see binary-compatible types here, since
952 * const-folding should have relabeled the const to exactly match the
953 * operator's declared type.
955 if (consttype != TEXTOID && consttype != BYTEAOID)
957 ReleaseVariableStats(vardata);
958 return DEFAULT_MATCH_SEL;
962 * Similarly, the exposed type of the left-hand side should be one of
963 * those we know. (Do not look at vardata.atttype, which might be
964 * something binary-compatible but different.) We can use it to choose
965 * the index opfamily from which we must draw the comparison operators.
967 * NOTE: It would be more correct to use the PATTERN opfamilies than the
968 * simple ones, but at the moment ANALYZE will not generate statistics for
969 * the PATTERN operators. But our results are so approximate anyway that
970 * it probably hardly matters.
972 vartype = vardata.vartype;
977 opfamily = TEXT_BTREE_FAM_OID;
980 opfamily = BPCHAR_BTREE_FAM_OID;
983 opfamily = NAME_BTREE_FAM_OID;
986 opfamily = BYTEA_BTREE_FAM_OID;
989 ReleaseVariableStats(vardata);
990 return DEFAULT_MATCH_SEL;
993 /* divide pattern into fixed prefix and remainder */
994 patt = (Const *) other;
995 pstatus = pattern_fixed_prefix(patt, ptype, &prefix, &rest);
998 * If necessary, coerce the prefix constant to the right type. (The "rest"
999 * constant need not be changed.)
1001 if (prefix && prefix->consttype != vartype)
1005 switch (prefix->consttype)
1008 prefixstr = DatumGetCString(DirectFunctionCall1(textout,
1009 prefix->constvalue));
1012 prefixstr = DatumGetCString(DirectFunctionCall1(byteaout,
1013 prefix->constvalue));
1016 elog(ERROR, "unrecognized consttype: %u",
1018 ReleaseVariableStats(vardata);
1019 return DEFAULT_MATCH_SEL;
1021 prefix = string_to_const(prefixstr, vartype);
1025 if (pstatus == Pattern_Prefix_Exact)
1028 * Pattern specifies an exact match, so pretend operator is '='
1030 Oid eqopr = get_opfamily_member(opfamily, vartype, vartype,
1031 BTEqualStrategyNumber);
1034 if (eqopr == InvalidOid)
1035 elog(ERROR, "no = operator for opfamily %u", opfamily);
1036 eqargs = list_make2(variable, prefix);
1037 result = DatumGetFloat8(DirectFunctionCall4(eqsel,
1038 PointerGetDatum(root),
1039 ObjectIdGetDatum(eqopr),
1040 PointerGetDatum(eqargs),
1041 Int32GetDatum(varRelid)));
1046 * Not exact-match pattern. If we have a sufficiently large
1047 * histogram, estimate selectivity for the histogram part of the
1048 * population by counting matches in the histogram. If not, estimate
1049 * selectivity of the fixed prefix and remainder of pattern
1050 * separately, then combine the two to get an estimate of the
1051 * selectivity for the part of the column population represented by
1052 * the histogram. We then add up data for any most-common-values
1053 * values; these are not in the histogram population, and we can get
1054 * exact answers for them by applying the pattern operator, so there's
1055 * no reason to approximate. (If the MCVs cover a significant part of
1056 * the total population, this gives us a big leg up in accuracy.)
1064 /* Try to use the histogram entries to get selectivity */
1065 fmgr_info(get_opcode(operator), &opproc);
1067 selec = histogram_selectivity(&vardata, &opproc, constval, true,
1071 /* Nope, so fake it with the heuristic method */
1072 Selectivity prefixsel;
1073 Selectivity restsel;
1075 if (pstatus == Pattern_Prefix_Partial)
1076 prefixsel = prefix_selectivity(&vardata, vartype,
1080 restsel = pattern_selectivity(rest, ptype);
1081 selec = prefixsel * restsel;
1085 /* Yes, but don't believe extremely small or large estimates. */
1088 else if (selec > 0.9999)
1093 * If we have most-common-values info, add up the fractions of the MCV
1094 * entries that satisfy MCV OP PATTERN. These fractions contribute
1095 * directly to the result selectivity. Also add up the total fraction
1096 * represented by MCV entries.
1098 mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
1101 if (HeapTupleIsValid(vardata.statsTuple))
1102 nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
1107 * Now merge the results from the MCV and histogram calculations,
1108 * realizing that the histogram covers only the non-null values that
1109 * are not listed in MCV.
1111 selec *= 1.0 - nullfrac - sumcommon;
1114 /* result should be in range, but make sure... */
1115 CLAMP_PROBABILITY(selec);
1121 pfree(DatumGetPointer(prefix->constvalue));
1125 ReleaseVariableStats(vardata);
1131 * regexeqsel - Selectivity of regular-expression pattern match.
1134 regexeqsel(PG_FUNCTION_ARGS)
1136 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex));
1140 * icregexeqsel - Selectivity of case-insensitive regex match.
1143 icregexeqsel(PG_FUNCTION_ARGS)
1145 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC));
1149 * likesel - Selectivity of LIKE pattern match.
1152 likesel(PG_FUNCTION_ARGS)
1154 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like));
1158 * iclikesel - Selectivity of ILIKE pattern match.
1161 iclikesel(PG_FUNCTION_ARGS)
1163 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC));
1167 * regexnesel - Selectivity of regular-expression pattern non-match.
1170 regexnesel(PG_FUNCTION_ARGS)
1174 result = patternsel(fcinfo, Pattern_Type_Regex);
1175 result = 1.0 - result;
1176 PG_RETURN_FLOAT8(result);
1180 * icregexnesel - Selectivity of case-insensitive regex non-match.
1183 icregexnesel(PG_FUNCTION_ARGS)
1187 result = patternsel(fcinfo, Pattern_Type_Regex_IC);
1188 result = 1.0 - result;
1189 PG_RETURN_FLOAT8(result);
1193 * nlikesel - Selectivity of LIKE pattern non-match.
1196 nlikesel(PG_FUNCTION_ARGS)
1200 result = patternsel(fcinfo, Pattern_Type_Like);
1201 result = 1.0 - result;
1202 PG_RETURN_FLOAT8(result);
1206 * icnlikesel - Selectivity of ILIKE pattern non-match.
1209 icnlikesel(PG_FUNCTION_ARGS)
1213 result = patternsel(fcinfo, Pattern_Type_Like_IC);
1214 result = 1.0 - result;
1215 PG_RETURN_FLOAT8(result);
1219 * booltestsel - Selectivity of BooleanTest Node.
1222 booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
1223 int varRelid, JoinType jointype)
1225 VariableStatData vardata;
1228 examine_variable(root, arg, varRelid, &vardata);
1230 if (HeapTupleIsValid(vardata.statsTuple))
1232 Form_pg_statistic stats;
1239 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1240 freq_null = stats->stanullfrac;
1242 if (get_attstatsslot(vardata.statsTuple,
1243 vardata.atttype, vardata.atttypmod,
1244 STATISTIC_KIND_MCV, InvalidOid,
1246 &numbers, &nnumbers)
1253 * Get first MCV frequency and derive frequency for true.
1255 if (DatumGetBool(values[0]))
1256 freq_true = numbers[0];
1258 freq_true = 1.0 - numbers[0] - freq_null;
1261 * Next derive frequency for false. Then use these as appropriate
1262 * to derive frequency for each case.
1264 freq_false = 1.0 - freq_true - freq_null;
1266 switch (booltesttype)
1269 /* select only NULL values */
1272 case IS_NOT_UNKNOWN:
1273 /* select non-NULL values */
1274 selec = 1.0 - freq_null;
1277 /* select only TRUE values */
1281 /* select non-TRUE values */
1282 selec = 1.0 - freq_true;
1285 /* select only FALSE values */
1289 /* select non-FALSE values */
1290 selec = 1.0 - freq_false;
1293 elog(ERROR, "unrecognized booltesttype: %d",
1294 (int) booltesttype);
1295 selec = 0.0; /* Keep compiler quiet */
1299 free_attstatsslot(vardata.atttype, values, nvalues,
1305 * No most-common-value info available. Still have null fraction
1306 * information, so use it for IS [NOT] UNKNOWN. Otherwise adjust
1307 * for null fraction and assume an even split for boolean tests.
1309 switch (booltesttype)
1314 * Use freq_null directly.
1318 case IS_NOT_UNKNOWN:
1321 * Select not unknown (not null) values. Calculate from
1324 selec = 1.0 - freq_null;
1330 selec = (1.0 - freq_null) / 2.0;
1333 elog(ERROR, "unrecognized booltesttype: %d",
1334 (int) booltesttype);
1335 selec = 0.0; /* Keep compiler quiet */
1343 * If we can't get variable statistics for the argument, perhaps
1344 * clause_selectivity can do something with it. We ignore the
1345 * possibility of a NULL value when using clause_selectivity, and just
1346 * assume the value is either TRUE or FALSE.
1348 switch (booltesttype)
1351 selec = DEFAULT_UNK_SEL;
1353 case IS_NOT_UNKNOWN:
1354 selec = DEFAULT_NOT_UNK_SEL;
1358 selec = (double) clause_selectivity(root, arg,
1359 varRelid, jointype);
1363 selec = 1.0 - (double) clause_selectivity(root, arg,
1364 varRelid, jointype);
1367 elog(ERROR, "unrecognized booltesttype: %d",
1368 (int) booltesttype);
1369 selec = 0.0; /* Keep compiler quiet */
1374 ReleaseVariableStats(vardata);
1376 /* result should be in range, but make sure... */
1377 CLAMP_PROBABILITY(selec);
1379 return (Selectivity) selec;
1383 * nulltestsel - Selectivity of NullTest Node.
1386 nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
1387 Node *arg, int varRelid)
1389 VariableStatData vardata;
1392 examine_variable(root, arg, varRelid, &vardata);
1394 if (HeapTupleIsValid(vardata.statsTuple))
1396 Form_pg_statistic stats;
1399 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1400 freq_null = stats->stanullfrac;
1402 switch (nulltesttype)
1407 * Use freq_null directly.
1414 * Select not unknown (not null) values. Calculate from
1417 selec = 1.0 - freq_null;
1420 elog(ERROR, "unrecognized nulltesttype: %d",
1421 (int) nulltesttype);
1422 return (Selectivity) 0; /* keep compiler quiet */
1428 * No ANALYZE stats available, so make a guess
1430 switch (nulltesttype)
1433 selec = DEFAULT_UNK_SEL;
1436 selec = DEFAULT_NOT_UNK_SEL;
1439 elog(ERROR, "unrecognized nulltesttype: %d",
1440 (int) nulltesttype);
1441 return (Selectivity) 0; /* keep compiler quiet */
1445 ReleaseVariableStats(vardata);
1447 /* result should be in range, but make sure... */
1448 CLAMP_PROBABILITY(selec);
1450 return (Selectivity) selec;
1454 * strip_array_coercion - strip binary-compatible relabeling from an array expr
1456 * For array values, the parser doesn't generate simple RelabelType nodes,
1457 * but function calls of array_type_coerce() or array_type_length_coerce().
1458 * If we want to cope with binary-compatible situations we have to look
1459 * through these calls whenever the element-type coercion is binary-compatible.
1462 strip_array_coercion(Node *node)
1464 /* could be more than one level, so loop */
1467 if (node && IsA(node, RelabelType))
1469 /* We don't really expect this case, but may as well cope */
1470 node = (Node *) ((RelabelType *) node)->arg;
1472 else if (node && IsA(node, FuncExpr))
1474 FuncExpr *fexpr = (FuncExpr *) node;
1480 /* must be the right function(s) */
1481 if (!(fexpr->funcid == F_ARRAY_TYPE_COERCE ||
1482 fexpr->funcid == F_ARRAY_TYPE_LENGTH_COERCE))
1485 /* fetch source and destination array element types */
1486 arg1 = (Node *) linitial(fexpr->args);
1487 src_elem_type = get_element_type(exprType(arg1));
1488 if (src_elem_type == InvalidOid)
1489 break; /* probably shouldn't happen */
1490 tgt_elem_type = get_element_type(fexpr->funcresulttype);
1491 if (tgt_elem_type == InvalidOid)
1492 break; /* probably shouldn't happen */
1494 /* find out how to coerce */
1495 if (!find_coercion_pathway(tgt_elem_type, src_elem_type,
1496 COERCION_EXPLICIT, &funcId))
1497 break; /* definitely shouldn't happen */
1499 if (OidIsValid(funcId))
1500 break; /* non-binary-compatible coercion */
1502 node = arg1; /* OK to look through the node */
1511 * scalararraysel - Selectivity of ScalarArrayOpExpr Node.
1514 scalararraysel(PlannerInfo *root,
1515 ScalarArrayOpExpr *clause,
1516 bool is_join_clause,
1517 int varRelid, JoinType jointype)
1519 Oid operator = clause->opno;
1520 bool useOr = clause->useOr;
1523 Oid nominal_element_type;
1524 RegProcedure oprsel;
1525 FmgrInfo oprselproc;
1530 * First, look up the underlying operator's selectivity estimator. Punt if
1531 * it hasn't got one.
1535 oprsel = get_oprjoin(operator);
1536 selarg4 = Int16GetDatum(jointype);
1540 oprsel = get_oprrest(operator);
1541 selarg4 = Int32GetDatum(varRelid);
1544 return (Selectivity) 0.5;
1545 fmgr_info(oprsel, &oprselproc);
1547 /* deconstruct the expression */
1548 Assert(list_length(clause->args) == 2);
1549 leftop = (Node *) linitial(clause->args);
1550 rightop = (Node *) lsecond(clause->args);
1552 /* get nominal (after relabeling) element type of rightop */
1553 nominal_element_type = get_element_type(exprType(rightop));
1554 if (!OidIsValid(nominal_element_type))
1555 return (Selectivity) 0.5; /* probably shouldn't happen */
1557 /* look through any binary-compatible relabeling of rightop */
1558 rightop = strip_array_coercion(rightop);
1561 * We consider three cases:
1563 * 1. rightop is an Array constant: deconstruct the array, apply the
1564 * operator's selectivity function for each array element, and merge the
1565 * results in the same way that clausesel.c does for AND/OR combinations.
1567 * 2. rightop is an ARRAY[] construct: apply the operator's selectivity
1568 * function for each element of the ARRAY[] construct, and merge.
1570 * 3. otherwise, make a guess ...
1572 if (rightop && IsA(rightop, Const))
1574 Datum arraydatum = ((Const *) rightop)->constvalue;
1575 bool arrayisnull = ((Const *) rightop)->constisnull;
1576 ArrayType *arrayval;
1585 if (arrayisnull) /* qual can't succeed if null array */
1586 return (Selectivity) 0.0;
1587 arrayval = DatumGetArrayTypeP(arraydatum);
1588 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
1589 &elmlen, &elmbyval, &elmalign);
1590 deconstruct_array(arrayval,
1591 ARR_ELEMTYPE(arrayval),
1592 elmlen, elmbyval, elmalign,
1593 &elem_values, &elem_nulls, &num_elems);
1594 s1 = useOr ? 0.0 : 1.0;
1595 for (i = 0; i < num_elems; i++)
1600 args = list_make2(leftop,
1601 makeConst(nominal_element_type,
1606 s2 = DatumGetFloat8(FunctionCall4(&oprselproc,
1607 PointerGetDatum(root),
1608 ObjectIdGetDatum(operator),
1609 PointerGetDatum(args),
1612 s1 = s1 + s2 - s1 * s2;
1617 else if (rightop && IsA(rightop, ArrayExpr) &&
1618 !((ArrayExpr *) rightop)->multidims)
1620 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1625 get_typlenbyval(arrayexpr->element_typeid,
1626 &elmlen, &elmbyval);
1627 s1 = useOr ? 0.0 : 1.0;
1628 foreach(l, arrayexpr->elements)
1630 Node *elem = (Node *) lfirst(l);
1635 * Theoretically, if elem isn't of nominal_element_type we should
1636 * insert a RelabelType, but it seems unlikely that any operator
1637 * estimation function would really care ...
1639 args = list_make2(leftop, elem);
1640 s2 = DatumGetFloat8(FunctionCall4(&oprselproc,
1641 PointerGetDatum(root),
1642 ObjectIdGetDatum(operator),
1643 PointerGetDatum(args),
1646 s1 = s1 + s2 - s1 * s2;
1653 CaseTestExpr *dummyexpr;
1659 * We need a dummy rightop to pass to the operator selectivity
1660 * routine. It can be pretty much anything that doesn't look like a
1661 * constant; CaseTestExpr is a convenient choice.
1663 dummyexpr = makeNode(CaseTestExpr);
1664 dummyexpr->typeId = nominal_element_type;
1665 dummyexpr->typeMod = -1;
1666 args = list_make2(leftop, dummyexpr);
1667 s2 = DatumGetFloat8(FunctionCall4(&oprselproc,
1668 PointerGetDatum(root),
1669 ObjectIdGetDatum(operator),
1670 PointerGetDatum(args),
1672 s1 = useOr ? 0.0 : 1.0;
1675 * Arbitrarily assume 10 elements in the eventual array value (see
1676 * also estimate_array_length)
1678 for (i = 0; i < 10; i++)
1681 s1 = s1 + s2 - s1 * s2;
1687 /* result should be in range, but make sure... */
1688 CLAMP_PROBABILITY(s1);
1694 * Estimate number of elements in the array yielded by an expression.
1696 * It's important that this agree with scalararraysel.
1699 estimate_array_length(Node *arrayexpr)
1701 /* look through any binary-compatible relabeling of arrayexpr */
1702 arrayexpr = strip_array_coercion(arrayexpr);
1704 if (arrayexpr && IsA(arrayexpr, Const))
1706 Datum arraydatum = ((Const *) arrayexpr)->constvalue;
1707 bool arrayisnull = ((Const *) arrayexpr)->constisnull;
1708 ArrayType *arrayval;
1712 arrayval = DatumGetArrayTypeP(arraydatum);
1713 return ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1715 else if (arrayexpr && IsA(arrayexpr, ArrayExpr) &&
1716 !((ArrayExpr *) arrayexpr)->multidims)
1718 return list_length(((ArrayExpr *) arrayexpr)->elements);
1722 /* default guess --- see also scalararraysel */
1728 * rowcomparesel - Selectivity of RowCompareExpr Node.
1730 * We estimate RowCompare selectivity by considering just the first (high
1731 * order) columns, which makes it equivalent to an ordinary OpExpr. While
1732 * this estimate could be refined by considering additional columns, it
1733 * seems unlikely that we could do a lot better without multi-column
1737 rowcomparesel(PlannerInfo *root,
1738 RowCompareExpr *clause,
1739 int varRelid, JoinType jointype)
1742 Oid opno = linitial_oid(clause->opnos);
1744 bool is_join_clause;
1746 /* Build equivalent arg list for single operator */
1747 opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
1749 /* Decide if it's a join clause, same as for OpExpr */
1753 * If we are considering a nestloop join then all clauses are
1754 * restriction clauses, since we are only interested in the one
1757 is_join_clause = false;
1762 * Otherwise, it's a join if there's more than one relation used.
1763 * Notice we ignore the low-order columns here.
1765 is_join_clause = (NumRelids((Node *) opargs) > 1);
1770 /* Estimate selectivity for a join clause. */
1771 s1 = join_selectivity(root, opno,
1777 /* Estimate selectivity for a restriction clause. */
1778 s1 = restriction_selectivity(root, opno,
1787 * eqjoinsel - Join selectivity of "="
1790 eqjoinsel(PG_FUNCTION_ARGS)
1792 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1793 Oid operator = PG_GETARG_OID(1);
1794 List *args = (List *) PG_GETARG_POINTER(2);
1795 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
1797 VariableStatData vardata1;
1798 VariableStatData vardata2;
1801 Form_pg_statistic stats1 = NULL;
1802 Form_pg_statistic stats2 = NULL;
1803 bool have_mcvs1 = false;
1804 Datum *values1 = NULL;
1806 float4 *numbers1 = NULL;
1808 bool have_mcvs2 = false;
1809 Datum *values2 = NULL;
1811 float4 *numbers2 = NULL;
1814 get_join_variables(root, args, &vardata1, &vardata2);
1816 nd1 = get_variable_numdistinct(&vardata1);
1817 nd2 = get_variable_numdistinct(&vardata2);
1819 if (HeapTupleIsValid(vardata1.statsTuple))
1821 stats1 = (Form_pg_statistic) GETSTRUCT(vardata1.statsTuple);
1822 have_mcvs1 = get_attstatsslot(vardata1.statsTuple,
1827 &values1, &nvalues1,
1828 &numbers1, &nnumbers1);
1831 if (HeapTupleIsValid(vardata2.statsTuple))
1833 stats2 = (Form_pg_statistic) GETSTRUCT(vardata2.statsTuple);
1834 have_mcvs2 = get_attstatsslot(vardata2.statsTuple,
1839 &values2, &nvalues2,
1840 &numbers2, &nnumbers2);
1843 if (have_mcvs1 && have_mcvs2)
1846 * We have most-common-value lists for both relations. Run through
1847 * the lists to see which MCVs actually join to each other with the
1848 * given operator. This allows us to determine the exact join
1849 * selectivity for the portion of the relations represented by the MCV
1850 * lists. We still have to estimate for the remaining population, but
1851 * in a skewed distribution this gives us a big leg up in accuracy.
1852 * For motivation see the analysis in Y. Ioannidis and S.
1853 * Christodoulakis, "On the propagation of errors in the size of join
1854 * results", Technical Report 1018, Computer Science Dept., University
1855 * of Wisconsin, Madison, March 1991 (available from ftp.cs.wisc.edu).
1860 double nullfrac1 = stats1->stanullfrac;
1861 double nullfrac2 = stats2->stanullfrac;
1862 double matchprodfreq,
1874 fmgr_info(get_opcode(operator), &eqproc);
1875 hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
1876 hasmatch2 = (bool *) palloc0(nvalues2 * sizeof(bool));
1879 * If we are doing any variant of JOIN_IN, pretend all the values of
1880 * the righthand relation are unique (ie, act as if it's been
1883 * NOTE: it might seem that we should unique-ify the lefthand input
1884 * when considering JOIN_REVERSE_IN. But this is not so, because the
1885 * join clause we've been handed has not been commuted from the way
1886 * the parser originally wrote it. We know that the unique side of
1887 * the IN clause is *always* on the right.
1889 * NOTE: it would be dangerous to try to be smart about JOIN_LEFT or
1890 * JOIN_RIGHT here, because we do not have enough information to
1891 * determine which var is really on which side of the join. Perhaps
1892 * someday we should pass in more information.
1894 if (jointype == JOIN_IN ||
1895 jointype == JOIN_REVERSE_IN ||
1896 jointype == JOIN_UNIQUE_INNER ||
1897 jointype == JOIN_UNIQUE_OUTER)
1899 float4 oneovern = 1.0 / nd2;
1901 for (i = 0; i < nvalues2; i++)
1902 numbers2[i] = oneovern;
1903 nullfrac2 = oneovern;
1907 * Note we assume that each MCV will match at most one member of the
1908 * other MCV list. If the operator isn't really equality, there could
1909 * be multiple matches --- but we don't look for them, both for speed
1910 * and because the math wouldn't add up...
1912 matchprodfreq = 0.0;
1914 for (i = 0; i < nvalues1; i++)
1918 for (j = 0; j < nvalues2; j++)
1922 if (DatumGetBool(FunctionCall2(&eqproc,
1926 hasmatch1[i] = hasmatch2[j] = true;
1927 matchprodfreq += numbers1[i] * numbers2[j];
1933 CLAMP_PROBABILITY(matchprodfreq);
1934 /* Sum up frequencies of matched and unmatched MCVs */
1935 matchfreq1 = unmatchfreq1 = 0.0;
1936 for (i = 0; i < nvalues1; i++)
1939 matchfreq1 += numbers1[i];
1941 unmatchfreq1 += numbers1[i];
1943 CLAMP_PROBABILITY(matchfreq1);
1944 CLAMP_PROBABILITY(unmatchfreq1);
1945 matchfreq2 = unmatchfreq2 = 0.0;
1946 for (i = 0; i < nvalues2; i++)
1949 matchfreq2 += numbers2[i];
1951 unmatchfreq2 += numbers2[i];
1953 CLAMP_PROBABILITY(matchfreq2);
1954 CLAMP_PROBABILITY(unmatchfreq2);
1959 * Compute total frequency of non-null values that are not in the MCV
1962 otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
1963 otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
1964 CLAMP_PROBABILITY(otherfreq1);
1965 CLAMP_PROBABILITY(otherfreq2);
1968 * We can estimate the total selectivity from the point of view of
1969 * relation 1 as: the known selectivity for matched MCVs, plus
1970 * unmatched MCVs that are assumed to match against random members of
1971 * relation 2's non-MCV population, plus non-MCV values that are
1972 * assumed to match against random members of relation 2's unmatched
1973 * MCVs plus non-MCV values.
1975 totalsel1 = matchprodfreq;
1977 totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - nvalues2);
1979 totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
1981 /* Same estimate from the point of view of relation 2. */
1982 totalsel2 = matchprodfreq;
1984 totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - nvalues1);
1986 totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
1990 * Use the smaller of the two estimates. This can be justified in
1991 * essentially the same terms as given below for the no-stats case: to
1992 * a first approximation, we are estimating from the point of view of
1993 * the relation with smaller nd.
1995 selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
2000 * We do not have MCV lists for both sides. Estimate the join
2001 * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). This
2002 * is plausible if we assume that the join operator is strict and the
2003 * non-null values are about equally distributed: a given non-null
2004 * tuple of rel1 will join to either zero or N2*(1-nullfrac2)/nd2 rows
2005 * of rel2, so total join rows are at most
2006 * N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join selectivity of
2007 * not more than (1-nullfrac1)*(1-nullfrac2)/nd2. By the same logic it
2008 * is not more than (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression
2009 * with MIN() is an upper bound. Using the MIN() means we estimate
2010 * from the point of view of the relation with smaller nd (since the
2011 * larger nd is determining the MIN). It is reasonable to assume that
2012 * most tuples in this rel will have join partners, so the bound is
2013 * probably reasonably tight and should be taken as-is.
2015 * XXX Can we be smarter if we have an MCV list for just one side? It
2016 * seems that if we assume equal distribution for the other side, we
2017 * end up with the same answer anyway.
2019 double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
2020 double nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
2022 selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
2030 free_attstatsslot(vardata1.atttype, values1, nvalues1,
2031 numbers1, nnumbers1);
2033 free_attstatsslot(vardata2.atttype, values2, nvalues2,
2034 numbers2, nnumbers2);
2036 ReleaseVariableStats(vardata1);
2037 ReleaseVariableStats(vardata2);
2039 CLAMP_PROBABILITY(selec);
2041 PG_RETURN_FLOAT8((float8) selec);
2045 * neqjoinsel - Join selectivity of "!="
2048 neqjoinsel(PG_FUNCTION_ARGS)
2050 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2051 Oid operator = PG_GETARG_OID(1);
2052 List *args = (List *) PG_GETARG_POINTER(2);
2053 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
2058 * We want 1 - eqjoinsel() where the equality operator is the one
2059 * associated with this != operator, that is, its negator.
2061 eqop = get_negator(operator);
2064 result = DatumGetFloat8(DirectFunctionCall4(eqjoinsel,
2065 PointerGetDatum(root),
2066 ObjectIdGetDatum(eqop),
2067 PointerGetDatum(args),
2068 Int16GetDatum(jointype)));
2072 /* Use default selectivity (should we raise an error instead?) */
2073 result = DEFAULT_EQ_SEL;
2075 result = 1.0 - result;
2076 PG_RETURN_FLOAT8(result);
2080 * scalarltjoinsel - Join selectivity of "<" and "<=" for scalars
2083 scalarltjoinsel(PG_FUNCTION_ARGS)
2085 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
2089 * scalargtjoinsel - Join selectivity of ">" and ">=" for scalars
2092 scalargtjoinsel(PG_FUNCTION_ARGS)
2094 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
2098 * regexeqjoinsel - Join selectivity of regular-expression pattern match.
2101 regexeqjoinsel(PG_FUNCTION_ARGS)
2103 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
2107 * icregexeqjoinsel - Join selectivity of case-insensitive regex match.
2110 icregexeqjoinsel(PG_FUNCTION_ARGS)
2112 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
2116 * likejoinsel - Join selectivity of LIKE pattern match.
2119 likejoinsel(PG_FUNCTION_ARGS)
2121 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
2125 * iclikejoinsel - Join selectivity of ILIKE pattern match.
2128 iclikejoinsel(PG_FUNCTION_ARGS)
2130 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
2134 * regexnejoinsel - Join selectivity of regex non-match.
2137 regexnejoinsel(PG_FUNCTION_ARGS)
2141 result = DatumGetFloat8(regexeqjoinsel(fcinfo));
2142 result = 1.0 - result;
2143 PG_RETURN_FLOAT8(result);
2147 * icregexnejoinsel - Join selectivity of case-insensitive regex non-match.
2150 icregexnejoinsel(PG_FUNCTION_ARGS)
2154 result = DatumGetFloat8(icregexeqjoinsel(fcinfo));
2155 result = 1.0 - result;
2156 PG_RETURN_FLOAT8(result);
2160 * nlikejoinsel - Join selectivity of LIKE pattern non-match.
2163 nlikejoinsel(PG_FUNCTION_ARGS)
2167 result = DatumGetFloat8(likejoinsel(fcinfo));
2168 result = 1.0 - result;
2169 PG_RETURN_FLOAT8(result);
2173 * icnlikejoinsel - Join selectivity of ILIKE pattern non-match.
2176 icnlikejoinsel(PG_FUNCTION_ARGS)
2180 result = DatumGetFloat8(iclikejoinsel(fcinfo));
2181 result = 1.0 - result;
2182 PG_RETURN_FLOAT8(result);
2186 * mergejoinscansel - Scan selectivity of merge join.
2188 * A merge join will stop as soon as it exhausts either input stream.
2189 * Therefore, if we can estimate the ranges of both input variables,
2190 * we can estimate how much of the input will actually be read. This
2191 * can have a considerable impact on the cost when using indexscans.
2193 * clause should be a clause already known to be mergejoinable. opfamily,
2194 * strategy, and nulls_first specify the sort ordering being used.
2196 * *leftscan is set to the fraction of the left-hand variable expected
2197 * to be scanned (0 to 1), and similarly *rightscan for the right-hand
2201 mergejoinscansel(PlannerInfo *root, Node *clause,
2202 Oid opfamily, int strategy, bool nulls_first,
2203 Selectivity *leftscan,
2204 Selectivity *rightscan)
2208 VariableStatData leftvar,
2223 /* Set default results if we can't figure anything out. */
2224 *leftscan = *rightscan = 1.0;
2226 /* Deconstruct the merge clause */
2227 if (!is_opclause(clause))
2228 return; /* shouldn't happen */
2229 opno = ((OpExpr *) clause)->opno;
2230 left = get_leftop((Expr *) clause);
2231 right = get_rightop((Expr *) clause);
2233 return; /* shouldn't happen */
2235 /* Look for stats for the inputs */
2236 examine_variable(root, left, 0, &leftvar);
2237 examine_variable(root, right, 0, &rightvar);
2239 /* Extract the operator's declared left/right datatypes */
2240 get_op_opfamily_properties(opno, opfamily,
2245 Assert(op_strategy == BTEqualStrategyNumber);
2246 Assert(!op_recheck);
2249 * Look up the various operators we need. If we don't find them all,
2250 * it probably means the opfamily is broken, but we cope anyway.
2254 case BTLessStrategyNumber:
2255 lsortop = get_opfamily_member(opfamily, op_lefttype, op_lefttype,
2256 BTLessStrategyNumber);
2257 rsortop = get_opfamily_member(opfamily, op_righttype, op_righttype,
2258 BTLessStrategyNumber);
2259 leop = get_opfamily_member(opfamily, op_lefttype, op_righttype,
2260 BTLessEqualStrategyNumber);
2261 revleop = get_opfamily_member(opfamily, op_righttype, op_lefttype,
2262 BTLessEqualStrategyNumber);
2264 case BTGreaterStrategyNumber:
2265 /* descending-order case */
2266 lsortop = get_opfamily_member(opfamily, op_lefttype, op_lefttype,
2267 BTGreaterStrategyNumber);
2268 rsortop = get_opfamily_member(opfamily, op_righttype, op_righttype,
2269 BTGreaterStrategyNumber);
2270 leop = get_opfamily_member(opfamily, op_lefttype, op_righttype,
2271 BTGreaterEqualStrategyNumber);
2272 revleop = get_opfamily_member(opfamily, op_righttype, op_lefttype,
2273 BTGreaterEqualStrategyNumber);
2276 goto fail; /* shouldn't get here */
2279 if (!OidIsValid(lsortop) ||
2280 !OidIsValid(rsortop) ||
2281 !OidIsValid(leop) ||
2282 !OidIsValid(revleop))
2283 goto fail; /* insufficient info in catalogs */
2285 /* Try to get maximum values of both inputs */
2286 if (!get_variable_maximum(root, &leftvar, lsortop, &leftmax))
2287 goto fail; /* no max available from stats */
2289 if (!get_variable_maximum(root, &rightvar, rsortop, &rightmax))
2290 goto fail; /* no max available from stats */
2293 * Now, the fraction of the left variable that will be scanned is the
2294 * fraction that's <= the right-side maximum value. But only believe
2295 * non-default estimates, else stick with our 1.0. Also, if the sort
2296 * order is nulls-first, we're going to have to read over any nulls too.
2298 selec = scalarineqsel(root, leop, false, &leftvar,
2299 rightmax, op_righttype);
2300 if (selec != DEFAULT_INEQ_SEL)
2302 if (nulls_first && HeapTupleIsValid(leftvar.statsTuple))
2304 Form_pg_statistic stats;
2306 stats = (Form_pg_statistic) GETSTRUCT(leftvar.statsTuple);
2307 selec += stats->stanullfrac;
2308 CLAMP_PROBABILITY(selec);
2313 /* And similarly for the right variable. */
2314 selec = scalarineqsel(root, revleop, false, &rightvar,
2315 leftmax, op_lefttype);
2316 if (selec != DEFAULT_INEQ_SEL)
2318 if (nulls_first && HeapTupleIsValid(rightvar.statsTuple))
2320 Form_pg_statistic stats;
2322 stats = (Form_pg_statistic) GETSTRUCT(rightvar.statsTuple);
2323 selec += stats->stanullfrac;
2324 CLAMP_PROBABILITY(selec);
2330 * Only one of the two fractions can really be less than 1.0; believe the
2331 * smaller estimate and reset the other one to exactly 1.0. If we get
2332 * exactly equal estimates (as can easily happen with self-joins), believe
2335 if (*leftscan > *rightscan)
2337 else if (*leftscan < *rightscan)
2340 *leftscan = *rightscan = 1.0;
2343 ReleaseVariableStats(leftvar);
2344 ReleaseVariableStats(rightvar);
2349 * Helper routine for estimate_num_groups: add an item to a list of
2350 * GroupVarInfos, but only if it's not known equal to any of the existing
2355 Node *var; /* might be an expression, not just a Var */
2356 RelOptInfo *rel; /* relation it belongs to */
2357 double ndistinct; /* # distinct values */
2361 add_unique_group_var(PlannerInfo *root, List *varinfos,
2362 Node *var, VariableStatData *vardata)
2364 GroupVarInfo *varinfo;
2368 ndistinct = get_variable_numdistinct(vardata);
2370 /* cannot use foreach here because of possible list_delete */
2371 lc = list_head(varinfos);
2374 varinfo = (GroupVarInfo *) lfirst(lc);
2376 /* must advance lc before list_delete possibly pfree's it */
2379 /* Drop exact duplicates */
2380 if (equal(var, varinfo->var))
2384 * Drop known-equal vars, but only if they belong to different
2385 * relations (see comments for estimate_num_groups)
2387 if (vardata->rel != varinfo->rel &&
2388 exprs_known_equal(root, var, varinfo->var))
2390 if (varinfo->ndistinct <= ndistinct)
2392 /* Keep older item, forget new one */
2397 /* Delete the older item */
2398 varinfos = list_delete_ptr(varinfos, varinfo);
2403 varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
2406 varinfo->rel = vardata->rel;
2407 varinfo->ndistinct = ndistinct;
2408 varinfos = lappend(varinfos, varinfo);
2413 * estimate_num_groups - Estimate number of groups in a grouped query
2415 * Given a query having a GROUP BY clause, estimate how many groups there
2416 * will be --- ie, the number of distinct combinations of the GROUP BY
2419 * This routine is also used to estimate the number of rows emitted by
2420 * a DISTINCT filtering step; that is an isomorphic problem. (Note:
2421 * actually, we only use it for DISTINCT when there's no grouping or
2422 * aggregation ahead of the DISTINCT.)
2426 * groupExprs - list of expressions being grouped by
2427 * input_rows - number of rows estimated to arrive at the group/unique
2430 * Given the lack of any cross-correlation statistics in the system, it's
2431 * impossible to do anything really trustworthy with GROUP BY conditions
2432 * involving multiple Vars. We should however avoid assuming the worst
2433 * case (all possible cross-product terms actually appear as groups) since
2434 * very often the grouped-by Vars are highly correlated. Our current approach
2436 * 1. Reduce the given expressions to a list of unique Vars used. For
2437 * example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
2438 * It is clearly correct not to count the same Var more than once.
2439 * It is also reasonable to treat f(x) the same as x: f() cannot
2440 * increase the number of distinct values (unless it is volatile,
2441 * which we consider unlikely for grouping), but it probably won't
2442 * reduce the number of distinct values much either.
2443 * As a special case, if a GROUP BY expression can be matched to an
2444 * expressional index for which we have statistics, then we treat the
2445 * whole expression as though it were just a Var.
2446 * 2. If the list contains Vars of different relations that are known equal
2447 * due to equivalence classes, then drop all but one of the Vars from each
2448 * known-equal set, keeping the one with smallest estimated # of values
2449 * (since the extra values of the others can't appear in joined rows).
2450 * Note the reason we only consider Vars of different relations is that
2451 * if we considered ones of the same rel, we'd be double-counting the
2452 * restriction selectivity of the equality in the next step.
2453 * 3. For Vars within a single source rel, we multiply together the numbers
2454 * of values, clamp to the number of rows in the rel (divided by 10 if
2455 * more than one Var), and then multiply by the selectivity of the
2456 * restriction clauses for that rel. When there's more than one Var,
2457 * the initial product is probably too high (it's the worst case) but
2458 * clamping to a fraction of the rel's rows seems to be a helpful
2459 * heuristic for not letting the estimate get out of hand. (The factor
2460 * of 10 is derived from pre-Postgres-7.4 practice.) Multiplying
2461 * by the restriction selectivity is effectively assuming that the
2462 * restriction clauses are independent of the grouping, which is a crummy
2463 * assumption, but it's hard to do better.
2464 * 4. If there are Vars from multiple rels, we repeat step 3 for each such
2465 * rel, and multiply the results together.
2466 * Note that rels not containing grouped Vars are ignored completely, as are
2467 * join clauses. Such rels cannot increase the number of groups, and we
2468 * assume such clauses do not reduce the number either (somewhat bogus,
2469 * but we don't have the info to do better).
2472 estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows)
2474 List *varinfos = NIL;
2478 /* We should not be called unless query has GROUP BY (or DISTINCT) */
2479 Assert(groupExprs != NIL);
2482 * Steps 1/2: find the unique Vars used, treating an expression as a Var
2483 * if we can find stats for it. For each one, record the statistical
2484 * estimate of number of distinct values (total in its table, without
2485 * regard for filtering).
2487 foreach(l, groupExprs)
2489 Node *groupexpr = (Node *) lfirst(l);
2490 VariableStatData vardata;
2495 * If examine_variable is able to deduce anything about the GROUP BY
2496 * expression, treat it as a single variable even if it's really more
2499 examine_variable(root, groupexpr, 0, &vardata);
2500 if (vardata.statsTuple != NULL || vardata.isunique)
2502 varinfos = add_unique_group_var(root, varinfos,
2503 groupexpr, &vardata);
2504 ReleaseVariableStats(vardata);
2507 ReleaseVariableStats(vardata);
2510 * Else pull out the component Vars
2512 varshere = pull_var_clause(groupexpr, false);
2515 * If we find any variable-free GROUP BY item, then either it is a
2516 * constant (and we can ignore it) or it contains a volatile function;
2517 * in the latter case we punt and assume that each input row will
2518 * yield a distinct group.
2520 if (varshere == NIL)
2522 if (contain_volatile_functions(groupexpr))
2528 * Else add variables to varinfos list
2530 foreach(l2, varshere)
2532 Node *var = (Node *) lfirst(l2);
2534 examine_variable(root, var, 0, &vardata);
2535 varinfos = add_unique_group_var(root, varinfos, var, &vardata);
2536 ReleaseVariableStats(vardata);
2540 /* If now no Vars, we must have an all-constant GROUP BY list. */
2541 if (varinfos == NIL)
2545 * Steps 3/4: group Vars by relation and estimate total numdistinct.
2547 * For each iteration of the outer loop, we process the frontmost Var in
2548 * varinfos, plus all other Vars in the same relation. We remove these
2549 * Vars from the newvarinfos list for the next iteration. This is the
2550 * easiest way to group Vars of same rel together.
2556 GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
2557 RelOptInfo *rel = varinfo1->rel;
2558 double reldistinct = varinfo1->ndistinct;
2559 double relmaxndistinct = reldistinct;
2560 int relvarcount = 1;
2561 List *newvarinfos = NIL;
2564 * Get the product of numdistinct estimates of the Vars for this rel.
2565 * Also, construct new varinfos list of remaining Vars.
2567 for_each_cell(l, lnext(list_head(varinfos)))
2569 GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
2571 if (varinfo2->rel == varinfo1->rel)
2573 reldistinct *= varinfo2->ndistinct;
2574 if (relmaxndistinct < varinfo2->ndistinct)
2575 relmaxndistinct = varinfo2->ndistinct;
2580 /* not time to process varinfo2 yet */
2581 newvarinfos = lcons(varinfo2, newvarinfos);
2586 * Sanity check --- don't divide by zero if empty relation.
2588 Assert(rel->reloptkind == RELOPT_BASEREL);
2589 if (rel->tuples > 0)
2592 * Clamp to size of rel, or size of rel / 10 if multiple Vars. The
2593 * fudge factor is because the Vars are probably correlated but we
2594 * don't know by how much. We should never clamp to less than the
2595 * largest ndistinct value for any of the Vars, though, since
2596 * there will surely be at least that many groups.
2598 double clamp = rel->tuples;
2600 if (relvarcount > 1)
2603 if (clamp < relmaxndistinct)
2605 clamp = relmaxndistinct;
2606 /* for sanity in case some ndistinct is too large: */
2607 if (clamp > rel->tuples)
2608 clamp = rel->tuples;
2611 if (reldistinct > clamp)
2612 reldistinct = clamp;
2615 * Multiply by restriction selectivity.
2617 reldistinct *= rel->rows / rel->tuples;
2620 * Update estimate of total distinct groups.
2622 numdistinct *= reldistinct;
2625 varinfos = newvarinfos;
2626 } while (varinfos != NIL);
2628 numdistinct = ceil(numdistinct);
2630 /* Guard against out-of-range answers */
2631 if (numdistinct > input_rows)
2632 numdistinct = input_rows;
2633 if (numdistinct < 1.0)
2640 * Estimate hash bucketsize fraction (ie, number of entries in a bucket
2641 * divided by total tuples in relation) if the specified expression is used
2644 * XXX This is really pretty bogus since we're effectively assuming that the
2645 * distribution of hash keys will be the same after applying restriction
2646 * clauses as it was in the underlying relation. However, we are not nearly
2647 * smart enough to figure out how the restrict clauses might change the
2648 * distribution, so this will have to do for now.
2650 * We are passed the number of buckets the executor will use for the given
2651 * input relation. If the data were perfectly distributed, with the same
2652 * number of tuples going into each available bucket, then the bucketsize
2653 * fraction would be 1/nbuckets. But this happy state of affairs will occur
2654 * only if (a) there are at least nbuckets distinct data values, and (b)
2655 * we have a not-too-skewed data distribution. Otherwise the buckets will
2656 * be nonuniformly occupied. If the other relation in the join has a key
2657 * distribution similar to this one's, then the most-loaded buckets are
2658 * exactly those that will be probed most often. Therefore, the "average"
2659 * bucket size for costing purposes should really be taken as something close
2660 * to the "worst case" bucket size. We try to estimate this by adjusting the
2661 * fraction if there are too few distinct data values, and then scaling up
2662 * by the ratio of the most common value's frequency to the average frequency.
2664 * If no statistics are available, use a default estimate of 0.1. This will
2665 * discourage use of a hash rather strongly if the inner relation is large,
2666 * which is what we want. We do not want to hash unless we know that the
2667 * inner rel is well-dispersed (or the alternatives seem much worse).
2670 estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
2672 VariableStatData vardata;
2681 examine_variable(root, hashkey, 0, &vardata);
2683 /* Get number of distinct values and fraction that are null */
2684 ndistinct = get_variable_numdistinct(&vardata);
2686 if (HeapTupleIsValid(vardata.statsTuple))
2688 Form_pg_statistic stats;
2690 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
2691 stanullfrac = stats->stanullfrac;
2696 * Believe a default ndistinct only if it came from stats. Otherwise
2697 * punt and return 0.1, per comments above.
2699 if (ndistinct == DEFAULT_NUM_DISTINCT)
2701 ReleaseVariableStats(vardata);
2702 return (Selectivity) 0.1;
2708 /* Compute avg freq of all distinct data values in raw relation */
2709 avgfreq = (1.0 - stanullfrac) / ndistinct;
2712 * Adjust ndistinct to account for restriction clauses. Observe we are
2713 * assuming that the data distribution is affected uniformly by the
2714 * restriction clauses!
2716 * XXX Possibly better way, but much more expensive: multiply by
2717 * selectivity of rel's restriction clauses that mention the target Var.
2720 ndistinct *= vardata.rel->rows / vardata.rel->tuples;
2723 * Initial estimate of bucketsize fraction is 1/nbuckets as long as the
2724 * number of buckets is less than the expected number of distinct values;
2725 * otherwise it is 1/ndistinct.
2727 if (ndistinct > nbuckets)
2728 estfract = 1.0 / nbuckets;
2730 estfract = 1.0 / ndistinct;
2733 * Look up the frequency of the most common value, if available.
2737 if (HeapTupleIsValid(vardata.statsTuple))
2739 if (get_attstatsslot(vardata.statsTuple,
2740 vardata.atttype, vardata.atttypmod,
2741 STATISTIC_KIND_MCV, InvalidOid,
2742 NULL, NULL, &numbers, &nnumbers))
2745 * The first MCV stat is for the most common value.
2748 mcvfreq = numbers[0];
2749 free_attstatsslot(vardata.atttype, NULL, 0,
2755 * Adjust estimated bucketsize upward to account for skewed distribution.
2757 if (avgfreq > 0.0 && mcvfreq > avgfreq)
2758 estfract *= mcvfreq / avgfreq;
2761 * Clamp bucketsize to sane range (the above adjustment could easily
2762 * produce an out-of-range result). We set the lower bound a little above
2763 * zero, since zero isn't a very sane result.
2765 if (estfract < 1.0e-6)
2767 else if (estfract > 1.0)
2770 ReleaseVariableStats(vardata);
2772 return (Selectivity) estfract;
2776 /*-------------------------------------------------------------------------
2780 *-------------------------------------------------------------------------
2785 * Convert non-NULL values of the indicated types to the comparison
2786 * scale needed by scalarineqsel().
2787 * Returns "true" if successful.
2789 * XXX this routine is a hack: ideally we should look up the conversion
2790 * subroutines in pg_type.
2792 * All numeric datatypes are simply converted to their equivalent
2793 * "double" values. (NUMERIC values that are outside the range of "double"
2794 * are clamped to +/- HUGE_VAL.)
2796 * String datatypes are converted by convert_string_to_scalar(),
2797 * which is explained below. The reason why this routine deals with
2798 * three values at a time, not just one, is that we need it for strings.
2800 * The bytea datatype is just enough different from strings that it has
2801 * to be treated separately.
2803 * The several datatypes representing absolute times are all converted
2804 * to Timestamp, which is actually a double, and then we just use that
2805 * double value. Note this will give correct results even for the "special"
2806 * values of Timestamp, since those are chosen to compare correctly;
2807 * see timestamp_cmp.
2809 * The several datatypes representing relative times (intervals) are all
2810 * converted to measurements expressed in seconds.
2813 convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
2814 Datum lobound, Datum hibound, Oid boundstypid,
2815 double *scaledlobound, double *scaledhibound)
2818 * Both the valuetypid and the boundstypid should exactly match the
2819 * declared input type(s) of the operator we are invoked for, so we just
2820 * error out if either is not recognized.
2822 * XXX The histogram we are interpolating between points of could belong
2823 * to a column that's only binary-compatible with the declared type. In
2824 * essence we are assuming that the semantics of binary-compatible types
2825 * are enough alike that we can use a histogram generated with one type's
2826 * operators to estimate selectivity for the other's. This is outright
2827 * wrong in some cases --- in particular signed versus unsigned
2828 * interpretation could trip us up. But it's useful enough in the
2829 * majority of cases that we do it anyway. Should think about more
2830 * rigorous ways to do it.
2835 * Built-in numeric types
2846 case REGPROCEDUREOID:
2848 case REGOPERATOROID:
2851 *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
2852 *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
2853 *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
2857 * Built-in string types
2865 char *valstr = convert_string_datum(value, valuetypid);
2866 char *lostr = convert_string_datum(lobound, boundstypid);
2867 char *histr = convert_string_datum(hibound, boundstypid);
2869 convert_string_to_scalar(valstr, scaledvalue,
2870 lostr, scaledlobound,
2871 histr, scaledhibound);
2879 * Built-in bytea type
2883 convert_bytea_to_scalar(value, scaledvalue,
2884 lobound, scaledlobound,
2885 hibound, scaledhibound);
2890 * Built-in time types
2893 case TIMESTAMPTZOID:
2901 *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
2902 *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
2903 *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
2907 * Built-in network types
2912 *scaledvalue = convert_network_to_scalar(value, valuetypid);
2913 *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
2914 *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
2917 /* Don't know how to convert */
2918 *scaledvalue = *scaledlobound = *scaledhibound = 0;
2923 * Do convert_to_scalar()'s work for any numeric data type.
2926 convert_numeric_to_scalar(Datum value, Oid typid)
2931 return (double) DatumGetBool(value);
2933 return (double) DatumGetInt16(value);
2935 return (double) DatumGetInt32(value);
2937 return (double) DatumGetInt64(value);
2939 return (double) DatumGetFloat4(value);
2941 return (double) DatumGetFloat8(value);
2943 /* Note: out-of-range values will be clamped to +-HUGE_VAL */
2945 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
2949 case REGPROCEDUREOID:
2951 case REGOPERATOROID:
2954 /* we can treat OIDs as integers... */
2955 return (double) DatumGetObjectId(value);
2959 * Can't get here unless someone tries to use scalarltsel/scalargtsel on
2960 * an operator with one numeric and one non-numeric operand.
2962 elog(ERROR, "unsupported type: %u", typid);
2967 * Do convert_to_scalar()'s work for any character-string data type.
2969 * String datatypes are converted to a scale that ranges from 0 to 1,
2970 * where we visualize the bytes of the string as fractional digits.
2972 * We do not want the base to be 256, however, since that tends to
2973 * generate inflated selectivity estimates; few databases will have
2974 * occurrences of all 256 possible byte values at each position.
2975 * Instead, use the smallest and largest byte values seen in the bounds
2976 * as the estimated range for each byte, after some fudging to deal with
2977 * the fact that we probably aren't going to see the full range that way.
2979 * An additional refinement is that we discard any common prefix of the
2980 * three strings before computing the scaled values. This allows us to
2981 * "zoom in" when we encounter a narrow data range. An example is a phone
2982 * number database where all the values begin with the same area code.
2983 * (Actually, the bounds will be adjacent histogram-bin-boundary values,
2984 * so this is more likely to happen than you might think.)
2987 convert_string_to_scalar(char *value,
2988 double *scaledvalue,
2990 double *scaledlobound,
2992 double *scaledhibound)
2998 rangelo = rangehi = (unsigned char) hibound[0];
2999 for (sptr = lobound; *sptr; sptr++)
3001 if (rangelo > (unsigned char) *sptr)
3002 rangelo = (unsigned char) *sptr;
3003 if (rangehi < (unsigned char) *sptr)
3004 rangehi = (unsigned char) *sptr;
3006 for (sptr = hibound; *sptr; sptr++)
3008 if (rangelo > (unsigned char) *sptr)
3009 rangelo = (unsigned char) *sptr;
3010 if (rangehi < (unsigned char) *sptr)
3011 rangehi = (unsigned char) *sptr;
3013 /* If range includes any upper-case ASCII chars, make it include all */
3014 if (rangelo <= 'Z' && rangehi >= 'A')
3021 /* Ditto lower-case */
3022 if (rangelo <= 'z' && rangehi >= 'a')
3030 if (rangelo <= '9' && rangehi >= '0')
3039 * If range includes less than 10 chars, assume we have not got enough
3040 * data, and make it include regular ASCII set.
3042 if (rangehi - rangelo < 9)
3049 * Now strip any common prefix of the three strings.
3053 if (*lobound != *hibound || *lobound != *value)
3055 lobound++, hibound++, value++;
3059 * Now we can do the conversions.
3061 *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
3062 *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
3063 *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
3067 convert_one_string_to_scalar(char *value, int rangelo, int rangehi)
3069 int slen = strlen(value);
3075 return 0.0; /* empty string has scalar value 0 */
3078 * Since base is at least 10, need not consider more than about 20 chars
3083 /* Convert initial characters to fraction */
3084 base = rangehi - rangelo + 1;
3089 int ch = (unsigned char) *value++;
3093 else if (ch > rangehi)
3095 num += ((double) (ch - rangelo)) / denom;
3103 * Convert a string-type Datum into a palloc'd, null-terminated string.
3105 * When using a non-C locale, we must pass the string through strxfrm()
3106 * before continuing, so as to generate correct locale-specific results.
3109 convert_string_datum(Datum value, Oid typid)
3116 val = (char *) palloc(2);
3117 val[0] = DatumGetChar(value);
3124 char *str = (char *) VARDATA(DatumGetPointer(value));
3125 int strlength = VARSIZE(DatumGetPointer(value)) - VARHDRSZ;
3127 val = (char *) palloc(strlength + 1);
3128 memcpy(val, str, strlength);
3129 val[strlength] = '\0';
3134 NameData *nm = (NameData *) DatumGetPointer(value);
3136 val = pstrdup(NameStr(*nm));
3142 * Can't get here unless someone tries to use scalarltsel on an
3143 * operator with one string and one non-string operand.
3145 elog(ERROR, "unsupported type: %u", typid);
3149 if (!lc_collate_is_c())
3156 * Note: originally we guessed at a suitable output buffer size, and
3157 * only needed to call strxfrm twice if our guess was too small.
3158 * However, it seems that some versions of Solaris have buggy strxfrm
3159 * that can write past the specified buffer length in that scenario.
3160 * So, do it the dumb way for portability.
3162 * Yet other systems (e.g., glibc) sometimes return a smaller value
3163 * from the second call than the first; thus the Assert must be <= not
3164 * == as you'd expect. Can't any of these people program their way
3165 * out of a paper bag?
3167 #if _MSC_VER == 1400 /* VS.Net 2005 */
3170 * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx
3176 xfrmlen = strxfrm(x, val, 0);
3179 xfrmlen = strxfrm(NULL, val, 0);
3181 xfrmstr = (char *) palloc(xfrmlen + 1);
3182 xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
3183 Assert(xfrmlen2 <= xfrmlen);
3192 * Do convert_to_scalar()'s work for any bytea data type.
3194 * Very similar to convert_string_to_scalar except we can't assume
3195 * null-termination and therefore pass explicit lengths around.
3197 * Also, assumptions about likely "normal" ranges of characters have been
3198 * removed - a data range of 0..255 is always used, for now. (Perhaps
3199 * someday we will add information about actual byte data range to
3203 convert_bytea_to_scalar(Datum value,
3204 double *scaledvalue,
3206 double *scaledlobound,
3208 double *scaledhibound)
3212 valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
3213 loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
3214 hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
3217 unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
3218 *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
3219 *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
3222 * Assume bytea data is uniformly distributed across all byte values.
3228 * Now strip any common prefix of the three strings.
3230 minlen = Min(Min(valuelen, loboundlen), hiboundlen);
3231 for (i = 0; i < minlen; i++)
3233 if (*lostr != *histr || *lostr != *valstr)
3235 lostr++, histr++, valstr++;
3236 loboundlen--, hiboundlen--, valuelen--;
3240 * Now we can do the conversions.
3242 *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
3243 *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
3244 *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
3248 convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
3249 int rangelo, int rangehi)
3256 return 0.0; /* empty string has scalar value 0 */
3259 * Since base is 256, need not consider more than about 10 chars (even
3260 * this many seems like overkill)
3265 /* Convert initial characters to fraction */
3266 base = rangehi - rangelo + 1;
3269 while (valuelen-- > 0)
3275 else if (ch > rangehi)
3277 num += ((double) (ch - rangelo)) / denom;
3285 * Do convert_to_scalar()'s work for any timevalue data type.
3288 convert_timevalue_to_scalar(Datum value, Oid typid)
3293 return DatumGetTimestamp(value);
3294 case TIMESTAMPTZOID:
3295 return DatumGetTimestampTz(value);
3297 return DatumGetTimestamp(DirectFunctionCall1(abstime_timestamp,
3300 return DatumGetTimestamp(DirectFunctionCall1(date_timestamp,
3304 Interval *interval = DatumGetIntervalP(value);
3307 * Convert the month part of Interval to days using assumed
3308 * average month length of 365.25/12.0 days. Not too
3309 * accurate, but plenty good enough for our purposes.
3311 #ifdef HAVE_INT64_TIMESTAMP
3312 return interval->time + interval->day * (double) USECS_PER_DAY +
3313 interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * USECS_PER_DAY);
3315 return interval->time + interval->day * SECS_PER_DAY +
3316 interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * (double) SECS_PER_DAY);
3320 #ifdef HAVE_INT64_TIMESTAMP
3321 return (DatumGetRelativeTime(value) * 1000000.0);
3323 return DatumGetRelativeTime(value);
3327 TimeInterval tinterval = DatumGetTimeInterval(value);
3329 #ifdef HAVE_INT64_TIMESTAMP
3330 if (tinterval->status != 0)
3331 return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
3333 if (tinterval->status != 0)
3334 return tinterval->data[1] - tinterval->data[0];
3336 return 0; /* for lack of a better idea */
3339 return DatumGetTimeADT(value);
3342 TimeTzADT *timetz = DatumGetTimeTzADTP(value);
3344 /* use GMT-equivalent time */
3345 #ifdef HAVE_INT64_TIMESTAMP
3346 return (double) (timetz->time + (timetz->zone * 1000000.0));
3348 return (double) (timetz->time + timetz->zone);
3354 * Can't get here unless someone tries to use scalarltsel/scalargtsel on
3355 * an operator with one timevalue and one non-timevalue operand.
3357 elog(ERROR, "unsupported type: %u", typid);
3363 * get_restriction_variable
3364 * Examine the args of a restriction clause to see if it's of the
3365 * form (variable op pseudoconstant) or (pseudoconstant op variable),
3366 * where "variable" could be either a Var or an expression in vars of a
3367 * single relation. If so, extract information about the variable,
3368 * and also indicate which side it was on and the other argument.
3371 * root: the planner info
3372 * args: clause argument list
3373 * varRelid: see specs for restriction selectivity functions
3375 * Outputs: (these are valid only if TRUE is returned)
3376 * *vardata: gets information about variable (see examine_variable)
3377 * *other: gets other clause argument, aggressively reduced to a constant
3378 * *varonleft: set TRUE if variable is on the left, FALSE if on the right
3380 * Returns TRUE if a variable is identified, otherwise FALSE.
3382 * Note: if there are Vars on both sides of the clause, we must fail, because
3383 * callers are expecting that the other side will act like a pseudoconstant.
3386 get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
3387 VariableStatData *vardata, Node **other,
3392 VariableStatData rdata;
3394 /* Fail if not a binary opclause (probably shouldn't happen) */
3395 if (list_length(args) != 2)
3398 left = (Node *) linitial(args);
3399 right = (Node *) lsecond(args);
3402 * Examine both sides. Note that when varRelid is nonzero, Vars of other
3403 * relations will be treated as pseudoconstants.
3405 examine_variable(root, left, varRelid, vardata);
3406 examine_variable(root, right, varRelid, &rdata);
3409 * If one side is a variable and the other not, we win.
3411 if (vardata->rel && rdata.rel == NULL)
3414 *other = estimate_expression_value(root, rdata.var);
3415 /* Assume we need no ReleaseVariableStats(rdata) here */
3419 if (vardata->rel == NULL && rdata.rel)
3422 *other = estimate_expression_value(root, vardata->var);
3423 /* Assume we need no ReleaseVariableStats(*vardata) here */
3428 /* Ooops, clause has wrong structure (probably var op var) */
3429 ReleaseVariableStats(*vardata);
3430 ReleaseVariableStats(rdata);
3436 * get_join_variables
3437 * Apply examine_variable() to each side of a join clause.
3440 get_join_variables(PlannerInfo *root, List *args,
3441 VariableStatData *vardata1, VariableStatData *vardata2)
3446 if (list_length(args) != 2)
3447 elog(ERROR, "join operator should take two arguments");
3449 left = (Node *) linitial(args);
3450 right = (Node *) lsecond(args);
3452 examine_variable(root, left, 0, vardata1);
3453 examine_variable(root, right, 0, vardata2);
3458 * Try to look up statistical data about an expression.
3459 * Fill in a VariableStatData struct to describe the expression.
3462 * root: the planner info
3463 * node: the expression tree to examine
3464 * varRelid: see specs for restriction selectivity functions
3466 * Outputs: *vardata is filled as follows:
3467 * var: the input expression (with any binary relabeling stripped, if
3468 * it is or contains a variable; but otherwise the type is preserved)
3469 * rel: RelOptInfo for relation containing variable; NULL if expression
3470 * contains no Vars (NOTE this could point to a RelOptInfo of a
3471 * subquery, not one in the current query).
3472 * statsTuple: the pg_statistic entry for the variable, if one exists;
3474 * vartype: exposed type of the expression; this should always match
3475 * the declared input type of the operator we are estimating for.
3476 * atttype, atttypmod: type data to pass to get_attstatsslot(). This is
3477 * commonly the same as the exposed type of the variable argument,
3478 * but can be different in binary-compatible-type cases.
3480 * Caller is responsible for doing ReleaseVariableStats() before exiting.
3483 examine_variable(PlannerInfo *root, Node *node, int varRelid,
3484 VariableStatData *vardata)
3490 /* Make sure we don't return dangling pointers in vardata */
3491 MemSet(vardata, 0, sizeof(VariableStatData));
3493 /* Save the exposed type of the expression */
3494 vardata->vartype = exprType(node);
3496 /* Look inside any binary-compatible relabeling */
3498 if (IsA(node, RelabelType))
3499 basenode = (Node *) ((RelabelType *) node)->arg;
3503 /* Fast path for a simple Var */
3505 if (IsA(basenode, Var) &&
3506 (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
3508 Var *var = (Var *) basenode;
3511 vardata->var = basenode; /* return Var without relabeling */
3512 vardata->rel = find_base_rel(root, var->varno);
3513 vardata->atttype = var->vartype;
3514 vardata->atttypmod = var->vartypmod;
3516 rte = rt_fetch(var->varno, root->parse->rtable);
3521 * XXX This means the Var represents a column of an append
3522 * relation. Later add code to look at the member relations and
3523 * try to derive some kind of combined statistics?
3526 else if (rte->rtekind == RTE_RELATION)
3528 vardata->statsTuple = SearchSysCache(STATRELATT,
3529 ObjectIdGetDatum(rte->relid),
3530 Int16GetDatum(var->varattno),
3536 * XXX This means the Var comes from a JOIN or sub-SELECT. Later
3537 * add code to dig down into the join etc and see if we can trace
3538 * the variable to something with stats. (But beware of
3539 * sub-SELECTs with DISTINCT/GROUP BY/etc. Perhaps there are no
3540 * cases where this would really be useful, because we'd have
3541 * flattened the subselect if it is??)
3549 * Okay, it's a more complicated expression. Determine variable
3550 * membership. Note that when varRelid isn't zero, only vars of that
3551 * relation are considered "real" vars.
3553 varnos = pull_varnos(basenode);
3557 switch (bms_membership(varnos))
3560 /* No Vars at all ... must be pseudo-constant clause */
3563 if (varRelid == 0 || bms_is_member(varRelid, varnos))
3565 onerel = find_base_rel(root,
3566 (varRelid ? varRelid : bms_singleton_member(varnos)));
3567 vardata->rel = onerel;
3568 node = basenode; /* strip any relabeling */
3570 /* else treat it as a constant */
3575 /* treat it as a variable of a join relation */
3576 vardata->rel = find_join_rel(root, varnos);
3577 node = basenode; /* strip any relabeling */
3579 else if (bms_is_member(varRelid, varnos))
3581 /* ignore the vars belonging to other relations */
3582 vardata->rel = find_base_rel(root, varRelid);
3583 node = basenode; /* strip any relabeling */
3584 /* note: no point in expressional-index search here */
3586 /* else treat it as a constant */
3592 vardata->var = node;
3593 vardata->atttype = exprType(node);
3594 vardata->atttypmod = exprTypmod(node);
3599 * We have an expression in vars of a single relation. Try to match
3600 * it to expressional index columns, in hopes of finding some
3603 * XXX it's conceivable that there are multiple matches with different
3604 * index opfamilies; if so, we need to pick one that matches the
3605 * operator we are estimating for. FIXME later.
3609 foreach(ilist, onerel->indexlist)
3611 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
3612 ListCell *indexpr_item;
3615 indexpr_item = list_head(index->indexprs);
3616 if (indexpr_item == NULL)
3617 continue; /* no expressions here... */
3620 * Ignore partial indexes since they probably don't reflect
3621 * whole-relation statistics. Possibly reconsider this later.
3626 for (pos = 0; pos < index->ncolumns; pos++)
3628 if (index->indexkeys[pos] == 0)
3632 if (indexpr_item == NULL)
3633 elog(ERROR, "too few entries in indexprs list");
3634 indexkey = (Node *) lfirst(indexpr_item);
3635 if (indexkey && IsA(indexkey, RelabelType))
3636 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3637 if (equal(node, indexkey))
3640 * Found a match ... is it a unique index? Tests here
3641 * should match has_unique_index().
3643 if (index->unique &&
3644 index->ncolumns == 1 &&
3645 index->indpred == NIL)
3646 vardata->isunique = true;
3647 /* Has it got stats? */
3648 vardata->statsTuple = SearchSysCache(STATRELATT,
3649 ObjectIdGetDatum(index->indexoid),
3650 Int16GetDatum(pos + 1),
3652 if (vardata->statsTuple)
3655 indexpr_item = lnext(indexpr_item);
3658 if (vardata->statsTuple)
3665 * get_variable_numdistinct
3666 * Estimate the number of distinct values of a variable.
3668 * vardata: results of examine_variable
3670 * NB: be careful to produce an integral result, since callers may compare
3671 * the result to exact integer counts.
3674 get_variable_numdistinct(VariableStatData *vardata)
3680 * Determine the stadistinct value to use. There are cases where we can
3681 * get an estimate even without a pg_statistic entry, or can get a better
3682 * value than is in pg_statistic.
3684 if (HeapTupleIsValid(vardata->statsTuple))
3686 /* Use the pg_statistic entry */
3687 Form_pg_statistic stats;
3689 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
3690 stadistinct = stats->stadistinct;
3692 else if (vardata->vartype == BOOLOID)
3695 * Special-case boolean columns: presumably, two distinct values.
3697 * Are there any other datatypes we should wire in special estimates
3705 * We don't keep statistics for system columns, but in some cases we
3706 * can infer distinctness anyway.
3708 if (vardata->var && IsA(vardata->var, Var))
3710 switch (((Var *) vardata->var)->varattno)
3712 case ObjectIdAttributeNumber:
3713 case SelfItemPointerAttributeNumber:
3714 stadistinct = -1.0; /* unique */
3716 case TableOidAttributeNumber:
3717 stadistinct = 1.0; /* only 1 value */
3720 stadistinct = 0.0; /* means "unknown" */
3725 stadistinct = 0.0; /* means "unknown" */
3728 * XXX consider using estimate_num_groups on expressions?
3733 * If there is a unique index for the variable, assume it is unique no
3734 * matter what pg_statistic says (the statistics could be out of date).
3735 * Can skip search if we already think it's unique.
3737 if (stadistinct != -1.0)
3739 if (vardata->isunique)
3741 else if (vardata->var && IsA(vardata->var, Var) &&
3743 has_unique_index(vardata->rel,
3744 ((Var *) vardata->var)->varattno))
3749 * If we had an absolute estimate, use that.
3751 if (stadistinct > 0.0)
3755 * Otherwise we need to get the relation size; punt if not available.
3757 if (vardata->rel == NULL)
3758 return DEFAULT_NUM_DISTINCT;
3759 ntuples = vardata->rel->tuples;
3761 return DEFAULT_NUM_DISTINCT;
3764 * If we had a relative estimate, use that.
3766 if (stadistinct < 0.0)
3767 return floor((-stadistinct * ntuples) + 0.5);
3770 * With no data, estimate ndistinct = ntuples if the table is small, else
3773 if (ntuples < DEFAULT_NUM_DISTINCT)
3776 return DEFAULT_NUM_DISTINCT;
3780 * get_variable_maximum
3781 * Estimate the maximum value of the specified variable.
3782 * If successful, store value in *max and return TRUE.
3783 * If no data available, return FALSE.
3785 * sortop is the "<" comparison operator to use. (To extract the
3786 * minimum instead of the maximum, just pass the ">" operator instead.)
3789 get_variable_maximum(PlannerInfo *root, VariableStatData *vardata,
3790 Oid sortop, Datum *max)
3793 bool have_max = false;
3794 Form_pg_statistic stats;
3801 if (!HeapTupleIsValid(vardata->statsTuple))
3803 /* no stats available, so default result */
3806 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
3808 get_typlenbyval(vardata->atttype, &typLen, &typByVal);
3811 * If there is a histogram, grab the last or first value as appropriate.
3813 * If there is a histogram that is sorted with some other operator than
3814 * the one we want, fail --- this suggests that there is data we can't
3817 if (get_attstatsslot(vardata->statsTuple,
3818 vardata->atttype, vardata->atttypmod,
3819 STATISTIC_KIND_HISTOGRAM, sortop,
3825 tmax = datumCopy(values[nvalues - 1], typByVal, typLen);
3828 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3832 Oid rsortop = get_commutator(sortop);
3834 if (OidIsValid(rsortop) &&
3835 get_attstatsslot(vardata->statsTuple,
3836 vardata->atttype, vardata->atttypmod,
3837 STATISTIC_KIND_HISTOGRAM, rsortop,
3843 tmax = datumCopy(values[0], typByVal, typLen);
3846 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3848 else if (get_attstatsslot(vardata->statsTuple,
3849 vardata->atttype, vardata->atttypmod,
3850 STATISTIC_KIND_HISTOGRAM, InvalidOid,
3854 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3860 * If we have most-common-values info, look for a large MCV. This is
3861 * needed even if we also have a histogram, since the histogram excludes
3862 * the MCVs. However, usually the MCVs will not be the extreme values, so
3863 * avoid unnecessary data copying.
3865 if (get_attstatsslot(vardata->statsTuple,
3866 vardata->atttype, vardata->atttypmod,
3867 STATISTIC_KIND_MCV, InvalidOid,
3871 bool large_mcv = false;
3874 fmgr_info(get_opcode(sortop), &opproc);
3876 for (i = 0; i < nvalues; i++)
3881 large_mcv = have_max = true;
3883 else if (DatumGetBool(FunctionCall2(&opproc, tmax, values[i])))
3890 tmax = datumCopy(tmax, typByVal, typLen);
3891 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3899 /*-------------------------------------------------------------------------
3901 * Pattern analysis functions
3903 * These routines support analysis of LIKE and regular-expression patterns
3904 * by the planner/optimizer. It's important that they agree with the
3905 * regular-expression code in backend/regex/ and the LIKE code in
3906 * backend/utils/adt/like.c. Also, the computation of the fixed prefix
3907 * must be conservative: if we report a string longer than the true fixed
3908 * prefix, the query may produce actually wrong answers, rather than just
3909 * getting a bad selectivity estimate!
3911 * Note that the prefix-analysis functions are called from
3912 * backend/optimizer/path/indxpath.c as well as from routines in this file.
3914 *-------------------------------------------------------------------------
3918 * Extract the fixed prefix, if any, for a pattern.
3920 * *prefix is set to a palloc'd prefix string (in the form of a Const node),
3921 * or to NULL if no fixed prefix exists for the pattern.
3922 * *rest is set to a palloc'd Const representing the remainder of the pattern
3923 * after the portion describing the fixed prefix.
3924 * Each of these has the same type (TEXT or BYTEA) as the given pattern Const.
3926 * The return value distinguishes no fixed prefix, a partial prefix,
3927 * or an exact-match-only pattern.
3930 static Pattern_Prefix_Status
3931 like_fixed_prefix(Const *patt_const, bool case_insensitive,
3932 Const **prefix_const, Const **rest_const)
3938 Oid typeid = patt_const->consttype;
3941 bool is_multibyte = (pg_database_encoding_max_length() > 1);
3943 /* the right-hand const is type text or bytea */
3944 Assert(typeid == BYTEAOID || typeid == TEXTOID);
3946 if (typeid == BYTEAOID && case_insensitive)
3948 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3949 errmsg("case insensitive matching not supported on type bytea")));
3951 if (typeid != BYTEAOID)
3953 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
3954 pattlen = strlen(patt);
3958 bytea *bstr = DatumGetByteaP(patt_const->constvalue);
3960 pattlen = VARSIZE(bstr) - VARHDRSZ;
3961 patt = (char *) palloc(pattlen);
3962 memcpy(patt, VARDATA(bstr), pattlen);
3963 if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
3967 match = palloc(pattlen + 1);
3969 for (pos = 0; pos < pattlen; pos++)
3971 /* % and _ are wildcard characters in LIKE */
3972 if (patt[pos] == '%' ||
3976 /* Backslash escapes the next character */
3977 if (patt[pos] == '\\')
3985 * XXX In multibyte character sets, we can't trust isalpha, so assume
3986 * any multibyte char is potentially case-varying.
3988 if (case_insensitive)
3990 if (is_multibyte && (unsigned char) patt[pos] >= 0x80)
3992 if (isalpha((unsigned char) patt[pos]))
3997 * NOTE: this code used to think that %% meant a literal %, but
3998 * textlike() itself does not think that, and the SQL92 spec doesn't
3999 * say any such thing either.
4001 match[match_pos++] = patt[pos];
4004 match[match_pos] = '\0';
4007 if (typeid != BYTEAOID)
4009 *prefix_const = string_to_const(match, typeid);
4010 *rest_const = string_to_const(rest, typeid);
4014 *prefix_const = string_to_bytea_const(match, match_pos);
4015 *rest_const = string_to_bytea_const(rest, pattlen - pos);
4021 /* in LIKE, an empty pattern is an exact match! */
4023 return Pattern_Prefix_Exact; /* reached end of pattern, so exact */
4026 return Pattern_Prefix_Partial;
4028 return Pattern_Prefix_None;
4031 static Pattern_Prefix_Status
4032 regex_fixed_prefix(Const *patt_const, bool case_insensitive,
4033 Const **prefix_const, Const **rest_const)
4040 bool have_leading_paren;
4043 Oid typeid = patt_const->consttype;
4044 bool is_basic = regex_flavor_is_basic();
4045 bool is_multibyte = (pg_database_encoding_max_length() > 1);
4048 * Should be unnecessary, there are no bytea regex operators defined. As
4049 * such, it should be noted that the rest of this function has *not* been
4050 * made safe for binary (possibly NULL containing) strings.
4052 if (typeid == BYTEAOID)
4054 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4055 errmsg("regular-expression matching not supported on type bytea")));
4057 /* the right-hand const is type text for all of these */
4058 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
4061 * Check for ARE director prefix. It's worth our trouble to recognize
4062 * this because similar_escape() uses it.
4065 if (strncmp(patt, "***:", 4) == 0)
4071 /* Pattern must be anchored left */
4072 if (patt[pos] != '^')
4076 *prefix_const = NULL;
4077 *rest_const = string_to_const(rest, typeid);
4079 return Pattern_Prefix_None;
4084 * If '|' is present in pattern, then there may be multiple alternatives
4085 * for the start of the string. (There are cases where this isn't so,
4086 * for instance if the '|' is inside parens, but detecting that reliably
4089 if (strchr(patt + pos, '|') != NULL)
4093 *prefix_const = NULL;
4094 *rest_const = string_to_const(rest, typeid);
4096 return Pattern_Prefix_None;
4099 /* OK, allocate space for pattern */
4100 match = palloc(strlen(patt) + 1);
4101 prev_match_pos = match_pos = 0;
4104 * We special-case the syntax '^(...)$' because psql uses it. But beware:
4105 * in BRE mode these parentheses are just ordinary characters. Also,
4106 * sequences beginning "(?" are not what they seem, unless they're "(?:".
4107 * (We should recognize that, too, because of similar_escape().)
4109 * Note: it's a bit bogus to be depending on the current regex_flavor
4110 * setting here, because the setting could change before the pattern is
4111 * used. We minimize the risk by trusting the flavor as little as we can,
4112 * but perhaps it would be a good idea to get rid of the "basic" setting.
4114 have_leading_paren = false;
4115 if (patt[pos] == '(' && !is_basic &&
4116 (patt[pos + 1] != '?' || patt[pos + 2] == ':'))
4118 have_leading_paren = true;
4119 pos += (patt[pos + 1] != '?' ? 1 : 3);
4122 /* Scan remainder of pattern */
4129 * Check for characters that indicate multiple possible matches here.
4130 * Also, drop out at ')' or '$' so the termination test works right.
4132 if (patt[pos] == '.' ||
4141 * XXX In multibyte character sets, we can't trust isalpha, so assume
4142 * any multibyte char is potentially case-varying.
4144 if (case_insensitive)
4146 if (is_multibyte && (unsigned char) patt[pos] >= 0x80)
4148 if (isalpha((unsigned char) patt[pos]))
4153 * Check for quantifiers. Except for +, this means the preceding
4154 * character is optional, so we must remove it from the prefix too!
4155 * Note: in BREs, \{ is a quantifier.
4157 if (patt[pos] == '*' ||
4160 (patt[pos] == '\\' && patt[pos + 1] == '{'))
4162 match_pos = prev_match_pos;
4166 if (patt[pos] == '+')
4173 * Normally, backslash quotes the next character. But in AREs,
4174 * backslash followed by alphanumeric is an escape, not a quoted
4175 * character. Must treat it as having multiple possible matches.
4176 * In BREs, \( is a parenthesis, so don't trust that either.
4177 * Note: since only ASCII alphanumerics are escapes, we don't have
4178 * to be paranoid about multibyte here.
4180 if (patt[pos] == '\\')
4182 if (isalnum((unsigned char) patt[pos + 1]) || patt[pos + 1] == '(')
4185 if (patt[pos] == '\0')
4188 /* save position in case we need to back up on next loop cycle */
4189 prev_match_pos = match_pos;
4191 /* must use encoding-aware processing here */
4192 len = pg_mblen(&patt[pos]);
4193 memcpy(&match[match_pos], &patt[pos], len);
4198 match[match_pos] = '\0';
4201 if (have_leading_paren && patt[pos] == ')')
4204 if (patt[pos] == '$' && patt[pos + 1] == '\0')
4206 rest = &patt[pos + 1];
4208 *prefix_const = string_to_const(match, typeid);
4209 *rest_const = string_to_const(rest, typeid);
4214 return Pattern_Prefix_Exact; /* pattern specifies exact match */
4217 *prefix_const = string_to_const(match, typeid);
4218 *rest_const = string_to_const(rest, typeid);
4224 return Pattern_Prefix_Partial;
4226 return Pattern_Prefix_None;
4229 Pattern_Prefix_Status
4230 pattern_fixed_prefix(Const *patt, Pattern_Type ptype,
4231 Const **prefix, Const **rest)
4233 Pattern_Prefix_Status result;
4237 case Pattern_Type_Like:
4238 result = like_fixed_prefix(patt, false, prefix, rest);
4240 case Pattern_Type_Like_IC:
4241 result = like_fixed_prefix(patt, true, prefix, rest);
4243 case Pattern_Type_Regex:
4244 result = regex_fixed_prefix(patt, false, prefix, rest);
4246 case Pattern_Type_Regex_IC:
4247 result = regex_fixed_prefix(patt, true, prefix, rest);
4250 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
4251 result = Pattern_Prefix_None; /* keep compiler quiet */
4258 * Estimate the selectivity of a fixed prefix for a pattern match.
4260 * A fixed prefix "foo" is estimated as the selectivity of the expression
4261 * "variable >= 'foo' AND variable < 'fop'" (see also indxpath.c).
4263 * The selectivity estimate is with respect to the portion of the column
4264 * population represented by the histogram --- the caller must fold this
4265 * together with info about MCVs and NULLs.
4267 * We use the >= and < operators from the specified btree opfamily to do the
4268 * estimation. The given variable and Const must be of the associated
4271 * XXX Note: we make use of the upper bound to estimate operator selectivity
4272 * even if the locale is such that we cannot rely on the upper-bound string.
4273 * The selectivity only needs to be approximately right anyway, so it seems
4274 * more useful to use the upper-bound code than not.
4277 prefix_selectivity(VariableStatData *vardata,
4278 Oid vartype, Oid opfamily, Const *prefixcon)
4280 Selectivity prefixsel;
4283 Const *greaterstrcon;
4285 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
4286 BTGreaterEqualStrategyNumber);
4287 if (cmpopr == InvalidOid)
4288 elog(ERROR, "no >= operator for opfamily %u", opfamily);
4289 fmgr_info(get_opcode(cmpopr), &opproc);
4291 prefixsel = ineq_histogram_selectivity(vardata, &opproc, true,
4292 prefixcon->constvalue,
4293 prefixcon->consttype);
4295 if (prefixsel <= 0.0)
4297 /* No histogram is present ... return a suitable default estimate */
4302 * If we can create a string larger than the prefix, say
4306 greaterstrcon = make_greater_string(prefixcon);
4311 cmpopr = get_opfamily_member(opfamily, vartype, vartype,
4312 BTLessStrategyNumber);
4313 if (cmpopr == InvalidOid)
4314 elog(ERROR, "no < operator for opfamily %u", opfamily);
4315 fmgr_info(get_opcode(cmpopr), &opproc);
4317 topsel = ineq_histogram_selectivity(vardata, &opproc, false,
4318 greaterstrcon->constvalue,
4319 greaterstrcon->consttype);
4321 /* ineq_histogram_selectivity worked before, it shouldn't fail now */
4322 Assert(topsel > 0.0);
4325 * Merge the two selectivities in the same way as for a range query
4326 * (see clauselist_selectivity()). Note that we don't need to worry
4327 * about double-exclusion of nulls, since ineq_histogram_selectivity
4328 * doesn't count those anyway.
4330 prefixsel = topsel + prefixsel - 1.0;
4333 * A zero or negative prefixsel should be converted into a small
4334 * positive value; we probably are dealing with a very tight range and
4335 * got a bogus result due to roundoff errors.
4337 if (prefixsel <= 0.0)
4338 prefixsel = 1.0e-10;
4346 * Estimate the selectivity of a pattern of the specified type.
4347 * Note that any fixed prefix of the pattern will have been removed already.
4349 * For now, we use a very simplistic approach: fixed characters reduce the
4350 * selectivity a good deal, character ranges reduce it a little,
4351 * wildcards (such as % for LIKE or .* for regex) increase it.
4354 #define FIXED_CHAR_SEL 0.20 /* about 1/5 */
4355 #define CHAR_RANGE_SEL 0.25
4356 #define ANY_CHAR_SEL 0.9 /* not 1, since it won't match end-of-string */
4357 #define FULL_WILDCARD_SEL 5.0
4358 #define PARTIAL_WILDCARD_SEL 2.0
4361 like_selectivity(Const *patt_const, bool case_insensitive)
4363 Selectivity sel = 1.0;
4365 Oid typeid = patt_const->consttype;
4369 /* the right-hand const is type text or bytea */
4370 Assert(typeid == BYTEAOID || typeid == TEXTOID);
4372 if (typeid == BYTEAOID && case_insensitive)
4374 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4375 errmsg("case insensitive matching not supported on type bytea")));
4377 if (typeid != BYTEAOID)
4379 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
4380 pattlen = strlen(patt);
4384 bytea *bstr = DatumGetByteaP(patt_const->constvalue);
4386 pattlen = VARSIZE(bstr) - VARHDRSZ;
4387 patt = (char *) palloc(pattlen);
4388 memcpy(patt, VARDATA(bstr), pattlen);
4389 if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
4393 /* Skip any leading wildcard; it's already factored into initial sel */
4394 for (pos = 0; pos < pattlen; pos++)
4396 if (patt[pos] != '%' && patt[pos] != '_')
4400 for (; pos < pattlen; pos++)
4402 /* % and _ are wildcard characters in LIKE */
4403 if (patt[pos] == '%')
4404 sel *= FULL_WILDCARD_SEL;
4405 else if (patt[pos] == '_')
4406 sel *= ANY_CHAR_SEL;
4407 else if (patt[pos] == '\\')
4409 /* Backslash quotes the next character */
4413 sel *= FIXED_CHAR_SEL;
4416 sel *= FIXED_CHAR_SEL;
4418 /* Could get sel > 1 if multiple wildcards */
4427 regex_selectivity_sub(char *patt, int pattlen, bool case_insensitive)
4429 Selectivity sel = 1.0;
4430 int paren_depth = 0;
4431 int paren_pos = 0; /* dummy init to keep compiler quiet */
4434 for (pos = 0; pos < pattlen; pos++)
4436 if (patt[pos] == '(')
4438 if (paren_depth == 0)
4439 paren_pos = pos; /* remember start of parenthesized item */
4442 else if (patt[pos] == ')' && paren_depth > 0)
4445 if (paren_depth == 0)
4446 sel *= regex_selectivity_sub(patt + (paren_pos + 1),
4447 pos - (paren_pos + 1),
4450 else if (patt[pos] == '|' && paren_depth == 0)
4453 * If unquoted | is present at paren level 0 in pattern, we have
4454 * multiple alternatives; sum their probabilities.
4456 sel += regex_selectivity_sub(patt + (pos + 1),
4457 pattlen - (pos + 1),
4459 break; /* rest of pattern is now processed */
4461 else if (patt[pos] == '[')
4463 bool negclass = false;
4465 if (patt[++pos] == '^')
4470 if (patt[pos] == ']') /* ']' at start of class is not
4473 while (pos < pattlen && patt[pos] != ']')
4475 if (paren_depth == 0)
4476 sel *= (negclass ? (1.0 - CHAR_RANGE_SEL) : CHAR_RANGE_SEL);
4478 else if (patt[pos] == '.')
4480 if (paren_depth == 0)
4481 sel *= ANY_CHAR_SEL;
4483 else if (patt[pos] == '*' ||
4487 /* Ought to be smarter about quantifiers... */
4488 if (paren_depth == 0)
4489 sel *= PARTIAL_WILDCARD_SEL;
4491 else if (patt[pos] == '{')
4493 while (pos < pattlen && patt[pos] != '}')
4495 if (paren_depth == 0)
4496 sel *= PARTIAL_WILDCARD_SEL;
4498 else if (patt[pos] == '\\')
4500 /* backslash quotes the next character */
4504 if (paren_depth == 0)
4505 sel *= FIXED_CHAR_SEL;
4509 if (paren_depth == 0)
4510 sel *= FIXED_CHAR_SEL;
4513 /* Could get sel > 1 if multiple wildcards */
4520 regex_selectivity(Const *patt_const, bool case_insensitive)
4525 Oid typeid = patt_const->consttype;
4528 * Should be unnecessary, there are no bytea regex operators defined. As
4529 * such, it should be noted that the rest of this function has *not* been
4530 * made safe for binary (possibly NULL containing) strings.
4532 if (typeid == BYTEAOID)
4534 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4535 errmsg("regular-expression matching not supported on type bytea")));
4537 /* the right-hand const is type text for all of these */
4538 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
4539 pattlen = strlen(patt);
4541 /* If patt doesn't end with $, consider it to have a trailing wildcard */
4542 if (pattlen > 0 && patt[pattlen - 1] == '$' &&
4543 (pattlen == 1 || patt[pattlen - 2] != '\\'))
4545 /* has trailing $ */
4546 sel = regex_selectivity_sub(patt, pattlen - 1, case_insensitive);
4551 sel = regex_selectivity_sub(patt, pattlen, case_insensitive);
4552 sel *= FULL_WILDCARD_SEL;
4560 pattern_selectivity(Const *patt, Pattern_Type ptype)
4566 case Pattern_Type_Like:
4567 result = like_selectivity(patt, false);
4569 case Pattern_Type_Like_IC:
4570 result = like_selectivity(patt, true);
4572 case Pattern_Type_Regex:
4573 result = regex_selectivity(patt, false);
4575 case Pattern_Type_Regex_IC:
4576 result = regex_selectivity(patt, true);
4579 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
4580 result = 1.0; /* keep compiler quiet */
4588 * Try to generate a string greater than the given string or any
4589 * string it is a prefix of. If successful, return a palloc'd string
4590 * in the form of a Const pointer; else return NULL.
4592 * The key requirement here is that given a prefix string, say "foo",
4593 * we must be able to generate another string "fop" that is greater
4594 * than all strings "foobar" starting with "foo".
4596 * If we max out the righthand byte, truncate off the last character
4597 * and start incrementing the next. For example, if "z" were the last
4598 * character in the sort order, then we could produce "foo" as a
4599 * string greater than "fonz".
4601 * This could be rather slow in the worst case, but in most cases we
4602 * won't have to try more than one or two strings before succeeding.
4604 * NOTE: at present this assumes we are in the C locale, so that simple
4605 * bytewise comparison applies. However, we might be in a multibyte
4606 * encoding such as UTF8, so we do have to watch out for generating
4607 * invalid encoding sequences.
4610 make_greater_string(const Const *str_const)
4612 Oid datatype = str_const->consttype;
4616 /* Get the string and a modifiable copy */
4617 if (datatype == NAMEOID)
4619 workstr = DatumGetCString(DirectFunctionCall1(nameout,
4620 str_const->constvalue));
4621 len = strlen(workstr);
4623 else if (datatype == BYTEAOID)
4625 bytea *bstr = DatumGetByteaP(str_const->constvalue);
4627 len = VARSIZE(bstr) - VARHDRSZ;
4628 workstr = (char *) palloc(len);
4629 memcpy(workstr, VARDATA(bstr), len);
4630 if ((Pointer) bstr != DatumGetPointer(str_const->constvalue))
4635 workstr = DatumGetCString(DirectFunctionCall1(textout,
4636 str_const->constvalue));
4637 len = strlen(workstr);
4642 unsigned char *lastchar = (unsigned char *) (workstr + len - 1);
4643 unsigned char savelastchar = *lastchar;
4646 * Try to generate a larger string by incrementing the last byte.
4648 while (*lastchar < (unsigned char) 255)
4650 Const *workstr_const;
4654 if (datatype != BYTEAOID)
4656 /* do not generate invalid encoding sequences */
4657 if (!pg_verifymbstr(workstr, len, true))
4659 workstr_const = string_to_const(workstr, datatype);
4662 workstr_const = string_to_bytea_const(workstr, len);
4665 return workstr_const;
4668 /* restore last byte so we don't confuse pg_mbcliplen */
4669 *lastchar = savelastchar;
4672 * Truncate off the last character, which might be more than 1 byte,
4673 * depending on the character encoding.
4675 if (datatype != BYTEAOID && pg_database_encoding_max_length() > 1)
4676 len = pg_mbcliplen(workstr, len, len - 1);
4680 if (datatype != BYTEAOID)
4681 workstr[len] = '\0';
4691 * Generate a Datum of the appropriate type from a C string.
4692 * Note that all of the supported types are pass-by-ref, so the
4693 * returned value should be pfree'd if no longer needed.
4696 string_to_datum(const char *str, Oid datatype)
4698 Assert(str != NULL);
4701 * We cheat a little by assuming that textin() will do for bpchar and
4702 * varchar constants too...
4704 if (datatype == NAMEOID)
4705 return DirectFunctionCall1(namein, CStringGetDatum(str));
4706 else if (datatype == BYTEAOID)
4707 return DirectFunctionCall1(byteain, CStringGetDatum(str));
4709 return DirectFunctionCall1(textin, CStringGetDatum(str));
4713 * Generate a Const node of the appropriate type from a C string.
4716 string_to_const(const char *str, Oid datatype)
4718 Datum conval = string_to_datum(str, datatype);
4720 return makeConst(datatype, ((datatype == NAMEOID) ? NAMEDATALEN : -1),
4721 conval, false, false);
4725 * Generate a Const node of bytea type from a binary C string and a length.
4728 string_to_bytea_const(const char *str, size_t str_len)
4730 bytea *bstr = palloc(VARHDRSZ + str_len);
4733 memcpy(VARDATA(bstr), str, str_len);
4734 VARATT_SIZEP(bstr) = VARHDRSZ + str_len;
4735 conval = PointerGetDatum(bstr);
4737 return makeConst(BYTEAOID, -1, conval, false, false);
4740 /*-------------------------------------------------------------------------
4742 * Index cost estimation functions
4744 * genericcostestimate is a general-purpose estimator for use when we
4745 * don't have any better idea about how to estimate. Index-type-specific
4746 * knowledge can be incorporated in the type-specific routines.
4748 * One bit of index-type-specific knowledge we can relatively easily use
4749 * in genericcostestimate is the estimate of the number of index tuples
4750 * visited. If numIndexTuples is not 0 then it is used as the estimate,
4751 * otherwise we compute a generic estimate.
4753 *-------------------------------------------------------------------------
4757 genericcostestimate(PlannerInfo *root,
4758 IndexOptInfo *index, List *indexQuals,
4759 RelOptInfo *outer_rel,
4760 double numIndexTuples,
4761 Cost *indexStartupCost,
4762 Cost *indexTotalCost,
4763 Selectivity *indexSelectivity,
4764 double *indexCorrelation)
4766 double numIndexPages;
4767 double num_sa_scans;
4768 double num_outer_scans;
4770 QualCost index_qual_cost;
4771 double qual_op_cost;
4772 double qual_arg_cost;
4773 List *selectivityQuals;
4777 * If the index is partial, AND the index predicate with the explicitly
4778 * given indexquals to produce a more accurate idea of the index
4779 * selectivity. This may produce redundant clauses. We get rid of exact
4780 * duplicates in the code below. We expect that most cases of partial
4781 * redundancy (such as "x < 4" from the qual and "x < 5" from the
4782 * predicate) will be recognized and handled correctly by
4783 * clauselist_selectivity(). This assumption is somewhat fragile, since
4784 * it depends on predicate_implied_by() and clauselist_selectivity()
4785 * having similar capabilities, and there are certainly many cases where
4786 * we will end up with a too-low selectivity estimate. This will bias the
4787 * system in favor of using partial indexes where possible, which is not
4788 * necessarily a bad thing. But it'd be nice to do better someday.
4790 * Note that index->indpred and indexQuals are both in implicit-AND form,
4791 * so ANDing them together just takes merging the lists. However,
4792 * eliminating duplicates is a bit trickier because indexQuals contains
4793 * RestrictInfo nodes and the indpred does not. It is okay to pass a
4794 * mixed list to clauselist_selectivity, but we have to work a bit to
4795 * generate a list without logical duplicates. (We could just list_union
4796 * indpred and strippedQuals, but then we'd not get caching of per-qual
4797 * selectivity estimates.)
4799 if (index->indpred != NIL)
4801 List *strippedQuals;
4802 List *predExtraQuals;
4804 strippedQuals = get_actual_clauses(indexQuals);
4805 predExtraQuals = list_difference(index->indpred, strippedQuals);
4806 selectivityQuals = list_concat(predExtraQuals, indexQuals);
4809 selectivityQuals = indexQuals;
4812 * Check for ScalarArrayOpExpr index quals, and estimate the number of
4813 * index scans that will be performed.
4816 foreach(l, indexQuals)
4818 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
4820 if (IsA(rinfo->clause, ScalarArrayOpExpr))
4822 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
4823 int alength = estimate_array_length(lsecond(saop->args));
4826 num_sa_scans *= alength;
4830 /* Estimate the fraction of main-table tuples that will be visited */
4831 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
4836 * If caller didn't give us an estimate, estimate the number of index
4837 * tuples that will be visited. We do it in this rather peculiar-looking
4838 * way in order to get the right answer for partial indexes.
4840 if (numIndexTuples <= 0.0)
4842 numIndexTuples = *indexSelectivity * index->rel->tuples;
4845 * The above calculation counts all the tuples visited across all
4846 * scans induced by ScalarArrayOpExpr nodes. We want to consider the
4847 * average per-indexscan number, so adjust. This is a handy place to
4848 * round to integer, too. (If caller supplied tuple estimate, it's
4849 * responsible for handling these considerations.)
4851 numIndexTuples = rint(numIndexTuples / num_sa_scans);
4855 * We can bound the number of tuples by the index size in any case. Also,
4856 * always estimate at least one tuple is touched, even when
4857 * indexSelectivity estimate is tiny.
4859 if (numIndexTuples > index->tuples)
4860 numIndexTuples = index->tuples;
4861 if (numIndexTuples < 1.0)
4862 numIndexTuples = 1.0;
4865 * Estimate the number of index pages that will be retrieved.
4867 * We use the simplistic method of taking a pro-rata fraction of the total
4868 * number of index pages. In effect, this counts only leaf pages and not
4869 * any overhead such as index metapage or upper tree levels. In practice
4870 * this seems a better approximation than charging for access to the upper
4871 * levels, perhaps because those tend to stay in cache under load.
4873 if (index->pages > 1 && index->tuples > 1)
4874 numIndexPages = ceil(numIndexTuples * index->pages / index->tuples);
4876 numIndexPages = 1.0;
4879 * Now compute the disk access costs.
4881 * The above calculations are all per-index-scan. However, if we are in a
4882 * nestloop inner scan, we can expect the scan to be repeated (with
4883 * different search keys) for each row of the outer relation. Likewise,
4884 * ScalarArrayOpExpr quals result in multiple index scans. This creates
4885 * the potential for cache effects to reduce the number of disk page
4886 * fetches needed. We want to estimate the average per-scan I/O cost in
4887 * the presence of caching.
4889 * We use the Mackert-Lohman formula (see costsize.c for details) to
4890 * estimate the total number of page fetches that occur. While this
4891 * wasn't what it was designed for, it seems a reasonable model anyway.
4892 * Note that we are counting pages not tuples anymore, so we take N = T =
4893 * index size, as if there were one "tuple" per page.
4895 if (outer_rel != NULL && outer_rel->rows > 1)
4897 num_outer_scans = outer_rel->rows;
4898 num_scans = num_sa_scans * num_outer_scans;
4902 num_outer_scans = 1;
4903 num_scans = num_sa_scans;
4908 double pages_fetched;
4910 /* total page fetches ignoring cache effects */
4911 pages_fetched = numIndexPages * num_scans;
4913 /* use Mackert and Lohman formula to adjust for cache effects */
4914 pages_fetched = index_pages_fetched(pages_fetched,
4916 (double) index->pages,
4920 * Now compute the total disk access cost, and then report a pro-rated
4921 * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
4922 * since that's internal to the indexscan.)
4924 *indexTotalCost = (pages_fetched * random_page_cost) / num_outer_scans;
4929 * For a single index scan, we just charge random_page_cost per page
4932 *indexTotalCost = numIndexPages * random_page_cost;
4936 * A difficulty with the leaf-pages-only cost approach is that for small
4937 * selectivities (eg, single index tuple fetched) all indexes will look
4938 * equally attractive because we will estimate exactly 1 leaf page to be
4939 * fetched. All else being equal, we should prefer physically smaller
4940 * indexes over larger ones. (An index might be smaller because it is
4941 * partial or because it contains fewer columns; presumably the other
4942 * columns in the larger index aren't useful to the query, or the larger
4943 * index would have better selectivity.)
4945 * We can deal with this by adding a very small "fudge factor" that
4946 * depends on the index size. The fudge factor used here is one
4947 * random_page_cost per 100000 index pages, which should be small enough
4948 * to not alter index-vs-seqscan decisions, but will prevent indexes of
4949 * different sizes from looking exactly equally attractive.
4951 *indexTotalCost += index->pages * random_page_cost / 100000.0;
4954 * CPU cost: any complex expressions in the indexquals will need to be
4955 * evaluated once at the start of the scan to reduce them to runtime keys
4956 * to pass to the index AM (see nodeIndexscan.c). We model the per-tuple
4957 * CPU costs as cpu_index_tuple_cost plus one cpu_operator_cost per
4958 * indexqual operator. Because we have numIndexTuples as a per-scan
4959 * number, we have to multiply by num_sa_scans to get the correct result
4960 * for ScalarArrayOpExpr cases.
4962 * Note: this neglects the possible costs of rechecking lossy operators
4963 * and OR-clause expressions. Detecting that that might be needed seems
4964 * more expensive than it's worth, though, considering all the other
4965 * inaccuracies here ...
4967 cost_qual_eval(&index_qual_cost, indexQuals);
4968 qual_op_cost = cpu_operator_cost * list_length(indexQuals);
4969 qual_arg_cost = index_qual_cost.startup +
4970 index_qual_cost.per_tuple - qual_op_cost;
4971 if (qual_arg_cost < 0) /* just in case... */
4973 *indexStartupCost = qual_arg_cost;
4974 *indexTotalCost += qual_arg_cost;
4975 *indexTotalCost += numIndexTuples * num_sa_scans * (cpu_index_tuple_cost + qual_op_cost);
4978 * We also add a CPU-cost component to represent the general costs of
4979 * starting an indexscan, such as analysis of btree index keys and
4980 * initial tree descent. This is estimated at 100x cpu_operator_cost,
4981 * which is a bit arbitrary but seems the right order of magnitude.
4982 * (As noted above, we don't charge any I/O for touching upper tree
4983 * levels, but charging nothing at all has been found too optimistic.)
4985 * Although this is startup cost with respect to any one scan, we add
4986 * it to the "total" cost component because it's only very interesting
4987 * in the many-ScalarArrayOpExpr-scan case, and there it will be paid
4988 * over the life of the scan node.
4990 *indexTotalCost += num_sa_scans * 100.0 * cpu_operator_cost;
4993 * Generic assumption about index correlation: there isn't any.
4995 *indexCorrelation = 0.0;
5000 btcostestimate(PG_FUNCTION_ARGS)
5002 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
5003 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
5004 List *indexQuals = (List *) PG_GETARG_POINTER(2);
5005 RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(3);
5006 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(4);
5007 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(5);
5008 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(6);
5009 double *indexCorrelation = (double *) PG_GETARG_POINTER(7);
5013 double numIndexTuples;
5014 List *indexBoundQuals;
5018 double num_sa_scans;
5022 * For a btree scan, only leading '=' quals plus inequality quals for the
5023 * immediately next attribute contribute to index selectivity (these are
5024 * the "boundary quals" that determine the starting and stopping points of
5025 * the index scan). Additional quals can suppress visits to the heap, so
5026 * it's OK to count them in indexSelectivity, but they should not count
5027 * for estimating numIndexTuples. So we must examine the given indexQuals
5028 * to find out which ones count as boundary quals. We rely on the
5029 * knowledge that they are given in index column order.
5031 * For a RowCompareExpr, we consider only the first column, just as
5032 * rowcomparesel() does.
5034 * If there's a ScalarArrayOpExpr in the quals, we'll actually perform N
5035 * index scans not one, but the ScalarArrayOpExpr's operator can be
5036 * considered to act the same as it normally does.
5038 indexBoundQuals = NIL;
5043 foreach(l, indexQuals)
5045 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
5052 Assert(IsA(rinfo, RestrictInfo));
5053 clause = rinfo->clause;
5054 if (IsA(clause, OpExpr))
5056 leftop = get_leftop(clause);
5057 rightop = get_rightop(clause);
5058 clause_op = ((OpExpr *) clause)->opno;
5060 else if (IsA(clause, RowCompareExpr))
5062 RowCompareExpr *rc = (RowCompareExpr *) clause;
5064 leftop = (Node *) linitial(rc->largs);
5065 rightop = (Node *) linitial(rc->rargs);
5066 clause_op = linitial_oid(rc->opnos);
5068 else if (IsA(clause, ScalarArrayOpExpr))
5070 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
5072 leftop = (Node *) linitial(saop->args);
5073 rightop = (Node *) lsecond(saop->args);
5074 clause_op = saop->opno;
5079 elog(ERROR, "unsupported indexqual type: %d",
5080 (int) nodeTag(clause));
5081 continue; /* keep compiler quiet */
5083 if (match_index_to_operand(leftop, indexcol, index))
5085 /* clause_op is correct */
5087 else if (match_index_to_operand(rightop, indexcol, index))
5089 /* Must flip operator to get the opfamily member */
5090 clause_op = get_commutator(clause_op);
5094 /* Must be past the end of quals for indexcol, try next */
5096 break; /* done if no '=' qual for indexcol */
5099 if (match_index_to_operand(leftop, indexcol, index))
5101 /* clause_op is correct */
5103 else if (match_index_to_operand(rightop, indexcol, index))
5105 /* Must flip operator to get the opfamily member */
5106 clause_op = get_commutator(clause_op);
5110 /* No quals for new indexcol, so we are done */
5114 op_strategy = get_op_opfamily_strategy(clause_op,
5115 index->opfamily[indexcol]);
5116 Assert(op_strategy != 0); /* not a member of opfamily?? */
5117 if (op_strategy == BTEqualStrategyNumber)
5119 /* count up number of SA scans induced by indexBoundQuals only */
5120 if (IsA(clause, ScalarArrayOpExpr))
5122 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
5123 int alength = estimate_array_length(lsecond(saop->args));
5126 num_sa_scans *= alength;
5128 indexBoundQuals = lappend(indexBoundQuals, rinfo);
5132 * If index is unique and we found an '=' clause for each column, we can
5133 * just assume numIndexTuples = 1 and skip the expensive
5134 * clauselist_selectivity calculations.
5136 if (index->unique &&
5137 indexcol == index->ncolumns - 1 &&
5140 numIndexTuples = 1.0;
5143 Selectivity btreeSelectivity;
5145 btreeSelectivity = clauselist_selectivity(root, indexBoundQuals,
5148 numIndexTuples = btreeSelectivity * index->rel->tuples;
5150 * As in genericcostestimate(), we have to adjust for any
5151 * ScalarArrayOpExpr quals included in indexBoundQuals, and then
5154 numIndexTuples = rint(numIndexTuples / num_sa_scans);
5157 genericcostestimate(root, index, indexQuals, outer_rel, numIndexTuples,
5158 indexStartupCost, indexTotalCost,
5159 indexSelectivity, indexCorrelation);
5162 * If we can get an estimate of the first column's ordering correlation C
5163 * from pg_statistic, estimate the index correlation as C for a
5164 * single-column index, or C * 0.75 for multiple columns. (The idea here
5165 * is that multiple columns dilute the importance of the first column's
5166 * ordering, but don't negate it entirely. Before 8.0 we divided the
5167 * correlation by the number of columns, but that seems too strong.)
5169 * We can skip all this if we found a ScalarArrayOpExpr, because then the
5170 * call must be for a bitmap index scan, and the caller isn't going to
5171 * care what the index correlation is.
5176 if (index->indexkeys[0] != 0)
5178 /* Simple variable --- look to stats for the underlying table */
5179 relid = getrelid(index->rel->relid, root->parse->rtable);
5180 Assert(relid != InvalidOid);
5181 colnum = index->indexkeys[0];
5185 /* Expression --- maybe there are stats for the index itself */
5186 relid = index->indexoid;
5190 tuple = SearchSysCache(STATRELATT,
5191 ObjectIdGetDatum(relid),
5192 Int16GetDatum(colnum),
5195 if (HeapTupleIsValid(tuple))
5200 if (get_attstatsslot(tuple, InvalidOid, 0,
5201 STATISTIC_KIND_CORRELATION,
5202 index->fwdsortop[0],
5203 NULL, NULL, &numbers, &nnumbers))
5205 double varCorrelation;
5207 Assert(nnumbers == 1);
5208 varCorrelation = numbers[0];
5210 if (index->ncolumns > 1)
5211 *indexCorrelation = varCorrelation * 0.75;
5213 *indexCorrelation = varCorrelation;
5215 free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers);
5217 else if (get_attstatsslot(tuple, InvalidOid, 0,
5218 STATISTIC_KIND_CORRELATION,
5219 index->revsortop[0],
5220 NULL, NULL, &numbers, &nnumbers))
5222 double varCorrelation;
5224 Assert(nnumbers == 1);
5225 varCorrelation = numbers[0];
5227 if (index->ncolumns > 1)
5228 *indexCorrelation = - varCorrelation * 0.75;
5230 *indexCorrelation = - varCorrelation;
5232 free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers);
5234 ReleaseSysCache(tuple);
5241 hashcostestimate(PG_FUNCTION_ARGS)
5243 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
5244 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
5245 List *indexQuals = (List *) PG_GETARG_POINTER(2);
5246 RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(3);
5247 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(4);
5248 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(5);
5249 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(6);
5250 double *indexCorrelation = (double *) PG_GETARG_POINTER(7);
5252 genericcostestimate(root, index, indexQuals, outer_rel, 0.0,
5253 indexStartupCost, indexTotalCost,
5254 indexSelectivity, indexCorrelation);
5260 gistcostestimate(PG_FUNCTION_ARGS)
5262 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
5263 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
5264 List *indexQuals = (List *) PG_GETARG_POINTER(2);
5265 RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(3);
5266 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(4);
5267 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(5);
5268 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(6);
5269 double *indexCorrelation = (double *) PG_GETARG_POINTER(7);
5271 genericcostestimate(root, index, indexQuals, outer_rel, 0.0,
5272 indexStartupCost, indexTotalCost,
5273 indexSelectivity, indexCorrelation);
5279 gincostestimate(PG_FUNCTION_ARGS)
5281 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
5282 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
5283 List *indexQuals = (List *) PG_GETARG_POINTER(2);
5284 RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(3);
5285 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(4);
5286 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(5);
5287 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(6);
5288 double *indexCorrelation = (double *) PG_GETARG_POINTER(7);
5290 genericcostestimate(root, index, indexQuals, outer_rel, 0.0,
5291 indexStartupCost, indexTotalCost,
5292 indexSelectivity, indexCorrelation);