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-2005, 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.187 2005/07/21 04:41:43 momjian 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 "access/heapam.h"
80 #include "access/nbtree.h"
81 #include "access/tuptoaster.h"
82 #include "catalog/pg_namespace.h"
83 #include "catalog/pg_opclass.h"
84 #include "catalog/pg_operator.h"
85 #include "catalog/pg_proc.h"
86 #include "catalog/pg_statistic.h"
87 #include "catalog/pg_type.h"
88 #include "mb/pg_wchar.h"
89 #include "nodes/makefuncs.h"
90 #include "optimizer/clauses.h"
91 #include "optimizer/cost.h"
92 #include "optimizer/pathnode.h"
93 #include "optimizer/paths.h"
94 #include "optimizer/plancat.h"
95 #include "optimizer/prep.h"
96 #include "optimizer/restrictinfo.h"
97 #include "optimizer/tlist.h"
98 #include "optimizer/var.h"
99 #include "parser/parse_expr.h"
100 #include "parser/parse_func.h"
101 #include "parser/parse_oper.h"
102 #include "parser/parsetree.h"
103 #include "utils/builtins.h"
104 #include "utils/date.h"
105 #include "utils/datum.h"
106 #include "utils/int8.h"
107 #include "utils/lsyscache.h"
108 #include "utils/nabstime.h"
109 #include "utils/pg_locale.h"
110 #include "utils/selfuncs.h"
111 #include "utils/syscache.h"
114 /* Return data from examine_variable and friends */
117 Node *var; /* the Var or expression tree */
118 RelOptInfo *rel; /* Relation, or NULL if not identifiable */
119 HeapTuple statsTuple; /* pg_statistic tuple, or NULL if none */
120 /* NB: if statsTuple!=NULL, it must be freed when caller is done */
121 Oid vartype; /* exposed type of expression */
122 Oid atttype; /* type to pass to get_attstatsslot */
123 int32 atttypmod; /* typmod to pass to get_attstatsslot */
124 bool isunique; /* true if matched to a unique index */
127 #define ReleaseVariableStats(vardata) \
129 if (HeapTupleIsValid((vardata).statsTuple)) \
130 ReleaseSysCache((vardata).statsTuple); \
134 static bool convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
135 Datum lobound, Datum hibound, Oid boundstypid,
136 double *scaledlobound, double *scaledhibound);
137 static double convert_numeric_to_scalar(Datum value, Oid typid);
138 static void convert_string_to_scalar(unsigned char *value,
140 unsigned char *lobound,
141 double *scaledlobound,
142 unsigned char *hibound,
143 double *scaledhibound);
144 static void convert_bytea_to_scalar(Datum value,
147 double *scaledlobound,
149 double *scaledhibound);
150 static double convert_one_string_to_scalar(unsigned char *value,
151 int rangelo, int rangehi);
152 static double convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
153 int rangelo, int rangehi);
154 static unsigned char *convert_string_datum(Datum value, Oid typid);
155 static double convert_timevalue_to_scalar(Datum value, Oid typid);
156 static bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
157 VariableStatData *vardata, Node **other,
159 static void get_join_variables(PlannerInfo *root, List *args,
160 VariableStatData *vardata1,
161 VariableStatData *vardata2);
162 static void examine_variable(PlannerInfo *root, Node *node, int varRelid,
163 VariableStatData *vardata);
164 static double get_variable_numdistinct(VariableStatData *vardata);
165 static bool get_variable_maximum(PlannerInfo *root, VariableStatData *vardata,
166 Oid sortop, Datum *max);
167 static Selectivity prefix_selectivity(PlannerInfo *root, Node *variable,
168 Oid opclass, Const *prefix);
169 static Selectivity pattern_selectivity(Const *patt, Pattern_Type ptype);
170 static Datum string_to_datum(const char *str, Oid datatype);
171 static Const *string_to_const(const char *str, Oid datatype);
172 static Const *string_to_bytea_const(const char *str, size_t str_len);
176 * eqsel - Selectivity of "=" for any data types.
178 * Note: this routine is also used to estimate selectivity for some
179 * operators that are not "=" but have comparable selectivity behavior,
180 * such as "~=" (geometric approximate-match). Even for "=", we must
181 * keep in mind that the left and right datatypes may differ.
184 eqsel(PG_FUNCTION_ARGS)
186 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
187 Oid operator = PG_GETARG_OID(1);
188 List *args = (List *) PG_GETARG_POINTER(2);
189 int varRelid = PG_GETARG_INT32(3);
190 VariableStatData vardata;
200 * If expression is not variable = something or something = variable,
201 * then punt and return a default estimate.
203 if (!get_restriction_variable(root, args, varRelid,
204 &vardata, &other, &varonleft))
205 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
208 * If the something is a NULL constant, assume operator is strict and
209 * return zero, ie, operator will never return TRUE.
211 if (IsA(other, Const) &&
212 ((Const *) other)->constisnull)
214 ReleaseVariableStats(vardata);
215 PG_RETURN_FLOAT8(0.0);
218 if (HeapTupleIsValid(vardata.statsTuple))
220 Form_pg_statistic stats;
222 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
224 if (IsA(other, Const))
226 /* Variable is being compared to a known non-null constant */
227 Datum constval = ((Const *) other)->constvalue;
232 * Is the constant "=" to any of the column's most common
233 * values? (Although the given operator may not really be
234 * "=", we will assume that seeing whether it returns TRUE is
235 * an appropriate test. If you don't like this, maybe you
236 * shouldn't be using eqsel for your operator...)
238 if (get_attstatsslot(vardata.statsTuple,
239 vardata.atttype, vardata.atttypmod,
240 STATISTIC_KIND_MCV, InvalidOid,
242 &numbers, &nnumbers))
246 fmgr_info(get_opcode(operator), &eqproc);
248 for (i = 0; i < nvalues; i++)
250 /* be careful to apply operator right way 'round */
252 match = DatumGetBool(FunctionCall2(&eqproc,
256 match = DatumGetBool(FunctionCall2(&eqproc,
265 /* no most-common-value info available */
268 i = nvalues = nnumbers = 0;
274 * Constant is "=" to this common value. We know
275 * selectivity exactly (or as exactly as VACUUM could
276 * calculate it, anyway).
283 * Comparison is against a constant that is neither NULL
284 * nor any of the common values. Its selectivity cannot
287 double sumcommon = 0.0;
288 double otherdistinct;
290 for (i = 0; i < nnumbers; i++)
291 sumcommon += numbers[i];
292 selec = 1.0 - sumcommon - stats->stanullfrac;
293 CLAMP_PROBABILITY(selec);
296 * and in fact it's probably a good deal less. We
297 * approximate that all the not-common values share this
298 * remaining fraction equally, so we divide by the number
299 * of other distinct values.
301 otherdistinct = get_variable_numdistinct(&vardata)
303 if (otherdistinct > 1)
304 selec /= otherdistinct;
307 * Another cross-check: selectivity shouldn't be estimated
308 * as more than the least common "most common value".
310 if (nnumbers > 0 && selec > numbers[nnumbers - 1])
311 selec = numbers[nnumbers - 1];
314 free_attstatsslot(vardata.atttype, values, nvalues,
322 * Search is for a value that we do not know a priori, but we
323 * will assume it is not NULL. Estimate the selectivity as
324 * non-null fraction divided by number of distinct values, so
325 * that we get a result averaged over all possible values
326 * whether common or uncommon. (Essentially, we are assuming
327 * that the not-yet-known comparison value is equally likely
328 * to be any of the possible values, regardless of their
329 * frequency in the table. Is that a good idea?)
331 selec = 1.0 - stats->stanullfrac;
332 ndistinct = get_variable_numdistinct(&vardata);
337 * Cross-check: selectivity should never be estimated as more
338 * than the most common value's.
340 if (get_attstatsslot(vardata.statsTuple,
341 vardata.atttype, vardata.atttypmod,
342 STATISTIC_KIND_MCV, InvalidOid,
344 &numbers, &nnumbers))
346 if (nnumbers > 0 && selec > numbers[0])
348 free_attstatsslot(vardata.atttype, NULL, 0, numbers, nnumbers);
355 * No VACUUM ANALYZE stats available, so make a guess using
356 * estimated number of distinct values and assuming they are
357 * equally common. (The guess is unlikely to be very good, but we
358 * do know a few special cases.)
360 selec = 1.0 / get_variable_numdistinct(&vardata);
363 ReleaseVariableStats(vardata);
365 /* result should be in range, but make sure... */
366 CLAMP_PROBABILITY(selec);
368 PG_RETURN_FLOAT8((float8) selec);
372 * neqsel - Selectivity of "!=" for any data types.
374 * This routine is also used for some operators that are not "!="
375 * but have comparable selectivity behavior. See above comments
379 neqsel(PG_FUNCTION_ARGS)
381 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
382 Oid operator = PG_GETARG_OID(1);
383 List *args = (List *) PG_GETARG_POINTER(2);
384 int varRelid = PG_GETARG_INT32(3);
389 * We want 1 - eqsel() where the equality operator is the one
390 * associated with this != operator, that is, its negator.
392 eqop = get_negator(operator);
395 result = DatumGetFloat8(DirectFunctionCall4(eqsel,
396 PointerGetDatum(root),
397 ObjectIdGetDatum(eqop),
398 PointerGetDatum(args),
399 Int32GetDatum(varRelid)));
403 /* Use default selectivity (should we raise an error instead?) */
404 result = DEFAULT_EQ_SEL;
406 result = 1.0 - result;
407 PG_RETURN_FLOAT8(result);
411 * scalarineqsel - Selectivity of "<", "<=", ">", ">=" for scalars.
413 * This is the guts of both scalarltsel and scalargtsel. The caller has
414 * commuted the clause, if necessary, so that we can treat the variable as
415 * being on the left. The caller must also make sure that the other side
416 * of the clause is a non-null Const, and dissect same into a value and
419 * This routine works for any datatype (or pair of datatypes) known to
420 * convert_to_scalar(). If it is applied to some other datatype,
421 * it will return a default estimate.
424 scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
425 VariableStatData *vardata, Datum constval, Oid consttype)
427 Form_pg_statistic stats;
439 if (!HeapTupleIsValid(vardata->statsTuple))
441 /* no stats available, so default result */
442 return DEFAULT_INEQ_SEL;
444 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
446 fmgr_info(get_opcode(operator), &opproc);
449 * If we have most-common-values info, add up the fractions of the MCV
450 * entries that satisfy MCV OP CONST. These fractions contribute
451 * directly to the result selectivity. Also add up the total fraction
452 * represented by MCV entries.
457 if (get_attstatsslot(vardata->statsTuple,
458 vardata->atttype, vardata->atttypmod,
459 STATISTIC_KIND_MCV, InvalidOid,
461 &numbers, &nnumbers))
463 for (i = 0; i < nvalues; i++)
465 if (DatumGetBool(FunctionCall2(&opproc,
468 mcv_selec += numbers[i];
469 sumcommon += numbers[i];
471 free_attstatsslot(vardata->atttype, values, nvalues,
476 * If there is a histogram, determine which bin the constant falls in,
477 * and compute the resulting contribution to selectivity.
479 * Someday, VACUUM might store more than one histogram per rel/att,
480 * corresponding to more than one possible sort ordering defined for
481 * the column type. However, to make that work we will need to figure
482 * out which staop to search for --- it's not necessarily the one we
483 * have at hand! (For example, we might have a '<=' operator rather
484 * than the '<' operator that will appear in staop.) For now, assume
485 * that whatever appears in pg_statistic is sorted the same way our
486 * operator sorts, or the reverse way if isgt is TRUE.
490 if (get_attstatsslot(vardata->statsTuple,
491 vardata->atttype, vardata->atttypmod,
492 STATISTIC_KIND_HISTOGRAM, InvalidOid,
501 ltcmp = DatumGetBool(FunctionCall2(&opproc,
508 /* Constant is below lower histogram boundary. */
514 * Scan to find proper location. This could be made
515 * faster by using a binary-search method, but it's
516 * probably not worth the trouble for typical histogram
519 for (i = 1; i < nvalues; i++)
521 ltcmp = DatumGetBool(FunctionCall2(&opproc,
531 /* Constant is above upper histogram boundary. */
542 * We have values[i-1] < constant < values[i].
544 * Convert the constant and the two nearest bin boundary
545 * values to a uniform comparison scale, and do a
546 * linear interpolation within this bin.
548 if (convert_to_scalar(constval, consttype, &val,
549 values[i - 1], values[i],
555 /* cope if bin boundaries appear identical */
560 else if (val >= high)
564 binfrac = (val - low) / (high - low);
567 * Watch out for the possibility that we got a
568 * NaN or Infinity from the division. This
569 * can happen despite the previous checks, if
570 * for example "low" is -Infinity.
572 if (isnan(binfrac) ||
573 binfrac < 0.0 || binfrac > 1.0)
580 * Ideally we'd produce an error here, on the
581 * grounds that the given operator shouldn't have
582 * scalarXXsel registered as its selectivity func
583 * unless we can deal with its operand types. But
584 * currently, all manner of stuff is invoking
585 * scalarXXsel, so give a default estimate until
592 * Now, compute the overall selectivity across the
593 * values represented by the histogram. We have i-1
594 * full bins and binfrac partial bin below the
597 histfrac = (double) (i - 1) + binfrac;
598 histfrac /= (double) (nvalues - 1);
603 * Now histfrac = fraction of histogram entries below the
606 * Account for "<" vs ">"
608 hist_selec = isgt ? (1.0 - histfrac) : histfrac;
611 * The histogram boundaries are only approximate to begin
612 * with, and may well be out of date anyway. Therefore, don't
613 * believe extremely small or large selectivity estimates.
615 if (hist_selec < 0.0001)
617 else if (hist_selec > 0.9999)
621 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
625 * Now merge the results from the MCV and histogram calculations,
626 * realizing that the histogram covers only the non-null values that
627 * are not listed in MCV.
629 selec = 1.0 - stats->stanullfrac - sumcommon;
631 if (hist_selec > 0.0)
636 * If no histogram but there are values not accounted for by MCV,
637 * arbitrarily assume half of them will match.
644 /* result should be in range, but make sure... */
645 CLAMP_PROBABILITY(selec);
651 * scalarltsel - Selectivity of "<" (also "<=") for scalars.
654 scalarltsel(PG_FUNCTION_ARGS)
656 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
657 Oid operator = PG_GETARG_OID(1);
658 List *args = (List *) PG_GETARG_POINTER(2);
659 int varRelid = PG_GETARG_INT32(3);
660 VariableStatData vardata;
669 * If expression is not variable op something or something op
670 * variable, then punt and return a default estimate.
672 if (!get_restriction_variable(root, args, varRelid,
673 &vardata, &other, &varonleft))
674 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
677 * Can't do anything useful if the something is not a constant,
680 if (!IsA(other, Const))
682 ReleaseVariableStats(vardata);
683 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
687 * If the constant is NULL, assume operator is strict and return zero,
688 * ie, operator will never return TRUE.
690 if (((Const *) other)->constisnull)
692 ReleaseVariableStats(vardata);
693 PG_RETURN_FLOAT8(0.0);
695 constval = ((Const *) other)->constvalue;
696 consttype = ((Const *) other)->consttype;
699 * Force the var to be on the left to simplify logic in scalarineqsel.
703 /* we have var < other */
708 /* we have other < var, commute to make var > other */
709 operator = get_commutator(operator);
712 /* Use default selectivity (should we raise an error instead?) */
713 ReleaseVariableStats(vardata);
714 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
719 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
721 ReleaseVariableStats(vardata);
723 PG_RETURN_FLOAT8((float8) selec);
727 * scalargtsel - Selectivity of ">" (also ">=") for integers.
730 scalargtsel(PG_FUNCTION_ARGS)
732 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
733 Oid operator = PG_GETARG_OID(1);
734 List *args = (List *) PG_GETARG_POINTER(2);
735 int varRelid = PG_GETARG_INT32(3);
736 VariableStatData vardata;
745 * If expression is not variable op something or something op
746 * variable, then punt and return a default estimate.
748 if (!get_restriction_variable(root, args, varRelid,
749 &vardata, &other, &varonleft))
750 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
753 * Can't do anything useful if the something is not a constant,
756 if (!IsA(other, Const))
758 ReleaseVariableStats(vardata);
759 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
763 * If the constant is NULL, assume operator is strict and return zero,
764 * ie, operator will never return TRUE.
766 if (((Const *) other)->constisnull)
768 ReleaseVariableStats(vardata);
769 PG_RETURN_FLOAT8(0.0);
771 constval = ((Const *) other)->constvalue;
772 consttype = ((Const *) other)->consttype;
775 * Force the var to be on the left to simplify logic in scalarineqsel.
779 /* we have var > other */
784 /* we have other > var, commute to make var < other */
785 operator = get_commutator(operator);
788 /* Use default selectivity (should we raise an error instead?) */
789 ReleaseVariableStats(vardata);
790 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
795 selec = scalarineqsel(root, operator, isgt, &vardata, constval, consttype);
797 ReleaseVariableStats(vardata);
799 PG_RETURN_FLOAT8((float8) selec);
803 * patternsel - Generic code for pattern-match selectivity.
806 patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
808 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
811 Oid operator = PG_GETARG_OID(1);
813 List *args = (List *) PG_GETARG_POINTER(2);
814 int varRelid = PG_GETARG_INT32(3);
815 VariableStatData vardata;
823 Pattern_Prefix_Status pstatus;
825 Const *prefix = NULL;
830 * If expression is not variable op constant, then punt and return a
833 if (!get_restriction_variable(root, args, varRelid,
834 &vardata, &other, &varonleft))
835 return DEFAULT_MATCH_SEL;
836 if (!varonleft || !IsA(other, Const))
838 ReleaseVariableStats(vardata);
839 return DEFAULT_MATCH_SEL;
841 variable = (Node *) linitial(args);
844 * If the constant is NULL, assume operator is strict and return zero,
845 * ie, operator will never return TRUE.
847 if (((Const *) other)->constisnull)
849 ReleaseVariableStats(vardata);
852 constval = ((Const *) other)->constvalue;
853 consttype = ((Const *) other)->consttype;
856 * The right-hand const is type text or bytea for all supported
857 * operators. We do not expect to see binary-compatible types here,
858 * since const-folding should have relabeled the const to exactly
859 * match the operator's declared type.
861 if (consttype != TEXTOID && consttype != BYTEAOID)
863 ReleaseVariableStats(vardata);
864 return DEFAULT_MATCH_SEL;
868 * Similarly, the exposed type of the left-hand side should be one
869 * of those we know. (Do not look at vardata.atttype, which might be
870 * something binary-compatible but different.) We can use it to choose
871 * the index opclass from which we must draw the comparison operators.
873 * NOTE: It would be more correct to use the PATTERN opclasses than the
874 * simple ones, but at the moment ANALYZE will not generate statistics
875 * for the PATTERN operators. But our results are so approximate
876 * anyway that it probably hardly matters.
878 vartype = vardata.vartype;
883 opclass = TEXT_BTREE_OPS_OID;
886 opclass = VARCHAR_BTREE_OPS_OID;
889 opclass = BPCHAR_BTREE_OPS_OID;
892 opclass = NAME_BTREE_OPS_OID;
895 opclass = BYTEA_BTREE_OPS_OID;
898 ReleaseVariableStats(vardata);
899 return DEFAULT_MATCH_SEL;
902 /* divide pattern into fixed prefix and remainder */
903 patt = (Const *) other;
904 pstatus = pattern_fixed_prefix(patt, ptype, &prefix, &rest);
907 * If necessary, coerce the prefix constant to the right type. (The
908 * "rest" constant need not be changed.)
910 if (prefix && prefix->consttype != vartype)
914 switch (prefix->consttype)
917 prefixstr = DatumGetCString(DirectFunctionCall1(textout,
918 prefix->constvalue));
921 prefixstr = DatumGetCString(DirectFunctionCall1(byteaout,
922 prefix->constvalue));
925 elog(ERROR, "unrecognized consttype: %u",
927 ReleaseVariableStats(vardata);
928 return DEFAULT_MATCH_SEL;
930 prefix = string_to_const(prefixstr, vartype);
934 if (pstatus == Pattern_Prefix_Exact)
937 * Pattern specifies an exact match, so pretend operator is '='
939 Oid eqopr = get_opclass_member(opclass, InvalidOid,
940 BTEqualStrategyNumber);
943 if (eqopr == InvalidOid)
944 elog(ERROR, "no = operator for opclass %u", opclass);
945 eqargs = list_make2(variable, prefix);
946 result = DatumGetFloat8(DirectFunctionCall4(eqsel,
947 PointerGetDatum(root),
948 ObjectIdGetDatum(eqopr),
949 PointerGetDatum(eqargs),
950 Int32GetDatum(varRelid)));
955 * Not exact-match pattern. We estimate selectivity of the fixed
956 * prefix and remainder of pattern separately, then combine the
959 Selectivity prefixsel;
963 if (pstatus == Pattern_Prefix_Partial)
964 prefixsel = prefix_selectivity(root, variable, opclass, prefix);
967 restsel = pattern_selectivity(rest, ptype);
968 selec = prefixsel * restsel;
969 /* result should be in range, but make sure... */
970 CLAMP_PROBABILITY(selec);
976 pfree(DatumGetPointer(prefix->constvalue));
980 ReleaseVariableStats(vardata);
986 * regexeqsel - Selectivity of regular-expression pattern match.
989 regexeqsel(PG_FUNCTION_ARGS)
991 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex));
995 * icregexeqsel - Selectivity of case-insensitive regex match.
998 icregexeqsel(PG_FUNCTION_ARGS)
1000 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Regex_IC));
1004 * likesel - Selectivity of LIKE pattern match.
1007 likesel(PG_FUNCTION_ARGS)
1009 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like));
1013 * iclikesel - Selectivity of ILIKE pattern match.
1016 iclikesel(PG_FUNCTION_ARGS)
1018 PG_RETURN_FLOAT8(patternsel(fcinfo, Pattern_Type_Like_IC));
1022 * regexnesel - Selectivity of regular-expression pattern non-match.
1025 regexnesel(PG_FUNCTION_ARGS)
1029 result = patternsel(fcinfo, Pattern_Type_Regex);
1030 result = 1.0 - result;
1031 PG_RETURN_FLOAT8(result);
1035 * icregexnesel - Selectivity of case-insensitive regex non-match.
1038 icregexnesel(PG_FUNCTION_ARGS)
1042 result = patternsel(fcinfo, Pattern_Type_Regex_IC);
1043 result = 1.0 - result;
1044 PG_RETURN_FLOAT8(result);
1048 * nlikesel - Selectivity of LIKE pattern non-match.
1051 nlikesel(PG_FUNCTION_ARGS)
1055 result = patternsel(fcinfo, Pattern_Type_Like);
1056 result = 1.0 - result;
1057 PG_RETURN_FLOAT8(result);
1061 * icnlikesel - Selectivity of ILIKE pattern non-match.
1064 icnlikesel(PG_FUNCTION_ARGS)
1068 result = patternsel(fcinfo, Pattern_Type_Like_IC);
1069 result = 1.0 - result;
1070 PG_RETURN_FLOAT8(result);
1074 * booltestsel - Selectivity of BooleanTest Node.
1077 booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
1078 int varRelid, JoinType jointype)
1080 VariableStatData vardata;
1083 examine_variable(root, arg, varRelid, &vardata);
1085 if (HeapTupleIsValid(vardata.statsTuple))
1087 Form_pg_statistic stats;
1094 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1095 freq_null = stats->stanullfrac;
1097 if (get_attstatsslot(vardata.statsTuple,
1098 vardata.atttype, vardata.atttypmod,
1099 STATISTIC_KIND_MCV, InvalidOid,
1101 &numbers, &nnumbers)
1108 * Get first MCV frequency and derive frequency for true.
1110 if (DatumGetBool(values[0]))
1111 freq_true = numbers[0];
1113 freq_true = 1.0 - numbers[0] - freq_null;
1116 * Next derive frequency for false. Then use these as
1117 * appropriate to derive frequency for each case.
1119 freq_false = 1.0 - freq_true - freq_null;
1121 switch (booltesttype)
1124 /* select only NULL values */
1127 case IS_NOT_UNKNOWN:
1128 /* select non-NULL values */
1129 selec = 1.0 - freq_null;
1132 /* select only TRUE values */
1136 /* select non-TRUE values */
1137 selec = 1.0 - freq_true;
1140 /* select only FALSE values */
1144 /* select non-FALSE values */
1145 selec = 1.0 - freq_false;
1148 elog(ERROR, "unrecognized booltesttype: %d",
1149 (int) booltesttype);
1150 selec = 0.0; /* Keep compiler quiet */
1154 free_attstatsslot(vardata.atttype, values, nvalues,
1160 * No most-common-value info available. Still have null
1161 * fraction information, so use it for IS [NOT] UNKNOWN.
1162 * Otherwise adjust for null fraction and assume an even split
1163 * for boolean tests.
1165 switch (booltesttype)
1170 * Use freq_null directly.
1174 case IS_NOT_UNKNOWN:
1177 * Select not unknown (not null) values. Calculate
1180 selec = 1.0 - freq_null;
1186 selec = (1.0 - freq_null) / 2.0;
1189 elog(ERROR, "unrecognized booltesttype: %d",
1190 (int) booltesttype);
1191 selec = 0.0; /* Keep compiler quiet */
1199 * If we can't get variable statistics for the argument, perhaps
1200 * clause_selectivity can do something with it. We ignore the
1201 * possibility of a NULL value when using clause_selectivity, and
1202 * just assume the value is either TRUE or FALSE.
1204 switch (booltesttype)
1207 selec = DEFAULT_UNK_SEL;
1209 case IS_NOT_UNKNOWN:
1210 selec = DEFAULT_NOT_UNK_SEL;
1214 selec = (double) clause_selectivity(root, arg,
1215 varRelid, jointype);
1219 selec = 1.0 - (double) clause_selectivity(root, arg,
1220 varRelid, jointype);
1223 elog(ERROR, "unrecognized booltesttype: %d",
1224 (int) booltesttype);
1225 selec = 0.0; /* Keep compiler quiet */
1230 ReleaseVariableStats(vardata);
1232 /* result should be in range, but make sure... */
1233 CLAMP_PROBABILITY(selec);
1235 return (Selectivity) selec;
1239 * nulltestsel - Selectivity of NullTest Node.
1242 nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
1243 Node *arg, int varRelid)
1245 VariableStatData vardata;
1248 examine_variable(root, arg, varRelid, &vardata);
1250 if (HeapTupleIsValid(vardata.statsTuple))
1252 Form_pg_statistic stats;
1255 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
1256 freq_null = stats->stanullfrac;
1258 switch (nulltesttype)
1263 * Use freq_null directly.
1270 * Select not unknown (not null) values. Calculate from
1273 selec = 1.0 - freq_null;
1276 elog(ERROR, "unrecognized nulltesttype: %d",
1277 (int) nulltesttype);
1278 return (Selectivity) 0; /* keep compiler quiet */
1284 * No VACUUM ANALYZE stats available, so make a guess
1286 switch (nulltesttype)
1289 selec = DEFAULT_UNK_SEL;
1292 selec = DEFAULT_NOT_UNK_SEL;
1295 elog(ERROR, "unrecognized nulltesttype: %d",
1296 (int) nulltesttype);
1297 return (Selectivity) 0; /* keep compiler quiet */
1301 ReleaseVariableStats(vardata);
1303 /* result should be in range, but make sure... */
1304 CLAMP_PROBABILITY(selec);
1306 return (Selectivity) selec;
1310 * eqjoinsel - Join selectivity of "="
1313 eqjoinsel(PG_FUNCTION_ARGS)
1315 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1316 Oid operator = PG_GETARG_OID(1);
1317 List *args = (List *) PG_GETARG_POINTER(2);
1318 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
1320 VariableStatData vardata1;
1321 VariableStatData vardata2;
1324 Form_pg_statistic stats1 = NULL;
1325 Form_pg_statistic stats2 = NULL;
1326 bool have_mcvs1 = false;
1327 Datum *values1 = NULL;
1329 float4 *numbers1 = NULL;
1331 bool have_mcvs2 = false;
1332 Datum *values2 = NULL;
1334 float4 *numbers2 = NULL;
1337 get_join_variables(root, args, &vardata1, &vardata2);
1339 nd1 = get_variable_numdistinct(&vardata1);
1340 nd2 = get_variable_numdistinct(&vardata2);
1342 if (HeapTupleIsValid(vardata1.statsTuple))
1344 stats1 = (Form_pg_statistic) GETSTRUCT(vardata1.statsTuple);
1345 have_mcvs1 = get_attstatsslot(vardata1.statsTuple,
1350 &values1, &nvalues1,
1351 &numbers1, &nnumbers1);
1354 if (HeapTupleIsValid(vardata2.statsTuple))
1356 stats2 = (Form_pg_statistic) GETSTRUCT(vardata2.statsTuple);
1357 have_mcvs2 = get_attstatsslot(vardata2.statsTuple,
1362 &values2, &nvalues2,
1363 &numbers2, &nnumbers2);
1366 if (have_mcvs1 && have_mcvs2)
1369 * We have most-common-value lists for both relations. Run
1370 * through the lists to see which MCVs actually join to each other
1371 * with the given operator. This allows us to determine the exact
1372 * join selectivity for the portion of the relations represented
1373 * by the MCV lists. We still have to estimate for the remaining
1374 * population, but in a skewed distribution this gives us a big
1375 * leg up in accuracy. For motivation see the analysis in Y.
1376 * Ioannidis and S. Christodoulakis, "On the propagation of errors
1377 * in the size of join results", Technical Report 1018, Computer
1378 * Science Dept., University of Wisconsin, Madison, March 1991
1379 * (available from ftp.cs.wisc.edu).
1384 double nullfrac1 = stats1->stanullfrac;
1385 double nullfrac2 = stats2->stanullfrac;
1386 double matchprodfreq,
1398 fmgr_info(get_opcode(operator), &eqproc);
1399 hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool));
1400 hasmatch2 = (bool *) palloc0(nvalues2 * sizeof(bool));
1403 * If we are doing any variant of JOIN_IN, pretend all the values
1404 * of the righthand relation are unique (ie, act as if it's been
1407 * NOTE: it might seem that we should unique-ify the lefthand input
1408 * when considering JOIN_REVERSE_IN. But this is not so, because
1409 * the join clause we've been handed has not been commuted from
1410 * the way the parser originally wrote it. We know that the
1411 * unique side of the IN clause is *always* on the right.
1413 * NOTE: it would be dangerous to try to be smart about JOIN_LEFT or
1414 * JOIN_RIGHT here, because we do not have enough information to
1415 * determine which var is really on which side of the join.
1416 * Perhaps someday we should pass in more information.
1418 if (jointype == JOIN_IN ||
1419 jointype == JOIN_REVERSE_IN ||
1420 jointype == JOIN_UNIQUE_INNER ||
1421 jointype == JOIN_UNIQUE_OUTER)
1423 float4 oneovern = 1.0 / nd2;
1425 for (i = 0; i < nvalues2; i++)
1426 numbers2[i] = oneovern;
1427 nullfrac2 = oneovern;
1431 * Note we assume that each MCV will match at most one member of
1432 * the other MCV list. If the operator isn't really equality,
1433 * there could be multiple matches --- but we don't look for them,
1434 * both for speed and because the math wouldn't add up...
1436 matchprodfreq = 0.0;
1438 for (i = 0; i < nvalues1; i++)
1442 for (j = 0; j < nvalues2; j++)
1446 if (DatumGetBool(FunctionCall2(&eqproc,
1450 hasmatch1[i] = hasmatch2[j] = true;
1451 matchprodfreq += numbers1[i] * numbers2[j];
1457 CLAMP_PROBABILITY(matchprodfreq);
1458 /* Sum up frequencies of matched and unmatched MCVs */
1459 matchfreq1 = unmatchfreq1 = 0.0;
1460 for (i = 0; i < nvalues1; i++)
1463 matchfreq1 += numbers1[i];
1465 unmatchfreq1 += numbers1[i];
1467 CLAMP_PROBABILITY(matchfreq1);
1468 CLAMP_PROBABILITY(unmatchfreq1);
1469 matchfreq2 = unmatchfreq2 = 0.0;
1470 for (i = 0; i < nvalues2; i++)
1473 matchfreq2 += numbers2[i];
1475 unmatchfreq2 += numbers2[i];
1477 CLAMP_PROBABILITY(matchfreq2);
1478 CLAMP_PROBABILITY(unmatchfreq2);
1483 * Compute total frequency of non-null values that are not in the
1486 otherfreq1 = 1.0 - nullfrac1 - matchfreq1 - unmatchfreq1;
1487 otherfreq2 = 1.0 - nullfrac2 - matchfreq2 - unmatchfreq2;
1488 CLAMP_PROBABILITY(otherfreq1);
1489 CLAMP_PROBABILITY(otherfreq2);
1492 * We can estimate the total selectivity from the point of view of
1493 * relation 1 as: the known selectivity for matched MCVs, plus
1494 * unmatched MCVs that are assumed to match against random members
1495 * of relation 2's non-MCV population, plus non-MCV values that
1496 * are assumed to match against random members of relation 2's
1497 * unmatched MCVs plus non-MCV values.
1499 totalsel1 = matchprodfreq;
1501 totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - nvalues2);
1503 totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
1505 /* Same estimate from the point of view of relation 2. */
1506 totalsel2 = matchprodfreq;
1508 totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - nvalues1);
1510 totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
1514 * Use the smaller of the two estimates. This can be justified in
1515 * essentially the same terms as given below for the no-stats
1516 * case: to a first approximation, we are estimating from the
1517 * point of view of the relation with smaller nd.
1519 selec = (totalsel1 < totalsel2) ? totalsel1 : totalsel2;
1524 * We do not have MCV lists for both sides. Estimate the join
1525 * selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2).
1526 * This is plausible if we assume that the join operator is strict
1527 * and the non-null values are about equally distributed: a given
1528 * non-null tuple of rel1 will join to either zero or
1529 * N2*(1-nullfrac2)/nd2 rows of rel2, so total join rows are at
1530 * most N1*(1-nullfrac1)*N2*(1-nullfrac2)/nd2 giving a join
1531 * selectivity of not more than (1-nullfrac1)*(1-nullfrac2)/nd2.
1532 * By the same logic it is not more than
1533 * (1-nullfrac1)*(1-nullfrac2)/nd1, so the expression with MIN()
1534 * is an upper bound. Using the MIN() means we estimate from the
1535 * point of view of the relation with smaller nd (since the larger
1536 * nd is determining the MIN). It is reasonable to assume that
1537 * most tuples in this rel will have join partners, so the bound
1538 * is probably reasonably tight and should be taken as-is.
1540 * XXX Can we be smarter if we have an MCV list for just one side? It
1541 * seems that if we assume equal distribution for the other side,
1542 * we end up with the same answer anyway.
1544 double nullfrac1 = stats1 ? stats1->stanullfrac : 0.0;
1545 double nullfrac2 = stats2 ? stats2->stanullfrac : 0.0;
1547 selec = (1.0 - nullfrac1) * (1.0 - nullfrac2);
1555 free_attstatsslot(vardata1.atttype, values1, nvalues1,
1556 numbers1, nnumbers1);
1558 free_attstatsslot(vardata2.atttype, values2, nvalues2,
1559 numbers2, nnumbers2);
1561 ReleaseVariableStats(vardata1);
1562 ReleaseVariableStats(vardata2);
1564 CLAMP_PROBABILITY(selec);
1566 PG_RETURN_FLOAT8((float8) selec);
1570 * neqjoinsel - Join selectivity of "!="
1573 neqjoinsel(PG_FUNCTION_ARGS)
1575 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1576 Oid operator = PG_GETARG_OID(1);
1577 List *args = (List *) PG_GETARG_POINTER(2);
1578 JoinType jointype = (JoinType) PG_GETARG_INT16(3);
1583 * We want 1 - eqjoinsel() where the equality operator is the one
1584 * associated with this != operator, that is, its negator.
1586 eqop = get_negator(operator);
1589 result = DatumGetFloat8(DirectFunctionCall4(eqjoinsel,
1590 PointerGetDatum(root),
1591 ObjectIdGetDatum(eqop),
1592 PointerGetDatum(args),
1593 Int16GetDatum(jointype)));
1597 /* Use default selectivity (should we raise an error instead?) */
1598 result = DEFAULT_EQ_SEL;
1600 result = 1.0 - result;
1601 PG_RETURN_FLOAT8(result);
1605 * scalarltjoinsel - Join selectivity of "<" and "<=" for scalars
1608 scalarltjoinsel(PG_FUNCTION_ARGS)
1610 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1614 * scalargtjoinsel - Join selectivity of ">" and ">=" for scalars
1617 scalargtjoinsel(PG_FUNCTION_ARGS)
1619 PG_RETURN_FLOAT8(DEFAULT_INEQ_SEL);
1623 * regexeqjoinsel - Join selectivity of regular-expression pattern match.
1626 regexeqjoinsel(PG_FUNCTION_ARGS)
1628 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
1632 * icregexeqjoinsel - Join selectivity of case-insensitive regex match.
1635 icregexeqjoinsel(PG_FUNCTION_ARGS)
1637 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
1641 * likejoinsel - Join selectivity of LIKE pattern match.
1644 likejoinsel(PG_FUNCTION_ARGS)
1646 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
1650 * iclikejoinsel - Join selectivity of ILIKE pattern match.
1653 iclikejoinsel(PG_FUNCTION_ARGS)
1655 PG_RETURN_FLOAT8(DEFAULT_MATCH_SEL);
1659 * regexnejoinsel - Join selectivity of regex non-match.
1662 regexnejoinsel(PG_FUNCTION_ARGS)
1666 result = DatumGetFloat8(regexeqjoinsel(fcinfo));
1667 result = 1.0 - result;
1668 PG_RETURN_FLOAT8(result);
1672 * icregexnejoinsel - Join selectivity of case-insensitive regex non-match.
1675 icregexnejoinsel(PG_FUNCTION_ARGS)
1679 result = DatumGetFloat8(icregexeqjoinsel(fcinfo));
1680 result = 1.0 - result;
1681 PG_RETURN_FLOAT8(result);
1685 * nlikejoinsel - Join selectivity of LIKE pattern non-match.
1688 nlikejoinsel(PG_FUNCTION_ARGS)
1692 result = DatumGetFloat8(likejoinsel(fcinfo));
1693 result = 1.0 - result;
1694 PG_RETURN_FLOAT8(result);
1698 * icnlikejoinsel - Join selectivity of ILIKE pattern non-match.
1701 icnlikejoinsel(PG_FUNCTION_ARGS)
1705 result = DatumGetFloat8(iclikejoinsel(fcinfo));
1706 result = 1.0 - result;
1707 PG_RETURN_FLOAT8(result);
1711 * mergejoinscansel - Scan selectivity of merge join.
1713 * A merge join will stop as soon as it exhausts either input stream.
1714 * Therefore, if we can estimate the ranges of both input variables,
1715 * we can estimate how much of the input will actually be read. This
1716 * can have a considerable impact on the cost when using indexscans.
1718 * clause should be a clause already known to be mergejoinable.
1720 * *leftscan is set to the fraction of the left-hand variable expected
1721 * to be scanned (0 to 1), and similarly *rightscan for the right-hand
1725 mergejoinscansel(PlannerInfo *root, Node *clause,
1726 Selectivity *leftscan,
1727 Selectivity *rightscan)
1731 VariableStatData leftvar,
1747 /* Set default results if we can't figure anything out. */
1748 *leftscan = *rightscan = 1.0;
1750 /* Deconstruct the merge clause */
1751 if (!is_opclause(clause))
1752 return; /* shouldn't happen */
1753 opno = ((OpExpr *) clause)->opno;
1754 left = get_leftop((Expr *) clause);
1755 right = get_rightop((Expr *) clause);
1757 return; /* shouldn't happen */
1759 /* Look for stats for the inputs */
1760 examine_variable(root, left, 0, &leftvar);
1761 examine_variable(root, right, 0, &rightvar);
1763 /* Get the direct input types of the operator */
1764 lefttype = exprType(left);
1765 righttype = exprType(right);
1767 /* Verify mergejoinability and get left and right "<" operators */
1768 if (!op_mergejoinable(opno,
1771 goto fail; /* shouldn't happen */
1773 /* Try to get maximum values of both inputs */
1774 if (!get_variable_maximum(root, &leftvar, lsortop, &leftmax))
1775 goto fail; /* no max available from stats */
1777 if (!get_variable_maximum(root, &rightvar, rsortop, &rightmax))
1778 goto fail; /* no max available from stats */
1780 /* Look up the "left < right" and "left > right" operators */
1781 op_mergejoin_crossops(opno, <op, >op, NULL, NULL);
1783 /* Look up the "left <= right" operator */
1784 leop = get_negator(gtop);
1785 if (!OidIsValid(leop))
1786 goto fail; /* insufficient info in catalogs */
1788 /* Look up the "right > left" operator */
1789 revgtop = get_commutator(ltop);
1790 if (!OidIsValid(revgtop))
1791 goto fail; /* insufficient info in catalogs */
1793 /* Look up the "right <= left" operator */
1794 revleop = get_negator(revgtop);
1795 if (!OidIsValid(revleop))
1796 goto fail; /* insufficient info in catalogs */
1799 * Now, the fraction of the left variable that will be scanned is the
1800 * fraction that's <= the right-side maximum value. But only believe
1801 * non-default estimates, else stick with our 1.0.
1803 selec = scalarineqsel(root, leop, false, &leftvar,
1804 rightmax, righttype);
1805 if (selec != DEFAULT_INEQ_SEL)
1808 /* And similarly for the right variable. */
1809 selec = scalarineqsel(root, revleop, false, &rightvar,
1811 if (selec != DEFAULT_INEQ_SEL)
1815 * Only one of the two fractions can really be less than 1.0; believe
1816 * the smaller estimate and reset the other one to exactly 1.0. If we
1817 * get exactly equal estimates (as can easily happen with self-joins),
1820 if (*leftscan > *rightscan)
1822 else if (*leftscan < *rightscan)
1825 *leftscan = *rightscan = 1.0;
1828 ReleaseVariableStats(leftvar);
1829 ReleaseVariableStats(rightvar);
1834 * Helper routine for estimate_num_groups: add an item to a list of
1835 * GroupVarInfos, but only if it's not known equal to any of the existing
1840 Node *var; /* might be an expression, not just a Var */
1841 RelOptInfo *rel; /* relation it belongs to */
1842 double ndistinct; /* # distinct values */
1846 add_unique_group_var(PlannerInfo *root, List *varinfos,
1847 Node *var, VariableStatData *vardata)
1849 GroupVarInfo *varinfo;
1853 ndistinct = get_variable_numdistinct(vardata);
1855 /* cannot use foreach here because of possible list_delete */
1856 lc = list_head(varinfos);
1859 varinfo = (GroupVarInfo *) lfirst(lc);
1861 /* must advance lc before list_delete possibly pfree's it */
1864 /* Drop exact duplicates */
1865 if (equal(var, varinfo->var))
1869 * Drop known-equal vars, but only if they belong to different
1870 * relations (see comments for estimate_num_groups)
1872 if (vardata->rel != varinfo->rel &&
1873 exprs_known_equal(root, var, varinfo->var))
1875 if (varinfo->ndistinct <= ndistinct)
1877 /* Keep older item, forget new one */
1882 /* Delete the older item */
1883 varinfos = list_delete_ptr(varinfos, varinfo);
1888 varinfo = (GroupVarInfo *) palloc(sizeof(GroupVarInfo));
1891 varinfo->rel = vardata->rel;
1892 varinfo->ndistinct = ndistinct;
1893 varinfos = lappend(varinfos, varinfo);
1898 * estimate_num_groups - Estimate number of groups in a grouped query
1900 * Given a query having a GROUP BY clause, estimate how many groups there
1901 * will be --- ie, the number of distinct combinations of the GROUP BY
1904 * This routine is also used to estimate the number of rows emitted by
1905 * a DISTINCT filtering step; that is an isomorphic problem. (Note:
1906 * actually, we only use it for DISTINCT when there's no grouping or
1907 * aggregation ahead of the DISTINCT.)
1911 * groupExprs - list of expressions being grouped by
1912 * input_rows - number of rows estimated to arrive at the group/unique
1915 * Given the lack of any cross-correlation statistics in the system, it's
1916 * impossible to do anything really trustworthy with GROUP BY conditions
1917 * involving multiple Vars. We should however avoid assuming the worst
1918 * case (all possible cross-product terms actually appear as groups) since
1919 * very often the grouped-by Vars are highly correlated. Our current approach
1921 * 1. Reduce the given expressions to a list of unique Vars used. For
1922 * example, GROUP BY a, a + b is treated the same as GROUP BY a, b.
1923 * It is clearly correct not to count the same Var more than once.
1924 * It is also reasonable to treat f(x) the same as x: f() cannot
1925 * increase the number of distinct values (unless it is volatile,
1926 * which we consider unlikely for grouping), but it probably won't
1927 * reduce the number of distinct values much either.
1928 * As a special case, if a GROUP BY expression can be matched to an
1929 * expressional index for which we have statistics, then we treat the
1930 * whole expression as though it were just a Var.
1931 * 2. If the list contains Vars of different relations that are known equal
1932 * due to equijoin clauses, then drop all but one of the Vars from each
1933 * known-equal set, keeping the one with smallest estimated # of values
1934 * (since the extra values of the others can't appear in joined rows).
1935 * Note the reason we only consider Vars of different relations is that
1936 * if we considered ones of the same rel, we'd be double-counting the
1937 * restriction selectivity of the equality in the next step.
1938 * 3. For Vars within a single source rel, we multiply together the numbers
1939 * of values, clamp to the number of rows in the rel (divided by 10 if
1940 * more than one Var), and then multiply by the selectivity of the
1941 * restriction clauses for that rel. When there's more than one Var,
1942 * the initial product is probably too high (it's the worst case) but
1943 * clamping to a fraction of the rel's rows seems to be a helpful
1944 * heuristic for not letting the estimate get out of hand. (The factor
1945 * of 10 is derived from pre-Postgres-7.4 practice.) Multiplying
1946 * by the restriction selectivity is effectively assuming that the
1947 * restriction clauses are independent of the grouping, which is a crummy
1948 * assumption, but it's hard to do better.
1949 * 4. If there are Vars from multiple rels, we repeat step 3 for each such
1950 * rel, and multiply the results together.
1951 * Note that rels not containing grouped Vars are ignored completely, as are
1952 * join clauses other than the equijoin clauses used in step 2. Such rels
1953 * cannot increase the number of groups, and we assume such clauses do not
1954 * reduce the number either (somewhat bogus, but we don't have the info to
1958 estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows)
1960 List *varinfos = NIL;
1964 /* We should not be called unless query has GROUP BY (or DISTINCT) */
1965 Assert(groupExprs != NIL);
1968 * Steps 1/2: find the unique Vars used, treating an expression as a Var
1969 * if we can find stats for it. For each one, record the statistical
1970 * estimate of number of distinct values (total in its table, without
1971 * regard for filtering).
1973 foreach(l, groupExprs)
1975 Node *groupexpr = (Node *) lfirst(l);
1976 VariableStatData vardata;
1981 * If examine_variable is able to deduce anything about the GROUP BY
1982 * expression, treat it as a single variable even if it's really more
1985 examine_variable(root, groupexpr, 0, &vardata);
1986 if (vardata.statsTuple != NULL || vardata.isunique)
1988 varinfos = add_unique_group_var(root, varinfos,
1989 groupexpr, &vardata);
1990 ReleaseVariableStats(vardata);
1993 ReleaseVariableStats(vardata);
1996 * Else pull out the component Vars
1998 varshere = pull_var_clause(groupexpr, false);
2001 * If we find any variable-free GROUP BY item, then either it is a
2002 * constant (and we can ignore it) or it contains a volatile
2003 * function; in the latter case we punt and assume that each input
2004 * row will yield a distinct group.
2006 if (varshere == NIL)
2008 if (contain_volatile_functions(groupexpr))
2014 * Else add variables to varinfos list
2016 foreach(l2, varshere)
2018 Node *var = (Node *) lfirst(l2);
2020 examine_variable(root, var, 0, &vardata);
2021 varinfos = add_unique_group_var(root, varinfos, var, &vardata);
2022 ReleaseVariableStats(vardata);
2026 /* If now no Vars, we must have an all-constant GROUP BY list. */
2027 if (varinfos == NIL)
2031 * Steps 3/4: group Vars by relation and estimate total numdistinct.
2033 * For each iteration of the outer loop, we process the frontmost Var in
2034 * varinfos, plus all other Vars in the same relation. We remove
2035 * these Vars from the newvarinfos list for the next iteration. This
2036 * is the easiest way to group Vars of same rel together.
2042 GroupVarInfo *varinfo1 = (GroupVarInfo *) linitial(varinfos);
2043 RelOptInfo *rel = varinfo1->rel;
2044 double reldistinct = varinfo1->ndistinct;
2045 double relmaxndistinct = reldistinct;
2046 int relvarcount = 1;
2047 List *newvarinfos = NIL;
2050 * Get the product of numdistinct estimates of the Vars for this rel.
2051 * Also, construct new varinfos list of remaining Vars.
2053 for_each_cell(l, lnext(list_head(varinfos)))
2055 GroupVarInfo *varinfo2 = (GroupVarInfo *) lfirst(l);
2057 if (varinfo2->rel == varinfo1->rel)
2059 reldistinct *= varinfo2->ndistinct;
2060 if (relmaxndistinct < varinfo2->ndistinct)
2061 relmaxndistinct = varinfo2->ndistinct;
2066 /* not time to process varinfo2 yet */
2067 newvarinfos = lcons(varinfo2, newvarinfos);
2072 * Sanity check --- don't divide by zero if empty relation.
2074 Assert(rel->reloptkind == RELOPT_BASEREL);
2075 if (rel->tuples > 0)
2078 * Clamp to size of rel, or size of rel / 10 if multiple Vars.
2079 * The fudge factor is because the Vars are probably correlated
2080 * but we don't know by how much. We should never clamp to less
2081 * than the largest ndistinct value for any of the Vars, though,
2082 * since there will surely be at least that many groups.
2084 double clamp = rel->tuples;
2086 if (relvarcount > 1)
2089 if (clamp < relmaxndistinct)
2091 clamp = relmaxndistinct;
2092 /* for sanity in case some ndistinct is too large: */
2093 if (clamp > rel->tuples)
2094 clamp = rel->tuples;
2097 if (reldistinct > clamp)
2098 reldistinct = clamp;
2101 * Multiply by restriction selectivity.
2103 reldistinct *= rel->rows / rel->tuples;
2106 * Update estimate of total distinct groups.
2108 numdistinct *= reldistinct;
2111 varinfos = newvarinfos;
2112 } while (varinfos != NIL);
2114 numdistinct = ceil(numdistinct);
2116 /* Guard against out-of-range answers */
2117 if (numdistinct > input_rows)
2118 numdistinct = input_rows;
2119 if (numdistinct < 1.0)
2126 * Estimate hash bucketsize fraction (ie, number of entries in a bucket
2127 * divided by total tuples in relation) if the specified expression is used
2130 * XXX This is really pretty bogus since we're effectively assuming that the
2131 * distribution of hash keys will be the same after applying restriction
2132 * clauses as it was in the underlying relation. However, we are not nearly
2133 * smart enough to figure out how the restrict clauses might change the
2134 * distribution, so this will have to do for now.
2136 * We are passed the number of buckets the executor will use for the given
2137 * input relation. If the data were perfectly distributed, with the same
2138 * number of tuples going into each available bucket, then the bucketsize
2139 * fraction would be 1/nbuckets. But this happy state of affairs will occur
2140 * only if (a) there are at least nbuckets distinct data values, and (b)
2141 * we have a not-too-skewed data distribution. Otherwise the buckets will
2142 * be nonuniformly occupied. If the other relation in the join has a key
2143 * distribution similar to this one's, then the most-loaded buckets are
2144 * exactly those that will be probed most often. Therefore, the "average"
2145 * bucket size for costing purposes should really be taken as something close
2146 * to the "worst case" bucket size. We try to estimate this by adjusting the
2147 * fraction if there are too few distinct data values, and then scaling up
2148 * by the ratio of the most common value's frequency to the average frequency.
2150 * If no statistics are available, use a default estimate of 0.1. This will
2151 * discourage use of a hash rather strongly if the inner relation is large,
2152 * which is what we want. We do not want to hash unless we know that the
2153 * inner rel is well-dispersed (or the alternatives seem much worse).
2156 estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
2158 VariableStatData vardata;
2167 examine_variable(root, hashkey, 0, &vardata);
2169 /* Get number of distinct values and fraction that are null */
2170 ndistinct = get_variable_numdistinct(&vardata);
2172 if (HeapTupleIsValid(vardata.statsTuple))
2174 Form_pg_statistic stats;
2176 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
2177 stanullfrac = stats->stanullfrac;
2182 * Believe a default ndistinct only if it came from stats.
2183 * Otherwise punt and return 0.1, per comments above.
2185 if (ndistinct == DEFAULT_NUM_DISTINCT)
2187 ReleaseVariableStats(vardata);
2188 return (Selectivity) 0.1;
2194 /* Compute avg freq of all distinct data values in raw relation */
2195 avgfreq = (1.0 - stanullfrac) / ndistinct;
2198 * Adjust ndistinct to account for restriction clauses. Observe we
2199 * are assuming that the data distribution is affected uniformly by
2200 * the restriction clauses!
2202 * XXX Possibly better way, but much more expensive: multiply by
2203 * selectivity of rel's restriction clauses that mention the target
2207 ndistinct *= vardata.rel->rows / vardata.rel->tuples;
2210 * Initial estimate of bucketsize fraction is 1/nbuckets as long as
2211 * the number of buckets is less than the expected number of distinct
2212 * values; otherwise it is 1/ndistinct.
2214 if (ndistinct > nbuckets)
2215 estfract = 1.0 / nbuckets;
2217 estfract = 1.0 / ndistinct;
2220 * Look up the frequency of the most common value, if available.
2224 if (HeapTupleIsValid(vardata.statsTuple))
2226 if (get_attstatsslot(vardata.statsTuple,
2227 vardata.atttype, vardata.atttypmod,
2228 STATISTIC_KIND_MCV, InvalidOid,
2229 NULL, NULL, &numbers, &nnumbers))
2232 * The first MCV stat is for the most common value.
2235 mcvfreq = numbers[0];
2236 free_attstatsslot(vardata.atttype, NULL, 0,
2242 * Adjust estimated bucketsize upward to account for skewed
2245 if (avgfreq > 0.0 && mcvfreq > avgfreq)
2246 estfract *= mcvfreq / avgfreq;
2249 * Clamp bucketsize to sane range (the above adjustment could easily
2250 * produce an out-of-range result). We set the lower bound a little
2251 * above zero, since zero isn't a very sane result.
2253 if (estfract < 1.0e-6)
2255 else if (estfract > 1.0)
2258 ReleaseVariableStats(vardata);
2260 return (Selectivity) estfract;
2264 /*-------------------------------------------------------------------------
2268 *-------------------------------------------------------------------------
2273 * Convert non-NULL values of the indicated types to the comparison
2274 * scale needed by scalarltsel()/scalargtsel().
2275 * Returns "true" if successful.
2277 * XXX this routine is a hack: ideally we should look up the conversion
2278 * subroutines in pg_type.
2280 * All numeric datatypes are simply converted to their equivalent
2281 * "double" values. (NUMERIC values that are outside the range of "double"
2282 * are clamped to +/- HUGE_VAL.)
2284 * String datatypes are converted by convert_string_to_scalar(),
2285 * which is explained below. The reason why this routine deals with
2286 * three values at a time, not just one, is that we need it for strings.
2288 * The bytea datatype is just enough different from strings that it has
2289 * to be treated separately.
2291 * The several datatypes representing absolute times are all converted
2292 * to Timestamp, which is actually a double, and then we just use that
2293 * double value. Note this will give correct results even for the "special"
2294 * values of Timestamp, since those are chosen to compare correctly;
2295 * see timestamp_cmp.
2297 * The several datatypes representing relative times (intervals) are all
2298 * converted to measurements expressed in seconds.
2301 convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
2302 Datum lobound, Datum hibound, Oid boundstypid,
2303 double *scaledlobound, double *scaledhibound)
2306 * Both the valuetypid and the boundstypid should exactly match
2307 * the declared input type(s) of the operator we are invoked for,
2308 * so we just error out if either is not recognized.
2310 * XXX The histogram we are interpolating between points of could belong
2311 * to a column that's only binary-compatible with the declared type.
2312 * In essence we are assuming that the semantics of binary-compatible
2313 * types are enough alike that we can use a histogram generated with one
2314 * type's operators to estimate selectivity for the other's. This is
2315 * outright wrong in some cases --- in particular signed versus unsigned
2316 * interpretation could trip us up. But it's useful enough in the
2317 * majority of cases that we do it anyway. Should think about more
2318 * rigorous ways to do it.
2323 * Built-in numeric types
2334 case REGPROCEDUREOID:
2336 case REGOPERATOROID:
2339 *scaledvalue = convert_numeric_to_scalar(value, valuetypid);
2340 *scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
2341 *scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
2345 * Built-in string types
2353 unsigned char *valstr = convert_string_datum(value, valuetypid);
2354 unsigned char *lostr = convert_string_datum(lobound, boundstypid);
2355 unsigned char *histr = convert_string_datum(hibound, boundstypid);
2357 convert_string_to_scalar(valstr, scaledvalue,
2358 lostr, scaledlobound,
2359 histr, scaledhibound);
2367 * Built-in bytea type
2371 convert_bytea_to_scalar(value, scaledvalue,
2372 lobound, scaledlobound,
2373 hibound, scaledhibound);
2378 * Built-in time types
2381 case TIMESTAMPTZOID:
2389 *scaledvalue = convert_timevalue_to_scalar(value, valuetypid);
2390 *scaledlobound = convert_timevalue_to_scalar(lobound, boundstypid);
2391 *scaledhibound = convert_timevalue_to_scalar(hibound, boundstypid);
2395 * Built-in network types
2400 *scaledvalue = convert_network_to_scalar(value, valuetypid);
2401 *scaledlobound = convert_network_to_scalar(lobound, boundstypid);
2402 *scaledhibound = convert_network_to_scalar(hibound, boundstypid);
2405 /* Don't know how to convert */
2410 * Do convert_to_scalar()'s work for any numeric data type.
2413 convert_numeric_to_scalar(Datum value, Oid typid)
2418 return (double) DatumGetBool(value);
2420 return (double) DatumGetInt16(value);
2422 return (double) DatumGetInt32(value);
2424 return (double) DatumGetInt64(value);
2426 return (double) DatumGetFloat4(value);
2428 return (double) DatumGetFloat8(value);
2430 /* Note: out-of-range values will be clamped to +-HUGE_VAL */
2432 DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
2436 case REGPROCEDUREOID:
2438 case REGOPERATOROID:
2441 /* we can treat OIDs as integers... */
2442 return (double) DatumGetObjectId(value);
2446 * Can't get here unless someone tries to use scalarltsel/scalargtsel
2447 * on an operator with one numeric and one non-numeric operand.
2449 elog(ERROR, "unsupported type: %u", typid);
2454 * Do convert_to_scalar()'s work for any character-string data type.
2456 * String datatypes are converted to a scale that ranges from 0 to 1,
2457 * where we visualize the bytes of the string as fractional digits.
2459 * We do not want the base to be 256, however, since that tends to
2460 * generate inflated selectivity estimates; few databases will have
2461 * occurrences of all 256 possible byte values at each position.
2462 * Instead, use the smallest and largest byte values seen in the bounds
2463 * as the estimated range for each byte, after some fudging to deal with
2464 * the fact that we probably aren't going to see the full range that way.
2466 * An additional refinement is that we discard any common prefix of the
2467 * three strings before computing the scaled values. This allows us to
2468 * "zoom in" when we encounter a narrow data range. An example is a phone
2469 * number database where all the values begin with the same area code.
2470 * (Actually, the bounds will be adjacent histogram-bin-boundary values,
2471 * so this is more likely to happen than you might think.)
2474 convert_string_to_scalar(unsigned char *value,
2475 double *scaledvalue,
2476 unsigned char *lobound,
2477 double *scaledlobound,
2478 unsigned char *hibound,
2479 double *scaledhibound)
2483 unsigned char *sptr;
2485 rangelo = rangehi = hibound[0];
2486 for (sptr = lobound; *sptr; sptr++)
2488 if (rangelo > *sptr)
2490 if (rangehi < *sptr)
2493 for (sptr = hibound; *sptr; sptr++)
2495 if (rangelo > *sptr)
2497 if (rangehi < *sptr)
2500 /* If range includes any upper-case ASCII chars, make it include all */
2501 if (rangelo <= 'Z' && rangehi >= 'A')
2508 /* Ditto lower-case */
2509 if (rangelo <= 'z' && rangehi >= 'a')
2517 if (rangelo <= '9' && rangehi >= '0')
2526 * If range includes less than 10 chars, assume we have not got enough
2527 * data, and make it include regular ASCII set.
2529 if (rangehi - rangelo < 9)
2536 * Now strip any common prefix of the three strings.
2540 if (*lobound != *hibound || *lobound != *value)
2542 lobound++, hibound++, value++;
2546 * Now we can do the conversions.
2548 *scaledvalue = convert_one_string_to_scalar(value, rangelo, rangehi);
2549 *scaledlobound = convert_one_string_to_scalar(lobound, rangelo, rangehi);
2550 *scaledhibound = convert_one_string_to_scalar(hibound, rangelo, rangehi);
2554 convert_one_string_to_scalar(unsigned char *value, int rangelo, int rangehi)
2556 int slen = strlen((char *) value);
2562 return 0.0; /* empty string has scalar value 0 */
2565 * Since base is at least 10, need not consider more than about 20
2571 /* Convert initial characters to fraction */
2572 base = rangehi - rangelo + 1;
2581 else if (ch > rangehi)
2583 num += ((double) (ch - rangelo)) / denom;
2591 * Convert a string-type Datum into a palloc'd, null-terminated string.
2593 * When using a non-C locale, we must pass the string through strxfrm()
2594 * before continuing, so as to generate correct locale-specific results.
2596 static unsigned char *
2597 convert_string_datum(Datum value, Oid typid)
2604 val = (char *) palloc(2);
2605 val[0] = DatumGetChar(value);
2612 char *str = (char *) VARDATA(DatumGetPointer(value));
2613 int strlength = VARSIZE(DatumGetPointer(value)) - VARHDRSZ;
2615 val = (char *) palloc(strlength + 1);
2616 memcpy(val, str, strlength);
2617 val[strlength] = '\0';
2622 NameData *nm = (NameData *) DatumGetPointer(value);
2624 val = pstrdup(NameStr(*nm));
2630 * Can't get here unless someone tries to use scalarltsel on
2631 * an operator with one string and one non-string operand.
2633 elog(ERROR, "unsupported type: %u", typid);
2637 if (!lc_collate_is_c())
2644 * Note: originally we guessed at a suitable output buffer size,
2645 * and only needed to call strxfrm twice if our guess was too
2646 * small. However, it seems that some versions of Solaris have
2647 * buggy strxfrm that can write past the specified buffer length
2648 * in that scenario. So, do it the dumb way for portability.
2650 * Yet other systems (e.g., glibc) sometimes return a smaller value
2651 * from the second call than the first; thus the Assert must be <=
2652 * not == as you'd expect. Can't any of these people program
2653 * their way out of a paper bag?
2655 xfrmlen = strxfrm(NULL, val, 0);
2656 xfrmstr = (char *) palloc(xfrmlen + 1);
2657 xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1);
2658 Assert(xfrmlen2 <= xfrmlen);
2663 return (unsigned char *) val;
2667 * Do convert_to_scalar()'s work for any bytea data type.
2669 * Very similar to convert_string_to_scalar except we can't assume
2670 * null-termination and therefore pass explicit lengths around.
2672 * Also, assumptions about likely "normal" ranges of characters have been
2673 * removed - a data range of 0..255 is always used, for now. (Perhaps
2674 * someday we will add information about actual byte data range to
2678 convert_bytea_to_scalar(Datum value,
2679 double *scaledvalue,
2681 double *scaledlobound,
2683 double *scaledhibound)
2687 valuelen = VARSIZE(DatumGetPointer(value)) - VARHDRSZ,
2688 loboundlen = VARSIZE(DatumGetPointer(lobound)) - VARHDRSZ,
2689 hiboundlen = VARSIZE(DatumGetPointer(hibound)) - VARHDRSZ,
2692 unsigned char *valstr = (unsigned char *) VARDATA(DatumGetPointer(value)),
2693 *lostr = (unsigned char *) VARDATA(DatumGetPointer(lobound)),
2694 *histr = (unsigned char *) VARDATA(DatumGetPointer(hibound));
2697 * Assume bytea data is uniformly distributed across all byte values.
2703 * Now strip any common prefix of the three strings.
2705 minlen = Min(Min(valuelen, loboundlen), hiboundlen);
2706 for (i = 0; i < minlen; i++)
2708 if (*lostr != *histr || *lostr != *valstr)
2710 lostr++, histr++, valstr++;
2711 loboundlen--, hiboundlen--, valuelen--;
2715 * Now we can do the conversions.
2717 *scaledvalue = convert_one_bytea_to_scalar(valstr, valuelen, rangelo, rangehi);
2718 *scaledlobound = convert_one_bytea_to_scalar(lostr, loboundlen, rangelo, rangehi);
2719 *scaledhibound = convert_one_bytea_to_scalar(histr, hiboundlen, rangelo, rangehi);
2723 convert_one_bytea_to_scalar(unsigned char *value, int valuelen,
2724 int rangelo, int rangehi)
2731 return 0.0; /* empty string has scalar value 0 */
2734 * Since base is 256, need not consider more than about 10 chars (even
2735 * this many seems like overkill)
2740 /* Convert initial characters to fraction */
2741 base = rangehi - rangelo + 1;
2744 while (valuelen-- > 0)
2750 else if (ch > rangehi)
2752 num += ((double) (ch - rangelo)) / denom;
2760 * Do convert_to_scalar()'s work for any timevalue data type.
2763 convert_timevalue_to_scalar(Datum value, Oid typid)
2768 return DatumGetTimestamp(value);
2769 case TIMESTAMPTZOID:
2770 return DatumGetTimestampTz(value);
2772 return DatumGetTimestamp(DirectFunctionCall1(abstime_timestamp,
2775 return DatumGetTimestamp(DirectFunctionCall1(date_timestamp,
2779 Interval *interval = DatumGetIntervalP(value);
2782 * Convert the month part of Interval to days using
2783 * assumed average month length of 365.25/12.0 days. Not
2784 * too accurate, but plenty good enough for our purposes.
2786 #ifdef HAVE_INT64_TIMESTAMP
2787 return interval->time + interval->day * (double)USECS_PER_DAY +
2788 interval->month * ((DAYS_PER_YEAR / (double)MONTHS_PER_YEAR) * USECS_PER_DAY);
2790 return interval->time + interval->day * SECS_PER_DAY +
2791 interval->month * ((DAYS_PER_YEAR / (double)MONTHS_PER_YEAR) * (double)SECS_PER_DAY);
2795 #ifdef HAVE_INT64_TIMESTAMP
2796 return (DatumGetRelativeTime(value) * 1000000.0);
2798 return DatumGetRelativeTime(value);
2802 TimeInterval tinterval = DatumGetTimeInterval(value);
2804 #ifdef HAVE_INT64_TIMESTAMP
2805 if (tinterval->status != 0)
2806 return ((tinterval->data[1] - tinterval->data[0]) * 1000000.0);
2808 if (tinterval->status != 0)
2809 return tinterval->data[1] - tinterval->data[0];
2811 return 0; /* for lack of a better idea */
2814 return DatumGetTimeADT(value);
2817 TimeTzADT *timetz = DatumGetTimeTzADTP(value);
2819 /* use GMT-equivalent time */
2820 #ifdef HAVE_INT64_TIMESTAMP
2821 return (double) (timetz->time + (timetz->zone * 1000000.0));
2823 return (double) (timetz->time + timetz->zone);
2829 * Can't get here unless someone tries to use scalarltsel/scalargtsel
2830 * on an operator with one timevalue and one non-timevalue operand.
2832 elog(ERROR, "unsupported type: %u", typid);
2838 * get_restriction_variable
2839 * Examine the args of a restriction clause to see if it's of the
2840 * form (variable op pseudoconstant) or (pseudoconstant op variable),
2841 * where "variable" could be either a Var or an expression in vars of a
2842 * single relation. If so, extract information about the variable,
2843 * and also indicate which side it was on and the other argument.
2846 * root: the planner info
2847 * args: clause argument list
2848 * varRelid: see specs for restriction selectivity functions
2850 * Outputs: (these are valid only if TRUE is returned)
2851 * *vardata: gets information about variable (see examine_variable)
2852 * *other: gets other clause argument, aggressively reduced to a constant
2853 * *varonleft: set TRUE if variable is on the left, FALSE if on the right
2855 * Returns TRUE if a variable is identified, otherwise FALSE.
2857 * Note: if there are Vars on both sides of the clause, we must fail, because
2858 * callers are expecting that the other side will act like a pseudoconstant.
2861 get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
2862 VariableStatData *vardata, Node **other,
2867 VariableStatData rdata;
2869 /* Fail if not a binary opclause (probably shouldn't happen) */
2870 if (list_length(args) != 2)
2873 left = (Node *) linitial(args);
2874 right = (Node *) lsecond(args);
2877 * Examine both sides. Note that when varRelid is nonzero, Vars of
2878 * other relations will be treated as pseudoconstants.
2880 examine_variable(root, left, varRelid, vardata);
2881 examine_variable(root, right, varRelid, &rdata);
2884 * If one side is a variable and the other not, we win.
2886 if (vardata->rel && rdata.rel == NULL)
2889 *other = estimate_expression_value(rdata.var);
2890 /* Assume we need no ReleaseVariableStats(rdata) here */
2894 if (vardata->rel == NULL && rdata.rel)
2897 *other = estimate_expression_value(vardata->var);
2898 /* Assume we need no ReleaseVariableStats(*vardata) here */
2903 /* Ooops, clause has wrong structure (probably var op var) */
2904 ReleaseVariableStats(*vardata);
2905 ReleaseVariableStats(rdata);
2911 * get_join_variables
2912 * Apply examine_variable() to each side of a join clause.
2915 get_join_variables(PlannerInfo *root, List *args,
2916 VariableStatData *vardata1, VariableStatData *vardata2)
2921 if (list_length(args) != 2)
2922 elog(ERROR, "join operator should take two arguments");
2924 left = (Node *) linitial(args);
2925 right = (Node *) lsecond(args);
2927 examine_variable(root, left, 0, vardata1);
2928 examine_variable(root, right, 0, vardata2);
2933 * Try to look up statistical data about an expression.
2934 * Fill in a VariableStatData struct to describe the expression.
2937 * root: the planner info
2938 * node: the expression tree to examine
2939 * varRelid: see specs for restriction selectivity functions
2941 * Outputs: *vardata is filled as follows:
2942 * var: the input expression (with any binary relabeling stripped, if
2943 * it is or contains a variable; but otherwise the type is preserved)
2944 * rel: RelOptInfo for relation containing variable; NULL if expression
2945 * contains no Vars (NOTE this could point to a RelOptInfo of a
2946 * subquery, not one in the current query).
2947 * statsTuple: the pg_statistic entry for the variable, if one exists;
2949 * vartype: exposed type of the expression; this should always match
2950 * the declared input type of the operator we are estimating for.
2951 * atttype, atttypmod: type data to pass to get_attstatsslot(). This is
2952 * commonly the same as the exposed type of the variable argument,
2953 * but can be different in binary-compatible-type cases.
2955 * Caller is responsible for doing ReleaseVariableStats() before exiting.
2958 examine_variable(PlannerInfo *root, Node *node, int varRelid,
2959 VariableStatData *vardata)
2965 /* Make sure we don't return dangling pointers in vardata */
2966 MemSet(vardata, 0, sizeof(VariableStatData));
2968 /* Save the exposed type of the expression */
2969 vardata->vartype = exprType(node);
2971 /* Look inside any binary-compatible relabeling */
2973 if (IsA(node, RelabelType))
2974 basenode = (Node *) ((RelabelType *) node)->arg;
2978 /* Fast path for a simple Var */
2980 if (IsA(basenode, Var) &&
2981 (varRelid == 0 || varRelid == ((Var *) basenode)->varno))
2983 Var *var = (Var *) basenode;
2986 vardata->var = basenode; /* return Var without relabeling */
2987 vardata->rel = find_base_rel(root, var->varno);
2988 vardata->atttype = var->vartype;
2989 vardata->atttypmod = var->vartypmod;
2991 relid = getrelid(var->varno, root->parse->rtable);
2993 if (OidIsValid(relid))
2995 vardata->statsTuple = SearchSysCache(STATRELATT,
2996 ObjectIdGetDatum(relid),
2997 Int16GetDatum(var->varattno),
3003 * XXX This means the Var comes from a JOIN or sub-SELECT.
3004 * Later add code to dig down into the join etc and see if we
3005 * can trace the variable to something with stats. (But
3006 * beware of sub-SELECTs with DISTINCT/GROUP BY/etc. Perhaps
3007 * there are no cases where this would really be useful,
3008 * because we'd have flattened the subselect if it is??)
3016 * Okay, it's a more complicated expression. Determine variable
3017 * membership. Note that when varRelid isn't zero, only vars of that
3018 * relation are considered "real" vars.
3020 varnos = pull_varnos(basenode);
3024 switch (bms_membership(varnos))
3027 /* No Vars at all ... must be pseudo-constant clause */
3030 if (varRelid == 0 || bms_is_member(varRelid, varnos))
3032 onerel = find_base_rel(root,
3033 (varRelid ? varRelid : bms_singleton_member(varnos)));
3034 vardata->rel = onerel;
3035 node = basenode; /* strip any relabeling */
3037 /* else treat it as a constant */
3042 /* treat it as a variable of a join relation */
3043 vardata->rel = find_join_rel(root, varnos);
3044 node = basenode; /* strip any relabeling */
3046 else if (bms_is_member(varRelid, varnos))
3048 /* ignore the vars belonging to other relations */
3049 vardata->rel = find_base_rel(root, varRelid);
3050 node = basenode; /* strip any relabeling */
3051 /* note: no point in expressional-index search here */
3053 /* else treat it as a constant */
3059 vardata->var = node;
3060 vardata->atttype = exprType(node);
3061 vardata->atttypmod = exprTypmod(node);
3066 * We have an expression in vars of a single relation. Try to
3067 * match it to expressional index columns, in hopes of finding
3070 * XXX it's conceivable that there are multiple matches with
3071 * different index opclasses; if so, we need to pick one that
3072 * matches the operator we are estimating for. FIXME later.
3076 foreach(ilist, onerel->indexlist)
3078 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
3079 ListCell *indexpr_item;
3082 indexpr_item = list_head(index->indexprs);
3083 if (indexpr_item == NULL)
3084 continue; /* no expressions here... */
3087 * Ignore partial indexes since they probably don't reflect
3088 * whole-relation statistics. Possibly reconsider this later.
3093 for (pos = 0; pos < index->ncolumns; pos++)
3095 if (index->indexkeys[pos] == 0)
3099 if (indexpr_item == NULL)
3100 elog(ERROR, "too few entries in indexprs list");
3101 indexkey = (Node *) lfirst(indexpr_item);
3102 if (indexkey && IsA(indexkey, RelabelType))
3103 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3104 if (equal(node, indexkey))
3107 * Found a match ... is it a unique index? Tests
3108 * here should match has_unique_index().
3110 if (index->unique &&
3111 index->ncolumns == 1 &&
3112 index->indpred == NIL)
3113 vardata->isunique = true;
3114 /* Has it got stats? */
3115 vardata->statsTuple = SearchSysCache(STATRELATT,
3116 ObjectIdGetDatum(index->indexoid),
3117 Int16GetDatum(pos + 1),
3119 if (vardata->statsTuple)
3122 indexpr_item = lnext(indexpr_item);
3125 if (vardata->statsTuple)
3132 * get_variable_numdistinct
3133 * Estimate the number of distinct values of a variable.
3135 * vardata: results of examine_variable
3137 * NB: be careful to produce an integral result, since callers may compare
3138 * the result to exact integer counts.
3141 get_variable_numdistinct(VariableStatData *vardata)
3147 * Determine the stadistinct value to use. There are cases where we
3148 * can get an estimate even without a pg_statistic entry, or can get a
3149 * better value than is in pg_statistic.
3151 if (HeapTupleIsValid(vardata->statsTuple))
3153 /* Use the pg_statistic entry */
3154 Form_pg_statistic stats;
3156 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
3157 stadistinct = stats->stadistinct;
3159 else if (vardata->vartype == BOOLOID)
3162 * Special-case boolean columns: presumably, two distinct values.
3164 * Are there any other datatypes we should wire in special estimates
3172 * We don't keep statistics for system columns, but in some cases
3173 * we can infer distinctness anyway.
3175 if (vardata->var && IsA(vardata->var, Var))
3177 switch (((Var *) vardata->var)->varattno)
3179 case ObjectIdAttributeNumber:
3180 case SelfItemPointerAttributeNumber:
3181 stadistinct = -1.0; /* unique */
3183 case TableOidAttributeNumber:
3184 stadistinct = 1.0; /* only 1 value */
3187 stadistinct = 0.0; /* means "unknown" */
3192 stadistinct = 0.0; /* means "unknown" */
3195 * XXX consider using estimate_num_groups on expressions?
3200 * If there is a unique index for the variable, assume it is unique no
3201 * matter what pg_statistic says (the statistics could be out of
3202 * date). Can skip search if we already think it's unique.
3204 if (stadistinct != -1.0)
3206 if (vardata->isunique)
3208 else if (vardata->var && IsA(vardata->var, Var) &&
3210 has_unique_index(vardata->rel,
3211 ((Var *) vardata->var)->varattno))
3216 * If we had an absolute estimate, use that.
3218 if (stadistinct > 0.0)
3222 * Otherwise we need to get the relation size; punt if not available.
3224 if (vardata->rel == NULL)
3225 return DEFAULT_NUM_DISTINCT;
3226 ntuples = vardata->rel->tuples;
3228 return DEFAULT_NUM_DISTINCT;
3231 * If we had a relative estimate, use that.
3233 if (stadistinct < 0.0)
3234 return floor((-stadistinct * ntuples) + 0.5);
3237 * With no data, estimate ndistinct = ntuples if the table is small,
3240 if (ntuples < DEFAULT_NUM_DISTINCT)
3243 return DEFAULT_NUM_DISTINCT;
3247 * get_variable_maximum
3248 * Estimate the maximum value of the specified variable.
3249 * If successful, store value in *max and return TRUE.
3250 * If no data available, return FALSE.
3252 * sortop is the "<" comparison operator to use. (To extract the
3253 * minimum instead of the maximum, just pass the ">" operator instead.)
3256 get_variable_maximum(PlannerInfo *root, VariableStatData *vardata,
3257 Oid sortop, Datum *max)
3260 bool have_max = false;
3261 Form_pg_statistic stats;
3268 if (!HeapTupleIsValid(vardata->statsTuple))
3270 /* no stats available, so default result */
3273 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
3275 get_typlenbyval(vardata->atttype, &typLen, &typByVal);
3278 * If there is a histogram, grab the last or first value as
3281 * If there is a histogram that is sorted with some other operator than
3282 * the one we want, fail --- this suggests that there is data we can't
3285 if (get_attstatsslot(vardata->statsTuple,
3286 vardata->atttype, vardata->atttypmod,
3287 STATISTIC_KIND_HISTOGRAM, sortop,
3293 tmax = datumCopy(values[nvalues - 1], typByVal, typLen);
3296 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3300 Oid rsortop = get_commutator(sortop);
3302 if (OidIsValid(rsortop) &&
3303 get_attstatsslot(vardata->statsTuple,
3304 vardata->atttype, vardata->atttypmod,
3305 STATISTIC_KIND_HISTOGRAM, rsortop,
3311 tmax = datumCopy(values[0], typByVal, typLen);
3314 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3316 else if (get_attstatsslot(vardata->statsTuple,
3317 vardata->atttype, vardata->atttypmod,
3318 STATISTIC_KIND_HISTOGRAM, InvalidOid,
3322 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3328 * If we have most-common-values info, look for a large MCV. This is
3329 * needed even if we also have a histogram, since the histogram
3330 * excludes the MCVs. However, usually the MCVs will not be the
3331 * extreme values, so avoid unnecessary data copying.
3333 if (get_attstatsslot(vardata->statsTuple,
3334 vardata->atttype, vardata->atttypmod,
3335 STATISTIC_KIND_MCV, InvalidOid,
3339 bool large_mcv = false;
3342 fmgr_info(get_opcode(sortop), &opproc);
3344 for (i = 0; i < nvalues; i++)
3349 large_mcv = have_max = true;
3351 else if (DatumGetBool(FunctionCall2(&opproc, tmax, values[i])))
3358 tmax = datumCopy(tmax, typByVal, typLen);
3359 free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0);
3367 /*-------------------------------------------------------------------------
3369 * Pattern analysis functions
3371 * These routines support analysis of LIKE and regular-expression patterns
3372 * by the planner/optimizer. It's important that they agree with the
3373 * regular-expression code in backend/regex/ and the LIKE code in
3374 * backend/utils/adt/like.c.
3376 * Note that the prefix-analysis functions are called from
3377 * backend/optimizer/path/indxpath.c as well as from routines in this file.
3379 *-------------------------------------------------------------------------
3383 * Extract the fixed prefix, if any, for a pattern.
3385 * *prefix is set to a palloc'd prefix string (in the form of a Const node),
3386 * or to NULL if no fixed prefix exists for the pattern.
3387 * *rest is set to a palloc'd Const representing the remainder of the pattern
3388 * after the portion describing the fixed prefix.
3389 * Each of these has the same type (TEXT or BYTEA) as the given pattern Const.
3391 * The return value distinguishes no fixed prefix, a partial prefix,
3392 * or an exact-match-only pattern.
3395 static Pattern_Prefix_Status
3396 like_fixed_prefix(Const *patt_const, bool case_insensitive,
3397 Const **prefix_const, Const **rest_const)
3403 Oid typeid = patt_const->consttype;
3407 /* the right-hand const is type text or bytea */
3408 Assert(typeid == BYTEAOID || typeid == TEXTOID);
3410 if (typeid == BYTEAOID && case_insensitive)
3412 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3413 errmsg("case insensitive matching not supported on type bytea")));
3415 if (typeid != BYTEAOID)
3417 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
3418 pattlen = strlen(patt);
3422 bytea *bstr = DatumGetByteaP(patt_const->constvalue);
3424 pattlen = VARSIZE(bstr) - VARHDRSZ;
3427 patt = (char *) palloc(pattlen);
3428 memcpy(patt, VARDATA(bstr), pattlen);
3433 if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
3437 match = palloc(pattlen + 1);
3439 for (pos = 0; pos < pattlen; pos++)
3441 /* % and _ are wildcard characters in LIKE */
3442 if (patt[pos] == '%' ||
3446 /* Backslash escapes the next character */
3447 if (patt[pos] == '\\')
3450 if (patt[pos] == '\0' && typeid != BYTEAOID)
3455 * XXX I suspect isalpha() is not an adequately locale-sensitive
3456 * test for characters that can vary under case folding?
3458 if (case_insensitive && isalpha((unsigned char) patt[pos]))
3462 * NOTE: this code used to think that %% meant a literal %, but
3463 * textlike() itself does not think that, and the SQL92 spec
3464 * doesn't say any such thing either.
3466 match[match_pos++] = patt[pos];
3469 match[match_pos] = '\0';
3472 if (typeid != BYTEAOID)
3474 *prefix_const = string_to_const(match, typeid);
3475 *rest_const = string_to_const(rest, typeid);
3479 *prefix_const = string_to_bytea_const(match, match_pos);
3480 *rest_const = string_to_bytea_const(rest, pattlen - pos);
3487 /* in LIKE, an empty pattern is an exact match! */
3489 return Pattern_Prefix_Exact; /* reached end of pattern, so
3493 return Pattern_Prefix_Partial;
3495 return Pattern_Prefix_None;
3498 static Pattern_Prefix_Status
3499 regex_fixed_prefix(Const *patt_const, bool case_insensitive,
3500 Const **prefix_const, Const **rest_const)
3510 Oid typeid = patt_const->consttype;
3513 * Should be unnecessary, there are no bytea regex operators defined.
3514 * As such, it should be noted that the rest of this function has *not*
3515 * been made safe for binary (possibly NULL containing) strings.
3517 if (typeid == BYTEAOID)
3519 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3520 errmsg("regular-expression matching not supported on type bytea")));
3522 /* the right-hand const is type text for all of these */
3523 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
3525 /* Pattern must be anchored left */
3530 *prefix_const = NULL;
3531 *rest_const = string_to_const(rest, typeid);
3533 return Pattern_Prefix_None;
3537 * If unquoted | is present at paren level 0 in pattern, then there
3538 * are multiple alternatives for the start of the string.
3541 for (pos = 1; patt[pos]; pos++)
3543 if (patt[pos] == '|' && paren_depth == 0)
3547 *prefix_const = NULL;
3548 *rest_const = string_to_const(rest, typeid);
3550 return Pattern_Prefix_None;
3552 else if (patt[pos] == '(')
3554 else if (patt[pos] == ')' && paren_depth > 0)
3556 else if (patt[pos] == '\\')
3558 /* backslash quotes the next character */
3560 if (patt[pos] == '\0')
3565 /* OK, allocate space for pattern */
3566 match = palloc(strlen(patt) + 1);
3567 prev_match_pos = match_pos = 0;
3569 /* note start at pos 1 to skip leading ^ */
3570 for (prev_pos = pos = 1; patt[pos]; )
3575 * Check for characters that indicate multiple possible matches
3576 * here. XXX I suspect isalpha() is not an adequately
3577 * locale-sensitive test for characters that can vary under case
3580 if (patt[pos] == '.' ||
3584 (case_insensitive && isalpha((unsigned char) patt[pos])))
3588 * In AREs, backslash followed by alphanumeric is an escape, not
3589 * a quoted character. Must treat it as having multiple possible
3592 if (patt[pos] == '\\' && isalnum((unsigned char) patt[pos + 1]))
3596 * Check for quantifiers. Except for +, this means the preceding
3597 * character is optional, so we must remove it from the prefix
3600 if (patt[pos] == '*' ||
3604 match_pos = prev_match_pos;
3608 if (patt[pos] == '+')
3613 if (patt[pos] == '\\')
3615 /* backslash quotes the next character */
3617 if (patt[pos] == '\0')
3620 /* save position in case we need to back up on next loop cycle */
3621 prev_match_pos = match_pos;
3623 /* must use encoding-aware processing here */
3624 len = pg_mblen(&patt[pos]);
3625 memcpy(&match[match_pos], &patt[pos], len);
3630 match[match_pos] = '\0';
3633 if (patt[pos] == '$' && patt[pos + 1] == '\0')
3635 rest = &patt[pos + 1];
3637 *prefix_const = string_to_const(match, typeid);
3638 *rest_const = string_to_const(rest, typeid);
3643 return Pattern_Prefix_Exact; /* pattern specifies exact match */
3646 *prefix_const = string_to_const(match, typeid);
3647 *rest_const = string_to_const(rest, typeid);
3653 return Pattern_Prefix_Partial;
3655 return Pattern_Prefix_None;
3658 Pattern_Prefix_Status
3659 pattern_fixed_prefix(Const *patt, Pattern_Type ptype,
3660 Const **prefix, Const **rest)
3662 Pattern_Prefix_Status result;
3666 case Pattern_Type_Like:
3667 result = like_fixed_prefix(patt, false, prefix, rest);
3669 case Pattern_Type_Like_IC:
3670 result = like_fixed_prefix(patt, true, prefix, rest);
3672 case Pattern_Type_Regex:
3673 result = regex_fixed_prefix(patt, false, prefix, rest);
3675 case Pattern_Type_Regex_IC:
3676 result = regex_fixed_prefix(patt, true, prefix, rest);
3679 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
3680 result = Pattern_Prefix_None; /* keep compiler quiet */
3687 * Estimate the selectivity of a fixed prefix for a pattern match.
3689 * A fixed prefix "foo" is estimated as the selectivity of the expression
3690 * "variable >= 'foo' AND variable < 'fop'" (see also indxpath.c).
3692 * We use the >= and < operators from the specified btree opclass to do the
3693 * estimation. The given variable and Const must be of the associated
3696 * XXX Note: we make use of the upper bound to estimate operator selectivity
3697 * even if the locale is such that we cannot rely on the upper-bound string.
3698 * The selectivity only needs to be approximately right anyway, so it seems
3699 * more useful to use the upper-bound code than not.
3702 prefix_selectivity(PlannerInfo *root, Node *variable,
3703 Oid opclass, Const *prefixcon)
3705 Selectivity prefixsel;
3708 Const *greaterstrcon;
3710 cmpopr = get_opclass_member(opclass, InvalidOid,
3711 BTGreaterEqualStrategyNumber);
3712 if (cmpopr == InvalidOid)
3713 elog(ERROR, "no >= operator for opclass %u", opclass);
3714 cmpargs = list_make2(variable, prefixcon);
3715 /* Assume scalargtsel is appropriate for all supported types */
3716 prefixsel = DatumGetFloat8(DirectFunctionCall4(scalargtsel,
3717 PointerGetDatum(root),
3718 ObjectIdGetDatum(cmpopr),
3719 PointerGetDatum(cmpargs),
3723 * If we can create a string larger than the prefix, say
3727 greaterstrcon = make_greater_string(prefixcon);
3732 cmpopr = get_opclass_member(opclass, InvalidOid,
3733 BTLessStrategyNumber);
3734 if (cmpopr == InvalidOid)
3735 elog(ERROR, "no < operator for opclass %u", opclass);
3736 cmpargs = list_make2(variable, greaterstrcon);
3737 /* Assume scalarltsel is appropriate for all supported types */
3738 topsel = DatumGetFloat8(DirectFunctionCall4(scalarltsel,
3739 PointerGetDatum(root),
3740 ObjectIdGetDatum(cmpopr),
3741 PointerGetDatum(cmpargs),
3745 * Merge the two selectivities in the same way as for a range
3746 * query (see clauselist_selectivity()).
3748 prefixsel = topsel + prefixsel - 1.0;
3750 /* Adjust for double-exclusion of NULLs */
3751 prefixsel += nulltestsel(root, IS_NULL, variable, 0);
3754 * A zero or slightly negative prefixsel should be converted into
3755 * a small positive value; we probably are dealing with a very
3756 * tight range and got a bogus result due to roundoff errors.
3757 * However, if prefixsel is very negative, then we probably have
3758 * default selectivity estimates on one or both sides of the
3759 * range. In that case, insert a not-so-wildly-optimistic default
3762 if (prefixsel <= 0.0)
3764 if (prefixsel < -0.01)
3767 * No data available --- use a default estimate that is
3768 * small, but not real small.
3775 * It's just roundoff error; use a small positive value
3777 prefixsel = 1.0e-10;
3787 * Estimate the selectivity of a pattern of the specified type.
3788 * Note that any fixed prefix of the pattern will have been removed already.
3790 * For now, we use a very simplistic approach: fixed characters reduce the
3791 * selectivity a good deal, character ranges reduce it a little,
3792 * wildcards (such as % for LIKE or .* for regex) increase it.
3795 #define FIXED_CHAR_SEL 0.20 /* about 1/5 */
3796 #define CHAR_RANGE_SEL 0.25
3797 #define ANY_CHAR_SEL 0.9 /* not 1, since it won't match
3799 #define FULL_WILDCARD_SEL 5.0
3800 #define PARTIAL_WILDCARD_SEL 2.0
3803 like_selectivity(Const *patt_const, bool case_insensitive)
3805 Selectivity sel = 1.0;
3808 Oid typeid = patt_const->consttype;
3812 /* the right-hand const is type text or bytea */
3813 Assert(typeid == BYTEAOID || typeid == TEXTOID);
3815 if (typeid == BYTEAOID && case_insensitive)
3817 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3818 errmsg("case insensitive matching not supported on type bytea")));
3820 if (typeid != BYTEAOID)
3822 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
3823 pattlen = strlen(patt);
3827 bytea *bstr = DatumGetByteaP(patt_const->constvalue);
3829 pattlen = VARSIZE(bstr) - VARHDRSZ;
3832 patt = (char *) palloc(pattlen);
3833 memcpy(patt, VARDATA(bstr), pattlen);
3838 if ((Pointer) bstr != DatumGetPointer(patt_const->constvalue))
3841 /* patt should never be NULL in practice */
3842 Assert(patt != NULL);
3844 /* Skip any leading %; it's already factored into initial sel */
3845 start = (*patt == '%') ? 1 : 0;
3846 for (pos = start; pos < pattlen; pos++)
3848 /* % and _ are wildcard characters in LIKE */
3849 if (patt[pos] == '%')
3850 sel *= FULL_WILDCARD_SEL;
3851 else if (patt[pos] == '_')
3852 sel *= ANY_CHAR_SEL;
3853 else if (patt[pos] == '\\')
3855 /* Backslash quotes the next character */
3857 if (patt[pos] == '\0' && typeid != BYTEAOID)
3859 sel *= FIXED_CHAR_SEL;
3862 sel *= FIXED_CHAR_SEL;
3864 /* Could get sel > 1 if multiple wildcards */
3871 regex_selectivity_sub(char *patt, int pattlen, bool case_insensitive)
3873 Selectivity sel = 1.0;
3874 int paren_depth = 0;
3875 int paren_pos = 0; /* dummy init to keep compiler quiet */
3878 for (pos = 0; pos < pattlen; pos++)
3880 if (patt[pos] == '(')
3882 if (paren_depth == 0)
3883 paren_pos = pos; /* remember start of parenthesized item */
3886 else if (patt[pos] == ')' && paren_depth > 0)
3889 if (paren_depth == 0)
3890 sel *= regex_selectivity_sub(patt + (paren_pos + 1),
3891 pos - (paren_pos + 1),
3894 else if (patt[pos] == '|' && paren_depth == 0)
3897 * If unquoted | is present at paren level 0 in pattern, we
3898 * have multiple alternatives; sum their probabilities.
3900 sel += regex_selectivity_sub(patt + (pos + 1),
3901 pattlen - (pos + 1),
3903 break; /* rest of pattern is now processed */
3905 else if (patt[pos] == '[')
3907 bool negclass = false;
3909 if (patt[++pos] == '^')
3914 if (patt[pos] == ']') /* ']' at start of class is not
3917 while (pos < pattlen && patt[pos] != ']')
3919 if (paren_depth == 0)
3920 sel *= (negclass ? (1.0 - CHAR_RANGE_SEL) : CHAR_RANGE_SEL);
3922 else if (patt[pos] == '.')
3924 if (paren_depth == 0)
3925 sel *= ANY_CHAR_SEL;
3927 else if (patt[pos] == '*' ||
3931 /* Ought to be smarter about quantifiers... */
3932 if (paren_depth == 0)
3933 sel *= PARTIAL_WILDCARD_SEL;
3935 else if (patt[pos] == '{')
3937 while (pos < pattlen && patt[pos] != '}')
3939 if (paren_depth == 0)
3940 sel *= PARTIAL_WILDCARD_SEL;
3942 else if (patt[pos] == '\\')
3944 /* backslash quotes the next character */
3948 if (paren_depth == 0)
3949 sel *= FIXED_CHAR_SEL;
3953 if (paren_depth == 0)
3954 sel *= FIXED_CHAR_SEL;
3957 /* Could get sel > 1 if multiple wildcards */
3964 regex_selectivity(Const *patt_const, bool case_insensitive)
3969 Oid typeid = patt_const->consttype;
3972 * Should be unnecessary, there are no bytea regex operators defined.
3973 * As such, it should be noted that the rest of this function has *not*
3974 * been made safe for binary (possibly NULL containing) strings.
3976 if (typeid == BYTEAOID)
3978 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3979 errmsg("regular-expression matching not supported on type bytea")));
3981 /* the right-hand const is type text for all of these */
3982 patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
3983 pattlen = strlen(patt);
3985 /* If patt doesn't end with $, consider it to have a trailing wildcard */
3986 if (pattlen > 0 && patt[pattlen - 1] == '$' &&
3987 (pattlen == 1 || patt[pattlen - 2] != '\\'))
3989 /* has trailing $ */
3990 sel = regex_selectivity_sub(patt, pattlen - 1, case_insensitive);
3995 sel = regex_selectivity_sub(patt, pattlen, case_insensitive);
3996 sel *= FULL_WILDCARD_SEL;
4004 pattern_selectivity(Const *patt, Pattern_Type ptype)
4010 case Pattern_Type_Like:
4011 result = like_selectivity(patt, false);
4013 case Pattern_Type_Like_IC:
4014 result = like_selectivity(patt, true);
4016 case Pattern_Type_Regex:
4017 result = regex_selectivity(patt, false);
4019 case Pattern_Type_Regex_IC:
4020 result = regex_selectivity(patt, true);
4023 elog(ERROR, "unrecognized ptype: %d", (int) ptype);
4024 result = 1.0; /* keep compiler quiet */
4032 * Try to generate a string greater than the given string or any
4033 * string it is a prefix of. If successful, return a palloc'd string
4034 * in the form of a Const pointer; else return NULL.
4036 * The key requirement here is that given a prefix string, say "foo",
4037 * we must be able to generate another string "fop" that is greater
4038 * than all strings "foobar" starting with "foo".
4040 * If we max out the righthand byte, truncate off the last character
4041 * and start incrementing the next. For example, if "z" were the last
4042 * character in the sort order, then we could produce "foo" as a
4043 * string greater than "fonz".
4045 * This could be rather slow in the worst case, but in most cases we
4046 * won't have to try more than one or two strings before succeeding.
4048 * NOTE: at present this assumes we are in the C locale, so that simple
4049 * bytewise comparison applies. However, we might be in a multibyte
4050 * encoding such as UTF8, so we do have to watch out for generating
4051 * invalid encoding sequences.
4054 make_greater_string(const Const *str_const)
4056 Oid datatype = str_const->consttype;
4060 /* Get the string and a modifiable copy */
4061 if (datatype == NAMEOID)
4063 workstr = DatumGetCString(DirectFunctionCall1(nameout,
4064 str_const->constvalue));
4065 len = strlen(workstr);
4067 else if (datatype == BYTEAOID)
4069 bytea *bstr = DatumGetByteaP(str_const->constvalue);
4071 len = VARSIZE(bstr) - VARHDRSZ;
4074 workstr = (char *) palloc(len);
4075 memcpy(workstr, VARDATA(bstr), len);
4080 if ((Pointer) bstr != DatumGetPointer(str_const->constvalue))
4085 workstr = DatumGetCString(DirectFunctionCall1(textout,
4086 str_const->constvalue));
4087 len = strlen(workstr);
4092 unsigned char *lastchar = (unsigned char *) (workstr + len - 1);
4093 unsigned char savelastchar = *lastchar;
4096 * Try to generate a larger string by incrementing the last byte.
4098 while (*lastchar < (unsigned char) 255)
4100 Const *workstr_const;
4104 if (datatype != BYTEAOID)
4106 /* do not generate invalid encoding sequences */
4107 if (!pg_verifymbstr((const unsigned char *) workstr,
4110 workstr_const = string_to_const(workstr, datatype);
4113 workstr_const = string_to_bytea_const(workstr, len);
4116 return workstr_const;
4119 /* restore last byte so we don't confuse pg_mbcliplen */
4120 *lastchar = savelastchar;
4123 * Truncate off the last character, which might be more than 1
4124 * byte, depending on the character encoding.
4126 if (datatype != BYTEAOID && pg_database_encoding_max_length() > 1)
4127 len = pg_mbcliplen((const unsigned char *) workstr, len, len - 1);
4131 if (datatype != BYTEAOID)
4132 workstr[len] = '\0';
4136 if (workstr != NULL)
4143 * Generate a Datum of the appropriate type from a C string.
4144 * Note that all of the supported types are pass-by-ref, so the
4145 * returned value should be pfree'd if no longer needed.
4148 string_to_datum(const char *str, Oid datatype)
4150 Assert(str != NULL);
4153 * We cheat a little by assuming that textin() will do for bpchar and
4154 * varchar constants too...
4156 if (datatype == NAMEOID)
4157 return DirectFunctionCall1(namein, CStringGetDatum(str));
4158 else if (datatype == BYTEAOID)
4159 return DirectFunctionCall1(byteain, CStringGetDatum(str));
4161 return DirectFunctionCall1(textin, CStringGetDatum(str));
4165 * Generate a Const node of the appropriate type from a C string.
4168 string_to_const(const char *str, Oid datatype)
4170 Datum conval = string_to_datum(str, datatype);
4172 return makeConst(datatype, ((datatype == NAMEOID) ? NAMEDATALEN : -1),
4173 conval, false, false);
4177 * Generate a Const node of bytea type from a binary C string and a length.
4180 string_to_bytea_const(const char *str, size_t str_len)
4182 bytea *bstr = palloc(VARHDRSZ + str_len);
4185 memcpy(VARDATA(bstr), str, str_len);
4186 VARATT_SIZEP(bstr) = VARHDRSZ + str_len;
4187 conval = PointerGetDatum(bstr);
4189 return makeConst(BYTEAOID, -1, conval, false, false);
4192 /*-------------------------------------------------------------------------
4194 * Index cost estimation functions
4196 * genericcostestimate is a general-purpose estimator for use when we
4197 * don't have any better idea about how to estimate. Index-type-specific
4198 * knowledge can be incorporated in the type-specific routines.
4200 * One bit of index-type-specific knowledge we can relatively easily use
4201 * in genericcostestimate is the estimate of the number of index tuples
4202 * visited. If numIndexTuples is not 0 then it is used as the estimate,
4203 * otherwise we compute a generic estimate.
4205 *-------------------------------------------------------------------------
4209 genericcostestimate(PlannerInfo *root,
4210 IndexOptInfo *index, List *indexQuals,
4211 double numIndexTuples,
4212 Cost *indexStartupCost,
4213 Cost *indexTotalCost,
4214 Selectivity *indexSelectivity,
4215 double *indexCorrelation)
4217 double numIndexPages;
4218 QualCost index_qual_cost;
4219 double qual_op_cost;
4220 double qual_arg_cost;
4221 List *selectivityQuals;
4224 * If the index is partial, AND the index predicate with the
4225 * explicitly given indexquals to produce a more accurate idea of the
4226 * index selectivity. This may produce redundant clauses. We get rid
4227 * of exact duplicates in the code below. We expect that most cases
4228 * of partial redundancy (such as "x < 4" from the qual and "x < 5"
4229 * from the predicate) will be recognized and handled correctly by
4230 * clauselist_selectivity(). This assumption is somewhat fragile,
4231 * since it depends on predicate_implied_by() and clauselist_selectivity()
4232 * having similar capabilities, and there are certainly many cases where
4233 * we will end up with a too-low selectivity estimate. This will bias the
4234 * system in favor of using partial indexes where possible, which is not
4235 * necessarily a bad thing. But it'd be nice to do better someday.
4237 * Note that index->indpred and indexQuals are both in implicit-AND form,
4238 * so ANDing them together just takes merging the lists. However,
4239 * eliminating duplicates is a bit trickier because indexQuals
4240 * contains RestrictInfo nodes and the indpred does not. It is okay
4241 * to pass a mixed list to clauselist_selectivity, but we have to work
4242 * a bit to generate a list without logical duplicates. (We could
4243 * just list_union indpred and strippedQuals, but then we'd not get
4244 * caching of per-qual selectivity estimates.)
4246 if (index->indpred != NIL)
4248 List *strippedQuals;
4249 List *predExtraQuals;
4251 strippedQuals = get_actual_clauses(indexQuals);
4252 predExtraQuals = list_difference(index->indpred, strippedQuals);
4253 selectivityQuals = list_concat(predExtraQuals, indexQuals);
4256 selectivityQuals = indexQuals;
4258 /* Estimate the fraction of main-table tuples that will be visited */
4259 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
4264 * If caller didn't give us an estimate, estimate the number of index
4265 * tuples that will be visited. We do it in this rather peculiar-looking
4266 * way in order to get the right answer for partial indexes.
4268 if (numIndexTuples <= 0.0)
4269 numIndexTuples = *indexSelectivity * index->rel->tuples;
4272 * We can bound the number of tuples by the index size in any case.
4273 * Also, always estimate at least one tuple is touched, even when
4274 * indexSelectivity estimate is tiny.
4276 if (numIndexTuples > index->tuples)
4277 numIndexTuples = index->tuples;
4278 if (numIndexTuples < 1.0)
4279 numIndexTuples = 1.0;
4282 * Estimate the number of index pages that will be retrieved.
4284 * For all currently-supported index types, the first page of the index
4285 * is a metadata page, and we should figure on fetching that plus a
4286 * pro-rated fraction of the remaining pages.
4288 if (index->pages > 1 && index->tuples > 0)
4290 numIndexPages = (numIndexTuples / index->tuples) * (index->pages - 1);
4291 numIndexPages += 1; /* count the metapage too */
4292 numIndexPages = ceil(numIndexPages);
4295 numIndexPages = 1.0;
4298 * Compute the index access cost.
4300 * Disk cost: our generic assumption is that the index pages will be read
4301 * sequentially, so they have cost 1.0 each, not random_page_cost.
4303 *indexTotalCost = numIndexPages;
4306 * CPU cost: any complex expressions in the indexquals will need to be
4307 * evaluated once at the start of the scan to reduce them to runtime
4308 * keys to pass to the index AM (see nodeIndexscan.c). We model the
4309 * per-tuple CPU costs as cpu_index_tuple_cost plus one
4310 * cpu_operator_cost per indexqual operator.
4312 * Note: this neglects the possible costs of rechecking lossy operators
4313 * and OR-clause expressions. Detecting that that might be needed
4314 * seems more expensive than it's worth, though, considering all the
4315 * other inaccuracies here ...
4317 cost_qual_eval(&index_qual_cost, indexQuals);
4318 qual_op_cost = cpu_operator_cost * list_length(indexQuals);
4319 qual_arg_cost = index_qual_cost.startup +
4320 index_qual_cost.per_tuple - qual_op_cost;
4321 if (qual_arg_cost < 0) /* just in case... */
4323 *indexStartupCost = qual_arg_cost;
4324 *indexTotalCost += qual_arg_cost;
4325 *indexTotalCost += numIndexTuples * (cpu_index_tuple_cost + qual_op_cost);
4328 * Generic assumption about index correlation: there isn't any.
4330 *indexCorrelation = 0.0;
4335 btcostestimate(PG_FUNCTION_ARGS)
4337 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
4338 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
4339 List *indexQuals = (List *) PG_GETARG_POINTER(2);
4340 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
4341 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
4342 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
4343 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
4347 double numIndexTuples;
4348 List *indexBoundQuals;
4354 * For a btree scan, only leading '=' quals plus inequality quals
4355 * for the immediately next attribute contribute to index selectivity
4356 * (these are the "boundary quals" that determine the starting and
4357 * stopping points of the index scan). Additional quals can suppress
4358 * visits to the heap, so it's OK to count them in indexSelectivity,
4359 * but they should not count for estimating numIndexTuples. So we must
4360 * examine the given indexQuals to find out which ones count as boundary
4361 * quals. We rely on the knowledge that they are given in index column
4364 indexBoundQuals = NIL;
4367 foreach(l, indexQuals)
4369 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
4374 Assert(IsA(rinfo, RestrictInfo));
4375 clause = rinfo->clause;
4376 Assert(IsA(clause, OpExpr));
4377 clause_op = ((OpExpr *) clause)->opno;
4378 if (match_index_to_operand(get_leftop(clause), indexcol, index))
4380 /* clause_op is correct */
4382 else if (match_index_to_operand(get_rightop(clause), indexcol, index))
4384 /* Must flip operator to get the opclass member */
4385 clause_op = get_commutator(clause_op);
4389 /* Must be past the end of quals for indexcol, try next */
4391 break; /* done if no '=' qual for indexcol */
4394 if (match_index_to_operand(get_leftop(clause), indexcol, index))
4396 /* clause_op is correct */
4398 else if (match_index_to_operand(get_rightop(clause),
4401 /* Must flip operator to get the opclass member */
4402 clause_op = get_commutator(clause_op);
4406 /* No quals for new indexcol, so we are done */
4410 op_strategy = get_op_opclass_strategy(clause_op,
4411 index->classlist[indexcol]);
4412 Assert(op_strategy != 0); /* not a member of opclass?? */
4413 if (op_strategy == BTEqualStrategyNumber)
4415 indexBoundQuals = lappend(indexBoundQuals, rinfo);
4419 * If index is unique and we found an '=' clause for each column,
4420 * we can just assume numIndexTuples = 1 and skip the expensive
4421 * clauselist_selectivity calculations.
4423 if (index->unique && indexcol == index->ncolumns - 1 && eqQualHere)
4424 numIndexTuples = 1.0;
4427 Selectivity btreeSelectivity;
4429 btreeSelectivity = clauselist_selectivity(root, indexBoundQuals,
4432 numIndexTuples = btreeSelectivity * index->rel->tuples;
4435 genericcostestimate(root, index, indexQuals, numIndexTuples,
4436 indexStartupCost, indexTotalCost,
4437 indexSelectivity, indexCorrelation);
4440 * If we can get an estimate of the first column's ordering
4441 * correlation C from pg_statistic, estimate the index correlation as
4442 * C for a single-column index, or C * 0.75 for multiple columns.
4443 * (The idea here is that multiple columns dilute the importance of
4444 * the first column's ordering, but don't negate it entirely. Before
4445 * 8.0 we divided the correlation by the number of columns, but that
4446 * seems too strong.)
4448 if (index->indexkeys[0] != 0)
4450 /* Simple variable --- look to stats for the underlying table */
4451 relid = getrelid(index->rel->relid, root->parse->rtable);
4452 Assert(relid != InvalidOid);
4453 colnum = index->indexkeys[0];
4457 /* Expression --- maybe there are stats for the index itself */
4458 relid = index->indexoid;
4462 tuple = SearchSysCache(STATRELATT,
4463 ObjectIdGetDatum(relid),
4464 Int16GetDatum(colnum),
4467 if (HeapTupleIsValid(tuple))
4474 /* XXX this code would break with different storage type */
4475 get_atttypetypmod(relid, colnum, &typid, &typmod);
4477 if (get_attstatsslot(tuple, typid, typmod,
4478 STATISTIC_KIND_CORRELATION,
4480 NULL, NULL, &numbers, &nnumbers))
4482 double varCorrelation;
4484 Assert(nnumbers == 1);
4485 varCorrelation = numbers[0];
4487 if (index->ncolumns > 1)
4488 *indexCorrelation = varCorrelation * 0.75;
4490 *indexCorrelation = varCorrelation;
4492 free_attstatsslot(typid, NULL, 0, numbers, nnumbers);
4494 ReleaseSysCache(tuple);
4501 rtcostestimate(PG_FUNCTION_ARGS)
4503 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
4504 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
4505 List *indexQuals = (List *) PG_GETARG_POINTER(2);
4506 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
4507 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
4508 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
4509 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
4511 genericcostestimate(root, index, indexQuals, 0.0,
4512 indexStartupCost, indexTotalCost,
4513 indexSelectivity, indexCorrelation);
4519 hashcostestimate(PG_FUNCTION_ARGS)
4521 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
4522 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
4523 List *indexQuals = (List *) PG_GETARG_POINTER(2);
4524 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
4525 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
4526 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
4527 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
4529 genericcostestimate(root, index, indexQuals, 0.0,
4530 indexStartupCost, indexTotalCost,
4531 indexSelectivity, indexCorrelation);
4537 gistcostestimate(PG_FUNCTION_ARGS)
4539 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
4540 IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
4541 List *indexQuals = (List *) PG_GETARG_POINTER(2);
4542 Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
4543 Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
4544 Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
4545 double *indexCorrelation = (double *) PG_GETARG_POINTER(6);
4547 genericcostestimate(root, index, indexQuals, 0.0,
4548 indexStartupCost, indexTotalCost,
4549 indexSelectivity, indexCorrelation);