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 9035 of file selfuncs.c.

9039{
9040 IndexOptInfo *index = path->indexinfo;
9042 double numPages = index->pages;
9043 RelOptInfo *baserel = index->rel;
9046 Cost spc_random_page_cost;
9047 double qual_arg_cost;
9048 double qualSelectivity;
9050 double indexRanges;
9051 double minimalRanges;
9052 double estimatedRanges;
9053 double selec;
9054 Relation indexRel;
9055 ListCell *l;
9057
9058 Assert(rte->rtekind == RTE_RELATION);
9059
9060 /* fetch estimated page cost for the tablespace containing the index */
9061 get_tablespace_page_costs(index->reltablespace,
9062 &spc_random_page_cost,
9064
9065 /*
9066 * Obtain some data from the index itself, if possible. Otherwise invent
9067 * some plausible internal statistics based on the relation page count.
9068 */
9069 if (!index->hypothetical)
9070 {
9071 /*
9072 * A lock should have already been obtained on the index in plancat.c.
9073 */
9074 indexRel = index_open(index->indexoid, NoLock);
9075 brinGetStats(indexRel, &statsData);
9076 index_close(indexRel, NoLock);
9077
9078 /* work out the actual number of ranges in the index */
9079 indexRanges = Max(ceil((double) baserel->pages /
9080 statsData.pagesPerRange), 1.0);
9081 }
9082 else
9083 {
9084 /*
9085 * Assume default number of pages per range, and estimate the number
9086 * of ranges based on that.
9087 */
9088 indexRanges = Max(ceil((double) baserel->pages /
9090
9092 statsData.revmapNumPages = (indexRanges / REVMAP_PAGE_MAXITEMS) + 1;
9093 }
9094
9095 /*
9096 * Compute index correlation
9097 *
9098 * Because we can use all index quals equally when scanning, we can use
9099 * the largest correlation (in absolute value) among columns used by the
9100 * query. Start at zero, the worst possible case. If we cannot find any
9101 * correlation statistics, we will keep it as 0.
9102 */
9103 *indexCorrelation = 0;
9104
9105 foreach(l, path->indexclauses)
9106 {
9108 AttrNumber attnum = index->indexkeys[iclause->indexcol];
9109
9110 /* attempt to lookup stats in relation for this index column */
9111 if (attnum != 0)
9112 {
9113 /* Simple variable -- look to stats for the underlying table */
9116 {
9117 /*
9118 * The hook took control of acquiring a stats tuple. If it
9119 * did supply a tuple, it'd better have supplied a freefunc.
9120 */
9121 if (HeapTupleIsValid(vardata.statsTuple) && !vardata.freefunc)
9122 elog(ERROR,
9123 "no function provided to release variable stats with");
9124 }
9125 else
9126 {
9127 vardata.statsTuple =
9129 ObjectIdGetDatum(rte->relid),
9131 BoolGetDatum(false));
9132 vardata.freefunc = ReleaseSysCache;
9133 }
9134 }
9135 else
9136 {
9137 /*
9138 * Looks like we've found an expression column in the index. Let's
9139 * see if there's any stats for it.
9140 */
9141
9142 /* get the attnum from the 0-based index. */
9143 attnum = iclause->indexcol + 1;
9144
9146 (*get_index_stats_hook) (root, index->indexoid, attnum, &vardata))
9147 {
9148 /*
9149 * The hook took control of acquiring a stats tuple. If it
9150 * did supply a tuple, it'd better have supplied a freefunc.
9151 */
9152 if (HeapTupleIsValid(vardata.statsTuple) &&
9153 !vardata.freefunc)
9154 elog(ERROR, "no function provided to release variable stats with");
9155 }
9156 else
9157 {
9159 ObjectIdGetDatum(index->indexoid),
9161 BoolGetDatum(false));
9162 vardata.freefunc = ReleaseSysCache;
9163 }
9164 }
9165
9166 if (HeapTupleIsValid(vardata.statsTuple))
9167 {
9169
9170 if (get_attstatsslot(&sslot, vardata.statsTuple,
9173 {
9174 double varCorrelation = 0.0;
9175
9176 if (sslot.nnumbers > 0)
9177 varCorrelation = fabs(sslot.numbers[0]);
9178
9179 if (varCorrelation > *indexCorrelation)
9180 *indexCorrelation = varCorrelation;
9181
9183 }
9184 }
9185
9187 }
9188
9190 baserel->relid,
9191 JOIN_INNER, NULL);
9192
9193 /*
9194 * Now calculate the minimum possible ranges we could match with if all of
9195 * the rows were in the perfect order in the table's heap.
9196 */
9198
9199 /*
9200 * Now estimate the number of ranges that we'll touch by using the
9201 * indexCorrelation from the stats. Careful not to divide by zero (note
9202 * we're using the absolute value of the correlation).
9203 */
9204 if (*indexCorrelation < 1.0e-10)
9206 else
9207 estimatedRanges = Min(minimalRanges / *indexCorrelation, indexRanges);
9208
9209 /* we expect to visit this portion of the table */
9211
9213
9214 *indexSelectivity = selec;
9215
9216 /*
9217 * Compute the index qual costs, much as in genericcostestimate, to add to
9218 * the index costs. We can disregard indexorderbys, since BRIN doesn't
9219 * support those.
9220 */
9222
9223 /*
9224 * Compute the startup cost as the cost to read the whole revmap
9225 * sequentially, including the cost to execute the index quals.
9226 */
9227 *indexStartupCost =
9228 spc_seq_page_cost * statsData.revmapNumPages * loop_count;
9229 *indexStartupCost += qual_arg_cost;
9230
9231 /*
9232 * To read a BRIN index there might be a bit of back and forth over
9233 * regular pages, as revmap might point to them out of sequential order;
9234 * calculate the total cost as reading the whole index in random order.
9235 */
9236 *indexTotalCost = *indexStartupCost +
9237 spc_random_page_cost * (numPages - statsData.revmapNumPages) * loop_count;
9238
9239 /*
9240 * Charge a small amount per range tuple which we expect to match to. This
9241 * is meant to reflect the costs of manipulating the bitmap. The BRIN scan
9242 * will set a bit for each page in the range when we find a matching
9243 * range, so we must multiply the charge by the number of pages in the
9244 * range.
9245 */
9246 *indexTotalCost += 0.1 * cpu_operator_cost * estimatedRanges *
9247 statsData.pagesPerRange;
9248
9249 *indexPages = index->pages;
9250}
int16 AttrNumber
Definition attnum.h:21
void brinGetStats(Relation index, BrinStatsData *stats)
Definition brin.c:1653
#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:1150
#define Max(x, y)
Definition c.h:1144
#define Assert(condition)
Definition c.h:1002
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition clausesel.c:100
double cpu_operator_cost
Definition costsize.c:135
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
#define NoLock
Definition lockdefs.h:34
void free_attstatsslot(AttStatsSlot *sslot)
Definition lsyscache.c:3652
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition lsyscache.c:3542
#define ATTSTATSSLOT_NUMBERS
Definition lsyscache.h:44
double Cost
Definition nodes.h:259
@ JOIN_INNER
Definition nodes.h:301
@ RTE_RELATION
#define planner_rt_fetch(rti, root)
Definition pathnodes.h:704
int16 attnum
#define lfirst_node(type, lc)
Definition pg_list.h:176
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define InvalidOid
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
List * get_quals_from_indexclauses(List *indexclauses)
Definition selfuncs.c:7331
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:7361
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:2057
IndexOptInfo * indexinfo
Definition pathnodes.h:2056
Definition pg_list.h:54
Definition type.h:97
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache3(SysCacheIdentifier cacheId, Datum key1, Datum key2, Datum key3)
Definition syscache.c:241

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 7713 of file selfuncs.c.

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

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, GenericCosts::numNonLeafPages, 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 8645 of file selfuncs.c.

8649{
8650 IndexOptInfo *index = path->indexinfo;
8653 double numPages = index->pages,
8654 numTuples = index->tuples;
8655 double numEntryPages,
8658 numEntries;
8659 GinQualCounts counts;
8660 bool matchPossible;
8661 bool fullIndexScan;
8662 double partialScale;
8663 double entryPagesFetched,
8666 double qual_op_cost,
8668 spc_random_page_cost,
8671 Relation indexRel;
8673 ListCell *lc;
8674 int i;
8675
8676 /*
8677 * Obtain statistical information from the meta page, if possible. Else
8678 * set ginStats to zeroes, and we'll cope below.
8679 */
8680 if (!index->hypothetical)
8681 {
8682 /* Lock should have already been obtained in plancat.c */
8683 indexRel = index_open(index->indexoid, NoLock);
8684 ginGetStats(indexRel, &ginStats);
8685 index_close(indexRel, NoLock);
8686 }
8687 else
8688 {
8689 memset(&ginStats, 0, sizeof(ginStats));
8690 }
8691
8692 /*
8693 * Assuming we got valid (nonzero) stats at all, nPendingPages can be
8694 * trusted, but the other fields are data as of the last VACUUM. We can
8695 * scale them up to account for growth since then, but that method only
8696 * goes so far; in the worst case, the stats might be for a completely
8697 * empty index, and scaling them will produce pretty bogus numbers.
8698 * Somewhat arbitrarily, set the cutoff for doing scaling at 4X growth; if
8699 * it's grown more than that, fall back to estimating things only from the
8700 * assumed-accurate index size. But we'll trust nPendingPages in any case
8701 * so long as it's not clearly insane, ie, more than the index size.
8702 */
8703 if (ginStats.nPendingPages < numPages)
8704 numPendingPages = ginStats.nPendingPages;
8705 else
8706 numPendingPages = 0;
8707
8708 if (numPages > 0 && ginStats.nTotalPages <= numPages &&
8709 ginStats.nTotalPages > numPages / 4 &&
8710 ginStats.nEntryPages > 0 && ginStats.nEntries > 0)
8711 {
8712 /*
8713 * OK, the stats seem close enough to sane to be trusted. But we
8714 * still need to scale them by the ratio numPages / nTotalPages to
8715 * account for growth since the last VACUUM.
8716 */
8717 double scale = numPages / ginStats.nTotalPages;
8718
8719 numEntryPages = ceil(ginStats.nEntryPages * scale);
8720 numDataPages = ceil(ginStats.nDataPages * scale);
8721 numEntries = ceil(ginStats.nEntries * scale);
8722 /* ensure we didn't round up too much */
8726 }
8727 else
8728 {
8729 /*
8730 * We might get here because it's a hypothetical index, or an index
8731 * created pre-9.1 and never vacuumed since upgrading (in which case
8732 * its stats would read as zeroes), or just because it's grown too
8733 * much since the last VACUUM for us to put our faith in scaling.
8734 *
8735 * Invent some plausible internal statistics based on the index page
8736 * count (and clamp that to at least 10 pages, just in case). We
8737 * estimate that 90% of the index is entry pages, and the rest is data
8738 * pages. Estimate 100 entries per entry page; this is rather bogus
8739 * since it'll depend on the size of the keys, but it's more robust
8740 * than trying to predict the number of entries per heap tuple.
8741 */
8742 numPages = Max(numPages, 10);
8746 }
8747
8748 /* In an empty index, numEntries could be zero. Avoid divide-by-zero */
8749 if (numEntries < 1)
8750 numEntries = 1;
8751
8752 /*
8753 * If the index is partial, AND the index predicate with the index-bound
8754 * quals to produce a more accurate idea of the number of rows covered by
8755 * the bound conditions.
8756 */
8758
8759 /* Estimate the fraction of main-table tuples that will be visited */
8760 *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
8761 index->rel->relid,
8762 JOIN_INNER,
8763 NULL);
8764
8765 /* fetch estimated page cost for tablespace containing index */
8766 get_tablespace_page_costs(index->reltablespace,
8767 &spc_random_page_cost,
8768 NULL);
8769
8770 /*
8771 * Generic assumption about index correlation: there isn't any.
8772 */
8773 *indexCorrelation = 0.0;
8774
8775 /*
8776 * Examine quals to estimate number of search entries & partial matches
8777 */
8778 memset(&counts, 0, sizeof(counts));
8779 counts.arrayScans = 1;
8780 matchPossible = true;
8781
8782 foreach(lc, path->indexclauses)
8783 {
8785 ListCell *lc2;
8786
8787 foreach(lc2, iclause->indexquals)
8788 {
8790 Expr *clause = rinfo->clause;
8791
8792 if (IsA(clause, OpExpr))
8793 {
8795 index,
8796 iclause->indexcol,
8797 (OpExpr *) clause,
8798 &counts);
8799 if (!matchPossible)
8800 break;
8801 }
8802 else if (IsA(clause, ScalarArrayOpExpr))
8803 {
8805 index,
8806 iclause->indexcol,
8807 (ScalarArrayOpExpr *) clause,
8808 numEntries,
8809 &counts);
8810 if (!matchPossible)
8811 break;
8812 }
8813 else
8814 {
8815 /* shouldn't be anything else for a GIN index */
8816 elog(ERROR, "unsupported GIN indexqual type: %d",
8817 (int) nodeTag(clause));
8818 }
8819 }
8820 }
8821
8822 /* Fall out if there were any provably-unsatisfiable quals */
8823 if (!matchPossible)
8824 {
8825 *indexStartupCost = 0;
8826 *indexTotalCost = 0;
8827 *indexSelectivity = 0;
8828 return;
8829 }
8830
8831 /*
8832 * If attribute has a full scan and at the same time doesn't have normal
8833 * scan, then we'll have to scan all non-null entries of that attribute.
8834 * Currently, we don't have per-attribute statistics for GIN. Thus, we
8835 * must assume the whole GIN index has to be scanned in this case.
8836 */
8837 fullIndexScan = false;
8838 for (i = 0; i < index->nkeycolumns; i++)
8839 {
8840 if (counts.attHasFullScan[i] && !counts.attHasNormalScan[i])
8841 {
8842 fullIndexScan = true;
8843 break;
8844 }
8845 }
8846
8847 if (fullIndexScan || indexQuals == NIL)
8848 {
8849 /*
8850 * Full index scan will be required. We treat this as if every key in
8851 * the index had been listed in the query; is that reasonable?
8852 */
8853 counts.partialEntries = 0;
8854 counts.exactEntries = numEntries;
8855 counts.searchEntries = numEntries;
8856 }
8857
8858 /* Will we have more than one iteration of a nestloop scan? */
8860
8861 /*
8862 * Compute cost to begin scan, first of all, pay attention to pending
8863 * list.
8864 */
8866
8867 /*
8868 * Estimate number of entry pages read. We need to do
8869 * counts.searchEntries searches. Use a power function as it should be,
8870 * but tuples on leaf pages usually is much greater. Here we include all
8871 * searches in entry tree, including search of first entry in partial
8872 * match algorithm
8873 */
8875
8876 /*
8877 * Add an estimate of entry pages read by partial match algorithm. It's a
8878 * scan over leaf pages in entry tree. We haven't any useful stats here,
8879 * so estimate it as proportion. Because counts.partialEntries is really
8880 * pretty bogus (see code above), it's possible that it is more than
8881 * numEntries; clamp the proportion to ensure sanity.
8882 */
8885
8887
8888 /*
8889 * Partial match algorithm reads all data pages before doing actual scan,
8890 * so it's a startup cost. Again, we haven't any useful stats here, so
8891 * estimate it as proportion.
8892 */
8894
8895 *indexStartupCost = 0;
8896 *indexTotalCost = 0;
8897
8898 /*
8899 * Add a CPU-cost component to represent the costs of initial entry btree
8900 * descent. We don't charge any I/O cost for touching upper btree levels,
8901 * since they tend to stay in cache, but we still have to do about log2(N)
8902 * comparisons to descend a btree of N leaf tuples. We charge one
8903 * cpu_operator_cost per comparison.
8904 *
8905 * If there are ScalarArrayOpExprs, charge this once per SA scan. The
8906 * ones after the first one are not startup cost so far as the overall
8907 * plan is concerned, so add them only to "total" cost.
8908 */
8909 if (numEntries > 1) /* avoid computing log(0) */
8910 {
8912 *indexStartupCost += descentCost * counts.searchEntries;
8913 *indexTotalCost += counts.arrayScans * descentCost * counts.searchEntries;
8914 }
8915
8916 /*
8917 * Add a cpu cost per entry-page fetched. This is not amortized over a
8918 * loop.
8919 */
8922
8923 /*
8924 * Add a cpu cost per data-page fetched. This is also not amortized over a
8925 * loop. Since those are the data pages from the partial match algorithm,
8926 * charge them as startup cost.
8927 */
8929
8930 /*
8931 * Since we add the startup cost to the total cost later on, remove the
8932 * initial arrayscan from the total.
8933 */
8934 *indexTotalCost += dataPagesFetched * (counts.arrayScans - 1) * DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost;
8935
8936 /*
8937 * Calculate cache effects if more than one scan due to nestloops or array
8938 * quals. The result is pro-rated per nestloop scan, but the array qual
8939 * factor shouldn't be pro-rated (compare genericcostestimate).
8940 */
8941 if (outer_scans > 1 || counts.arrayScans > 1)
8942 {
8953 }
8954
8955 /*
8956 * Here we use random page cost because logically-close pages could be far
8957 * apart on disk.
8958 */
8959 *indexStartupCost += (entryPagesFetched + dataPagesFetched) * spc_random_page_cost;
8960
8961 /*
8962 * Now compute the number of data pages fetched during the scan.
8963 *
8964 * We assume every entry to have the same number of items, and that there
8965 * is no overlap between them. (XXX: tsvector and array opclasses collect
8966 * statistics on the frequency of individual keys; it would be nice to use
8967 * those here.)
8968 */
8970
8971 /*
8972 * If there is a lot of overlap among the entries, in particular if one of
8973 * the entries is very frequent, the above calculation can grossly
8974 * under-estimate. As a simple cross-check, calculate a lower bound based
8975 * on the overall selectivity of the quals. At a minimum, we must read
8976 * one item pointer for each matching entry.
8977 *
8978 * The width of each item pointer varies, based on the level of
8979 * compression. We don't have statistics on that, but an average of
8980 * around 3 bytes per item is fairly typical.
8981 */
8982 dataPagesFetchedBySel = ceil(*indexSelectivity *
8983 (numTuples / (BLCKSZ / 3)));
8986
8987 /* Add one page cpu-cost to the startup cost */
8988 *indexStartupCost += DEFAULT_PAGE_CPU_MULTIPLIER * cpu_operator_cost * counts.searchEntries;
8989
8990 /*
8991 * Add once again a CPU-cost for those data pages, before amortizing for
8992 * cache.
8993 */
8995
8996 /* Account for cache effects, the same as above */
8997 if (outer_scans > 1 || counts.arrayScans > 1)
8998 {
9004 }
9005
9006 /* And apply random_page_cost as the cost per page */
9007 *indexTotalCost += *indexStartupCost +
9008 dataPagesFetched * spc_random_page_cost;
9009
9010 /*
9011 * Add on index qual eval costs, much as in genericcostestimate. We charge
9012 * cpu but we can disregard indexorderbys, since GIN doesn't support
9013 * those.
9014 */
9017
9018 *indexStartupCost += qual_arg_cost;
9019 *indexTotalCost += qual_arg_cost;
9020
9021 /*
9022 * Add a cpu cost per search entry, corresponding to the actual visited
9023 * entries.
9024 */
9025 *indexTotalCost += (counts.searchEntries * counts.arrayScans) * (qual_op_cost);
9026 /* Now add a cpu cost per tuple in the posting lists / trees */
9027 *indexTotalCost += (numTuples * *indexSelectivity) * (cpu_index_tuple_cost);
9029}
uint32 BlockNumber
Definition block.h:31
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition costsize.c:897
double cpu_index_tuple_cost
Definition costsize.c:134
void ginGetStats(Relation index, GinStatsData *stats)
Definition ginutil.c:578
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:8529
static bool gincost_opexpr(PlannerInfo *root, IndexOptInfo *index, int indexcol, OpExpr *clause, GinQualCounts *counts)
Definition selfuncs.c:8479
bool attHasNormalScan[INDEX_MAX_KEYS]
Definition selfuncs.c:8352
double exactEntries
Definition selfuncs.c:8354
double arrayScans
Definition selfuncs.c:8356
double partialEntries
Definition selfuncs.c:8353
bool attHasFullScan[INDEX_MAX_KEYS]
Definition selfuncs.c:8351
double searchEntries
Definition selfuncs.c:8355

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 8230 of file selfuncs.c.

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

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 8185 of file selfuncs.c.

8189{
8190 GenericCosts costs = {0};
8191
8192 /* As in btcostestimate, count only the metapage as non-leaf */
8193 costs.numNonLeafPages = 1;
8194
8195 genericcostestimate(root, path, loop_count, &costs);
8196
8197 /*
8198 * A hash index has no descent costs as such, since the index AM can go
8199 * directly to the target bucket after computing the hash value. There
8200 * are a couple of other hash-specific costs that we could conceivably add
8201 * here, though:
8202 *
8203 * Ideally we'd charge spc_random_page_cost for each page in the target
8204 * bucket, not just the numIndexPages pages that genericcostestimate
8205 * thought we'd visit. However in most cases we don't know which bucket
8206 * that will be. There's no point in considering the average bucket size
8207 * because the hash AM makes sure that's always one page.
8208 *
8209 * Likewise, we could consider charging some CPU for each index tuple in
8210 * the bucket, if we knew how many there were. But the per-tuple cost is
8211 * just a hash value comparison, not a general datatype-dependent
8212 * comparison, so any such charge ought to be quite a bit less than
8213 * cpu_operator_cost; which makes it probably not worth worrying about.
8214 *
8215 * A bigger issue is that chance hash-value collisions will result in
8216 * wasted probes into the heap. We don't currently attempt to model this
8217 * cost on the grounds that it's rare, but maybe it's not rare enough.
8218 * (Any fix for this ought to consider the generic lossy-operator problem,
8219 * though; it's not entirely hash-specific.)
8220 */
8221
8222 *indexStartupCost = costs.indexStartupCost;
8223 *indexTotalCost = costs.indexTotalCost;
8224 *indexSelectivity = costs.indexSelectivity;
8225 *indexCorrelation = costs.indexCorrelation;
8226 *indexPages = costs.numIndexPages;
8227}

References fb(), genericcostestimate(), GenericCosts::indexCorrelation, GenericCosts::indexSelectivity, GenericCosts::indexStartupCost, GenericCosts::indexTotalCost, GenericCosts::numIndexPages, GenericCosts::numNonLeafPages, 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 8287 of file selfuncs.c.

8291{
8292 IndexOptInfo *index = path->indexinfo;
8293 GenericCosts costs = {0};
8295
8296 /* As in btcostestimate, count only the metapage as non-leaf */
8297 costs.numNonLeafPages = 1;
8298
8299 genericcostestimate(root, path, loop_count, &costs);
8300
8301 /*
8302 * We model index descent costs similarly to those for btree, but to do
8303 * that we first need an idea of the tree height. We somewhat arbitrarily
8304 * assume that the fanout is 100, meaning the tree height is at most
8305 * log100(index->pages).
8306 *
8307 * Although this computation isn't really expensive enough to require
8308 * caching, we might as well use index->tree_height to cache it.
8309 */
8310 if (index->tree_height < 0) /* unknown? */
8311 {
8312 if (index->pages > 1) /* avoid computing log(0) */
8313 index->tree_height = (int) (log(index->pages) / log(100.0));
8314 else
8315 index->tree_height = 0;
8316 }
8317
8318 /*
8319 * Add a CPU-cost component to represent the costs of initial descent. We
8320 * just use log(N) here not log2(N) since the branching factor isn't
8321 * necessarily two anyway. As for btree, charge once per SA scan.
8322 */
8323 if (index->tuples > 1) /* avoid computing log(0) */
8324 {
8327 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8328 }
8329
8330 /*
8331 * Likewise add a per-page charge, calculated the same as for btrees.
8332 */
8335 costs.indexTotalCost += costs.num_sa_scans * descentCost;
8336
8337 *indexStartupCost = costs.indexStartupCost;
8338 *indexTotalCost = costs.indexTotalCost;
8339 *indexSelectivity = costs.indexSelectivity;
8340 *indexCorrelation = costs.indexCorrelation;
8341 *indexPages = costs.numIndexPages;
8342}

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, GenericCosts::numNonLeafPages, and root.

Referenced by spghandler().