|
Chapter 44. Index Cost Estimation Functions
Every index access method must provide a cost estimation function for use by the planner/optimizer. The procedure OID of this function is given in the amcostestimate field of the access method's pg_am entry.
The amcostestimate function is given a list of WHERE clauses that have been determined to be usable with the index. It must return estimates of the cost of accessing the index and the selectivity of the WHERE clauses (that is, the fraction of main-table tuples that will be retrieved during the index scan). For simple cases, nearly all the work of the cost estimator can be done by calling standard routines in the optimizer; the point of having an amcostestimate function is to allow index access methods to provide index-type-specific knowledge, in case it is possible to improve on the standard estimates. Each amcostestimate function must have the signature: void amcostestimate (Query *root, RelOptInfo *rel, IndexOptInfo *index, List *indexQuals, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity);The first four parameters are inputs:
The last three parameters are pass-by-reference outputs:
Note that cost estimate functions must be written in C, not in SQL or any available procedural language, because they must access internal data structures of the planner/optimizer. The index access costs should be computed in the units used by src/backend/optimizer/path/costsize.c: a sequential disk block fetch has cost 1.0, a nonsequential fetch has cost random_page_cost, and the cost of processing one index tuple should usually be taken as cpu_index_tuple_cost (which is a user-adjustable optimizer parameter). In addition, an appropriate multiple of cpu_operator_cost should be charged for any comparison operators invoked during index processing (especially evaluation of the indexQuals themselves). The access costs should include all disk and CPU costs associated with scanning the index itself, but NOT the costs of retrieving or processing the main-table tuples that are identified by the index. The "startup cost" is the part of the total scan cost that must be expended before we can begin to fetch the first tuple. For most indexes this can be taken as zero, but an index type with a high startup cost might want to set it nonzero. The indexSelectivity should be set to the estimated fraction of the main table tuples that will be retrieved during the index scan. In the case of a lossy index, this will typically be higher than the fraction of tuples that actually pass the given qual conditions. Cost Estimation A typical cost estimator will proceed as follows:
Examples of cost estimator functions can be found in src/backend/utils/adt/selfuncs.c. By convention, the pg_proc entry for an amcostestimate function should show prorettype = 0 pronargs = 7 proargtypes = 0 0 0 0 0 0 0We use zero ("opaque") for all the arguments since none of them have types that are known in pg_type. |
|||||||||||||||||
With any suggestions or questions please feel free to contact us |