PostgreSQL Source Code git master
Loading...
Searching...
No Matches
index_selfuncs.h File Reference
#include "access/amapi.h"
Include dependency graph for index_selfuncs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void brincostestimate (struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void btcostestimate (struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void hashcostestimate (struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void gistcostestimate (struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void spgcostestimate (struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 
void gincostestimate (struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
 

Function Documentation

◆ brincostestimate()

void brincostestimate ( struct PlannerInfo root,
struct IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double indexCorrelation,
double indexPages 
)
extern

Definition at line 8971 of file selfuncs.c.

8975{
8976 IndexOptInfo *index = path->indexinfo;
8978 double numPages = index->pages;
8979 RelOptInfo *baserel = index->rel;
8982 Cost spc_random_page_cost;
8983 double qual_arg_cost;
8984 double qualSelectivity;
8986 double indexRanges;
8987 double minimalRanges;
8988 double estimatedRanges;
8989 double selec;
8990 Relation indexRel;
8991 ListCell *l;
8993
8994 Assert(rte->rtekind == RTE_RELATION);
8995
8996 /* fetch estimated page cost for the tablespace containing the index */
8997 get_tablespace_page_costs(index->reltablespace,
8998 &spc_random_page_cost,
9000
9001 /*
9002 * Obtain some data from the index itself, if possible. Otherwise invent
9003 * some plausible internal statistics based on the relation page count.
9004 */
9005 if (!index->hypothetical)
9006 {
9007 /*
9008 * A lock should have already been obtained on the index in plancat.c.
9009 */
9010 indexRel = index_open(index->indexoid, NoLock);
9011 brinGetStats(indexRel, &statsData);
9012 index_close(indexRel, NoLock);
9013
9014 /* work out the actual number of ranges in the index */
9015 indexRanges = Max(ceil((double) baserel->pages /
9016 statsData.pagesPerRange), 1.0);
9017 }
9018 else
9019 {
9020 /*
9021 * Assume default number of pages per range, and estimate the number
9022 * of ranges based on that.
9023 */
9024 indexRanges = Max(ceil((double) baserel->pages /
9026
9028 statsData.revmapNumPages = (indexRanges / REVMAP_PAGE_MAXITEMS) + 1;
9029 }
9030
9031 /*
9032 * Compute index correlation
9033 *
9034 * Because we can use all index quals equally when scanning, we can use
9035 * the largest correlation (in absolute value) among columns used by the
9036 * query. Start at zero, the worst possible case. If we cannot find any
9037 * correlation statistics, we will keep it as 0.
9038 */
9039 *indexCorrelation = 0;
9040
9041 foreach(l, path->indexclauses)
9042 {
9044 AttrNumber attnum = index->indexkeys[iclause->indexcol];
9045
9046 /* attempt to lookup stats in relation for this index column */
9047 if (attnum != 0)
9048 {
9049 /* Simple variable -- look to stats for the underlying table */
9052 {
9053 /*
9054 * The hook took control of acquiring a stats tuple. If it
9055 * did supply a tuple, it'd better have supplied a freefunc.
9056 */
9057 if (HeapTupleIsValid(vardata.statsTuple) && !vardata.freefunc)
9058 elog(ERROR,
9059 "no function provided to release variable stats with");
9060 }
9061 else
9062 {
9063 vardata.statsTuple =
9065 ObjectIdGetDatum(rte->relid),
9067 BoolGetDatum(false));
9068 vardata.freefunc = ReleaseSysCache;
9069 }
9070 }
9071 else
9072 {
9073 /*
9074 * Looks like we've found an expression column in the index. Let's
9075 * see if there's any stats for it.
9076 */
9077
9078 /* get the attnum from the 0-based index. */
9079 attnum = iclause->indexcol + 1;
9080
9082 (*get_index_stats_hook) (root, index->indexoid, attnum, &vardata))
9083 {
9084 /*
9085 * The hook took control of acquiring a stats tuple. If it
9086 * did supply a tuple, it'd better have supplied a freefunc.
9087 */
9088 if (HeapTupleIsValid(vardata.statsTuple) &&
9089 !vardata.freefunc)
9090 elog(ERROR, "no function provided to release variable stats with");
9091 }
9092 else
9093 {
9095 ObjectIdGetDatum(index->indexoid),
9097 BoolGetDatum(false));
9098 vardata.freefunc = ReleaseSysCache;
9099 }
9100 }
9101
9102 if (HeapTupleIsValid(vardata.statsTuple))
9103 {
9105
9106 if (get_attstatsslot(&sslot, vardata.statsTuple,
9109 {
9110 double varCorrelation = 0.0;
9111
9112 if (sslot.nnumbers > 0)
9113 varCorrelation = fabs(sslot.numbers[0]);
9114
9115 if (varCorrelation > *indexCorrelation)
9116 *indexCorrelation = varCorrelation;
9117
9119 }
9120 }
9121
9123 }
9124
9126 baserel->relid,
9127 JOIN_INNER, NULL);
9128
9129 /*
9130 * Now calculate the minimum possible ranges we could match with if all of
9131 * the rows were in the perfect order in the table's heap.
9132 */
9134
9135 /*
9136 * Now estimate the number of ranges that we'll touch by using the
9137 * indexCorrelation from the stats. Careful not to divide by zero (note
9138 * we're using the absolute value of the correlation).
9139 */
9140 if (*indexCorrelation < 1.0e-10)
9142 else
9143 estimatedRanges = Min(minimalRanges / *indexCorrelation, indexRanges);
9144
9145 /* we expect to visit this portion of the table */
9147
9149
9150 *indexSelectivity = selec;
9151
9152 /*
9153 * Compute the index qual costs, much as in genericcostestimate, to add to
9154 * the index costs. We can disregard indexorderbys, since BRIN doesn't
9155 * support those.
9156 */
9158
9159 /*
9160 * Compute the startup cost as the cost to read the whole revmap
9161 * sequentially, including the cost to execute the index quals.
9162 */
9163 *indexStartupCost =
9164 spc_seq_page_cost * statsData.revmapNumPages * loop_count;
9165 *indexStartupCost += qual_arg_cost;
9166
9167 /*
9168 * To read a BRIN index there might be a bit of back and forth over
9169 * regular pages, as revmap might point to them out of sequential order;
9170 * calculate the total cost as reading the whole index in random order.
9171 */
9172 *indexTotalCost = *indexStartupCost +
9173 spc_random_page_cost * (numPages - statsData.revmapNumPages) * loop_count;
9174
9175 /*
9176 * Charge a small amount per range tuple which we expect to match to. This
9177 * is meant to reflect the costs of manipulating the bitmap. The BRIN scan
9178 * will set a bit for each page in the range when we find a matching
9179 * range, so we must multiply the charge by the number of pages in the
9180 * range.
9181 */
9182 *indexTotalCost += 0.1 * cpu_operator_cost * estimatedRanges *
9183 statsData.pagesPerRange;
9184
9185 *indexPages = index->pages;
9186}
int16 AttrNumber
Definition attnum.h:21
void brinGetStats(Relation index, BrinStatsData *stats)
Definition brin.c:1651
#define BRIN_DEFAULT_PAGES_PER_RANGE
Definition brin.h:40
#define REVMAP_PAGE_MAXITEMS
Definition brin_page.h:93
#define Min(x, y)
Definition c.h:1054
#define Max(x, y)
Definition c.h:1048
#define Assert(condition)
Definition c.h:906
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition clausesel.c:100
double cpu_operator_cost
Definition costsize.c:134
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:133
#define NoLock
Definition lockdefs.h:34
void free_attstatsslot(AttStatsSlot *sslot)
Definition lsyscache.c:3496
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition lsyscache.c:3386
#define ATTSTATSSLOT_NUMBERS
Definition lsyscache.h:44
double Cost
Definition nodes.h:261
@ JOIN_INNER
Definition nodes.h:303
@ RTE_RELATION
#define planner_rt_fetch(rti, root)
Definition pathnodes.h:692
int16 attnum
#define lfirst_node(type, lc)
Definition pg_list.h:176
static Datum Int16GetDatum(int16 X)
Definition postgres.h:182
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
#define InvalidOid
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
List * get_quals_from_indexclauses(List *indexclauses)
Definition selfuncs.c:7291
get_index_stats_hook_type get_index_stats_hook
Definition selfuncs.c:184
Cost index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
Definition selfuncs.c:7321
get_relation_stats_hook_type get_relation_stats_hook
Definition selfuncs.c:183
#define ReleaseVariableStats(vardata)
Definition selfuncs.h:101
#define CLAMP_PROBABILITY(p)
Definition selfuncs.h:63
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition spccache.c:183
List * indexclauses
Definition pathnodes.h:2043
IndexOptInfo * indexinfo
Definition pathnodes.h:2042
Definition pg_list.h:54
Definition type.h:96
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache3(SysCacheIdentifier cacheId, Datum key1, Datum key2, Datum key3)
Definition syscache.c:240

References Assert, attnum, ATTSTATSSLOT_NUMBERS, BoolGetDatum(), BRIN_DEFAULT_PAGES_PER_RANGE, brinGetStats(), CLAMP_PROBABILITY, clauselist_selectivity(), cpu_operator_cost, elog, ERROR, fb(), free_attstatsslot(), get_attstatsslot(), get_index_stats_hook, get_quals_from_indexclauses(), get_relation_stats_hook, get_tablespace_page_costs(), HeapTupleIsValid, index_close(), index_open(), index_other_operands_eval_cost(), IndexPath::indexclauses, IndexPath::indexinfo, Int16GetDatum(), InvalidOid, JOIN_INNER, lfirst_node, Max, Min, NoLock, ObjectIdGetDatum(), planner_rt_fetch, ReleaseSysCache(), ReleaseVariableStats, REVMAP_PAGE_MAXITEMS, root, RTE_RELATION, and SearchSysCache3().

Referenced by brinhandler().

◆ btcostestimate()

void btcostestimate ( struct PlannerInfo root,
struct IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double indexCorrelation,
double indexPages 
)
extern

Definition at line 7666 of file selfuncs.c.

7670{
7671 IndexOptInfo *index = path->indexinfo;
7672 GenericCosts costs = {0};
7674 double numIndexTuples;
7678 int indexcol;
7679 bool eqQualHere;
7680 bool found_row_compare;
7681 bool found_array;
7682 bool found_is_null_op;
7683 bool have_correlation = false;
7684 double num_sa_scans;
7685 double correlation = 0.0;
7686 ListCell *lc;
7687
7688 /*
7689 * For a btree scan, only leading '=' quals plus inequality quals for the
7690 * immediately next attribute contribute to index selectivity (these are
7691 * the "boundary quals" that determine the starting and stopping points of
7692 * the index scan). Additional quals can suppress visits to the heap, so
7693 * it's OK to count them in indexSelectivity, but they should not count
7694 * for estimating numIndexTuples. So we must examine the given indexquals
7695 * to find out which ones count as boundary quals. We rely on the
7696 * knowledge that they are given in index column order. Note that nbtree
7697 * preprocessing can add skip arrays that act as leading '=' quals in the
7698 * absence of ordinary input '=' quals, so in practice _most_ input quals
7699 * are able to act as index bound quals (which we take into account here).
7700 *
7701 * For a RowCompareExpr, we consider only the first column, just as
7702 * rowcomparesel() does.
7703 *
7704 * If there's a SAOP or skip array in the quals, we'll actually perform up
7705 * to N index descents (not just one), but the underlying array key's
7706 * operator can be considered to act the same as it normally does.
7707 */
7710 indexcol = 0;
7711 eqQualHere = false;
7712 found_row_compare = false;
7713 found_array = false;
7714 found_is_null_op = false;
7715 num_sa_scans = 1;
7716 foreach(lc, path->indexclauses)
7717 {
7719 ListCell *lc2;
7720
7721 if (indexcol < iclause->indexcol)
7722 {
7723 double num_sa_scans_prev_cols = num_sa_scans;
7724
7725 /*
7726 * Beginning of a new column's quals.
7727 *
7728 * Skip scans use skip arrays, which are ScalarArrayOp style
7729 * arrays that generate their elements procedurally and on demand.
7730 * Given a multi-column index on "(a, b)", and an SQL WHERE clause
7731 * "WHERE b = 42", a skip scan will effectively use an indexqual
7732 * "WHERE a = ANY('{every col a value}') AND b = 42". (Obviously,
7733 * the array on "a" must also return "IS NULL" matches, since our
7734 * WHERE clause used no strict operator on "a").
7735 *
7736 * Here we consider how nbtree will backfill skip arrays for any
7737 * index columns that lacked an '=' qual. This maintains our
7738 * num_sa_scans estimate, and determines if this new column (the
7739 * "iclause->indexcol" column, not the prior "indexcol" column)
7740 * can have its RestrictInfos/quals added to indexBoundQuals.
7741 *
7742 * We'll need to handle columns that have inequality quals, where
7743 * the skip array generates values from a range constrained by the
7744 * quals (not every possible value). We've been maintaining
7745 * indexSkipQuals to help with this; it will now contain all of
7746 * the prior column's quals (that is, indexcol's quals) when they
7747 * might be used for this.
7748 */
7750 {
7751 /*
7752 * Skip arrays can't be added after a RowCompare input qual
7753 * due to limitations in nbtree
7754 */
7755 break;
7756 }
7757 if (eqQualHere)
7758 {
7759 /*
7760 * Don't need to add a skip array for an indexcol that already
7761 * has an '=' qual/equality constraint
7762 */
7763 indexcol++;
7765 }
7766 eqQualHere = false;
7767
7768 while (indexcol < iclause->indexcol)
7769 {
7770 double ndistinct;
7771 bool isdefault = true;
7772
7773 found_array = true;
7774
7775 /*
7776 * A skipped attribute's ndistinct forms the basis of our
7777 * estimate of the total number of "array elements" used by
7778 * its skip array at runtime. Look that up first.
7779 */
7781 ndistinct = get_variable_numdistinct(&vardata, &isdefault);
7782
7783 if (indexcol == 0)
7784 {
7785 /*
7786 * Get an estimate of the leading column's correlation in
7787 * passing (avoids rereading variable stats below)
7788 */
7789 if (HeapTupleIsValid(vardata.statsTuple))
7791 have_correlation = true;
7792 }
7793
7795
7796 /*
7797 * If ndistinct is a default estimate, conservatively assume
7798 * that no skipping will happen at runtime
7799 */
7800 if (isdefault)
7801 {
7802 num_sa_scans = num_sa_scans_prev_cols;
7803 break; /* done building indexBoundQuals */
7804 }
7805
7806 /*
7807 * Apply indexcol's indexSkipQuals selectivity to ndistinct
7808 */
7809 if (indexSkipQuals != NIL)
7810 {
7813
7814 /*
7815 * If the index is partial, AND the index predicate with
7816 * the index-bound quals to produce a more accurate idea
7817 * of the number of distinct values for prior indexcol
7818 */
7821
7823 index->rel->relid,
7824 JOIN_INNER,
7825 NULL);
7826
7827 /*
7828 * If ndistinctfrac is selective (on its own), the scan is
7829 * unlikely to benefit from repositioning itself using
7830 * later quals. Do not allow iclause->indexcol's quals to
7831 * be added to indexBoundQuals (it would increase descent
7832 * costs, without lowering numIndexTuples costs by much).
7833 */
7835 {
7836 num_sa_scans = num_sa_scans_prev_cols;
7837 break; /* done building indexBoundQuals */
7838 }
7839
7840 /* Adjust ndistinct downward */
7841 ndistinct = rint(ndistinct * ndistinctfrac);
7842 ndistinct = Max(ndistinct, 1);
7843 }
7844
7845 /*
7846 * When there's no inequality quals, account for the need to
7847 * find an initial value by counting -inf/+inf as a value.
7848 *
7849 * We don't charge anything extra for possible next/prior key
7850 * index probes, which are sometimes used to find the next
7851 * valid skip array element (ahead of using the located
7852 * element value to relocate the scan to the next position
7853 * that might contain matching tuples). It seems hard to do
7854 * better here. Use of the skip support infrastructure often
7855 * avoids most next/prior key probes. But even when it can't,
7856 * there's a decent chance that most individual next/prior key
7857 * probes will locate a leaf page whose key space overlaps all
7858 * of the scan's keys (even the lower-order keys) -- which
7859 * also avoids the need for a separate, extra index descent.
7860 * Note also that these probes are much cheaper than non-probe
7861 * primitive index scans: they're reliably very selective.
7862 */
7863 if (indexSkipQuals == NIL)
7864 ndistinct += 1;
7865
7866 /*
7867 * Update num_sa_scans estimate by multiplying by ndistinct.
7868 *
7869 * We make the pessimistic assumption that there is no
7870 * naturally occurring cross-column correlation. This is
7871 * often wrong, but it seems best to err on the side of not
7872 * expecting skipping to be helpful...
7873 */
7874 num_sa_scans *= ndistinct;
7875
7876 /*
7877 * ...but back out of adding this latest group of 1 or more
7878 * skip arrays when num_sa_scans exceeds the total number of
7879 * index pages (revert to num_sa_scans from before indexcol).
7880 * This causes a sharp discontinuity in cost (as a function of
7881 * the indexcol's ndistinct), but that is representative of
7882 * actual runtime costs.
7883 *
7884 * Note that skipping is helpful when each primitive index
7885 * scan only manages to skip over 1 or 2 irrelevant leaf pages
7886 * on average. Skip arrays bring savings in CPU costs due to
7887 * the scan not needing to evaluate indexquals against every
7888 * tuple, which can greatly exceed any savings in I/O costs.
7889 * This test is a test of whether num_sa_scans implies that
7890 * we're past the point where the ability to skip ceases to
7891 * lower the scan's costs (even qual evaluation CPU costs).
7892 */
7893 if (index->pages < num_sa_scans)
7894 {
7895 num_sa_scans = num_sa_scans_prev_cols;
7896 break; /* done building indexBoundQuals */
7897 }
7898
7899 indexcol++;
7901 }
7902
7903 /*
7904 * Finished considering the need to add skip arrays to bridge an
7905 * initial eqQualHere gap between the old and new index columns
7906 * (or there was no initial eqQualHere gap in the first place).
7907 *
7908 * If an initial gap could not be bridged, then new column's quals
7909 * (i.e. iclause->indexcol's quals) won't go into indexBoundQuals,
7910 * and so won't affect our final numIndexTuples estimate.
7911 */
7912 if (indexcol != iclause->indexcol)
7913 break; /* done building indexBoundQuals */
7914 }
7915
7916 Assert(indexcol == iclause->indexcol);
7917
7918 /* Examine each indexqual associated with this index clause */
7919 foreach(lc2, iclause->indexquals)
7920 {
7922 Expr *clause = rinfo->clause;
7923 Oid clause_op = InvalidOid;
7924 int op_strategy;
7925
7926 if (IsA(clause, OpExpr))
7927 {
7928 OpExpr *op = (OpExpr *) clause;
7929
7930 clause_op = op->opno;
7931 }
7932 else if (IsA(clause, RowCompareExpr))
7933 {
7934 RowCompareExpr *rc = (RowCompareExpr *) clause;
7935
7936 clause_op = linitial_oid(rc->opnos);
7937 found_row_compare = true;
7938 }
7939 else if (IsA(clause, ScalarArrayOpExpr))
7940 {
7941 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
7942 Node *other_operand = (Node *) lsecond(saop->args);
7944
7945 clause_op = saop->opno;
7946 found_array = true;
7947 /* estimate SA descents by indexBoundQuals only */
7948 if (alength > 1)
7949 num_sa_scans *= alength;
7950 }
7951 else if (IsA(clause, NullTest))
7952 {
7953 NullTest *nt = (NullTest *) clause;
7954
7955 if (nt->nulltesttype == IS_NULL)
7956 {
7957 found_is_null_op = true;
7958 /* IS NULL is like = for selectivity/skip scan purposes */
7959 eqQualHere = true;
7960 }
7961 }
7962 else
7963 elog(ERROR, "unsupported indexqual type: %d",
7964 (int) nodeTag(clause));
7965
7966 /* check for equality operator */
7967 if (OidIsValid(clause_op))
7968 {
7969 op_strategy = get_op_opfamily_strategy(clause_op,
7970 index->opfamily[indexcol]);
7971 Assert(op_strategy != 0); /* not a member of opfamily?? */
7972 if (op_strategy == BTEqualStrategyNumber)
7973 eqQualHere = true;
7974 }
7975
7977
7978 /*
7979 * We apply inequality selectivities to estimate index descent
7980 * costs with scans that use skip arrays. Save this indexcol's
7981 * RestrictInfos if it looks like they'll be needed for that.
7982 */
7983 if (!eqQualHere && !found_row_compare &&
7984 indexcol < index->nkeycolumns - 1)
7986 }
7987 }
7988
7989 /*
7990 * If index is unique and we found an '=' clause for each column, we can
7991 * just assume numIndexTuples = 1 and skip the expensive
7992 * clauselist_selectivity calculations. However, an array or NullTest
7993 * always invalidates that theory (even when eqQualHere has been set).
7994 */
7995 if (index->unique &&
7996 indexcol == index->nkeycolumns - 1 &&
7997 eqQualHere &&
7998 !found_array &&
8000 numIndexTuples = 1.0;
8001 else
8002 {
8005
8006 /*
8007 * If the index is partial, AND the index predicate with the
8008 * index-bound quals to produce a more accurate idea of the number of
8009 * rows covered by the bound conditions.
8010 */
8012
8014 index->rel->relid,
8015 JOIN_INNER,
8016 NULL);
8017 numIndexTuples = btreeSelectivity * index->rel->tuples;
8018
8019 /*
8020 * btree automatically combines individual array element primitive
8021 * index scans whenever the tuples covered by the next set of array
8022 * keys are close to tuples covered by the current set. That puts a
8023 * natural ceiling on the worst case number of descents -- there
8024 * cannot possibly be more than one descent per leaf page scanned.
8025 *
8026 * Clamp the number of descents to at most 1/3 the number of index
8027 * pages. This avoids implausibly high estimates with low selectivity
8028 * paths, where scans usually require only one or two descents. This
8029 * is most likely to help when there are several SAOP clauses, where
8030 * naively accepting the total number of distinct combinations of
8031 * array elements as the number of descents would frequently lead to
8032 * wild overestimates.
8033 *
8034 * We somewhat arbitrarily don't just make the cutoff the total number
8035 * of leaf pages (we make it 1/3 the total number of pages instead) to
8036 * give the btree code credit for its ability to continue on the leaf
8037 * level with low selectivity scans.
8038 *
8039 * Note: num_sa_scans includes both ScalarArrayOp array elements and
8040 * skip array elements whose qual affects our numIndexTuples estimate.
8041 */
8042 num_sa_scans = Min(num_sa_scans, ceil(index->pages * 0.3333333));
8043 num_sa_scans = Max(num_sa_scans, 1);
8044
8045 /*
8046 * As in genericcostestimate(), we have to adjust for any array quals
8047 * included in indexBoundQuals, and then round to integer.
8048 *
8049 * It is tempting to make genericcostestimate behave as if array
8050 * clauses work in almost the same way as scalar operators during
8051 * btree scans, making the top-level scan look like a continuous scan
8052 * (as opposed to num_sa_scans-many primitive index scans). After
8053 * all, btree scans mostly work like that at runtime. However, such a
8054 * scheme would badly bias genericcostestimate's simplistic approach
8055 * to calculating numIndexPages through prorating.
8056 *
8057 * Stick with the approach taken by non-native SAOP scans for now.
8058 * genericcostestimate will use the Mackert-Lohman formula to
8059 * compensate for repeat page fetches, even though that definitely
8060 * won't happen during btree scans (not for leaf pages, at least).
8061 * We're usually very pessimistic about the number of primitive index
8062 * scans that will be required, but it's not clear how to do better.
8063 */
8064 numIndexTuples = rint(numIndexTuples / num_sa_scans);
8065 }
8066
8067 /*
8068 * Now do generic index cost estimation.
8069 */
8070 costs.numIndexTuples = numIndexTuples;
8071 costs.num_sa_scans = num_sa_scans;
8072
8073 genericcostestimate(root, path, loop_count, &costs);
8074
8075 /*
8076 * Add a CPU-cost component to represent the costs of initial btree
8077 * descent. We don't charge any I/O cost for touching upper btree levels,
8078 * since they tend to stay in cache, but we still have to do about log2(N)
8079 * comparisons to descend a btree of N leaf tuples. We charge one
8080 * cpu_operator_cost per comparison.
8081 *
8082 * If there are SAOP or skip array keys, charge this once per estimated
8083 * index descent. The ones after the first one are not startup cost so
8084 * far as the overall plan goes, so just add them to "total" cost.
8085 */
8086 if (index->tuples > 1) /* avoid computing log(0) */
8087 {
8088 descentCost = ceil(log(index->tuples) / log(2.0)) * cpu_operator_cost;
8090 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8091 }
8092
8093 /*
8094 * Even though we're not charging I/O cost for touching upper btree pages,
8095 * it's still reasonable to charge some CPU cost per page descended
8096 * through. Moreover, if we had no such charge at all, bloated indexes
8097 * would appear to have the same search cost as unbloated ones, at least
8098 * in cases where only a single leaf page is expected to be visited. This
8099 * cost is somewhat arbitrarily set at 50x cpu_operator_cost per page
8100 * touched. The number of such pages is btree tree height plus one (ie,
8101 * we charge for the leaf page too). As above, charge once per estimated
8102 * SAOP/skip array descent.
8103 */
8106 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8107
8108 if (!have_correlation)
8109 {
8111 if (HeapTupleIsValid(vardata.statsTuple))
8114 }
8115 else
8116 {
8117 /* btcost_correlation already called earlier on */
8119 }
8120
8121 *indexStartupCost = costs.indexStartupCost;
8122 *indexTotalCost = costs.indexTotalCost;
8123 *indexSelectivity = costs.indexSelectivity;
8124 *indexCorrelation = costs.indexCorrelation;
8125 *indexPages = costs.numIndexPages;
8126}
#define OidIsValid(objectId)
Definition c.h:821
List * lappend(List *list, void *datum)
Definition list.c:339
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition lsyscache.c:85
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define nodeTag(nodeptr)
Definition nodes.h:139
double Selectivity
Definition nodes.h:260
#define NIL
Definition pg_list.h:68
#define lsecond(l)
Definition pg_list.h:183
#define linitial_oid(l)
Definition pg_list.h:180
unsigned int Oid
@ IS_NULL
Definition primnodes.h:1978
List * add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
Definition selfuncs.c:7598
#define DEFAULT_PAGE_CPU_MULTIPLIER
Definition selfuncs.c:144
double estimate_array_length(PlannerInfo *root, Node *arrayexpr)
Definition selfuncs.c:2223
void genericcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, GenericCosts *costs)
Definition selfuncs.c:7375
static void examine_indexcol_variable(PlannerInfo *root, IndexOptInfo *index, int indexcol, VariableStatData *vardata)
Definition selfuncs.c:6479
static double btcost_correlation(IndexOptInfo *index, VariableStatData *vardata)
Definition selfuncs.c:7629
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition selfuncs.c:6582
#define DEFAULT_RANGE_INEQ_SEL
Definition selfuncs.h:40
#define BTEqualStrategyNumber
Definition stratnum.h:31
Selectivity indexSelectivity
Definition selfuncs.h:129
Cost indexStartupCost
Definition selfuncs.h:127
double indexCorrelation
Definition selfuncs.h:130
double num_sa_scans
Definition selfuncs.h:136
Cost indexTotalCost
Definition selfuncs.h:128
double numIndexPages
Definition selfuncs.h:133
double numIndexTuples
Definition selfuncs.h:134
Definition nodes.h:135
Oid opno
Definition primnodes.h:851
Expr * clause
Definition pathnodes.h:2886

References add_predicate_to_index_quals(), ScalarArrayOpExpr::args, Assert, btcost_correlation(), BTEqualStrategyNumber, RestrictInfo::clause, clauselist_selectivity(), cpu_operator_cost, DEFAULT_PAGE_CPU_MULTIPLIER, DEFAULT_RANGE_INEQ_SEL, elog, ERROR, estimate_array_length(), examine_indexcol_variable(), fb(), genericcostestimate(), get_op_opfamily_strategy(), get_variable_numdistinct(), HeapTupleIsValid, IndexPath::indexclauses, GenericCosts::indexCorrelation, IndexPath::indexinfo, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, InvalidOid, IS_NULL, IsA, JOIN_INNER, lappend(), lfirst_node, linitial_oid, lsecond, Max, Min, NIL, nodeTag, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, GenericCosts::numIndexTuples, OidIsValid, OpExpr::opno, ScalarArrayOpExpr::opno, ReleaseVariableStats, and root.

Referenced by bthandler().

◆ gincostestimate()

void gincostestimate ( struct PlannerInfo root,
struct IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double indexCorrelation,
double indexPages 
)
extern

Definition at line 8581 of file selfuncs.c.

8585{
8586 IndexOptInfo *index = path->indexinfo;
8589 double numPages = index->pages,
8590 numTuples = index->tuples;
8591 double numEntryPages,
8594 numEntries;
8595 GinQualCounts counts;
8596 bool matchPossible;
8597 bool fullIndexScan;
8598 double partialScale;
8599 double entryPagesFetched,
8602 double qual_op_cost,
8604 spc_random_page_cost,
8607 Relation indexRel;
8609 ListCell *lc;
8610 int i;
8611
8612 /*
8613 * Obtain statistical information from the meta page, if possible. Else
8614 * set ginStats to zeroes, and we'll cope below.
8615 */
8616 if (!index->hypothetical)
8617 {
8618 /* Lock should have already been obtained in plancat.c */
8619 indexRel = index_open(index->indexoid, NoLock);
8620 ginGetStats(indexRel, &ginStats);
8621 index_close(indexRel, NoLock);
8622 }
8623 else
8624 {
8625 memset(&ginStats, 0, sizeof(ginStats));
8626 }
8627
8628 /*
8629 * Assuming we got valid (nonzero) stats at all, nPendingPages can be
8630 * trusted, but the other fields are data as of the last VACUUM. We can
8631 * scale them up to account for growth since then, but that method only
8632 * goes so far; in the worst case, the stats might be for a completely
8633 * empty index, and scaling them will produce pretty bogus numbers.
8634 * Somewhat arbitrarily, set the cutoff for doing scaling at 4X growth; if
8635 * it's grown more than that, fall back to estimating things only from the
8636 * assumed-accurate index size. But we'll trust nPendingPages in any case
8637 * so long as it's not clearly insane, ie, more than the index size.
8638 */
8639 if (ginStats.nPendingPages < numPages)
8640 numPendingPages = ginStats.nPendingPages;
8641 else
8642 numPendingPages = 0;
8643
8644 if (numPages > 0 && ginStats.nTotalPages <= numPages &&
8645 ginStats.nTotalPages > numPages / 4 &&
8646 ginStats.nEntryPages > 0 && ginStats.nEntries > 0)
8647 {
8648 /*
8649 * OK, the stats seem close enough to sane to be trusted. But we
8650 * still need to scale them by the ratio numPages / nTotalPages to
8651 * account for growth since the last VACUUM.
8652 */
8653 double scale = numPages / ginStats.nTotalPages;
8654
8655 numEntryPages = ceil(ginStats.nEntryPages * scale);
8656 numDataPages = ceil(ginStats.nDataPages * scale);
8657 numEntries = ceil(ginStats.nEntries * scale);
8658 /* ensure we didn't round up too much */
8662 }
8663 else
8664 {
8665 /*
8666 * We might get here because it's a hypothetical index, or an index
8667 * created pre-9.1 and never vacuumed since upgrading (in which case
8668 * its stats would read as zeroes), or just because it's grown too
8669 * much since the last VACUUM for us to put our faith in scaling.
8670 *
8671 * Invent some plausible internal statistics based on the index page
8672 * count (and clamp that to at least 10 pages, just in case). We
8673 * estimate that 90% of the index is entry pages, and the rest is data
8674 * pages. Estimate 100 entries per entry page; this is rather bogus
8675 * since it'll depend on the size of the keys, but it's more robust
8676 * than trying to predict the number of entries per heap tuple.
8677 */
8678 numPages = Max(numPages, 10);
8682 }
8683
8684 /* In an empty index, numEntries could be zero. Avoid divide-by-zero */
8685 if (numEntries < 1)
8686 numEntries = 1;
8687
8688 /*
8689 * If the index is partial, AND the index predicate with the index-bound
8690 * quals to produce a more accurate idea of the number of rows covered by
8691 * the bound conditions.
8692 */
8694
8695 /* Estimate the fraction of main-table tuples that will be visited */
8696 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
8697 index->rel->relid,
8698 JOIN_INNER,
8699 NULL);
8700
8701 /* fetch estimated page cost for tablespace containing index */
8702 get_tablespace_page_costs(index->reltablespace,
8703 &spc_random_page_cost,
8704 NULL);
8705
8706 /*
8707 * Generic assumption about index correlation: there isn't any.
8708 */
8709 *indexCorrelation = 0.0;
8710
8711 /*
8712 * Examine quals to estimate number of search entries & partial matches
8713 */
8714 memset(&counts, 0, sizeof(counts));
8715 counts.arrayScans = 1;
8716 matchPossible = true;
8717
8718 foreach(lc, path->indexclauses)
8719 {
8721 ListCell *lc2;
8722
8723 foreach(lc2, iclause->indexquals)
8724 {
8726 Expr *clause = rinfo->clause;
8727
8728 if (IsA(clause, OpExpr))
8729 {
8731 index,
8732 iclause->indexcol,
8733 (OpExpr *) clause,
8734 &counts);
8735 if (!matchPossible)
8736 break;
8737 }
8738 else if (IsA(clause, ScalarArrayOpExpr))
8739 {
8741 index,
8742 iclause->indexcol,
8743 (ScalarArrayOpExpr *) clause,
8744 numEntries,
8745 &counts);
8746 if (!matchPossible)
8747 break;
8748 }
8749 else
8750 {
8751 /* shouldn't be anything else for a GIN index */
8752 elog(ERROR, "unsupported GIN indexqual type: %d",
8753 (int) nodeTag(clause));
8754 }
8755 }
8756 }
8757
8758 /* Fall out if there were any provably-unsatisfiable quals */
8759 if (!matchPossible)
8760 {
8761 *indexStartupCost = 0;
8762 *indexTotalCost = 0;
8763 *indexSelectivity = 0;
8764 return;
8765 }
8766
8767 /*
8768 * If attribute has a full scan and at the same time doesn't have normal
8769 * scan, then we'll have to scan all non-null entries of that attribute.
8770 * Currently, we don't have per-attribute statistics for GIN. Thus, we
8771 * must assume the whole GIN index has to be scanned in this case.
8772 */
8773 fullIndexScan = false;
8774 for (i = 0; i < index->nkeycolumns; i++)
8775 {
8776 if (counts.attHasFullScan[i] && !counts.attHasNormalScan[i])
8777 {
8778 fullIndexScan = true;
8779 break;
8780 }
8781 }
8782
8783 if (fullIndexScan || indexQuals == NIL)
8784 {
8785 /*
8786 * Full index scan will be required. We treat this as if every key in
8787 * the index had been listed in the query; is that reasonable?
8788 */
8789 counts.partialEntries = 0;
8790 counts.exactEntries = numEntries;
8791 counts.searchEntries = numEntries;
8792 }
8793
8794 /* Will we have more than one iteration of a nestloop scan? */
8796
8797 /*
8798 * Compute cost to begin scan, first of all, pay attention to pending
8799 * list.
8800 */
8802
8803 /*
8804 * Estimate number of entry pages read. We need to do
8805 * counts.searchEntries searches. Use a power function as it should be,
8806 * but tuples on leaf pages usually is much greater. Here we include all
8807 * searches in entry tree, including search of first entry in partial
8808 * match algorithm
8809 */
8811
8812 /*
8813 * Add an estimate of entry pages read by partial match algorithm. It's a
8814 * scan over leaf pages in entry tree. We haven't any useful stats here,
8815 * so estimate it as proportion. Because counts.partialEntries is really
8816 * pretty bogus (see code above), it's possible that it is more than
8817 * numEntries; clamp the proportion to ensure sanity.
8818 */
8821
8823
8824 /*
8825 * Partial match algorithm reads all data pages before doing actual scan,
8826 * so it's a startup cost. Again, we haven't any useful stats here, so
8827 * estimate it as proportion.
8828 */
8830
8831 *indexStartupCost = 0;
8832 *indexTotalCost = 0;
8833
8834 /*
8835 * Add a CPU-cost component to represent the costs of initial entry btree
8836 * descent. We don't charge any I/O cost for touching upper btree levels,
8837 * since they tend to stay in cache, but we still have to do about log2(N)
8838 * comparisons to descend a btree of N leaf tuples. We charge one
8839 * cpu_operator_cost per comparison.
8840 *
8841 * If there are ScalarArrayOpExprs, charge this once per SA scan. The
8842 * ones after the first one are not startup cost so far as the overall
8843 * plan is concerned, so add them only to "total" cost.
8844 */
8845 if (numEntries > 1) /* avoid computing log(0) */
8846 {
8848 *indexStartupCost += descentCost * counts.searchEntries;
8849 *indexTotalCost += counts.arrayScans * descentCost * counts.searchEntries;
8850 }
8851
8852 /*
8853 * Add a cpu cost per entry-page fetched. This is not amortized over a
8854 * loop.
8855 */
8858
8859 /*
8860 * Add a cpu cost per data-page fetched. This is also not amortized over a
8861 * loop. Since those are the data pages from the partial match algorithm,
8862 * charge them as startup cost.
8863 */
8865
8866 /*
8867 * Since we add the startup cost to the total cost later on, remove the
8868 * initial arrayscan from the total.
8869 */
8870 *indexTotalCost += dataPagesFetched * (counts.arrayScans - 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
8871
8872 /*
8873 * Calculate cache effects if more than one scan due to nestloops or array
8874 * quals. The result is pro-rated per nestloop scan, but the array qual
8875 * factor shouldn't be pro-rated (compare genericcostestimate).
8876 */
8877 if (outer_scans > 1 || counts.arrayScans > 1)
8878 {
8889 }
8890
8891 /*
8892 * Here we use random page cost because logically-close pages could be far
8893 * apart on disk.
8894 */
8895 *indexStartupCost += (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
8896
8897 /*
8898 * Now compute the number of data pages fetched during the scan.
8899 *
8900 * We assume every entry to have the same number of items, and that there
8901 * is no overlap between them. (XXX: tsvector and array opclasses collect
8902 * statistics on the frequency of individual keys; it would be nice to use
8903 * those here.)
8904 */
8906
8907 /*
8908 * If there is a lot of overlap among the entries, in particular if one of
8909 * the entries is very frequent, the above calculation can grossly
8910 * under-estimate. As a simple cross-check, calculate a lower bound based
8911 * on the overall selectivity of the quals. At a minimum, we must read
8912 * one item pointer for each matching entry.
8913 *
8914 * The width of each item pointer varies, based on the level of
8915 * compression. We don't have statistics on that, but an average of
8916 * around 3 bytes per item is fairly typical.
8917 */
8918 dataPagesFetchedBySel = ceil(*indexSelectivity *
8919 (numTuples / (BLCKSZ / 3)));
8922
8923 /* Add one page cpu-cost to the startup cost */
8924 *indexStartupCost += DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost * counts.searchEntries;
8925
8926 /*
8927 * Add once again a CPU-cost for those data pages, before amortizing for
8928 * cache.
8929 */
8931
8932 /* Account for cache effects, the same as above */
8933 if (outer_scans > 1 || counts.arrayScans > 1)
8934 {
8940 }
8941
8942 /* And apply random_page_cost as the cost per page */
8943 *indexTotalCost += *indexStartupCost +
8944 dataPagesFetched * spc_random_page_cost;
8945
8946 /*
8947 * Add on index qual eval costs, much as in genericcostestimate. We charge
8948 * cpu but we can disregard indexorderbys, since GIN doesn't support
8949 * those.
8950 */
8953
8954 *indexStartupCost += qual_arg_cost;
8955 *indexTotalCost += qual_arg_cost;
8956
8957 /*
8958 * Add a cpu cost per search entry, corresponding to the actual visited
8959 * entries.
8960 */
8961 *indexTotalCost += (counts.searchEntries * counts.arrayScans) * (qual_op_cost);
8962 /* Now add a cpu cost per tuple in the posting lists / trees */
8963 *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost);
8965}
uint32 BlockNumber
Definition block.h:31
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition costsize.c:896
double cpu_index_tuple_cost
Definition costsize.c:133
void ginGetStats(Relation index, GinStatsData *stats)
Definition ginutil.c:591
int i
Definition isn.c:77
static int list_length(const List *l)
Definition pg_list.h:152
static int scale
Definition pgbench.c:182
static bool gincost_scalararrayopexpr(PlannerInfo *root, IndexOptInfo *index, int indexcol, ScalarArrayOpExpr *clause, double numIndexEntries, GinQualCounts *counts)
Definition selfuncs.c:8465
static bool gincost_opexpr(PlannerInfo *root, IndexOptInfo *index, int indexcol, OpExpr *clause, GinQualCounts *counts)
Definition selfuncs.c:8415
bool attHasNormalScan[INDEX_MAX_KEYS]
Definition selfuncs.c:8288
double exactEntries
Definition selfuncs.c:8290
double arrayScans
Definition selfuncs.c:8292
double partialEntries
Definition selfuncs.c:8289
bool attHasFullScan[INDEX_MAX_KEYS]
Definition selfuncs.c:8287
double searchEntries
Definition selfuncs.c:8291

References add_predicate_to_index_quals(), GinQualCounts::arrayScans, GinQualCounts::attHasFullScan, GinQualCounts::attHasNormalScan, RestrictInfo::clause, clauselist_selectivity(), cpu_index_tuple_cost, cpu_operator_cost, DEFAULT_PAGE_CPU_MULTIPLIER, elog, ERROR, GinQualCounts::exactEntries, fb(), get_quals_from_indexclauses(), get_tablespace_page_costs(), gincost_opexpr(), gincost_scalararrayopexpr(), ginGetStats(), i, index_close(), index_open(), index_other_operands_eval_cost(), index_pages_fetched(), IndexPath::indexclauses, IndexPath::indexinfo, IsA, JOIN_INNER, lfirst_node, list_length(), Max, Min, NIL, nodeTag, NoLock, GinQualCounts::partialEntries, root, scale, and GinQualCounts::searchEntries.

Referenced by ginhandler().

◆ gistcostestimate()

void gistcostestimate ( struct PlannerInfo root,
struct IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double indexCorrelation,
double indexPages 
)
extern

Definition at line 8171 of file selfuncs.c.

8175{
8176 IndexOptInfo *index = path->indexinfo;
8177 GenericCosts costs = {0};
8179
8180 genericcostestimate(root, path, loop_count, &costs);
8181
8182 /*
8183 * We model index descent costs similarly to those for btree, but to do
8184 * that we first need an idea of the tree height. We somewhat arbitrarily
8185 * assume that the fanout is 100, meaning the tree height is at most
8186 * log100(index->pages).
8187 *
8188 * Although this computation isn't really expensive enough to require
8189 * caching, we might as well use index->tree_height to cache it.
8190 */
8191 if (index->tree_height < 0) /* unknown? */
8192 {
8193 if (index->pages > 1) /* avoid computing log(0) */
8194 index->tree_height = (int) (log(index->pages) / log(100.0));
8195 else
8196 index->tree_height = 0;
8197 }
8198
8199 /*
8200 * Add a CPU-cost component to represent the costs of initial descent. We
8201 * just use log(N) here not log2(N) since the branching factor isn't
8202 * necessarily two anyway. As for btree, charge once per SA scan.
8203 */
8204 if (index->tuples > 1) /* avoid computing log(0) */
8205 {
8208 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8209 }
8210
8211 /*
8212 * Likewise add a per-page charge, calculated the same as for btrees.
8213 */
8216 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8217
8218 *indexStartupCost = costs.indexStartupCost;
8219 *indexTotalCost = costs.indexTotalCost;
8220 *indexSelectivity = costs.indexSelectivity;
8221 *indexCorrelation = costs.indexCorrelation;
8222 *indexPages = costs.numIndexPages;
8223}

References cpu_operator_cost, DEFAULT_PAGE_CPU_MULTIPLIER, fb(), genericcostestimate(), GenericCosts::indexCorrelation, IndexPath::indexinfo, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, and root.

Referenced by gisthandler().

◆ hashcostestimate()

void hashcostestimate ( struct PlannerInfo root,
struct IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double indexCorrelation,
double indexPages 
)
extern

Definition at line 8129 of file selfuncs.c.

8133{
8134 GenericCosts costs = {0};
8135
8136 genericcostestimate(root, path, loop_count, &costs);
8137
8138 /*
8139 * A hash index has no descent costs as such, since the index AM can go
8140 * directly to the target bucket after computing the hash value. There
8141 * are a couple of other hash-specific costs that we could conceivably add
8142 * here, though:
8143 *
8144 * Ideally we'd charge spc_random_page_cost for each page in the target
8145 * bucket, not just the numIndexPages pages that genericcostestimate
8146 * thought we'd visit. However in most cases we don't know which bucket
8147 * that will be. There's no point in considering the average bucket size
8148 * because the hash AM makes sure that's always one page.
8149 *
8150 * Likewise, we could consider charging some CPU for each index tuple in
8151 * the bucket, if we knew how many there were. But the per-tuple cost is
8152 * just a hash value comparison, not a general datatype-dependent
8153 * comparison, so any such charge ought to be quite a bit less than
8154 * cpu_operator_cost; which makes it probably not worth worrying about.
8155 *
8156 * A bigger issue is that chance hash-value collisions will result in
8157 * wasted probes into the heap. We don't currently attempt to model this
8158 * cost on the grounds that it's rare, but maybe it's not rare enough.
8159 * (Any fix for this ought to consider the generic lossy-operator problem,
8160 * though; it's not entirely hash-specific.)
8161 */
8162
8163 *indexStartupCost = costs.indexStartupCost;
8164 *indexTotalCost = costs.indexTotalCost;
8165 *indexSelectivity = costs.indexSelectivity;
8166 *indexCorrelation = costs.indexCorrelation;
8167 *indexPages = costs.numIndexPages;
8168}

References fb(), genericcostestimate(), GenericCosts::indexCorrelation, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, GenericCosts::numIndexPages, and root.

Referenced by hashhandler().

◆ spgcostestimate()

void spgcostestimate ( struct PlannerInfo root,
struct IndexPath path,
double  loop_count,
Cost indexStartupCost,
Cost indexTotalCost,
Selectivity indexSelectivity,
double indexCorrelation,
double indexPages 
)
extern

Definition at line 8226 of file selfuncs.c.

8230{
8231 IndexOptInfo *index = path->indexinfo;
8232 GenericCosts costs = {0};
8234
8235 genericcostestimate(root, path, loop_count, &costs);
8236
8237 /*
8238 * We model index descent costs similarly to those for btree, but to do
8239 * that we first need an idea of the tree height. We somewhat arbitrarily
8240 * assume that the fanout is 100, meaning the tree height is at most
8241 * log100(index->pages).
8242 *
8243 * Although this computation isn't really expensive enough to require
8244 * caching, we might as well use index->tree_height to cache it.
8245 */
8246 if (index->tree_height < 0) /* unknown? */
8247 {
8248 if (index->pages > 1) /* avoid computing log(0) */
8249 index->tree_height = (int) (log(index->pages) / log(100.0));
8250 else
8251 index->tree_height = 0;
8252 }
8253
8254 /*
8255 * Add a CPU-cost component to represent the costs of initial descent. We
8256 * just use log(N) here not log2(N) since the branching factor isn't
8257 * necessarily two anyway. As for btree, charge once per SA scan.
8258 */
8259 if (index->tuples > 1) /* avoid computing log(0) */
8260 {
8263 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8264 }
8265
8266 /*
8267 * Likewise add a per-page charge, calculated the same as for btrees.
8268 */
8271 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8272
8273 *indexStartupCost = costs.indexStartupCost;
8274 *indexTotalCost = costs.indexTotalCost;
8275 *indexSelectivity = costs.indexSelectivity;
8276 *indexCorrelation = costs.indexCorrelation;
8277 *indexPages = costs.numIndexPages;
8278}

References cpu_operator_cost, DEFAULT_PAGE_CPU_MULTIPLIER, fb(), genericcostestimate(), GenericCosts::indexCorrelation, IndexPath::indexinfo, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, GenericCosts::num_sa_scans, GenericCosts::numIndexPages, and root.

Referenced by spghandler().