PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
costsize.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/amapi.h"
#include "access/htup_details.h"
#include "access/tsmapi.h"
#include "executor/executor.h"
#include "executor/nodeHash.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
#include "utils/spccache.h"
#include "utils/tuplesort.h"
Include dependency graph for costsize.c:

Go to the source code of this file.

Data Structures

struct  cost_qual_eval_context
 

Macros

#define LOG2(x)   (log(x) / 0.693147180559945)
 

Functions

static Listextract_nonindex_conditions (List *qual_clauses, List *indexquals)
 
static MergeScanSelCachecached_scansel (PlannerInfo *root, RestrictInfo *rinfo, PathKey *pathkey)
 
static void cost_rescan (PlannerInfo *root, Path *path, Cost *rescan_startup_cost, Cost *rescan_total_cost)
 
static bool cost_qual_eval_walker (Node *node, cost_qual_eval_context *context)
 
static void get_restriction_qual_cost (PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
 
static bool has_indexed_join_quals (NestPath *joinpath)
 
static double approx_tuple_count (PlannerInfo *root, JoinPath *path, List *quals)
 
static double calc_joinrel_size_estimate (PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, double outer_rows, double inner_rows, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static Selectivity get_foreign_key_join_selectivity (PlannerInfo *root, Relids outer_relids, Relids inner_relids, SpecialJoinInfo *sjinfo, List **restrictlist)
 
static void set_rel_width (PlannerInfo *root, RelOptInfo *rel)
 
static double relation_byte_size (double tuples, int width)
 
static double page_size (double tuples, int width)
 
static double get_parallel_divisor (Path *path)
 
double clamp_row_est (double nrows)
 
void cost_seqscan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_samplescan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_gather (GatherPath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, double *rows)
 
void cost_gather_merge (GatherMergePath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, Cost input_startup_cost, Cost input_total_cost, double *rows)
 
void cost_index (IndexPath *path, PlannerInfo *root, double loop_count, bool partial_path)
 
double index_pages_fetched (double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
 
static double get_indexpath_pages (Path *bitmapqual)
 
void cost_bitmap_heap_scan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
 
void cost_bitmap_tree_node (Path *path, Cost *cost, Selectivity *selec)
 
void cost_bitmap_and_node (BitmapAndPath *path, PlannerInfo *root)
 
void cost_bitmap_or_node (BitmapOrPath *path, PlannerInfo *root)
 
void cost_tidscan (Path *path, PlannerInfo *root, RelOptInfo *baserel, List *tidquals, ParamPathInfo *param_info)
 
void cost_subqueryscan (SubqueryScanPath *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_functionscan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_tablefuncscan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_valuesscan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_ctescan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_namedtuplestorescan (Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
 
void cost_recursive_union (Path *runion, Path *nrterm, Path *rterm)
 
void cost_sort (Path *path, PlannerInfo *root, List *pathkeys, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
 
void cost_merge_append (Path *path, PlannerInfo *root, List *pathkeys, int n_streams, Cost input_startup_cost, Cost input_total_cost, double tuples)
 
void cost_material (Path *path, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
 
void cost_agg (Path *path, PlannerInfo *root, AggStrategy aggstrategy, const AggClauseCosts *aggcosts, int numGroupCols, double numGroups, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
 
void cost_windowagg (Path *path, PlannerInfo *root, List *windowFuncs, int numPartCols, int numOrderCols, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
 
void cost_group (Path *path, PlannerInfo *root, int numGroupCols, double numGroups, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
 
void initial_cost_nestloop (PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
 
void final_cost_nestloop (PlannerInfo *root, NestPath *path, JoinCostWorkspace *workspace, JoinPathExtraData *extra)
 
void initial_cost_mergejoin (PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *mergeclauses, Path *outer_path, Path *inner_path, List *outersortkeys, List *innersortkeys, JoinPathExtraData *extra)
 
void final_cost_mergejoin (PlannerInfo *root, MergePath *path, JoinCostWorkspace *workspace, JoinPathExtraData *extra)
 
void initial_cost_hashjoin (PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *hashclauses, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
 
void final_cost_hashjoin (PlannerInfo *root, HashPath *path, JoinCostWorkspace *workspace, JoinPathExtraData *extra)
 
void cost_subplan (PlannerInfo *root, SubPlan *subplan, Plan *plan)
 
void cost_qual_eval (QualCost *cost, List *quals, PlannerInfo *root)
 
void cost_qual_eval_node (QualCost *cost, Node *qual, PlannerInfo *root)
 
void compute_semi_anti_join_factors (PlannerInfo *root, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist, SemiAntiJoinFactors *semifactors)
 
void set_baserel_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
double get_parameterized_baserel_size (PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
 
void set_joinrel_size_estimates (PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
double get_parameterized_joinrel_size (PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
 
void set_subquery_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
void set_function_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
void set_tablefunc_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
void set_values_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
void set_cte_size_estimates (PlannerInfo *root, RelOptInfo *rel, double cte_rows)
 
void set_namedtuplestore_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
void set_foreign_size_estimates (PlannerInfo *root, RelOptInfo *rel)
 
PathTargetset_pathtarget_cost_width (PlannerInfo *root, PathTarget *target)
 
double compute_bitmap_pages (PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
 

Variables

double seq_page_cost = DEFAULT_SEQ_PAGE_COST
 
double random_page_cost = DEFAULT_RANDOM_PAGE_COST
 
double cpu_tuple_cost = DEFAULT_CPU_TUPLE_COST
 
double cpu_index_tuple_cost = DEFAULT_CPU_INDEX_TUPLE_COST
 
double cpu_operator_cost = DEFAULT_CPU_OPERATOR_COST
 
double parallel_tuple_cost = DEFAULT_PARALLEL_TUPLE_COST
 
double parallel_setup_cost = DEFAULT_PARALLEL_SETUP_COST
 
int effective_cache_size = DEFAULT_EFFECTIVE_CACHE_SIZE
 
Cost disable_cost = 1.0e10
 
int max_parallel_workers_per_gather = 2
 
bool enable_seqscan = true
 
bool enable_indexscan = true
 
bool enable_indexonlyscan = true
 
bool enable_bitmapscan = true
 
bool enable_tidscan = true
 
bool enable_sort = true
 
bool enable_hashagg = true
 
bool enable_nestloop = true
 
bool enable_material = true
 
bool enable_mergejoin = true
 
bool enable_hashjoin = true
 
bool enable_gathermerge = true
 

Macro Definition Documentation

#define LOG2 (   x)    (log(x) / 0.693147180559945)

Definition at line 101 of file costsize.c.

Referenced by cost_gather_merge(), cost_merge_append(), and cost_sort().

Function Documentation

static double approx_tuple_count ( PlannerInfo root,
JoinPath path,
List quals 
)
static

Definition at line 3956 of file costsize.c.

References clamp_row_est(), clause_selectivity(), SpecialJoinInfo::delay_upper_joins, JoinPath::innerjoinpath, JOIN_INNER, SpecialJoinInfo::jointype, lfirst, SpecialJoinInfo::lhs_strict, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, JoinPath::outerjoinpath, Path::parent, RelOptInfo::relids, Path::rows, SpecialJoinInfo::semi_can_btree, SpecialJoinInfo::semi_can_hash, SpecialJoinInfo::semi_operators, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, SpecialJoinInfo::syn_righthand, T_SpecialJoinInfo, and SpecialJoinInfo::type.

Referenced by final_cost_hashjoin(), and final_cost_mergejoin().

3957 {
3958  double tuples;
3959  double outer_tuples = path->outerjoinpath->rows;
3960  double inner_tuples = path->innerjoinpath->rows;
3961  SpecialJoinInfo sjinfo;
3962  Selectivity selec = 1.0;
3963  ListCell *l;
3964 
3965  /*
3966  * Make up a SpecialJoinInfo for JOIN_INNER semantics.
3967  */
3968  sjinfo.type = T_SpecialJoinInfo;
3969  sjinfo.min_lefthand = path->outerjoinpath->parent->relids;
3970  sjinfo.min_righthand = path->innerjoinpath->parent->relids;
3971  sjinfo.syn_lefthand = path->outerjoinpath->parent->relids;
3972  sjinfo.syn_righthand = path->innerjoinpath->parent->relids;
3973  sjinfo.jointype = JOIN_INNER;
3974  /* we don't bother trying to make the remaining fields valid */
3975  sjinfo.lhs_strict = false;
3976  sjinfo.delay_upper_joins = false;
3977  sjinfo.semi_can_btree = false;
3978  sjinfo.semi_can_hash = false;
3979  sjinfo.semi_operators = NIL;
3980  sjinfo.semi_rhs_exprs = NIL;
3981 
3982  /* Get the approximate selectivity */
3983  foreach(l, quals)
3984  {
3985  Node *qual = (Node *) lfirst(l);
3986 
3987  /* Note that clause_selectivity will be able to cache its result */
3988  selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo);
3989  }
3990 
3991  /* Apply it to the input relation sizes */
3992  tuples = selec * outer_tuples * inner_tuples;
3993 
3994  return clamp_row_est(tuples);
3995 }
#define NIL
Definition: pg_list.h:69
bool semi_can_btree
Definition: relation.h:1925
Relids min_righthand
Definition: relation.h:1918
Path * innerjoinpath
Definition: relation.h:1297
NodeTag type
Definition: relation.h:1916
Definition: nodes.h:509
double Selectivity
Definition: nodes.h:639
Relids syn_lefthand
Definition: relation.h:1919
Relids syn_righthand
Definition: relation.h:1920
List * semi_rhs_exprs
Definition: relation.h:1928
bool semi_can_hash
Definition: relation.h:1926
RelOptInfo * parent
Definition: relation.h:954
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:572
Relids relids
Definition: relation.h:525
Path * outerjoinpath
Definition: relation.h:1296
bool delay_upper_joins
Definition: relation.h:1923
#define lfirst(lc)
Definition: pg_list.h:106
double rows
Definition: relation.h:964
JoinType jointype
Definition: relation.h:1921
List * semi_operators
Definition: relation.h:1927
double clamp_row_est(double nrows)
Definition: costsize.c:173
Relids min_lefthand
Definition: relation.h:1917
static MergeScanSelCache * cached_scansel ( PlannerInfo root,
RestrictInfo rinfo,
PathKey pathkey 
)
static

Definition at line 2843 of file costsize.c.

References RestrictInfo::clause, MergeScanSelCache::collation, EquivalenceClass::ec_collation, lappend(), MergeScanSelCache::leftendsel, MergeScanSelCache::leftstartsel, lfirst, MemoryContextSwitchTo(), mergejoinscansel(), MergeScanSelCache::nulls_first, MergeScanSelCache::opfamily, palloc(), PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, PlannerInfo::planner_cxt, MergeScanSelCache::rightendsel, MergeScanSelCache::rightstartsel, RestrictInfo::scansel_cache, and MergeScanSelCache::strategy.

Referenced by initial_cost_mergejoin().

2844 {
2845  MergeScanSelCache *cache;
2846  ListCell *lc;
2847  Selectivity leftstartsel,
2848  leftendsel,
2849  rightstartsel,
2850  rightendsel;
2851  MemoryContext oldcontext;
2852 
2853  /* Do we have this result already? */
2854  foreach(lc, rinfo->scansel_cache)
2855  {
2856  cache = (MergeScanSelCache *) lfirst(lc);
2857  if (cache->opfamily == pathkey->pk_opfamily &&
2858  cache->collation == pathkey->pk_eclass->ec_collation &&
2859  cache->strategy == pathkey->pk_strategy &&
2860  cache->nulls_first == pathkey->pk_nulls_first)
2861  return cache;
2862  }
2863 
2864  /* Nope, do the computation */
2865  mergejoinscansel(root,
2866  (Node *) rinfo->clause,
2867  pathkey->pk_opfamily,
2868  pathkey->pk_strategy,
2869  pathkey->pk_nulls_first,
2870  &leftstartsel,
2871  &leftendsel,
2872  &rightstartsel,
2873  &rightendsel);
2874 
2875  /* Cache the result in suitably long-lived workspace */
2876  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2877 
2878  cache = (MergeScanSelCache *) palloc(sizeof(MergeScanSelCache));
2879  cache->opfamily = pathkey->pk_opfamily;
2880  cache->collation = pathkey->pk_eclass->ec_collation;
2881  cache->strategy = pathkey->pk_strategy;
2882  cache->nulls_first = pathkey->pk_nulls_first;
2883  cache->leftstartsel = leftstartsel;
2884  cache->leftendsel = leftendsel;
2885  cache->rightstartsel = rightstartsel;
2886  cache->rightendsel = rightendsel;
2887 
2888  rinfo->scansel_cache = lappend(rinfo->scansel_cache, cache);
2889 
2890  MemoryContextSwitchTo(oldcontext);
2891 
2892  return cache;
2893 }
Selectivity leftendsel
Definition: relation.h:1828
void mergejoinscansel(PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
Definition: selfuncs.c:2845
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:509
double Selectivity
Definition: nodes.h:639
int pk_strategy
Definition: relation.h:853
bool pk_nulls_first
Definition: relation.h:854
Selectivity rightstartsel
Definition: relation.h:1829
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1747
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:851
Oid pk_opfamily
Definition: relation.h:852
void * palloc(Size size)
Definition: mcxt.c:849
MemoryContext planner_cxt
Definition: relation.h:287
Selectivity rightendsel
Definition: relation.h:1830
List * scansel_cache
Definition: relation.h:1799
Selectivity leftstartsel
Definition: relation.h:1827
static double calc_joinrel_size_estimate ( PlannerInfo root,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
double  outer_rows,
double  inner_rows,
SpecialJoinInfo sjinfo,
List restrictlist 
)
static

Definition at line 4163 of file costsize.c.

References clamp_row_est(), clauselist_selectivity(), elog, ERROR, get_foreign_key_join_selectivity(), IS_OUTER_JOIN, RestrictInfo::is_pushed_down, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, lappend(), lfirst_node, list_free(), NIL, and RelOptInfo::relids.

Referenced by get_parameterized_joinrel_size(), and set_joinrel_size_estimates().

4170 {
4171  /* This apparently-useless variable dodges a compiler bug in VS2013: */
4172  List *restrictlist = restrictlist_in;
4173  JoinType jointype = sjinfo->jointype;
4174  Selectivity fkselec;
4175  Selectivity jselec;
4176  Selectivity pselec;
4177  double nrows;
4178 
4179  /*
4180  * Compute joinclause selectivity. Note that we are only considering
4181  * clauses that become restriction clauses at this join level; we are not
4182  * double-counting them because they were not considered in estimating the
4183  * sizes of the component rels.
4184  *
4185  * First, see whether any of the joinclauses can be matched to known FK
4186  * constraints. If so, drop those clauses from the restrictlist, and
4187  * instead estimate their selectivity using FK semantics. (We do this
4188  * without regard to whether said clauses are local or "pushed down".
4189  * Probably, an FK-matching clause could never be seen as pushed down at
4190  * an outer join, since it would be strict and hence would be grounds for
4191  * join strength reduction.) fkselec gets the net selectivity for
4192  * FK-matching clauses, or 1.0 if there are none.
4193  */
4194  fkselec = get_foreign_key_join_selectivity(root,
4195  outer_rel->relids,
4196  inner_rel->relids,
4197  sjinfo,
4198  &restrictlist);
4199 
4200  /*
4201  * For an outer join, we have to distinguish the selectivity of the join's
4202  * own clauses (JOIN/ON conditions) from any clauses that were "pushed
4203  * down". For inner joins we just count them all as joinclauses.
4204  */
4205  if (IS_OUTER_JOIN(jointype))
4206  {
4207  List *joinquals = NIL;
4208  List *pushedquals = NIL;
4209  ListCell *l;
4210 
4211  /* Grovel through the clauses to separate into two lists */
4212  foreach(l, restrictlist)
4213  {
4214  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
4215 
4216  if (rinfo->is_pushed_down)
4217  pushedquals = lappend(pushedquals, rinfo);
4218  else
4219  joinquals = lappend(joinquals, rinfo);
4220  }
4221 
4222  /* Get the separate selectivities */
4223  jselec = clauselist_selectivity(root,
4224  joinquals,
4225  0,
4226  jointype,
4227  sjinfo);
4228  pselec = clauselist_selectivity(root,
4229  pushedquals,
4230  0,
4231  jointype,
4232  sjinfo);
4233 
4234  /* Avoid leaking a lot of ListCells */
4235  list_free(joinquals);
4236  list_free(pushedquals);
4237  }
4238  else
4239  {
4240  jselec = clauselist_selectivity(root,
4241  restrictlist,
4242  0,
4243  jointype,
4244  sjinfo);
4245  pselec = 0.0; /* not used, keep compiler quiet */
4246  }
4247 
4248  /*
4249  * Basically, we multiply size of Cartesian product by selectivity.
4250  *
4251  * If we are doing an outer join, take that into account: the joinqual
4252  * selectivity has to be clamped using the knowledge that the output must
4253  * be at least as large as the non-nullable input. However, any
4254  * pushed-down quals are applied after the outer join, so their
4255  * selectivity applies fully.
4256  *
4257  * For JOIN_SEMI and JOIN_ANTI, the selectivity is defined as the fraction
4258  * of LHS rows that have matches, and we apply that straightforwardly.
4259  */
4260  switch (jointype)
4261  {
4262  case JOIN_INNER:
4263  nrows = outer_rows * inner_rows * fkselec * jselec;
4264  /* pselec not used */
4265  break;
4266  case JOIN_LEFT:
4267  nrows = outer_rows * inner_rows * fkselec * jselec;
4268  if (nrows < outer_rows)
4269  nrows = outer_rows;
4270  nrows *= pselec;
4271  break;
4272  case JOIN_FULL:
4273  nrows = outer_rows * inner_rows * fkselec * jselec;
4274  if (nrows < outer_rows)
4275  nrows = outer_rows;
4276  if (nrows < inner_rows)
4277  nrows = inner_rows;
4278  nrows *= pselec;
4279  break;
4280  case JOIN_SEMI:
4281  nrows = outer_rows * fkselec * jselec;
4282  /* pselec not used */
4283  break;
4284  case JOIN_ANTI:
4285  nrows = outer_rows * (1.0 - fkselec * jselec);
4286  nrows *= pselec;
4287  break;
4288  default:
4289  /* other values not expected here */
4290  elog(ERROR, "unrecognized join type: %d", (int) jointype);
4291  nrows = 0; /* keep compiler quiet */
4292  break;
4293  }
4294 
4295  return clamp_row_est(nrows);
4296 }
#define NIL
Definition: pg_list.h:69
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:722
double Selectivity
Definition: nodes.h:639
JoinType
Definition: nodes.h:673
static Selectivity get_foreign_key_join_selectivity(PlannerInfo *root, Relids outer_relids, Relids inner_relids, SpecialJoinInfo *sjinfo, List **restrictlist)
Definition: costsize.c:4314
#define ERROR
Definition: elog.h:43
#define lfirst_node(type, lc)
Definition: pg_list.h:109
Relids relids
Definition: relation.h:525
List * lappend(List *list, void *datum)
Definition: list.c:128
bool is_pushed_down
Definition: relation.h:1749
JoinType jointype
Definition: relation.h:1921
void list_free(List *list)
Definition: list.c:1133
#define elog
Definition: elog.h:219
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:99
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: pg_list.h:45
double clamp_row_est ( double  nrows)

Definition at line 173 of file costsize.c.

References rint().

Referenced by approx_tuple_count(), bernoulli_samplescangetsamplesize(), calc_joinrel_size_estimate(), compute_bitmap_pages(), cost_bitmap_heap_scan(), cost_index(), cost_seqscan(), cost_subplan(), create_bitmap_subplan(), create_limit_path(), estimate_hash_bucketsize(), estimate_num_groups(), estimate_path_cost_size(), estimate_size(), expression_returns_set_rows(), final_cost_hashjoin(), final_cost_mergejoin(), final_cost_nestloop(), get_parameterized_baserel_size(), get_variable_numdistinct(), initial_cost_mergejoin(), set_baserel_size_estimates(), set_foreign_size(), system_rows_samplescangetsamplesize(), system_samplescangetsamplesize(), and system_time_samplescangetsamplesize().

174 {
175  /*
176  * Force estimate to be at least one row, to make explain output look
177  * better and to avoid possible divide-by-zero when interpolating costs.
178  * Make it an integer, too.
179  */
180  if (nrows <= 1.0)
181  nrows = 1.0;
182  else
183  nrows = rint(nrows);
184 
185  return nrows;
186 }
double rint(double x)
Definition: rint.c:22
double compute_bitmap_pages ( PlannerInfo root,
RelOptInfo baserel,
Path bitmapqual,
int  loop_count,
Cost cost,
double *  tuple 
)

Definition at line 5102 of file costsize.c.

References clamp_row_est(), cost_bitmap_tree_node(), get_indexpath_pages(), index_pages_fetched(), RelOptInfo::pages, T, and RelOptInfo::tuples.

Referenced by cost_bitmap_heap_scan(), and create_partial_bitmap_paths().

5104 {
5105  Cost indexTotalCost;
5106  Selectivity indexSelectivity;
5107  double T;
5108  double pages_fetched;
5109  double tuples_fetched;
5110 
5111  /*
5112  * Fetch total cost of obtaining the bitmap, as well as its total
5113  * selectivity.
5114  */
5115  cost_bitmap_tree_node(bitmapqual, &indexTotalCost, &indexSelectivity);
5116 
5117  /*
5118  * Estimate number of main-table pages fetched.
5119  */
5120  tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
5121 
5122  T = (baserel->pages > 1) ? (double) baserel->pages : 1.0;
5123 
5124  if (loop_count > 1)
5125  {
5126  /*
5127  * For repeated bitmap scans, scale up the number of tuples fetched in
5128  * the Mackert and Lohman formula by the number of scans, so that we
5129  * estimate the number of pages fetched by all the scans. Then
5130  * pro-rate for one scan.
5131  */
5132  pages_fetched = index_pages_fetched(tuples_fetched * loop_count,
5133  baserel->pages,
5134  get_indexpath_pages(bitmapqual),
5135  root);
5136  pages_fetched /= loop_count;
5137  }
5138  else
5139  {
5140  /*
5141  * For a single scan, the number of heap pages that need to be fetched
5142  * is the same as the Mackert and Lohman formula for the case T <= b
5143  * (ie, no re-reads needed).
5144  */
5145  pages_fetched =
5146  (2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
5147  }
5148 
5149  if (pages_fetched >= T)
5150  pages_fetched = T;
5151  else
5152  pages_fetched = ceil(pages_fetched);
5153 
5154  if (cost)
5155  *cost = indexTotalCost;
5156  if (tuple)
5157  *tuple = tuples_fetched;
5158 
5159  return pages_fetched;
5160 }
double tuples
Definition: relation.h:565
double Selectivity
Definition: nodes.h:639
static const uint32 T[65]
Definition: md5.c:101
BlockNumber pages
Definition: relation.h:564
static double get_indexpath_pages(Path *bitmapqual)
Definition: costsize.c:878
double clamp_row_est(double nrows)
Definition: costsize.c:173
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:813
double Cost
Definition: nodes.h:640
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1029
void compute_semi_anti_join_factors ( PlannerInfo root,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
SpecialJoinInfo sjinfo,
List restrictlist,
SemiAntiJoinFactors semifactors 
)

Definition at line 3755 of file costsize.c.

References clauselist_selectivity(), SpecialJoinInfo::delay_upper_joins, IS_OUTER_JOIN, RestrictInfo::is_pushed_down, JOIN_ANTI, JOIN_INNER, JOIN_SEMI, SpecialJoinInfo::jointype, lappend(), lfirst_node, SpecialJoinInfo::lhs_strict, list_free(), SemiAntiJoinFactors::match_count, Max, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, SemiAntiJoinFactors::outer_match_frac, RelOptInfo::relids, RelOptInfo::rows, SpecialJoinInfo::semi_can_btree, SpecialJoinInfo::semi_can_hash, SpecialJoinInfo::semi_operators, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, SpecialJoinInfo::syn_righthand, T_SpecialJoinInfo, and SpecialJoinInfo::type.

Referenced by add_paths_to_joinrel().

3762 {
3763  Selectivity jselec;
3764  Selectivity nselec;
3765  Selectivity avgmatch;
3766  SpecialJoinInfo norm_sjinfo;
3767  List *joinquals;
3768  ListCell *l;
3769 
3770  /*
3771  * In an ANTI join, we must ignore clauses that are "pushed down", since
3772  * those won't affect the match logic. In a SEMI join, we do not
3773  * distinguish joinquals from "pushed down" quals, so just use the whole
3774  * restrictinfo list. For other outer join types, we should consider only
3775  * non-pushed-down quals, so that this devolves to an IS_OUTER_JOIN check.
3776  */
3777  if (IS_OUTER_JOIN(jointype))
3778  {
3779  joinquals = NIL;
3780  foreach(l, restrictlist)
3781  {
3782  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3783 
3784  if (!rinfo->is_pushed_down)
3785  joinquals = lappend(joinquals, rinfo);
3786  }
3787  }
3788  else
3789  joinquals = restrictlist;
3790 
3791  /*
3792  * Get the JOIN_SEMI or JOIN_ANTI selectivity of the join clauses.
3793  */
3794  jselec = clauselist_selectivity(root,
3795  joinquals,
3796  0,
3797  (jointype == JOIN_ANTI) ? JOIN_ANTI : JOIN_SEMI,
3798  sjinfo);
3799 
3800  /*
3801  * Also get the normal inner-join selectivity of the join clauses.
3802  */
3803  norm_sjinfo.type = T_SpecialJoinInfo;
3804  norm_sjinfo.min_lefthand = outerrel->relids;
3805  norm_sjinfo.min_righthand = innerrel->relids;
3806  norm_sjinfo.syn_lefthand = outerrel->relids;
3807  norm_sjinfo.syn_righthand = innerrel->relids;
3808  norm_sjinfo.jointype = JOIN_INNER;
3809  /* we don't bother trying to make the remaining fields valid */
3810  norm_sjinfo.lhs_strict = false;
3811  norm_sjinfo.delay_upper_joins = false;
3812  norm_sjinfo.semi_can_btree = false;
3813  norm_sjinfo.semi_can_hash = false;
3814  norm_sjinfo.semi_operators = NIL;
3815  norm_sjinfo.semi_rhs_exprs = NIL;
3816 
3817  nselec = clauselist_selectivity(root,
3818  joinquals,
3819  0,
3820  JOIN_INNER,
3821  &norm_sjinfo);
3822 
3823  /* Avoid leaking a lot of ListCells */
3824  if (IS_OUTER_JOIN(jointype))
3825  list_free(joinquals);
3826 
3827  /*
3828  * jselec can be interpreted as the fraction of outer-rel rows that have
3829  * any matches (this is true for both SEMI and ANTI cases). And nselec is
3830  * the fraction of the Cartesian product that matches. So, the average
3831  * number of matches for each outer-rel row that has at least one match is
3832  * nselec * inner_rows / jselec.
3833  *
3834  * Note: it is correct to use the inner rel's "rows" count here, even
3835  * though we might later be considering a parameterized inner path with
3836  * fewer rows. This is because we have included all the join clauses in
3837  * the selectivity estimate.
3838  */
3839  if (jselec > 0) /* protect against zero divide */
3840  {
3841  avgmatch = nselec * innerrel->rows / jselec;
3842  /* Clamp to sane range */
3843  avgmatch = Max(1.0, avgmatch);
3844  }
3845  else
3846  avgmatch = 1.0;
3847 
3848  semifactors->outer_match_frac = jselec;
3849  semifactors->match_count = avgmatch;
3850 }
#define NIL
Definition: pg_list.h:69
bool semi_can_btree
Definition: relation.h:1925
Relids min_righthand
Definition: relation.h:1918
Selectivity outer_match_frac
Definition: relation.h:2161
NodeTag type
Definition: relation.h:1916
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:722
double Selectivity
Definition: nodes.h:639
Relids syn_lefthand
Definition: relation.h:1919
Relids syn_righthand
Definition: relation.h:1920
List * semi_rhs_exprs
Definition: relation.h:1928
bool semi_can_hash
Definition: relation.h:1926
#define lfirst_node(type, lc)
Definition: pg_list.h:109
Relids relids
Definition: relation.h:525
List * lappend(List *list, void *datum)
Definition: list.c:128
bool delay_upper_joins
Definition: relation.h:1923
double rows
Definition: relation.h:528
bool is_pushed_down
Definition: relation.h:1749
#define Max(x, y)
Definition: c.h:800
JoinType jointype
Definition: relation.h:1921
Selectivity match_count
Definition: relation.h:2162
List * semi_operators
Definition: relation.h:1927
void list_free(List *list)
Definition: list.c:1133
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:99
Definition: pg_list.h:45
Relids min_lefthand
Definition: relation.h:1917
void cost_agg ( Path path,
PlannerInfo root,
AggStrategy  aggstrategy,
const AggClauseCosts aggcosts,
int  numGroupCols,
double  numGroups,
Cost  input_startup_cost,
Cost  input_total_cost,
double  input_tuples 
)

Definition at line 1873 of file costsize.c.

References AGG_HASHED, AGG_MIXED, AGG_PLAIN, AGG_SORTED, Assert, cpu_operator_cost, cpu_tuple_cost, disable_cost, enable_hashagg, AggClauseCosts::finalCost, MemSet, NULL, QualCost::per_tuple, Path::rows, QualCost::startup, Path::startup_cost, Path::total_cost, and AggClauseCosts::transCost.

Referenced by choose_hashed_setop(), create_agg_path(), create_groupingsets_path(), and create_unique_path().

1878 {
1879  double output_tuples;
1880  Cost startup_cost;
1881  Cost total_cost;
1882  AggClauseCosts dummy_aggcosts;
1883 
1884  /* Use all-zero per-aggregate costs if NULL is passed */
1885  if (aggcosts == NULL)
1886  {
1887  Assert(aggstrategy == AGG_HASHED);
1888  MemSet(&dummy_aggcosts, 0, sizeof(AggClauseCosts));
1889  aggcosts = &dummy_aggcosts;
1890  }
1891 
1892  /*
1893  * The transCost.per_tuple component of aggcosts should be charged once
1894  * per input tuple, corresponding to the costs of evaluating the aggregate
1895  * transfns and their input expressions (with any startup cost of course
1896  * charged but once). The finalCost component is charged once per output
1897  * tuple, corresponding to the costs of evaluating the finalfns.
1898  *
1899  * If we are grouping, we charge an additional cpu_operator_cost per
1900  * grouping column per input tuple for grouping comparisons.
1901  *
1902  * We will produce a single output tuple if not grouping, and a tuple per
1903  * group otherwise. We charge cpu_tuple_cost for each output tuple.
1904  *
1905  * Note: in this cost model, AGG_SORTED and AGG_HASHED have exactly the
1906  * same total CPU cost, but AGG_SORTED has lower startup cost. If the
1907  * input path is already sorted appropriately, AGG_SORTED should be
1908  * preferred (since it has no risk of memory overflow). This will happen
1909  * as long as the computed total costs are indeed exactly equal --- but if
1910  * there's roundoff error we might do the wrong thing. So be sure that
1911  * the computations below form the same intermediate values in the same
1912  * order.
1913  */
1914  if (aggstrategy == AGG_PLAIN)
1915  {
1916  startup_cost = input_total_cost;
1917  startup_cost += aggcosts->transCost.startup;
1918  startup_cost += aggcosts->transCost.per_tuple * input_tuples;
1919  startup_cost += aggcosts->finalCost;
1920  /* we aren't grouping */
1921  total_cost = startup_cost + cpu_tuple_cost;
1922  output_tuples = 1;
1923  }
1924  else if (aggstrategy == AGG_SORTED || aggstrategy == AGG_MIXED)
1925  {
1926  /* Here we are able to deliver output on-the-fly */
1927  startup_cost = input_startup_cost;
1928  total_cost = input_total_cost;
1929  if (aggstrategy == AGG_MIXED && !enable_hashagg)
1930  {
1931  startup_cost += disable_cost;
1932  total_cost += disable_cost;
1933  }
1934  /* calcs phrased this way to match HASHED case, see note above */
1935  total_cost += aggcosts->transCost.startup;
1936  total_cost += aggcosts->transCost.per_tuple * input_tuples;
1937  total_cost += (cpu_operator_cost * numGroupCols) * input_tuples;
1938  total_cost += aggcosts->finalCost * numGroups;
1939  total_cost += cpu_tuple_cost * numGroups;
1940  output_tuples = numGroups;
1941  }
1942  else
1943  {
1944  /* must be AGG_HASHED */
1945  startup_cost = input_total_cost;
1946  if (!enable_hashagg)
1947  startup_cost += disable_cost;
1948  startup_cost += aggcosts->transCost.startup;
1949  startup_cost += aggcosts->transCost.per_tuple * input_tuples;
1950  startup_cost += (cpu_operator_cost * numGroupCols) * input_tuples;
1951  total_cost = startup_cost;
1952  total_cost += aggcosts->finalCost * numGroups;
1953  total_cost += cpu_tuple_cost * numGroups;
1954  output_tuples = numGroups;
1955  }
1956 
1957  path->rows = output_tuples;
1958  path->startup_cost = startup_cost;
1959  path->total_cost = total_cost;
1960 }
#define MemSet(start, val, len)
Definition: c.h:857
QualCost transCost
Definition: relation.h:62
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
double cpu_operator_cost
Definition: costsize.c:108
Cost finalCost
Definition: relation.h:63
Cost total_cost
Definition: relation.h:966
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
double cpu_tuple_cost
Definition: costsize.c:106
bool enable_hashagg
Definition: costsize.c:124
double Cost
Definition: nodes.h:640
void cost_bitmap_and_node ( BitmapAndPath path,
PlannerInfo root 
)

Definition at line 1072 of file costsize.c.

References BitmapAndPath::bitmapquals, BitmapAndPath::bitmapselectivity, cost_bitmap_tree_node(), cpu_operator_cost, lfirst, list_head(), BitmapAndPath::path, Path::rows, Path::startup_cost, subpath(), and Path::total_cost.

Referenced by bitmap_and_cost_est(), and create_bitmap_and_path().

1073 {
1074  Cost totalCost;
1075  Selectivity selec;
1076  ListCell *l;
1077 
1078  /*
1079  * We estimate AND selectivity on the assumption that the inputs are
1080  * independent. This is probably often wrong, but we don't have the info
1081  * to do better.
1082  *
1083  * The runtime cost of the BitmapAnd itself is estimated at 100x
1084  * cpu_operator_cost for each tbm_intersect needed. Probably too small,
1085  * definitely too simplistic?
1086  */
1087  totalCost = 0.0;
1088  selec = 1.0;
1089  foreach(l, path->bitmapquals)
1090  {
1091  Path *subpath = (Path *) lfirst(l);
1092  Cost subCost;
1093  Selectivity subselec;
1094 
1095  cost_bitmap_tree_node(subpath, &subCost, &subselec);
1096 
1097  selec *= subselec;
1098 
1099  totalCost += subCost;
1100  if (l != list_head(path->bitmapquals))
1101  totalCost += 100.0 * cpu_operator_cost;
1102  }
1103  path->bitmapselectivity = selec;
1104  path->path.rows = 0; /* per above, not used */
1105  path->path.startup_cost = totalCost;
1106  path->path.total_cost = totalCost;
1107 }
double Selectivity
Definition: nodes.h:639
Selectivity bitmapselectivity
Definition: relation.h:1075
List * bitmapquals
Definition: relation.h:1074
Cost startup_cost
Definition: relation.h:965
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
double cpu_operator_cost
Definition: costsize.c:108
Cost total_cost
Definition: relation.h:966
#define lfirst(lc)
Definition: pg_list.h:106
double rows
Definition: relation.h:964
Definition: relation.h:948
double Cost
Definition: nodes.h:640
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1029
void cost_bitmap_heap_scan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info,
Path bitmapqual,
double  loop_count 
)

Definition at line 928 of file costsize.c.

References Assert, clamp_row_est(), compute_bitmap_pages(), PathTarget::cost, cpu_tuple_cost, disable_cost, enable_bitmapscan, get_parallel_divisor(), get_restriction_qual_cost(), get_tablespace_page_costs(), IsA, RelOptInfo::pages, Path::parallel_workers, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::reltablespace, RelOptInfo::rows, Path::rows, RTE_RELATION, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, T, and Path::total_cost.

Referenced by bitmap_and_cost_est(), bitmap_scan_cost_est(), and create_bitmap_heap_path().

931 {
932  Cost startup_cost = 0;
933  Cost run_cost = 0;
934  Cost indexTotalCost;
935  QualCost qpqual_cost;
936  Cost cpu_per_tuple;
937  Cost cost_per_page;
938  Cost cpu_run_cost;
939  double tuples_fetched;
940  double pages_fetched;
941  double spc_seq_page_cost,
942  spc_random_page_cost;
943  double T;
944 
945  /* Should only be applied to base relations */
946  Assert(IsA(baserel, RelOptInfo));
947  Assert(baserel->relid > 0);
948  Assert(baserel->rtekind == RTE_RELATION);
949 
950  /* Mark the path with the correct row estimate */
951  if (param_info)
952  path->rows = param_info->ppi_rows;
953  else
954  path->rows = baserel->rows;
955 
956  if (!enable_bitmapscan)
957  startup_cost += disable_cost;
958 
959  pages_fetched = compute_bitmap_pages(root, baserel, bitmapqual,
960  loop_count, &indexTotalCost,
961  &tuples_fetched);
962 
963  startup_cost += indexTotalCost;
964  T = (baserel->pages > 1) ? (double) baserel->pages : 1.0;
965 
966  /* Fetch estimated page costs for tablespace containing table. */
968  &spc_random_page_cost,
969  &spc_seq_page_cost);
970 
971  /*
972  * For small numbers of pages we should charge spc_random_page_cost
973  * apiece, while if nearly all the table's pages are being read, it's more
974  * appropriate to charge spc_seq_page_cost apiece. The effect is
975  * nonlinear, too. For lack of a better idea, interpolate like this to
976  * determine the cost per page.
977  */
978  if (pages_fetched >= 2.0)
979  cost_per_page = spc_random_page_cost -
980  (spc_random_page_cost - spc_seq_page_cost)
981  * sqrt(pages_fetched / T);
982  else
983  cost_per_page = spc_random_page_cost;
984 
985  run_cost += pages_fetched * cost_per_page;
986 
987  /*
988  * Estimate CPU costs per tuple.
989  *
990  * Often the indexquals don't need to be rechecked at each tuple ... but
991  * not always, especially not if there are enough tuples involved that the
992  * bitmaps become lossy. For the moment, just assume they will be
993  * rechecked always. This means we charge the full freight for all the
994  * scan clauses.
995  */
996  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
997 
998  startup_cost += qpqual_cost.startup;
999  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
1000  cpu_run_cost = cpu_per_tuple * tuples_fetched;
1001 
1002  /* Adjust costing for parallelism, if used. */
1003  if (path->parallel_workers > 0)
1004  {
1005  double parallel_divisor = get_parallel_divisor(path);
1006 
1007  /* The CPU cost is divided among all the workers. */
1008  cpu_run_cost /= parallel_divisor;
1009 
1010  path->rows = clamp_row_est(path->rows / parallel_divisor);
1011  }
1012 
1013 
1014  run_cost += cpu_run_cost;
1015 
1016  /* tlist eval costs are paid per output row, not per tuple scanned */
1017  startup_cost += path->pathtarget->cost.startup;
1018  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1019 
1020  path->startup_cost = startup_cost;
1021  path->total_cost = startup_cost + run_cost;
1022 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
PathTarget * pathtarget
Definition: relation.h:955
Oid reltablespace
Definition: relation.h:554
int parallel_workers
Definition: relation.h:961
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
static const uint32 T[65]
Definition: md5.c:101
static double get_parallel_divisor(Path *path)
Definition: costsize.c:5073
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:553
bool enable_bitmapscan
Definition: costsize.c:121
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
BlockNumber pages
Definition: relation.h:564
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
double clamp_row_est(double nrows)
Definition: costsize.c:173
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:5102
double Cost
Definition: nodes.h:640
void cost_bitmap_or_node ( BitmapOrPath path,
PlannerInfo root 
)

Definition at line 1116 of file costsize.c.

References BitmapOrPath::bitmapquals, BitmapOrPath::bitmapselectivity, cost_bitmap_tree_node(), cpu_operator_cost, IsA, lfirst, list_head(), Min, BitmapOrPath::path, Path::rows, Path::startup_cost, subpath(), and Path::total_cost.

Referenced by create_bitmap_or_path().

1117 {
1118  Cost totalCost;
1119  Selectivity selec;
1120  ListCell *l;
1121 
1122  /*
1123  * We estimate OR selectivity on the assumption that the inputs are
1124  * non-overlapping, since that's often the case in "x IN (list)" type
1125  * situations. Of course, we clamp to 1.0 at the end.
1126  *
1127  * The runtime cost of the BitmapOr itself is estimated at 100x
1128  * cpu_operator_cost for each tbm_union needed. Probably too small,
1129  * definitely too simplistic? We are aware that the tbm_unions are
1130  * optimized out when the inputs are BitmapIndexScans.
1131  */
1132  totalCost = 0.0;
1133  selec = 0.0;
1134  foreach(l, path->bitmapquals)
1135  {
1136  Path *subpath = (Path *) lfirst(l);
1137  Cost subCost;
1138  Selectivity subselec;
1139 
1140  cost_bitmap_tree_node(subpath, &subCost, &subselec);
1141 
1142  selec += subselec;
1143 
1144  totalCost += subCost;
1145  if (l != list_head(path->bitmapquals) &&
1146  !IsA(subpath, IndexPath))
1147  totalCost += 100.0 * cpu_operator_cost;
1148  }
1149  path->bitmapselectivity = Min(selec, 1.0);
1150  path->path.rows = 0; /* per above, not used */
1151  path->path.startup_cost = totalCost;
1152  path->path.total_cost = totalCost;
1153 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define Min(x, y)
Definition: c.h:806
double Selectivity
Definition: nodes.h:639
List * bitmapquals
Definition: relation.h:1087
Cost startup_cost
Definition: relation.h:965
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
double cpu_operator_cost
Definition: costsize.c:108
Selectivity bitmapselectivity
Definition: relation.h:1088
Cost total_cost
Definition: relation.h:966
#define lfirst(lc)
Definition: pg_list.h:106
double rows
Definition: relation.h:964
Definition: relation.h:948
double Cost
Definition: nodes.h:640
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1029
void cost_bitmap_tree_node ( Path path,
Cost cost,
Selectivity selec 
)

Definition at line 1029 of file costsize.c.

References cpu_operator_cost, elog, ERROR, IsA, nodeTag, Path::rows, and Path::total_cost.

Referenced by choose_bitmap_and(), compute_bitmap_pages(), cost_bitmap_and_node(), cost_bitmap_or_node(), and path_usage_comparator().

1030 {
1031  if (IsA(path, IndexPath))
1032  {
1033  *cost = ((IndexPath *) path)->indextotalcost;
1034  *selec = ((IndexPath *) path)->indexselectivity;
1035 
1036  /*
1037  * Charge a small amount per retrieved tuple to reflect the costs of
1038  * manipulating the bitmap. This is mostly to make sure that a bitmap
1039  * scan doesn't look to be the same cost as an indexscan to retrieve a
1040  * single tuple.
1041  */
1042  *cost += 0.1 * cpu_operator_cost * path->rows;
1043  }
1044  else if (IsA(path, BitmapAndPath))
1045  {
1046  *cost = path->total_cost;
1047  *selec = ((BitmapAndPath *) path)->bitmapselectivity;
1048  }
1049  else if (IsA(path, BitmapOrPath))
1050  {
1051  *cost = path->total_cost;
1052  *selec = ((BitmapOrPath *) path)->bitmapselectivity;
1053  }
1054  else
1055  {
1056  elog(ERROR, "unrecognized node type: %d", nodeTag(path));
1057  *cost = *selec = 0; /* keep compiler quiet */
1058  }
1059 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define ERROR
Definition: elog.h:43
double cpu_operator_cost
Definition: costsize.c:108
Cost total_cost
Definition: relation.h:966
double rows
Definition: relation.h:964
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define elog
Definition: elog.h:219
void cost_ctescan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 1483 of file costsize.c.

References Assert, PathTarget::cost, cpu_tuple_cost, get_restriction_qual_cost(), Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::rows, Path::rows, RTE_CTE, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_ctescan_path(), and create_worktablescan_path().

1485 {
1486  Cost startup_cost = 0;
1487  Cost run_cost = 0;
1488  QualCost qpqual_cost;
1489  Cost cpu_per_tuple;
1490 
1491  /* Should only be applied to base relations that are CTEs */
1492  Assert(baserel->relid > 0);
1493  Assert(baserel->rtekind == RTE_CTE);
1494 
1495  /* Mark the path with the correct row estimate */
1496  if (param_info)
1497  path->rows = param_info->ppi_rows;
1498  else
1499  path->rows = baserel->rows;
1500 
1501  /* Charge one CPU tuple cost per row for tuplestore manipulation */
1502  cpu_per_tuple = cpu_tuple_cost;
1503 
1504  /* Add scanning CPU costs */
1505  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1506 
1507  startup_cost += qpqual_cost.startup;
1508  cpu_per_tuple += cpu_tuple_cost + qpqual_cost.per_tuple;
1509  run_cost += cpu_per_tuple * baserel->tuples;
1510 
1511  /* tlist eval costs are paid per output row, not per tuple scanned */
1512  startup_cost += path->pathtarget->cost.startup;
1513  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1514 
1515  path->startup_cost = startup_cost;
1516  path->total_cost = startup_cost + run_cost;
1517 }
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
Index relid
Definition: relation.h:553
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
double Cost
Definition: nodes.h:640
void cost_functionscan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 1316 of file costsize.c.

References Assert, PathTarget::cost, cost_qual_eval_node(), cpu_tuple_cost, RangeTblEntry::functions, get_restriction_qual_cost(), Path::pathtarget, QualCost::per_tuple, planner_rt_fetch, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::rows, Path::rows, RTE_FUNCTION, RangeTblEntry::rtekind, QualCost::startup, Path::startup_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_functionscan_path().

1318 {
1319  Cost startup_cost = 0;
1320  Cost run_cost = 0;
1321  QualCost qpqual_cost;
1322  Cost cpu_per_tuple;
1323  RangeTblEntry *rte;
1324  QualCost exprcost;
1325 
1326  /* Should only be applied to base relations that are functions */
1327  Assert(baserel->relid > 0);
1328  rte = planner_rt_fetch(baserel->relid, root);
1329  Assert(rte->rtekind == RTE_FUNCTION);
1330 
1331  /* Mark the path with the correct row estimate */
1332  if (param_info)
1333  path->rows = param_info->ppi_rows;
1334  else
1335  path->rows = baserel->rows;
1336 
1337  /*
1338  * Estimate costs of executing the function expression(s).
1339  *
1340  * Currently, nodeFunctionscan.c always executes the functions to
1341  * completion before returning any rows, and caches the results in a
1342  * tuplestore. So the function eval cost is all startup cost, and per-row
1343  * costs are minimal.
1344  *
1345  * XXX in principle we ought to charge tuplestore spill costs if the
1346  * number of rows is large. However, given how phony our rowcount
1347  * estimates for functions tend to be, there's not a lot of point in that
1348  * refinement right now.
1349  */
1350  cost_qual_eval_node(&exprcost, (Node *) rte->functions, root);
1351 
1352  startup_cost += exprcost.startup + exprcost.per_tuple;
1353 
1354  /* Add scanning CPU costs */
1355  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1356 
1357  startup_cost += qpqual_cost.startup;
1358  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
1359  run_cost += cpu_per_tuple * baserel->tuples;
1360 
1361  /* tlist eval costs are paid per output row, not per tuple scanned */
1362  startup_cost += path->pathtarget->cost.startup;
1363  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1364 
1365  path->startup_cost = startup_cost;
1366  path->total_cost = startup_cost + run_cost;
1367 }
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3464
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Definition: nodes.h:509
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
Cost startup_cost
Definition: relation.h:965
Index relid
Definition: relation.h:553
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define Assert(condition)
Definition: c.h:675
List * functions
Definition: parsenodes.h:990
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
RTEKind rtekind
Definition: parsenodes.h:936
double Cost
Definition: nodes.h:640
void cost_gather ( GatherPath path,
PlannerInfo root,
RelOptInfo rel,
ParamPathInfo param_info,
double *  rows 
)

Definition at line 349 of file costsize.c.

References parallel_setup_cost, parallel_tuple_cost, GatherPath::path, ParamPathInfo::ppi_rows, RelOptInfo::rows, Path::rows, Path::startup_cost, GatherPath::subpath, and Path::total_cost.

Referenced by create_gather_path().

352 {
353  Cost startup_cost = 0;
354  Cost run_cost = 0;
355 
356  /* Mark the path with the correct row estimate */
357  if (rows)
358  path->path.rows = *rows;
359  else if (param_info)
360  path->path.rows = param_info->ppi_rows;
361  else
362  path->path.rows = rel->rows;
363 
364  startup_cost = path->subpath->startup_cost;
365 
366  run_cost = path->subpath->total_cost - path->subpath->startup_cost;
367 
368  /* Parallel setup and communication cost. */
369  startup_cost += parallel_setup_cost;
370  run_cost += parallel_tuple_cost * path->path.rows;
371 
372  path->path.startup_cost = startup_cost;
373  path->path.total_cost = (startup_cost + run_cost);
374 }
double parallel_setup_cost
Definition: costsize.c:110
Cost startup_cost
Definition: relation.h:965
Path * subpath
Definition: relation.h:1265
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
double rows
Definition: relation.h:964
double ppi_rows
Definition: relation.h:914
Path path
Definition: relation.h:1264
double Cost
Definition: nodes.h:640
double parallel_tuple_cost
Definition: costsize.c:109
void cost_gather_merge ( GatherMergePath path,
PlannerInfo root,
RelOptInfo rel,
ParamPathInfo param_info,
Cost  input_startup_cost,
Cost  input_total_cost,
double *  rows 
)

Definition at line 387 of file costsize.c.

References Assert, cpu_operator_cost, disable_cost, enable_gathermerge, LOG2, GatherMergePath::num_workers, parallel_setup_cost, parallel_tuple_cost, GatherMergePath::path, ParamPathInfo::ppi_rows, RelOptInfo::rows, Path::rows, Path::startup_cost, and Path::total_cost.

Referenced by create_gather_merge_path().

391 {
392  Cost startup_cost = 0;
393  Cost run_cost = 0;
394  Cost comparison_cost;
395  double N;
396  double logN;
397 
398  /* Mark the path with the correct row estimate */
399  if (rows)
400  path->path.rows = *rows;
401  else if (param_info)
402  path->path.rows = param_info->ppi_rows;
403  else
404  path->path.rows = rel->rows;
405 
406  if (!enable_gathermerge)
407  startup_cost += disable_cost;
408 
409  /*
410  * Add one to the number of workers to account for the leader. This might
411  * be overgenerous since the leader will do less work than other workers
412  * in typical cases, but we'll go with it for now.
413  */
414  Assert(path->num_workers > 0);
415  N = (double) path->num_workers + 1;
416  logN = LOG2(N);
417 
418  /* Assumed cost per tuple comparison */
419  comparison_cost = 2.0 * cpu_operator_cost;
420 
421  /* Heap creation cost */
422  startup_cost += comparison_cost * N * logN;
423 
424  /* Per-tuple heap maintenance cost */
425  run_cost += path->path.rows * comparison_cost * logN;
426 
427  /* small cost for heap management, like cost_merge_append */
428  run_cost += cpu_operator_cost * path->path.rows;
429 
430  /*
431  * Parallel setup and communication cost. Since Gather Merge, unlike
432  * Gather, requires us to block until a tuple is available from every
433  * worker, we bump the IPC cost up a little bit as compared with Gather.
434  * For lack of a better idea, charge an extra 5%.
435  */
436  startup_cost += parallel_setup_cost;
437  run_cost += parallel_tuple_cost * path->path.rows * 1.05;
438 
439  path->path.startup_cost = startup_cost + input_startup_cost;
440  path->path.total_cost = (startup_cost + run_cost + input_total_cost);
441 }
double parallel_setup_cost
Definition: costsize.c:110
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
double cpu_operator_cost
Definition: costsize.c:108
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
#define LOG2(x)
Definition: costsize.c:101
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
double ppi_rows
Definition: relation.h:914
bool enable_gathermerge
Definition: costsize.c:129
double Cost
Definition: nodes.h:640
double parallel_tuple_cost
Definition: costsize.c:109
void cost_group ( Path path,
PlannerInfo root,
int  numGroupCols,
double  numGroups,
Cost  input_startup_cost,
Cost  input_total_cost,
double  input_tuples 
)

Definition at line 2040 of file costsize.c.

References cpu_operator_cost, Path::rows, Path::startup_cost, and Path::total_cost.

Referenced by choose_hashed_setop(), and create_group_path().

2044 {
2045  Cost startup_cost;
2046  Cost total_cost;
2047 
2048  startup_cost = input_startup_cost;
2049  total_cost = input_total_cost;
2050 
2051  /*
2052  * Charge one cpu_operator_cost per comparison per input tuple. We assume
2053  * all columns get compared at most of the tuples.
2054  */
2055  total_cost += cpu_operator_cost * input_tuples * numGroupCols;
2056 
2057  path->rows = numGroups;
2058  path->startup_cost = startup_cost;
2059  path->total_cost = total_cost;
2060 }
Cost startup_cost
Definition: relation.h:965
double cpu_operator_cost
Definition: costsize.c:108
Cost total_cost
Definition: relation.h:966
double rows
Definition: relation.h:964
double Cost
Definition: nodes.h:640
void cost_index ( IndexPath path,
PlannerInfo root,
double  loop_count,
bool  partial_path 
)

Definition at line 462 of file costsize.c.

References RelOptInfo::allvisfrac, IndexOptInfo::amcostestimate, Assert, clamp_row_est(), compute_parallel_worker(), PathTarget::cost, cost_qual_eval(), cpu_tuple_cost, disable_cost, enable_indexscan, extract_nonindex_conditions(), get_parallel_divisor(), get_tablespace_page_costs(), index_pages_fetched(), IndexPath::indexinfo, IndexPath::indexquals, IndexPath::indexselectivity, IndexPath::indextotalcost, IndexOptInfo::indrestrictinfo, IsA, list_concat(), RelOptInfo::pages, IndexOptInfo::pages, Path::parallel_aware, Path::parallel_workers, Path::param_info, IndexPath::path, Path::pathtarget, Path::pathtype, QualCost::per_tuple, ParamPathInfo::ppi_clauses, ParamPathInfo::ppi_rows, IndexOptInfo::rel, RelOptInfo::relid, RelOptInfo::reltablespace, RelOptInfo::rows, Path::rows, RTE_RELATION, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, T_IndexOnlyScan, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_index_path(), and reparameterize_path().

464 {
465  IndexOptInfo *index = path->indexinfo;
466  RelOptInfo *baserel = index->rel;
467  bool indexonly = (path->path.pathtype == T_IndexOnlyScan);
468  amcostestimate_function amcostestimate;
469  List *qpquals;
470  Cost startup_cost = 0;
471  Cost run_cost = 0;
472  Cost cpu_run_cost = 0;
473  Cost indexStartupCost;
474  Cost indexTotalCost;
475  Selectivity indexSelectivity;
476  double indexCorrelation,
477  csquared;
478  double spc_seq_page_cost,
479  spc_random_page_cost;
480  Cost min_IO_cost,
481  max_IO_cost;
482  QualCost qpqual_cost;
483  Cost cpu_per_tuple;
484  double tuples_fetched;
485  double pages_fetched;
486  double rand_heap_pages;
487  double index_pages;
488 
489  /* Should only be applied to base relations */
490  Assert(IsA(baserel, RelOptInfo) &&
491  IsA(index, IndexOptInfo));
492  Assert(baserel->relid > 0);
493  Assert(baserel->rtekind == RTE_RELATION);
494 
495  /*
496  * Mark the path with the correct row estimate, and identify which quals
497  * will need to be enforced as qpquals. We need not check any quals that
498  * are implied by the index's predicate, so we can use indrestrictinfo not
499  * baserestrictinfo as the list of relevant restriction clauses for the
500  * rel.
501  */
502  if (path->path.param_info)
503  {
504  path->path.rows = path->path.param_info->ppi_rows;
505  /* qpquals come from the rel's restriction clauses and ppi_clauses */
506  qpquals = list_concat(
508  path->indexquals),
510  path->indexquals));
511  }
512  else
513  {
514  path->path.rows = baserel->rows;
515  /* qpquals come from just the rel's restriction clauses */
517  path->indexquals);
518  }
519 
520  if (!enable_indexscan)
521  startup_cost += disable_cost;
522  /* we don't need to check enable_indexonlyscan; indxpath.c does that */
523 
524  /*
525  * Call index-access-method-specific code to estimate the processing cost
526  * for scanning the index, as well as the selectivity of the index (ie,
527  * the fraction of main-table tuples we will have to retrieve) and its
528  * correlation to the main-table tuple order. We need a cast here because
529  * relation.h uses a weak function type to avoid including amapi.h.
530  */
531  amcostestimate = (amcostestimate_function) index->amcostestimate;
532  amcostestimate(root, path, loop_count,
533  &indexStartupCost, &indexTotalCost,
534  &indexSelectivity, &indexCorrelation,
535  &index_pages);
536 
537  /*
538  * Save amcostestimate's results for possible use in bitmap scan planning.
539  * We don't bother to save indexStartupCost or indexCorrelation, because a
540  * bitmap scan doesn't care about either.
541  */
542  path->indextotalcost = indexTotalCost;
543  path->indexselectivity = indexSelectivity;
544 
545  /* all costs for touching index itself included here */
546  startup_cost += indexStartupCost;
547  run_cost += indexTotalCost - indexStartupCost;
548 
549  /* estimate number of main-table tuples fetched */
550  tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
551 
552  /* fetch estimated page costs for tablespace containing table */
554  &spc_random_page_cost,
555  &spc_seq_page_cost);
556 
557  /*----------
558  * Estimate number of main-table pages fetched, and compute I/O cost.
559  *
560  * When the index ordering is uncorrelated with the table ordering,
561  * we use an approximation proposed by Mackert and Lohman (see
562  * index_pages_fetched() for details) to compute the number of pages
563  * fetched, and then charge spc_random_page_cost per page fetched.
564  *
565  * When the index ordering is exactly correlated with the table ordering
566  * (just after a CLUSTER, for example), the number of pages fetched should
567  * be exactly selectivity * table_size. What's more, all but the first
568  * will be sequential fetches, not the random fetches that occur in the
569  * uncorrelated case. So if the number of pages is more than 1, we
570  * ought to charge
571  * spc_random_page_cost + (pages_fetched - 1) * spc_seq_page_cost
572  * For partially-correlated indexes, we ought to charge somewhere between
573  * these two estimates. We currently interpolate linearly between the
574  * estimates based on the correlation squared (XXX is that appropriate?).
575  *
576  * If it's an index-only scan, then we will not need to fetch any heap
577  * pages for which the visibility map shows all tuples are visible.
578  * Hence, reduce the estimated number of heap fetches accordingly.
579  * We use the measured fraction of the entire heap that is all-visible,
580  * which might not be particularly relevant to the subset of the heap
581  * that this query will fetch; but it's not clear how to do better.
582  *----------
583  */
584  if (loop_count > 1)
585  {
586  /*
587  * For repeated indexscans, the appropriate estimate for the
588  * uncorrelated case is to scale up the number of tuples fetched in
589  * the Mackert and Lohman formula by the number of scans, so that we
590  * estimate the number of pages fetched by all the scans; then
591  * pro-rate the costs for one scan. In this case we assume all the
592  * fetches are random accesses.
593  */
594  pages_fetched = index_pages_fetched(tuples_fetched * loop_count,
595  baserel->pages,
596  (double) index->pages,
597  root);
598 
599  if (indexonly)
600  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
601 
602  rand_heap_pages = pages_fetched;
603 
604  max_IO_cost = (pages_fetched * spc_random_page_cost) / loop_count;
605 
606  /*
607  * In the perfectly correlated case, the number of pages touched by
608  * each scan is selectivity * table_size, and we can use the Mackert
609  * and Lohman formula at the page level to estimate how much work is
610  * saved by caching across scans. We still assume all the fetches are
611  * random, though, which is an overestimate that's hard to correct for
612  * without double-counting the cache effects. (But in most cases
613  * where such a plan is actually interesting, only one page would get
614  * fetched per scan anyway, so it shouldn't matter much.)
615  */
616  pages_fetched = ceil(indexSelectivity * (double) baserel->pages);
617 
618  pages_fetched = index_pages_fetched(pages_fetched * loop_count,
619  baserel->pages,
620  (double) index->pages,
621  root);
622 
623  if (indexonly)
624  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
625 
626  min_IO_cost = (pages_fetched * spc_random_page_cost) / loop_count;
627  }
628  else
629  {
630  /*
631  * Normal case: apply the Mackert and Lohman formula, and then
632  * interpolate between that and the correlation-derived result.
633  */
634  pages_fetched = index_pages_fetched(tuples_fetched,
635  baserel->pages,
636  (double) index->pages,
637  root);
638 
639  if (indexonly)
640  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
641 
642  rand_heap_pages = pages_fetched;
643 
644  /* max_IO_cost is for the perfectly uncorrelated case (csquared=0) */
645  max_IO_cost = pages_fetched * spc_random_page_cost;
646 
647  /* min_IO_cost is for the perfectly correlated case (csquared=1) */
648  pages_fetched = ceil(indexSelectivity * (double) baserel->pages);
649 
650  if (indexonly)
651  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
652 
653  if (pages_fetched > 0)
654  {
655  min_IO_cost = spc_random_page_cost;
656  if (pages_fetched > 1)
657  min_IO_cost += (pages_fetched - 1) * spc_seq_page_cost;
658  }
659  else
660  min_IO_cost = 0;
661  }
662 
663  if (partial_path)
664  {
665  /*
666  * For index only scans compute workers based on number of index pages
667  * fetched; the number of heap pages we fetch might be so small as to
668  * effectively rule out parallelism, which we don't want to do.
669  */
670  if (indexonly)
671  rand_heap_pages = -1;
672 
673  /*
674  * Estimate the number of parallel workers required to scan index. Use
675  * the number of heap pages computed considering heap fetches won't be
676  * sequential as for parallel scans the pages are accessed in random
677  * order.
678  */
680  rand_heap_pages, index_pages);
681 
682  /*
683  * Fall out if workers can't be assigned for parallel scan, because in
684  * such a case this path will be rejected. So there is no benefit in
685  * doing extra computation.
686  */
687  if (path->path.parallel_workers <= 0)
688  return;
689 
690  path->path.parallel_aware = true;
691  }
692 
693  /*
694  * Now interpolate based on estimated index order correlation to get total
695  * disk I/O cost for main table accesses.
696  */
697  csquared = indexCorrelation * indexCorrelation;
698 
699  run_cost += max_IO_cost + csquared * (min_IO_cost - max_IO_cost);
700 
701  /*
702  * Estimate CPU costs per tuple.
703  *
704  * What we want here is cpu_tuple_cost plus the evaluation costs of any
705  * qual clauses that we have to evaluate as qpquals.
706  */
707  cost_qual_eval(&qpqual_cost, qpquals, root);
708 
709  startup_cost += qpqual_cost.startup;
710  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
711 
712  cpu_run_cost += cpu_per_tuple * tuples_fetched;
713 
714  /* tlist eval costs are paid per output row, not per tuple scanned */
715  startup_cost += path->path.pathtarget->cost.startup;
716  cpu_run_cost += path->path.pathtarget->cost.per_tuple * path->path.rows;
717 
718  /* Adjust costing for parallelism, if used. */
719  if (path->path.parallel_workers > 0)
720  {
721  double parallel_divisor = get_parallel_divisor(&path->path);
722 
723  path->path.rows = clamp_row_est(path->path.rows / parallel_divisor);
724 
725  /* The CPU cost is divided among all the workers. */
726  cpu_run_cost /= parallel_divisor;
727  }
728 
729  run_cost += cpu_run_cost;
730 
731  path->path.startup_cost = startup_cost;
732  path->path.total_cost = startup_cost + run_cost;
733 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
PathTarget * pathtarget
Definition: relation.h:955
Path path
Definition: relation.h:1030
IndexOptInfo * indexinfo
Definition: relation.h:1031
double tuples
Definition: relation.h:565
Oid reltablespace
Definition: relation.h:554
int parallel_workers
Definition: relation.h:961
ParamPathInfo * param_info
Definition: relation.h:957
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static List * extract_nonindex_conditions(List *qual_clauses, List *indexquals)
Definition: costsize.c:753
double Selectivity
Definition: nodes.h:639
Cost startup
Definition: relation.h:45
double allvisfrac
Definition: relation.h:566
Definition: type.h:89
BlockNumber pages
Definition: relation.h:636
NodeTag pathtype
Definition: relation.h:952
Cost per_tuple
Definition: relation.h:46
List * indexquals
Definition: relation.h:1033
RelOptInfo * rel
Definition: relation.h:633
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3438
Cost startup_cost
Definition: relation.h:965
Cost indextotalcost
Definition: relation.h:1038
Cost disable_cost
Definition: costsize.c:114
Selectivity indexselectivity
Definition: relation.h:1039
static double get_parallel_divisor(Path *path)
Definition: costsize.c:5073
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:553
List * indrestrictinfo
Definition: relation.h:658
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3067
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
BlockNumber pages
Definition: relation.h:564
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
List * ppi_clauses
Definition: relation.h:915
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
bool parallel_aware
Definition: relation.h:959
void(* amcostestimate)()
Definition: relation.h:678
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: pg_list.h:45
bool enable_indexscan
Definition: costsize.c:119
void(* amcostestimate_function)(struct PlannerInfo *root, struct IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: amapi.h:92
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:813
double Cost
Definition: nodes.h:640
void cost_material ( Path path,
Cost  input_startup_cost,
Cost  input_total_cost,
double  tuples,
int  width 
)

Definition at line 1819 of file costsize.c.

References cpu_operator_cost, relation_byte_size(), Path::rows, seq_page_cost, Path::startup_cost, Path::total_cost, and work_mem.

Referenced by create_material_path(), and materialize_finished_plan().

1822 {
1823  Cost startup_cost = input_startup_cost;
1824  Cost run_cost = input_total_cost - input_startup_cost;
1825  double nbytes = relation_byte_size(tuples, width);
1826  long work_mem_bytes = work_mem * 1024L;
1827 
1828  path->rows = tuples;
1829 
1830  /*
1831  * Whether spilling or not, charge 2x cpu_operator_cost per tuple to
1832  * reflect bookkeeping overhead. (This rate must be more than what
1833  * cost_rescan charges for materialize, ie, cpu_operator_cost per tuple;
1834  * if it is exactly the same then there will be a cost tie between
1835  * nestloop with A outer, materialized B inner and nestloop with B outer,
1836  * materialized A inner. The extra cost ensures we'll prefer
1837  * materializing the smaller rel.) Note that this is normally a good deal
1838  * less than cpu_tuple_cost; which is OK because a Material plan node
1839  * doesn't do qual-checking or projection, so it's got less overhead than
1840  * most plan nodes.
1841  */
1842  run_cost += 2 * cpu_operator_cost * tuples;
1843 
1844  /*
1845  * If we will spill to disk, charge at the rate of seq_page_cost per page.
1846  * This cost is assumed to be evenly spread through the plan run phase,
1847  * which isn't exactly accurate but our cost model doesn't allow for
1848  * nonuniform costs within the run phase.
1849  */
1850  if (nbytes > work_mem_bytes)
1851  {
1852  double npages = ceil(nbytes / BLCKSZ);
1853 
1854  run_cost += seq_page_cost * npages;
1855  }
1856 
1857  path->startup_cost = startup_cost;
1858  path->total_cost = startup_cost + run_cost;
1859 }
Cost startup_cost
Definition: relation.h:965
double cpu_operator_cost
Definition: costsize.c:108
static double relation_byte_size(double tuples, int width)
Definition: costsize.c:5052
int work_mem
Definition: globals.c:113
Cost total_cost
Definition: relation.h:966
double rows
Definition: relation.h:964
double seq_page_cost
Definition: costsize.c:104
double Cost
Definition: nodes.h:640
void cost_merge_append ( Path path,
PlannerInfo root,
List pathkeys,
int  n_streams,
Cost  input_startup_cost,
Cost  input_total_cost,
double  tuples 
)

Definition at line 1768 of file costsize.c.

References cpu_operator_cost, LOG2, Path::startup_cost, and Path::total_cost.

Referenced by create_merge_append_path().

1772 {
1773  Cost startup_cost = 0;
1774  Cost run_cost = 0;
1775  Cost comparison_cost;
1776  double N;
1777  double logN;
1778 
1779  /*
1780  * Avoid log(0)...
1781  */
1782  N = (n_streams < 2) ? 2.0 : (double) n_streams;
1783  logN = LOG2(N);
1784 
1785  /* Assumed cost per tuple comparison */
1786  comparison_cost = 2.0 * cpu_operator_cost;
1787 
1788  /* Heap creation cost */
1789  startup_cost += comparison_cost * N * logN;
1790 
1791  /* Per-tuple heap maintenance cost */
1792  run_cost += tuples * comparison_cost * logN;
1793 
1794  /*
1795  * Also charge a small amount (arbitrarily set equal to operator cost) per
1796  * extracted tuple. We don't charge cpu_tuple_cost because a MergeAppend
1797  * node doesn't do qual-checking or projection, so it has less overhead
1798  * than most plan nodes.
1799  */
1800  run_cost += cpu_operator_cost * tuples;
1801 
1802  path->startup_cost = startup_cost + input_startup_cost;
1803  path->total_cost = startup_cost + run_cost + input_total_cost;
1804 }
Cost startup_cost
Definition: relation.h:965
double cpu_operator_cost
Definition: costsize.c:108
Cost total_cost
Definition: relation.h:966
#define LOG2(x)
Definition: costsize.c:101
double Cost
Definition: nodes.h:640
void cost_namedtuplestorescan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 1524 of file costsize.c.

References Assert, cpu_tuple_cost, get_restriction_qual_cost(), QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::rows, Path::rows, RTE_NAMEDTUPLESTORE, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_namedtuplestorescan_path().

1526 {
1527  Cost startup_cost = 0;
1528  Cost run_cost = 0;
1529  QualCost qpqual_cost;
1530  Cost cpu_per_tuple;
1531 
1532  /* Should only be applied to base relations that are Tuplestores */
1533  Assert(baserel->relid > 0);
1534  Assert(baserel->rtekind == RTE_NAMEDTUPLESTORE);
1535 
1536  /* Mark the path with the correct row estimate */
1537  if (param_info)
1538  path->rows = param_info->ppi_rows;
1539  else
1540  path->rows = baserel->rows;
1541 
1542  /* Charge one CPU tuple cost per row for tuplestore manipulation */
1543  cpu_per_tuple = cpu_tuple_cost;
1544 
1545  /* Add scanning CPU costs */
1546  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1547 
1548  startup_cost += qpqual_cost.startup;
1549  cpu_per_tuple += cpu_tuple_cost + qpqual_cost.per_tuple;
1550  run_cost += cpu_per_tuple * baserel->tuples;
1551 
1552  path->startup_cost = startup_cost;
1553  path->total_cost = startup_cost + run_cost;
1554 }
double tuples
Definition: relation.h:565
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
Index relid
Definition: relation.h:553
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
double Cost
Definition: nodes.h:640
void cost_qual_eval ( QualCost cost,
List quals,
PlannerInfo root 
)

Definition at line 3438 of file costsize.c.

References cost_qual_eval_walker(), lfirst, QualCost::per_tuple, cost_qual_eval_context::root, QualCost::startup, and cost_qual_eval_context::total.

Referenced by cost_index(), cost_subplan(), cost_tidscan(), create_result_path(), estimate_path_cost_size(), final_cost_hashjoin(), final_cost_mergejoin(), final_cost_nestloop(), get_restriction_qual_cost(), inline_function(), plan_cluster_use_sort(), postgresGetForeignJoinPaths(), postgresGetForeignRelSize(), set_baserel_size_estimates(), and set_foreign_size_estimates().

3439 {
3440  cost_qual_eval_context context;
3441  ListCell *l;
3442 
3443  context.root = root;
3444  context.total.startup = 0;
3445  context.total.per_tuple = 0;
3446 
3447  /* We don't charge any cost for the implicit ANDing at top level ... */
3448 
3449  foreach(l, quals)
3450  {
3451  Node *qual = (Node *) lfirst(l);
3452 
3453  cost_qual_eval_walker(qual, &context);
3454  }
3455 
3456  *cost = context.total;
3457 }
PlannerInfo * root
Definition: costsize.c:133
Definition: nodes.h:509
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
Definition: costsize.c:3478
#define lfirst(lc)
Definition: pg_list.h:106
void cost_qual_eval_node ( QualCost cost,
Node qual,
PlannerInfo root 
)

Definition at line 3464 of file costsize.c.

References cost_qual_eval_walker(), QualCost::per_tuple, cost_qual_eval_context::root, QualCost::startup, and cost_qual_eval_context::total.

Referenced by add_placeholders_to_joinrel(), cost_functionscan(), cost_tablefuncscan(), cost_windowagg(), get_agg_clause_costs_walker(), make_sort_input_target(), order_qual_clauses(), orderby_operands_eval_cost(), other_operands_eval_cost(), set_pathtarget_cost_width(), and set_rel_width().

3465 {
3466  cost_qual_eval_context context;
3467 
3468  context.root = root;
3469  context.total.startup = 0;
3470  context.total.per_tuple = 0;
3471 
3472  cost_qual_eval_walker(qual, &context);
3473 
3474  *cost = context.total;
3475 }
PlannerInfo * root
Definition: costsize.c:133
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
Definition: costsize.c:3478
static bool cost_qual_eval_walker ( Node node,
cost_qual_eval_context context 
)
static

Definition at line 3478 of file costsize.c.

References CoerceViaIO::arg, ArrayCoerceExpr::arg, ScalarArrayOpExpr::args, RestrictInfo::clause, cpu_operator_cost, disable_cost, ArrayCoerceExpr::elemfuncid, elog, ERROR, estimate_array_length(), RestrictInfo::eval_cost, expression_tree_walker(), exprType(), get_func_cost(), get_opcode(), getTypeInputInfo(), getTypeOutputInfo(), IsA, lfirst_oid, linitial, lsecond, NULL, OidIsValid, ScalarArrayOpExpr::opfuncid, RowCompareExpr::opnos, RestrictInfo::orclause, SubPlan::per_call_cost, QualCost::per_tuple, RestrictInfo::pseudoconstant, CoerceViaIO::resulttype, cost_qual_eval_context::root, set_opfuncid(), set_sa_opfuncid(), QualCost::startup, SubPlan::startup_cost, AlternativeSubPlan::subplans, and cost_qual_eval_context::total.

Referenced by cost_qual_eval(), and cost_qual_eval_node().

3479 {
3480  if (node == NULL)
3481  return false;
3482 
3483  /*
3484  * RestrictInfo nodes contain an eval_cost field reserved for this
3485  * routine's use, so that it's not necessary to evaluate the qual clause's
3486  * cost more than once. If the clause's cost hasn't been computed yet,
3487  * the field's startup value will contain -1.
3488  */
3489  if (IsA(node, RestrictInfo))
3490  {
3491  RestrictInfo *rinfo = (RestrictInfo *) node;
3492 
3493  if (rinfo->eval_cost.startup < 0)
3494  {
3495  cost_qual_eval_context locContext;
3496 
3497  locContext.root = context->root;
3498  locContext.total.startup = 0;
3499  locContext.total.per_tuple = 0;
3500 
3501  /*
3502  * For an OR clause, recurse into the marked-up tree so that we
3503  * set the eval_cost for contained RestrictInfos too.
3504  */
3505  if (rinfo->orclause)
3506  cost_qual_eval_walker((Node *) rinfo->orclause, &locContext);
3507  else
3508  cost_qual_eval_walker((Node *) rinfo->clause, &locContext);
3509 
3510  /*
3511  * If the RestrictInfo is marked pseudoconstant, it will be tested
3512  * only once, so treat its cost as all startup cost.
3513  */
3514  if (rinfo->pseudoconstant)
3515  {
3516  /* count one execution during startup */
3517  locContext.total.startup += locContext.total.per_tuple;
3518  locContext.total.per_tuple = 0;
3519  }
3520  rinfo->eval_cost = locContext.total;
3521  }
3522  context->total.startup += rinfo->eval_cost.startup;
3523  context->total.per_tuple += rinfo->eval_cost.per_tuple;
3524  /* do NOT recurse into children */
3525  return false;
3526  }
3527 
3528  /*
3529  * For each operator or function node in the given tree, we charge the
3530  * estimated execution cost given by pg_proc.procost (remember to multiply
3531  * this by cpu_operator_cost).
3532  *
3533  * Vars and Consts are charged zero, and so are boolean operators (AND,
3534  * OR, NOT). Simplistic, but a lot better than no model at all.
3535  *
3536  * Should we try to account for the possibility of short-circuit
3537  * evaluation of AND/OR? Probably *not*, because that would make the
3538  * results depend on the clause ordering, and we are not in any position
3539  * to expect that the current ordering of the clauses is the one that's
3540  * going to end up being used. The above per-RestrictInfo caching would
3541  * not mix well with trying to re-order clauses anyway.
3542  *
3543  * Another issue that is entirely ignored here is that if a set-returning
3544  * function is below top level in the tree, the functions/operators above
3545  * it will need to be evaluated multiple times. In practical use, such
3546  * cases arise so seldom as to not be worth the added complexity needed;
3547  * moreover, since our rowcount estimates for functions tend to be pretty
3548  * phony, the results would also be pretty phony.
3549  */
3550  if (IsA(node, FuncExpr))
3551  {
3552  context->total.per_tuple +=
3553  get_func_cost(((FuncExpr *) node)->funcid) * cpu_operator_cost;
3554  }
3555  else if (IsA(node, OpExpr) ||
3556  IsA(node, DistinctExpr) ||
3557  IsA(node, NullIfExpr))
3558  {
3559  /* rely on struct equivalence to treat these all alike */
3560  set_opfuncid((OpExpr *) node);
3561  context->total.per_tuple +=
3562  get_func_cost(((OpExpr *) node)->opfuncid) * cpu_operator_cost;
3563  }
3564  else if (IsA(node, ScalarArrayOpExpr))
3565  {
3566  /*
3567  * Estimate that the operator will be applied to about half of the
3568  * array elements before the answer is determined.
3569  */
3570  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
3571  Node *arraynode = (Node *) lsecond(saop->args);
3572 
3573  set_sa_opfuncid(saop);
3574  context->total.per_tuple += get_func_cost(saop->opfuncid) *
3575  cpu_operator_cost * estimate_array_length(arraynode) * 0.5;
3576  }
3577  else if (IsA(node, Aggref) ||
3578  IsA(node, WindowFunc))
3579  {
3580  /*
3581  * Aggref and WindowFunc nodes are (and should be) treated like Vars,
3582  * ie, zero execution cost in the current model, because they behave
3583  * essentially like Vars at execution. We disregard the costs of
3584  * their input expressions for the same reason. The actual execution
3585  * costs of the aggregate/window functions and their arguments have to
3586  * be factored into plan-node-specific costing of the Agg or WindowAgg
3587  * plan node.
3588  */
3589  return false; /* don't recurse into children */
3590  }
3591  else if (IsA(node, CoerceViaIO))
3592  {
3593  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3594  Oid iofunc;
3595  Oid typioparam;
3596  bool typisvarlena;
3597 
3598  /* check the result type's input function */
3599  getTypeInputInfo(iocoerce->resulttype,
3600  &iofunc, &typioparam);
3601  context->total.per_tuple += get_func_cost(iofunc) * cpu_operator_cost;
3602  /* check the input type's output function */
3603  getTypeOutputInfo(exprType((Node *) iocoerce->arg),
3604  &iofunc, &typisvarlena);
3605  context->total.per_tuple += get_func_cost(iofunc) * cpu_operator_cost;
3606  }
3607  else if (IsA(node, ArrayCoerceExpr))
3608  {
3609  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
3610  Node *arraynode = (Node *) acoerce->arg;
3611 
3612  if (OidIsValid(acoerce->elemfuncid))
3613  context->total.per_tuple += get_func_cost(acoerce->elemfuncid) *
3615  }
3616  else if (IsA(node, RowCompareExpr))
3617  {
3618  /* Conservatively assume we will check all the columns */
3619  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3620  ListCell *lc;
3621 
3622  foreach(lc, rcexpr->opnos)
3623  {
3624  Oid opid = lfirst_oid(lc);
3625 
3626  context->total.per_tuple += get_func_cost(get_opcode(opid)) *
3628  }
3629  }
3630  else if (IsA(node, MinMaxExpr) ||
3631  IsA(node, SQLValueFunction) ||
3632  IsA(node, XmlExpr) ||
3633  IsA(node, CoerceToDomain) ||
3634  IsA(node, NextValueExpr))
3635  {
3636  /* Treat all these as having cost 1 */
3637  context->total.per_tuple += cpu_operator_cost;
3638  }
3639  else if (IsA(node, CurrentOfExpr))
3640  {
3641  /* Report high cost to prevent selection of anything but TID scan */
3642  context->total.startup += disable_cost;
3643  }
3644  else if (IsA(node, SubLink))
3645  {
3646  /* This routine should not be applied to un-planned expressions */
3647  elog(ERROR, "cannot handle unplanned sub-select");
3648  }
3649  else if (IsA(node, SubPlan))
3650  {
3651  /*
3652  * A subplan node in an expression typically indicates that the
3653  * subplan will be executed on each evaluation, so charge accordingly.
3654  * (Sub-selects that can be executed as InitPlans have already been
3655  * removed from the expression.)
3656  */
3657  SubPlan *subplan = (SubPlan *) node;
3658 
3659  context->total.startup += subplan->startup_cost;
3660  context->total.per_tuple += subplan->per_call_cost;
3661 
3662  /*
3663  * We don't want to recurse into the testexpr, because it was already
3664  * counted in the SubPlan node's costs. So we're done.
3665  */
3666  return false;
3667  }
3668  else if (IsA(node, AlternativeSubPlan))
3669  {
3670  /*
3671  * Arbitrarily use the first alternative plan for costing. (We should
3672  * certainly only include one alternative, and we don't yet have
3673  * enough information to know which one the executor is most likely to
3674  * use.)
3675  */
3676  AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
3677 
3678  return cost_qual_eval_walker((Node *) linitial(asplan->subplans),
3679  context);
3680  }
3681  else if (IsA(node, PlaceHolderVar))
3682  {
3683  /*
3684  * A PlaceHolderVar should be given cost zero when considering general
3685  * expression evaluation costs. The expense of doing the contained
3686  * expression is charged as part of the tlist eval costs of the scan
3687  * or join where the PHV is first computed (see set_rel_width and
3688  * add_placeholders_to_joinrel). If we charged it again here, we'd be
3689  * double-counting the cost for each level of plan that the PHV
3690  * bubbles up through. Hence, return without recursing into the
3691  * phexpr.
3692  */
3693  return false;
3694  }
3695 
3696  /* recurse into children */
3698  (void *) context);
3699 }
QualCost eval_cost
Definition: relation.h:1784
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
PlannerInfo * root
Definition: costsize.c:133
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
Expr * orclause
Definition: relation.h:1778
Oid resulttype
Definition: primnodes.h:812
bool pseudoconstant
Definition: relation.h:1755
Definition: nodes.h:509
float4 get_func_cost(Oid funcid)
Definition: lsyscache.c:1641
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define lsecond(l)
Definition: pg_list.h:116
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2094
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Cost disable_cost
Definition: costsize.c:114
double cpu_operator_cost
Definition: costsize.c:108
Expr * arg
Definition: primnodes.h:811
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2599
Expr * clause
Definition: relation.h:1747
Cost per_call_cost
Definition: primnodes.h:713
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
Definition: costsize.c:3478
#define NULL
Definition: c.h:229
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1843
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1613
Cost startup_cost
Definition: primnodes.h:712
#define elog
Definition: elog.h:219
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1624
#define lfirst_oid(lc)
Definition: pg_list.h:108
void cost_recursive_union ( Path runion,
Path nrterm,
Path rterm 
)

Definition at line 1564 of file costsize.c.

References cpu_tuple_cost, Max, Path::pathtarget, Path::rows, Path::startup_cost, Path::total_cost, and PathTarget::width.

Referenced by create_recursiveunion_path().

1565 {
1566  Cost startup_cost;
1567  Cost total_cost;
1568  double total_rows;
1569 
1570  /* We probably have decent estimates for the non-recursive term */
1571  startup_cost = nrterm->startup_cost;
1572  total_cost = nrterm->total_cost;
1573  total_rows = nrterm->rows;
1574 
1575  /*
1576  * We arbitrarily assume that about 10 recursive iterations will be
1577  * needed, and that we've managed to get a good fix on the cost and output
1578  * size of each one of them. These are mighty shaky assumptions but it's
1579  * hard to see how to do better.
1580  */
1581  total_cost += 10 * rterm->total_cost;
1582  total_rows += 10 * rterm->rows;
1583 
1584  /*
1585  * Also charge cpu_tuple_cost per row to account for the costs of
1586  * manipulating the tuplestores. (We don't worry about possible
1587  * spill-to-disk costs.)
1588  */
1589  total_cost += cpu_tuple_cost * total_rows;
1590 
1591  runion->startup_cost = startup_cost;
1592  runion->total_cost = total_cost;
1593  runion->rows = total_rows;
1594  runion->pathtarget->width = Max(nrterm->pathtarget->width,
1595  rterm->pathtarget->width);
1596 }
PathTarget * pathtarget
Definition: relation.h:955
Cost startup_cost
Definition: relation.h:965
Cost total_cost
Definition: relation.h:966
#define Max(x, y)
Definition: c.h:800
double rows
Definition: relation.h:964
double cpu_tuple_cost
Definition: costsize.c:106
int width
Definition: relation.h:887
double Cost
Definition: nodes.h:640
static void cost_rescan ( PlannerInfo root,
Path path,
Cost rescan_startup_cost,
Cost rescan_total_cost 
)
static

Definition at line 3331 of file costsize.c.

References cpu_operator_cost, cpu_tuple_cost, Path::pathtarget, Path::pathtype, relation_byte_size(), Path::rows, seq_page_cost, Path::startup_cost, T_CteScan, T_FunctionScan, T_HashJoin, T_Material, T_Sort, T_WorkTableScan, Path::total_cost, PathTarget::width, and work_mem.

Referenced by initial_cost_nestloop().

3334 {
3335  switch (path->pathtype)
3336  {
3337  case T_FunctionScan:
3338 
3339  /*
3340  * Currently, nodeFunctionscan.c always executes the function to
3341  * completion before returning any rows, and caches the results in
3342  * a tuplestore. So the function eval cost is all startup cost
3343  * and isn't paid over again on rescans. However, all run costs
3344  * will be paid over again.
3345  */
3346  *rescan_startup_cost = 0;
3347  *rescan_total_cost = path->total_cost - path->startup_cost;
3348  break;
3349  case T_HashJoin:
3350 
3351  /*
3352  * If it's a single-batch join, we don't need to rebuild the hash
3353  * table during a rescan.
3354  */
3355  if (((HashPath *) path)->num_batches == 1)
3356  {
3357  /* Startup cost is exactly the cost of hash table building */
3358  *rescan_startup_cost = 0;
3359  *rescan_total_cost = path->total_cost - path->startup_cost;
3360  }
3361  else
3362  {
3363  /* Otherwise, no special treatment */
3364  *rescan_startup_cost = path->startup_cost;
3365  *rescan_total_cost = path->total_cost;
3366  }
3367  break;
3368  case T_CteScan:
3369  case T_WorkTableScan:
3370  {
3371  /*
3372  * These plan types materialize their final result in a
3373  * tuplestore or tuplesort object. So the rescan cost is only
3374  * cpu_tuple_cost per tuple, unless the result is large enough
3375  * to spill to disk.
3376  */
3377  Cost run_cost = cpu_tuple_cost * path->rows;
3378  double nbytes = relation_byte_size(path->rows,
3379  path->pathtarget->width);
3380  long work_mem_bytes = work_mem * 1024L;
3381 
3382  if (nbytes > work_mem_bytes)
3383  {
3384  /* It will spill, so account for re-read cost */
3385  double npages = ceil(nbytes / BLCKSZ);
3386 
3387  run_cost += seq_page_cost * npages;
3388  }
3389  *rescan_startup_cost = 0;
3390  *rescan_total_cost = run_cost;
3391  }
3392  break;
3393  case T_Material:
3394  case T_Sort:
3395  {
3396  /*
3397  * These plan types not only materialize their results, but do
3398  * not implement qual filtering or projection. So they are
3399  * even cheaper to rescan than the ones above. We charge only
3400  * cpu_operator_cost per tuple. (Note: keep that in sync with
3401  * the run_cost charge in cost_sort, and also see comments in
3402  * cost_material before you change it.)
3403  */
3404  Cost run_cost = cpu_operator_cost * path->rows;
3405  double nbytes = relation_byte_size(path->rows,
3406  path->pathtarget->width);
3407  long work_mem_bytes = work_mem * 1024L;
3408 
3409  if (nbytes > work_mem_bytes)
3410  {
3411  /* It will spill, so account for re-read cost */
3412  double npages = ceil(nbytes / BLCKSZ);
3413 
3414  run_cost += seq_page_cost * npages;
3415  }
3416  *rescan_startup_cost = 0;
3417  *rescan_total_cost = run_cost;
3418  }
3419  break;
3420  default:
3421  *rescan_startup_cost = path->startup_cost;
3422  *rescan_total_cost = path->total_cost;
3423  break;
3424  }
3425 }
PathTarget * pathtarget
Definition: relation.h:955
Definition: nodes.h:75
NodeTag pathtype
Definition: relation.h:952
Cost startup_cost
Definition: relation.h:965
double cpu_operator_cost
Definition: costsize.c:108
static double relation_byte_size(double tuples, int width)
Definition: costsize.c:5052
int work_mem
Definition: globals.c:113
Cost total_cost
Definition: relation.h:966
double rows
Definition: relation.h:964
double cpu_tuple_cost
Definition: costsize.c:106
int width
Definition: relation.h:887
double seq_page_cost
Definition: costsize.c:104
double Cost
Definition: nodes.h:640
void cost_samplescan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 274 of file costsize.c.

References Assert, PathTarget::cost, cpu_tuple_cost, get_restriction_qual_cost(), get_tablespace_page_costs(), GetTsmRoutine(), TsmRoutine::NextSampleBlock, NULL, RelOptInfo::pages, Path::pathtarget, QualCost::per_tuple, planner_rt_fetch, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::reltablespace, RelOptInfo::rows, Path::rows, RTE_RELATION, RangeTblEntry::rtekind, QualCost::startup, Path::startup_cost, RangeTblEntry::tablesample, Path::total_cost, TableSampleClause::tsmhandler, and RelOptInfo::tuples.

Referenced by create_samplescan_path().

276 {
277  Cost startup_cost = 0;
278  Cost run_cost = 0;
279  RangeTblEntry *rte;
280  TableSampleClause *tsc;
281  TsmRoutine *tsm;
282  double spc_seq_page_cost,
283  spc_random_page_cost,
284  spc_page_cost;
285  QualCost qpqual_cost;
286  Cost cpu_per_tuple;
287 
288  /* Should only be applied to base relations with tablesample clauses */
289  Assert(baserel->relid > 0);
290  rte = planner_rt_fetch(baserel->relid, root);
291  Assert(rte->rtekind == RTE_RELATION);
292  tsc = rte->tablesample;
293  Assert(tsc != NULL);
294  tsm = GetTsmRoutine(tsc->tsmhandler);
295 
296  /* Mark the path with the correct row estimate */
297  if (param_info)
298  path->rows = param_info->ppi_rows;
299  else
300  path->rows = baserel->rows;
301 
302  /* fetch estimated page cost for tablespace containing table */
304  &spc_random_page_cost,
305  &spc_seq_page_cost);
306 
307  /* if NextSampleBlock is used, assume random access, else sequential */
308  spc_page_cost = (tsm->NextSampleBlock != NULL) ?
309  spc_random_page_cost : spc_seq_page_cost;
310 
311  /*
312  * disk costs (recall that baserel->pages has already been set to the
313  * number of pages the sampling method will visit)
314  */
315  run_cost += spc_page_cost * baserel->pages;
316 
317  /*
318  * CPU costs (recall that baserel->tuples has already been set to the
319  * number of tuples the sampling method will select). Note that we ignore
320  * execution cost of the TABLESAMPLE parameter expressions; they will be
321  * evaluated only once per scan, and in most usages they'll likely be
322  * simple constants anyway. We also don't charge anything for the
323  * calculations the sampling method might do internally.
324  */
325  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
326 
327  startup_cost += qpqual_cost.startup;
328  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
329  run_cost += cpu_per_tuple * baserel->tuples;
330  /* tlist eval costs are paid per output row, not per tuple scanned */
331  startup_cost += path->pathtarget->cost.startup;
332  run_cost += path->pathtarget->cost.per_tuple * path->rows;
333 
334  path->startup_cost = startup_cost;
335  path->total_cost = startup_cost + run_cost;
336 }
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Oid reltablespace
Definition: relation.h:554
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
Cost startup_cost
Definition: relation.h:965
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:72
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:553
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
BlockNumber pages
Definition: relation.h:564
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
RTEKind rtekind
Definition: parsenodes.h:936
struct TableSampleClause * tablesample
Definition: parsenodes.h:954
double Cost
Definition: nodes.h:640
void cost_seqscan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 197 of file costsize.c.

References Assert, clamp_row_est(), PathTarget::cost, cpu_tuple_cost, disable_cost, enable_seqscan, get_parallel_divisor(), get_restriction_qual_cost(), get_tablespace_page_costs(), NULL, RelOptInfo::pages, Path::parallel_workers, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::reltablespace, RelOptInfo::rows, Path::rows, RTE_RELATION, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_seqscan_path().

199 {
200  Cost startup_cost = 0;
201  Cost cpu_run_cost;
202  Cost disk_run_cost;
203  double spc_seq_page_cost;
204  QualCost qpqual_cost;
205  Cost cpu_per_tuple;
206 
207  /* Should only be applied to base relations */
208  Assert(baserel->relid > 0);
209  Assert(baserel->rtekind == RTE_RELATION);
210 
211  /* Mark the path with the correct row estimate */
212  if (param_info)
213  path->rows = param_info->ppi_rows;
214  else
215  path->rows = baserel->rows;
216 
217  if (!enable_seqscan)
218  startup_cost += disable_cost;
219 
220  /* fetch estimated page cost for tablespace containing table */
222  NULL,
223  &spc_seq_page_cost);
224 
225  /*
226  * disk costs
227  */
228  disk_run_cost = spc_seq_page_cost * baserel->pages;
229 
230  /* CPU costs */
231  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
232 
233  startup_cost += qpqual_cost.startup;
234  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
235  cpu_run_cost = cpu_per_tuple * baserel->tuples;
236  /* tlist eval costs are paid per output row, not per tuple scanned */
237  startup_cost += path->pathtarget->cost.startup;
238  cpu_run_cost += path->pathtarget->cost.per_tuple * path->rows;
239 
240  /* Adjust costing for parallelism, if used. */
241  if (path->parallel_workers > 0)
242  {
243  double parallel_divisor = get_parallel_divisor(path);
244 
245  /* The CPU cost is divided among all the workers. */
246  cpu_run_cost /= parallel_divisor;
247 
248  /*
249  * It may be possible to amortize some of the I/O cost, but probably
250  * not very much, because most operating systems already do aggressive
251  * prefetching. For now, we assume that the disk run cost can't be
252  * amortized at all.
253  */
254 
255  /*
256  * In the case of a parallel plan, the row count needs to represent
257  * the number of tuples processed per worker.
258  */
259  path->rows = clamp_row_est(path->rows / parallel_divisor);
260  }
261 
262  path->startup_cost = startup_cost;
263  path->total_cost = startup_cost + cpu_run_cost + disk_run_cost;
264 }
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Oid reltablespace
Definition: relation.h:554
int parallel_workers
Definition: relation.h:961
bool enable_seqscan
Definition: costsize.c:118
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
static double get_parallel_divisor(Path *path)
Definition: costsize.c:5073
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:553
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
BlockNumber pages
Definition: relation.h:564
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
double clamp_row_est(double nrows)
Definition: costsize.c:173
double Cost
Definition: nodes.h:640
void cost_sort ( Path path,
PlannerInfo root,
List pathkeys,
Cost  input_cost,
double  tuples,
int  width,
Cost  comparison_cost,
int  sort_mem,
double  limit_tuples 
)

Definition at line 1644 of file costsize.c.

References cpu_operator_cost, disable_cost, enable_sort, LOG2, random_page_cost, relation_byte_size(), Path::rows, seq_page_cost, Path::startup_cost, Path::total_cost, and tuplesort_merge_order().

Referenced by choose_hashed_setop(), create_gather_merge_path(), create_groupingsets_path(), create_merge_append_path(), create_sort_path(), create_unique_path(), initial_cost_mergejoin(), label_sort_with_costsize(), and plan_cluster_use_sort().

1648 {
1649  Cost startup_cost = input_cost;
1650  Cost run_cost = 0;
1651  double input_bytes = relation_byte_size(tuples, width);
1652  double output_bytes;
1653  double output_tuples;
1654  long sort_mem_bytes = sort_mem * 1024L;
1655 
1656  if (!enable_sort)
1657  startup_cost += disable_cost;
1658 
1659  path->rows = tuples;
1660 
1661  /*
1662  * We want to be sure the cost of a sort is never estimated as zero, even
1663  * if passed-in tuple count is zero. Besides, mustn't do log(0)...
1664  */
1665  if (tuples < 2.0)
1666  tuples = 2.0;
1667 
1668  /* Include the default cost-per-comparison */
1669  comparison_cost += 2.0 * cpu_operator_cost;
1670 
1671  /* Do we have a useful LIMIT? */
1672  if (limit_tuples > 0 && limit_tuples < tuples)
1673  {
1674  output_tuples = limit_tuples;
1675  output_bytes = relation_byte_size(output_tuples, width);
1676  }
1677  else
1678  {
1679  output_tuples = tuples;
1680  output_bytes = input_bytes;
1681  }
1682 
1683  if (output_bytes > sort_mem_bytes)
1684  {
1685  /*
1686  * We'll have to use a disk-based sort of all the tuples
1687  */
1688  double npages = ceil(input_bytes / BLCKSZ);
1689  double nruns = input_bytes / sort_mem_bytes;
1690  double mergeorder = tuplesort_merge_order(sort_mem_bytes);
1691  double log_runs;
1692  double npageaccesses;
1693 
1694  /*
1695  * CPU costs
1696  *
1697  * Assume about N log2 N comparisons
1698  */
1699  startup_cost += comparison_cost * tuples * LOG2(tuples);
1700 
1701  /* Disk costs */
1702 
1703  /* Compute logM(r) as log(r) / log(M) */
1704  if (nruns > mergeorder)
1705  log_runs = ceil(log(nruns) / log(mergeorder));
1706  else
1707  log_runs = 1.0;
1708  npageaccesses = 2.0 * npages * log_runs;
1709  /* Assume 3/4ths of accesses are sequential, 1/4th are not */
1710  startup_cost += npageaccesses *
1711  (seq_page_cost * 0.75 + random_page_cost * 0.25);
1712  }
1713  else if (tuples > 2 * output_tuples || input_bytes > sort_mem_bytes)
1714  {
1715  /*
1716  * We'll use a bounded heap-sort keeping just K tuples in memory, for
1717  * a total number of tuple comparisons of N log2 K; but the constant
1718  * factor is a bit higher than for quicksort. Tweak it so that the
1719  * cost curve is continuous at the crossover point.
1720  */
1721  startup_cost += comparison_cost * tuples * LOG2(2.0 * output_tuples);
1722  }
1723  else
1724  {
1725  /* We'll use plain quicksort on all the input tuples */
1726  startup_cost += comparison_cost * tuples * LOG2(tuples);
1727  }
1728 
1729  /*
1730  * Also charge a small amount (arbitrarily set equal to operator cost) per
1731  * extracted tuple. We don't charge cpu_tuple_cost because a Sort node
1732  * doesn't do qual-checking or projection, so it has less overhead than
1733  * most plan nodes. Note it's correct to use tuples not output_tuples
1734  * here --- the upper LIMIT will pro-rate the run cost so we'd be double
1735  * counting the LIMIT otherwise.
1736  */
1737  run_cost += cpu_operator_cost * tuples;
1738 
1739  path->startup_cost = startup_cost;
1740  path->total_cost = startup_cost + run_cost;
1741 }
bool enable_sort
Definition: costsize.c:123
double random_page_cost
Definition: costsize.c:105
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
double cpu_operator_cost
Definition: costsize.c:108
static double relation_byte_size(double tuples, int width)
Definition: costsize.c:5052
Cost total_cost
Definition: relation.h:966
#define LOG2(x)
Definition: costsize.c:101
double rows
Definition: relation.h:964
int tuplesort_merge_order(int64 allowedMem)
Definition: tuplesort.c:2305
double seq_page_cost
Definition: costsize.c:104
double Cost
Definition: nodes.h:640
void cost_subplan ( PlannerInfo root,
SubPlan subplan,
Plan plan 
)

Definition at line 3238 of file costsize.c.

References ALL_SUBLINK, ANY_SUBLINK, clamp_row_est(), cost_qual_eval(), cpu_operator_cost, ExecMaterializesOutput(), EXISTS_SUBLINK, make_ands_implicit(), NIL, nodeTag, SubPlan::parParam, SubPlan::per_call_cost, QualCost::per_tuple, Plan::plan_rows, QualCost::startup, Plan::startup_cost, SubPlan::startup_cost, SubPlan::subLinkType, SubPlan::testexpr, Plan::total_cost, and SubPlan::useHashTable.

Referenced by build_subplan(), SS_make_initplan_from_plan(), and SS_process_ctes().

3239 {
3240  QualCost sp_cost;
3241 
3242  /* Figure any cost for evaluating the testexpr */
3243  cost_qual_eval(&sp_cost,
3244  make_ands_implicit((Expr *) subplan->testexpr),
3245  root);
3246 
3247  if (subplan->useHashTable)
3248  {
3249  /*
3250  * If we are using a hash table for the subquery outputs, then the
3251  * cost of evaluating the query is a one-time cost. We charge one
3252  * cpu_operator_cost per tuple for the work of loading the hashtable,
3253  * too.
3254  */
3255  sp_cost.startup += plan->total_cost +
3256  cpu_operator_cost * plan->plan_rows;
3257 
3258  /*
3259  * The per-tuple costs include the cost of evaluating the lefthand
3260  * expressions, plus the cost of probing the hashtable. We already
3261  * accounted for the lefthand expressions as part of the testexpr, and
3262  * will also have counted one cpu_operator_cost for each comparison
3263  * operator. That is probably too low for the probing cost, but it's
3264  * hard to make a better estimate, so live with it for now.
3265  */
3266  }
3267  else
3268  {
3269  /*
3270  * Otherwise we will be rescanning the subplan output on each
3271  * evaluation. We need to estimate how much of the output we will
3272  * actually need to scan. NOTE: this logic should agree with the
3273  * tuple_fraction estimates used by make_subplan() in
3274  * plan/subselect.c.
3275  */
3276  Cost plan_run_cost = plan->total_cost - plan->startup_cost;
3277 
3278  if (subplan->subLinkType == EXISTS_SUBLINK)
3279  {
3280  /* we only need to fetch 1 tuple; clamp to avoid zero divide */
3281  sp_cost.per_tuple += plan_run_cost / clamp_row_est(plan->plan_rows);
3282  }
3283  else if (subplan->subLinkType == ALL_SUBLINK ||
3284  subplan->subLinkType == ANY_SUBLINK)
3285  {
3286  /* assume we need 50% of the tuples */
3287  sp_cost.per_tuple += 0.50 * plan_run_cost;
3288  /* also charge a cpu_operator_cost per row examined */
3289  sp_cost.per_tuple += 0.50 * plan->plan_rows * cpu_operator_cost;
3290  }
3291  else
3292  {
3293  /* assume we need all tuples */
3294  sp_cost.per_tuple += plan_run_cost;
3295  }
3296 
3297  /*
3298  * Also account for subplan's startup cost. If the subplan is
3299  * uncorrelated or undirect correlated, AND its topmost node is one
3300  * that materializes its output, assume that we'll only need to pay
3301  * its startup cost once; otherwise assume we pay the startup cost
3302  * every time.
3303  */
3304  if (subplan->parParam == NIL &&
3306  sp_cost.startup += plan->startup_cost;
3307  else
3308  sp_cost.per_tuple += plan->startup_cost;
3309  }
3310 
3311  subplan->startup_cost = sp_cost.startup;
3312  subplan->per_call_cost = sp_cost.per_tuple;
3313 }
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:131
SubLinkType subLinkType
Definition: primnodes.h:684
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
List * make_ands_implicit(Expr *clause)
Definition: clauses.c:378
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3438
Cost startup_cost
Definition: plannodes.h:125
double cpu_operator_cost
Definition: costsize.c:108
Node * testexpr
Definition: primnodes.h:686
Cost per_call_cost
Definition: primnodes.h:713
List * parParam
Definition: primnodes.h:709
#define nodeTag(nodeptr)
Definition: nodes.h:514
Cost total_cost
Definition: plannodes.h:126
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:572
bool useHashTable
Definition: primnodes.h:698
Cost startup_cost
Definition: primnodes.h:712
double clamp_row_est(double nrows)
Definition: costsize.c:173
double Cost
Definition: nodes.h:640
void cost_subqueryscan ( SubqueryScanPath path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 1267 of file costsize.c.

References Assert, PathTarget::cost, cpu_tuple_cost, get_restriction_qual_cost(), SubqueryScanPath::path, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::rows, Path::rows, RTE_SUBQUERY, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, SubqueryScanPath::subpath, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_subqueryscan_path().

1269 {
1270  Cost startup_cost;
1271  Cost run_cost;
1272  QualCost qpqual_cost;
1273  Cost cpu_per_tuple;
1274 
1275  /* Should only be applied to base relations that are subqueries */
1276  Assert(baserel->relid > 0);
1277  Assert(baserel->rtekind == RTE_SUBQUERY);
1278 
1279  /* Mark the path with the correct row estimate */
1280  if (param_info)
1281  path->path.rows = param_info->ppi_rows;
1282  else
1283  path->path.rows = baserel->rows;
1284 
1285  /*
1286  * Cost of path is cost of evaluating the subplan, plus cost of evaluating
1287  * any restriction clauses and tlist that will be attached to the
1288  * SubqueryScan node, plus cpu_tuple_cost to account for selection and
1289  * projection overhead.
1290  */
1291  path->path.startup_cost = path->subpath->startup_cost;
1292  path->path.total_cost = path->subpath->total_cost;
1293 
1294  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1295 
1296  startup_cost = qpqual_cost.startup;
1297  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
1298  run_cost = cpu_per_tuple * baserel->tuples;
1299 
1300  /* tlist eval costs are paid per output row, not per tuple scanned */
1301  startup_cost += path->path.pathtarget->cost.startup;
1302  run_cost += path->path.pathtarget->cost.per_tuple * path->path.rows;
1303 
1304  path->path.startup_cost += startup_cost;
1305  path->path.total_cost += startup_cost + run_cost;
1306 }
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
Index relid
Definition: relation.h:553
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
double Cost
Definition: nodes.h:640
void cost_tablefuncscan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 1377 of file costsize.c.

References Assert, PathTarget::cost, cost_qual_eval_node(), cpu_tuple_cost, get_restriction_qual_cost(), Path::pathtarget, QualCost::per_tuple, planner_rt_fetch, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::rows, Path::rows, RTE_TABLEFUNC, RangeTblEntry::rtekind, QualCost::startup, Path::startup_cost, RangeTblEntry::tablefunc, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_tablefuncscan_path().

1379 {
1380  Cost startup_cost = 0;
1381  Cost run_cost = 0;
1382  QualCost qpqual_cost;
1383  Cost cpu_per_tuple;
1384  RangeTblEntry *rte;
1385  QualCost exprcost;
1386 
1387  /* Should only be applied to base relations that are functions */
1388  Assert(baserel->relid > 0);
1389  rte = planner_rt_fetch(baserel->relid, root);
1390  Assert(rte->rtekind == RTE_TABLEFUNC);
1391 
1392  /* Mark the path with the correct row estimate */
1393  if (param_info)
1394  path->rows = param_info->ppi_rows;
1395  else
1396  path->rows = baserel->rows;
1397 
1398  /*
1399  * Estimate costs of executing the table func expression(s).
1400  *
1401  * XXX in principle we ought to charge tuplestore spill costs if the
1402  * number of rows is large. However, given how phony our rowcount
1403  * estimates for tablefuncs tend to be, there's not a lot of point in that
1404  * refinement right now.
1405  */
1406  cost_qual_eval_node(&exprcost, (Node *) rte->tablefunc, root);
1407 
1408  startup_cost += exprcost.startup + exprcost.per_tuple;
1409 
1410  /* Add scanning CPU costs */
1411  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1412 
1413  startup_cost += qpqual_cost.startup;
1414  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
1415  run_cost += cpu_per_tuple * baserel->tuples;
1416 
1417  /* tlist eval costs are paid per output row, not per tuple scanned */
1418  startup_cost += path->pathtarget->cost.startup;
1419  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1420 
1421  path->startup_cost = startup_cost;
1422  path->total_cost = startup_cost + run_cost;
1423 }
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3464
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Definition: nodes.h:509
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
TableFunc * tablefunc
Definition: parsenodes.h:996
Cost startup_cost
Definition: relation.h:965
Index relid
Definition: relation.h:553
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
RTEKind rtekind
Definition: parsenodes.h:936
double Cost
Definition: nodes.h:640
void cost_tidscan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
List tidquals,
ParamPathInfo param_info 
)

Definition at line 1164 of file costsize.c.

References ScalarArrayOpExpr::args, Assert, RelOptInfo::baserestrictcost, PathTarget::cost, cost_qual_eval(), cpu_tuple_cost, disable_cost, enable_tidscan, estimate_array_length(), get_restriction_qual_cost(), get_tablespace_page_costs(), IsA, lfirst, lsecond, NULL, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::reltablespace, RelOptInfo::rows, Path::rows, RTE_RELATION, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, and Path::total_cost.

Referenced by create_tidscan_path().

1166 {
1167  Cost startup_cost = 0;
1168  Cost run_cost = 0;
1169  bool isCurrentOf = false;
1170  QualCost qpqual_cost;
1171  Cost cpu_per_tuple;
1172  QualCost tid_qual_cost;
1173  int ntuples;
1174  ListCell *l;
1175  double spc_random_page_cost;
1176 
1177  /* Should only be applied to base relations */
1178  Assert(baserel->relid > 0);
1179  Assert(baserel->rtekind == RTE_RELATION);
1180 
1181  /* Mark the path with the correct row estimate */
1182  if (param_info)
1183  path->rows = param_info->ppi_rows;
1184  else
1185  path->rows = baserel->rows;
1186 
1187  /* Count how many tuples we expect to retrieve */
1188  ntuples = 0;
1189  foreach(l, tidquals)
1190  {
1191  if (IsA(lfirst(l), ScalarArrayOpExpr))
1192  {
1193  /* Each element of the array yields 1 tuple */
1195  Node *arraynode = (Node *) lsecond(saop->args);
1196 
1197  ntuples += estimate_array_length(arraynode);
1198  }
1199  else if (IsA(lfirst(l), CurrentOfExpr))
1200  {
1201  /* CURRENT OF yields 1 tuple */
1202  isCurrentOf = true;
1203  ntuples++;
1204  }
1205  else
1206  {
1207  /* It's just CTID = something, count 1 tuple */
1208  ntuples++;
1209  }
1210  }
1211 
1212  /*
1213  * We must force TID scan for WHERE CURRENT OF, because only nodeTidscan.c
1214  * understands how to do it correctly. Therefore, honor enable_tidscan
1215  * only when CURRENT OF isn't present. Also note that cost_qual_eval
1216  * counts a CurrentOfExpr as having startup cost disable_cost, which we
1217  * subtract off here; that's to prevent other plan types such as seqscan
1218  * from winning.
1219  */
1220  if (isCurrentOf)
1221  {
1223  startup_cost -= disable_cost;
1224  }
1225  else if (!enable_tidscan)
1226  startup_cost += disable_cost;
1227 
1228  /*
1229  * The TID qual expressions will be computed once, any other baserestrict
1230  * quals once per retrieved tuple.
1231  */
1232  cost_qual_eval(&tid_qual_cost, tidquals, root);
1233 
1234  /* fetch estimated page cost for tablespace containing table */
1236  &spc_random_page_cost,
1237  NULL);
1238 
1239  /* disk costs --- assume each tuple on a different page */
1240  run_cost += spc_random_page_cost * ntuples;
1241 
1242  /* Add scanning CPU costs */
1243  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1244 
1245  /* XXX currently we assume TID quals are a subset of qpquals */
1246  startup_cost += qpqual_cost.startup + tid_qual_cost.per_tuple;
1247  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple -
1248  tid_qual_cost.per_tuple;
1249  run_cost += cpu_per_tuple * ntuples;
1250 
1251  /* tlist eval costs are paid per output row, not per tuple scanned */
1252  startup_cost += path->pathtarget->cost.startup;
1253  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1254 
1255  path->startup_cost = startup_cost;
1256  path->total_cost = startup_cost + run_cost;
1257 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
PathTarget * pathtarget
Definition: relation.h:955
bool enable_tidscan
Definition: costsize.c:122
Oid reltablespace
Definition: relation.h:554
Definition: nodes.h:509
#define lsecond(l)
Definition: pg_list.h:116
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2094
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3438
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
Index relid
Definition: relation.h:553
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
QualCost baserestrictcost
Definition: relation.h:586
double Cost
Definition: nodes.h:640
void cost_valuesscan ( Path path,
PlannerInfo root,
RelOptInfo baserel,
ParamPathInfo param_info 
)

Definition at line 1433 of file costsize.c.

References Assert, PathTarget::cost, cpu_operator_cost, cpu_tuple_cost, get_restriction_qual_cost(), Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relid, RelOptInfo::rows, Path::rows, RTE_VALUES, RelOptInfo::rtekind, QualCost::startup, Path::startup_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_valuesscan_path().

1435 {
1436  Cost startup_cost = 0;
1437  Cost run_cost = 0;
1438  QualCost qpqual_cost;
1439  Cost cpu_per_tuple;
1440 
1441  /* Should only be applied to base relations that are values lists */
1442  Assert(baserel->relid > 0);
1443  Assert(baserel->rtekind == RTE_VALUES);
1444 
1445  /* Mark the path with the correct row estimate */
1446  if (param_info)
1447  path->rows = param_info->ppi_rows;
1448  else
1449  path->rows = baserel->rows;
1450 
1451  /*
1452  * For now, estimate list evaluation cost at one operator eval per list
1453  * (probably pretty bogus, but is it worth being smarter?)
1454  */
1455  cpu_per_tuple = cpu_operator_cost;
1456 
1457  /* Add scanning CPU costs */
1458  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1459 
1460  startup_cost += qpqual_cost.startup;
1461  cpu_per_tuple += cpu_tuple_cost + qpqual_cost.per_tuple;
1462  run_cost += cpu_per_tuple * baserel->tuples;
1463 
1464  /* tlist eval costs are paid per output row, not per tuple scanned */
1465  startup_cost += path->pathtarget->cost.startup;
1466  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1467 
1468  path->startup_cost = startup_cost;
1469  path->total_cost = startup_cost + run_cost;
1470 }
PathTarget * pathtarget
Definition: relation.h:955
double tuples
Definition: relation.h:565
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
double cpu_operator_cost
Definition: costsize.c:108
Index relid
Definition: relation.h:553
RTEKind rtekind
Definition: relation.h:555
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3714
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
double Cost
Definition: nodes.h:640
void cost_windowagg ( Path path,
PlannerInfo root,
List windowFuncs,
int  numPartCols,
int  numOrderCols,
Cost  input_startup_cost,
Cost  input_total_cost,
double  input_tuples 
)

Definition at line 1970 of file costsize.c.

References WindowFunc::aggfilter, WindowFunc::args, cost_qual_eval_node(), cpu_operator_cost, cpu_tuple_cost, get_func_cost(), lfirst_node, QualCost::per_tuple, Path::rows, QualCost::startup, Path::startup_cost, Path::total_cost, and WindowFunc::winfnoid.

Referenced by create_windowagg_path().

1974 {
1975  Cost startup_cost;
1976  Cost total_cost;
1977  ListCell *lc;
1978 
1979  startup_cost = input_startup_cost;
1980  total_cost = input_total_cost;
1981 
1982  /*
1983  * Window functions are assumed to cost their stated execution cost, plus
1984  * the cost of evaluating their input expressions, per tuple. Since they
1985  * may in fact evaluate their inputs at multiple rows during each cycle,
1986  * this could be a drastic underestimate; but without a way to know how
1987  * many rows the window function will fetch, it's hard to do better. In
1988  * any case, it's a good estimate for all the built-in window functions,
1989  * so we'll just do this for now.
1990  */
1991  foreach(lc, windowFuncs)
1992  {
1993  WindowFunc *wfunc = lfirst_node(WindowFunc, lc);
1994  Cost wfunccost;
1995  QualCost argcosts;
1996 
1997  wfunccost = get_func_cost(wfunc->winfnoid) * cpu_operator_cost;
1998 
1999  /* also add the input expressions' cost to per-input-row costs */
2000  cost_qual_eval_node(&argcosts, (Node *) wfunc->args, root);
2001  startup_cost += argcosts.startup;
2002  wfunccost += argcosts.per_tuple;
2003 
2004  /*
2005  * Add the filter's cost to per-input-row costs. XXX We should reduce
2006  * input expression costs according to filter selectivity.
2007  */
2008  cost_qual_eval_node(&argcosts, (Node *) wfunc->aggfilter, root);
2009  startup_cost += argcosts.startup;
2010  wfunccost += argcosts.per_tuple;
2011 
2012  total_cost += wfunccost * input_tuples;
2013  }
2014 
2015  /*
2016  * We also charge cpu_operator_cost per grouping column per tuple for
2017  * grouping comparisons, plus cpu_tuple_cost per tuple for general
2018  * overhead.
2019  *
2020  * XXX this neglects costs of spooling the data to disk when it overflows
2021  * work_mem. Sooner or later that should get accounted for.
2022  */
2023  total_cost += cpu_operator_cost * (numPartCols + numOrderCols) * input_tuples;
2024  total_cost += cpu_tuple_cost * input_tuples;
2025 
2026  path->rows = input_tuples;
2027  path->startup_cost = startup_cost;
2028  path->total_cost = total_cost;
2029 }
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3464
List * args
Definition: primnodes.h:359
Definition: nodes.h:509
float4 get_func_cost(Oid funcid)
Definition: lsyscache.c:1641
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
Cost startup_cost
Definition: relation.h:965
#define lfirst_node(type, lc)
Definition: pg_list.h:109
double cpu_operator_cost
Definition: costsize.c:108
Oid winfnoid
Definition: primnodes.h:355
Cost total_cost
Definition: relation.h:966
Expr * aggfilter
Definition: primnodes.h:360
double rows
Definition: relation.h:964
double cpu_tuple_cost
Definition: costsize.c:106
double Cost
Definition: nodes.h:640
static List * extract_nonindex_conditions ( List qual_clauses,
List indexquals 
)
static

Definition at line 753 of file costsize.c.

References is_redundant_derived_clause(), lappend(), lfirst_node, list_member_ptr(), NIL, RestrictInfo::pseudoconstant, and result.

Referenced by cost_index().

754 {
755  List *result = NIL;
756  ListCell *lc;
757 
758  foreach(lc, qual_clauses)
759  {
760  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
761 
762  if (rinfo->pseudoconstant)
763  continue; /* we may drop pseudoconstants here */
764  if (list_member_ptr(indexquals, rinfo))
765  continue; /* simple duplicate */
766  if (is_redundant_derived_clause(rinfo, indexquals))
767  continue; /* derived from same EquivalenceClass */
768  /* ... skip the predicate proof attempt createplan.c will try ... */
769  result = lappend(result, rinfo);
770  }
771  return result;
772 }
#define NIL
Definition: pg_list.h:69
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2455
bool pseudoconstant
Definition: relation.h:1755
return result
Definition: formatting.c:1633
#define lfirst_node(type, lc)
Definition: pg_list.h:109
List * lappend(List *list, void *datum)
Definition: list.c:128
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
Definition: pg_list.h:45
void final_cost_hashjoin ( PlannerInfo root,
HashPath path,
JoinCostWorkspace workspace,
JoinPathExtraData extra 
)

Definition at line 3012 of file costsize.c.

References approx_tuple_count(), Assert, bms_is_subset(), clamp_row_est(), RestrictInfo::clause, PathTarget::cost, cost_qual_eval(), cpu_tuple_cost, disable_cost, enable_hashjoin, estimate_hash_bucketsize(), get_leftop(), get_parallel_divisor(), get_rightop(), JoinPathExtraData::inner_unique, JoinPath::innerjoinpath, IsA, JOIN_ANTI, JOIN_SEMI, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, RestrictInfo::left_bucketsize, RestrictInfo::left_relids, lfirst_node, SemiAntiJoinFactors::match_count, HashPath::num_batches, JoinCostWorkspace::numbatches, JoinCostWorkspace::numbuckets, SemiAntiJoinFactors::outer_match_frac, JoinPath::outerjoinpath, Path::parallel_workers, Path::param_info, Path::parent, JoinPath::path, HashPath::path_hashclauses, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, RelOptInfo::relids, RestrictInfo::right_bucketsize, RestrictInfo::right_relids, rint(), RelOptInfo::rows, Path::rows, JoinCostWorkspace::run_cost, JoinPathExtraData::semifactors, QualCost::startup, Path::startup_cost, JoinCostWorkspace::startup_cost, and Path::total_cost.

Referenced by create_hashjoin_path().

3015 {
3016  Path *outer_path = path->jpath.outerjoinpath;
3017  Path *inner_path = path->jpath.innerjoinpath;
3018  double outer_path_rows = outer_path->rows;
3019  double inner_path_rows = inner_path->rows;
3020  List *hashclauses = path->path_hashclauses;
3021  Cost startup_cost = workspace->startup_cost;
3022  Cost run_cost = workspace->run_cost;
3023  int numbuckets = workspace->numbuckets;
3024  int numbatches = workspace->numbatches;
3025  Cost cpu_per_tuple;
3026  QualCost hash_qual_cost;
3027  QualCost qp_qual_cost;
3028  double hashjointuples;
3029  double virtualbuckets;
3030  Selectivity innerbucketsize;
3031  ListCell *hcl;
3032 
3033  /* Mark the path with the correct row estimate */
3034  if (path->jpath.path.param_info)
3035  path->jpath.path.rows = path->jpath.path.param_info->ppi_rows;
3036  else
3037  path->jpath.path.rows = path->jpath.path.parent->rows;
3038 
3039  /* For partial paths, scale row estimate. */
3040  if (path->jpath.path.parallel_workers > 0)
3041  {
3042  double parallel_divisor = get_parallel_divisor(&path->jpath.path);
3043 
3044  path->jpath.path.rows =
3045  clamp_row_est(path->jpath.path.rows / parallel_divisor);
3046  }
3047 
3048  /*
3049  * We could include disable_cost in the preliminary estimate, but that
3050  * would amount to optimizing for the case where the join method is
3051  * disabled, which doesn't seem like the way to bet.
3052  */
3053  if (!enable_hashjoin)
3054  startup_cost += disable_cost;
3055 
3056  /* mark the path with estimated # of batches */
3057  path->num_batches = numbatches;
3058 
3059  /* and compute the number of "virtual" buckets in the whole join */
3060  virtualbuckets = (double) numbuckets * (double) numbatches;
3061 
3062  /*
3063  * Determine bucketsize fraction for inner relation. We use the smallest
3064  * bucketsize estimated for any individual hashclause; this is undoubtedly
3065  * conservative.
3066  *
3067  * BUT: if inner relation has been unique-ified, we can assume it's good
3068  * for hashing. This is important both because it's the right answer, and
3069  * because we avoid contaminating the cache with a value that's wrong for
3070  * non-unique-ified paths.
3071  */
3072  if (IsA(inner_path, UniquePath))
3073  innerbucketsize = 1.0 / virtualbuckets;
3074  else
3075  {
3076  innerbucketsize = 1.0;
3077  foreach(hcl, hashclauses)
3078  {
3079  RestrictInfo *restrictinfo = lfirst_node(RestrictInfo, hcl);
3080  Selectivity thisbucketsize;
3081 
3082  /*
3083  * First we have to figure out which side of the hashjoin clause
3084  * is the inner side.
3085  *
3086  * Since we tend to visit the same clauses over and over when
3087  * planning a large query, we cache the bucketsize estimate in the
3088  * RestrictInfo node to avoid repeated lookups of statistics.
3089  */
3090  if (bms_is_subset(restrictinfo->right_relids,
3091  inner_path->parent->relids))
3092  {
3093  /* righthand side is inner */
3094  thisbucketsize = restrictinfo->right_bucketsize;
3095  if (thisbucketsize < 0)
3096  {
3097  /* not cached yet */
3098  thisbucketsize =
3100  get_rightop(restrictinfo->clause),
3101  virtualbuckets);
3102  restrictinfo->right_bucketsize = thisbucketsize;
3103  }
3104  }
3105  else
3106  {
3107  Assert(bms_is_subset(restrictinfo->left_relids,
3108  inner_path->parent->relids));
3109  /* lefthand side is inner */
3110  thisbucketsize = restrictinfo->left_bucketsize;
3111  if (thisbucketsize < 0)
3112  {
3113  /* not cached yet */
3114  thisbucketsize =
3116  get_leftop(restrictinfo->clause),
3117  virtualbuckets);
3118  restrictinfo->left_bucketsize = thisbucketsize;
3119  }
3120  }
3121 
3122  if (innerbucketsize > thisbucketsize)
3123  innerbucketsize = thisbucketsize;
3124  }
3125  }
3126 
3127  /*
3128  * Compute cost of the hashquals and qpquals (other restriction clauses)
3129  * separately.
3130  */
3131  cost_qual_eval(&hash_qual_cost, hashclauses, root);
3132  cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
3133  qp_qual_cost.startup -= hash_qual_cost.startup;
3134  qp_qual_cost.per_tuple -= hash_qual_cost.per_tuple;
3135 
3136  /* CPU costs */
3137 
3138  if (path->jpath.jointype == JOIN_SEMI ||
3139  path->jpath.jointype == JOIN_ANTI ||
3140  extra->inner_unique)
3141  {
3142  double outer_matched_rows;
3143  Selectivity inner_scan_frac;
3144 
3145  /*
3146  * With a SEMI or ANTI join, or if the innerrel is known unique, the
3147  * executor will stop after the first match.
3148  *
3149  * For an outer-rel row that has at least one match, we can expect the
3150  * bucket scan to stop after a fraction 1/(match_count+1) of the
3151  * bucket's rows, if the matches are evenly distributed. Since they
3152  * probably aren't quite evenly distributed, we apply a fuzz factor of
3153  * 2.0 to that fraction. (If we used a larger fuzz factor, we'd have
3154  * to clamp inner_scan_frac to at most 1.0; but since match_count is
3155  * at least 1, no such clamp is needed now.)
3156  */
3157  outer_matched_rows = rint(outer_path_rows * extra->semifactors.outer_match_frac);
3158  inner_scan_frac = 2.0 / (extra->semifactors.match_count + 1.0);
3159 
3160  startup_cost += hash_qual_cost.startup;
3161  run_cost += hash_qual_cost.per_tuple * outer_matched_rows *
3162  clamp_row_est(inner_path_rows * innerbucketsize * inner_scan_frac) * 0.5;
3163 
3164  /*
3165  * For unmatched outer-rel rows, the picture is quite a lot different.
3166  * In the first place, there is no reason to assume that these rows
3167  * preferentially hit heavily-populated buckets; instead assume they
3168  * are uncorrelated with the inner distribution and so they see an
3169  * average bucket size of inner_path_rows / virtualbuckets. In the
3170  * second place, it seems likely that they will have few if any exact
3171  * hash-code matches and so very few of the tuples in the bucket will
3172  * actually require eval of the hash quals. We don't have any good
3173  * way to estimate how many will, but for the moment assume that the
3174  * effective cost per bucket entry is one-tenth what it is for
3175  * matchable tuples.
3176  */
3177  run_cost += hash_qual_cost.per_tuple *
3178  (outer_path_rows - outer_matched_rows) *
3179  clamp_row_est(inner_path_rows / virtualbuckets) * 0.05;
3180 
3181  /* Get # of tuples that will pass the basic join */
3182  if (path->jpath.jointype == JOIN_SEMI)
3183  hashjointuples = outer_matched_rows;
3184  else
3185  hashjointuples = outer_path_rows - outer_matched_rows;
3186  }
3187  else
3188  {
3189  /*
3190  * The number of tuple comparisons needed is the number of outer
3191  * tuples times the typical number of tuples in a hash bucket, which
3192  * is the inner relation size times its bucketsize fraction. At each
3193  * one, we need to evaluate the hashjoin quals. But actually,
3194  * charging the full qual eval cost at each tuple is pessimistic,
3195  * since we don't evaluate the quals unless the hash values match
3196  * exactly. For lack of a better idea, halve the cost estimate to
3197  * allow for that.
3198  */
3199  startup_cost += hash_qual_cost.startup;
3200  run_cost += hash_qual_cost.per_tuple * outer_path_rows *
3201  clamp_row_est(inner_path_rows * innerbucketsize) * 0.5;
3202 
3203  /*
3204  * Get approx # tuples passing the hashquals. We use
3205  * approx_tuple_count here because we need an estimate done with
3206  * JOIN_INNER semantics.
3207  */
3208  hashjointuples = approx_tuple_count(root, &path->jpath, hashclauses);
3209  }
3210 
3211  /*
3212  * For each tuple that gets through the hashjoin proper, we charge
3213  * cpu_tuple_cost plus the cost of evaluating additional restriction
3214  * clauses that are to be applied at the join. (This is pessimistic since
3215  * not all of the quals may get evaluated at each tuple.)
3216  */
3217  startup_cost += qp_qual_cost.startup;
3218  cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
3219  run_cost += cpu_per_tuple * hashjointuples;
3220 
3221  /* tlist eval costs are paid per output row, not per tuple scanned */
3222  startup_cost += path->jpath.path.pathtarget->cost.startup;
3223  run_cost += path->jpath.path.pathtarget->cost.per_tuple * path->jpath.path.rows;
3224 
3225  path->jpath.path.startup_cost = startup_cost;
3226  path->jpath.path.total_cost = startup_cost + run_cost;
3227 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
JoinPath jpath
Definition: relation.h:1370
PathTarget * pathtarget
Definition: relation.h:955
SemiAntiJoinFactors semifactors
Definition: relation.h:2184
int num_batches
Definition: relation.h:1372
Selectivity outer_match_frac
Definition: relation.h:2161
Path * innerjoinpath
Definition: relation.h:1297
static double approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
Definition: costsize.c:3956
int parallel_workers
Definition: relation.h:961
ParamPathInfo * param_info
Definition: relation.h:957
Relids left_relids
Definition: relation.h:1774
double Selectivity
Definition: nodes.h:639
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3438
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
List * joinrestrictinfo
Definition: relation.h:1299
RelOptInfo * parent
Definition: relation.h:954
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
#define lfirst_node(type, lc)
Definition: pg_list.h:109
static double get_parallel_divisor(Path *path)
Definition: costsize.c:5073
Relids relids
Definition: relation.h:525
double rint(double x)
Definition: rint.c:22
Expr * clause
Definition: relation.h:1747
Path * outerjoinpath
Definition: relation.h:1296
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
Selectivity left_bucketsize
Definition: relation.h:1808
Relids right_relids
Definition: relation.h:1775
Path path
Definition: relation.h:1289
#define Assert(condition)
Definition: c.h:675
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
double ppi_rows
Definition: relation.h:914
bool enable_hashjoin
Definition: costsize.c:128
Selectivity estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
Definition: selfuncs.c:3592
Selectivity match_count
Definition: relation.h:2162
Selectivity right_bucketsize
Definition: relation.h:1809
JoinType jointype
Definition: relation.h:1291
List * path_hashclauses
Definition: relation.h:1371
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: pg_list.h:45
Definition: relation.h:948
double Cost
Definition: nodes.h:640
void final_cost_mergejoin ( PlannerInfo root,
MergePath path,
JoinCostWorkspace workspace,
JoinPathExtraData extra 
)

Definition at line 2599 of file costsize.c.

References approx_tuple_count(), clamp_row_est(), PathTarget::cost, cost_qual_eval(), cpu_operator_cost, cpu_tuple_cost, disable_cost, enable_material, enable_mergejoin, ExecSupportsMarkRestore(), get_parallel_divisor(), JoinCostWorkspace::inner_rows, JoinCostWorkspace::inner_run_cost, JoinCostWorkspace::inner_skip_rows, JoinPathExtraData::inner_unique, JoinPath::innerjoinpath, MergePath::innersortkeys, IsA, JOIN_ANTI, JOIN_SEMI, JoinPath::joinrestrictinfo, JoinPath::jointype, MergePath::jpath, list_length(), MergePath::materialize_inner, NIL, JoinCostWorkspace::outer_rows, JoinCostWorkspace::outer_skip_rows, JoinPath::outerjoinpath, Path::parallel_workers, Path::param_info, Path::parent, JoinPath::path, MergePath::path_mergeclauses, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, relation_byte_size(), RelOptInfo::rows, Path::rows, JoinCostWorkspace::run_cost, MergePath::skip_mark_restore, QualCost::startup, Path::startup_cost, JoinCostWorkspace::startup_cost, Path::total_cost, PathTarget::width, and work_mem.

Referenced by create_mergejoin_path().

2602 {
2603  Path *outer_path = path->jpath.outerjoinpath;
2604  Path *inner_path = path->jpath.innerjoinpath;
2605  double inner_path_rows = inner_path->rows;
2606  List *mergeclauses = path->path_mergeclauses;
2607  List *innersortkeys = path->innersortkeys;
2608  Cost startup_cost = workspace->startup_cost;
2609  Cost run_cost = workspace->run_cost;
2610  Cost inner_run_cost = workspace->inner_run_cost;
2611  double outer_rows = workspace->outer_rows;
2612  double inner_rows = workspace->inner_rows;
2613  double outer_skip_rows = workspace->outer_skip_rows;
2614  double inner_skip_rows = workspace->inner_skip_rows;
2615  Cost cpu_per_tuple,
2616  bare_inner_cost,
2617  mat_inner_cost;
2618  QualCost merge_qual_cost;
2619  QualCost qp_qual_cost;
2620  double mergejointuples,
2621  rescannedtuples;
2622  double rescanratio;
2623 
2624  /* Protect some assumptions below that rowcounts aren't zero or NaN */
2625  if (inner_path_rows <= 0 || isnan(inner_path_rows))
2626  inner_path_rows = 1;
2627 
2628  /* Mark the path with the correct row estimate */
2629  if (path->jpath.path.param_info)
2630  path->jpath.path.rows = path->jpath.path.param_info->ppi_rows;
2631  else
2632  path->jpath.path.rows = path->jpath.path.parent->rows;
2633 
2634  /* For partial paths, scale row estimate. */
2635  if (path->jpath.path.parallel_workers > 0)
2636  {
2637  double parallel_divisor = get_parallel_divisor(&path->jpath.path);
2638 
2639  path->jpath.path.rows =
2640  clamp_row_est(path->jpath.path.rows / parallel_divisor);
2641  }
2642 
2643  /*
2644  * We could include disable_cost in the preliminary estimate, but that
2645  * would amount to optimizing for the case where the join method is
2646  * disabled, which doesn't seem like the way to bet.
2647  */
2648  if (!enable_mergejoin)
2649  startup_cost += disable_cost;
2650 
2651  /*
2652  * Compute cost of the mergequals and qpquals (other restriction clauses)
2653  * separately.
2654  */
2655  cost_qual_eval(&merge_qual_cost, mergeclauses, root);
2656  cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
2657  qp_qual_cost.startup -= merge_qual_cost.startup;
2658  qp_qual_cost.per_tuple -= merge_qual_cost.per_tuple;
2659 
2660  /*
2661  * With a SEMI or ANTI join, or if the innerrel is known unique, the
2662  * executor will stop scanning for matches after the first match. When
2663  * all the joinclauses are merge clauses, this means we don't ever need to
2664  * back up the merge, and so we can skip mark/restore overhead.
2665  */
2666  if ((path->jpath.jointype == JOIN_SEMI ||
2667  path->jpath.jointype == JOIN_ANTI ||
2668  extra->inner_unique) &&
2671  path->skip_mark_restore = true;
2672  else
2673  path->skip_mark_restore = false;
2674 
2675  /*
2676  * Get approx # tuples passing the mergequals. We use approx_tuple_count
2677  * here because we need an estimate done with JOIN_INNER semantics.
2678  */
2679  mergejointuples = approx_tuple_count(root, &path->jpath, mergeclauses);
2680 
2681  /*
2682  * When there are equal merge keys in the outer relation, the mergejoin
2683  * must rescan any matching tuples in the inner relation. This means
2684  * re-fetching inner tuples; we have to estimate how often that happens.
2685  *
2686  * For regular inner and outer joins, the number of re-fetches can be
2687  * estimated approximately as size of merge join output minus size of
2688  * inner relation. Assume that the distinct key values are 1, 2, ..., and
2689  * denote the number of values of each key in the outer relation as m1,
2690  * m2, ...; in the inner relation, n1, n2, ... Then we have
2691  *
2692  * size of join = m1 * n1 + m2 * n2 + ...
2693  *
2694  * number of rescanned tuples = (m1 - 1) * n1 + (m2 - 1) * n2 + ... = m1 *
2695  * n1 + m2 * n2 + ... - (n1 + n2 + ...) = size of join - size of inner
2696  * relation
2697  *
2698  * This equation works correctly for outer tuples having no inner match
2699  * (nk = 0), but not for inner tuples having no outer match (mk = 0); we
2700  * are effectively subtracting those from the number of rescanned tuples,
2701  * when we should not. Can we do better without expensive selectivity
2702  * computations?
2703  *
2704  * The whole issue is moot if we are working from a unique-ified outer
2705  * input, or if we know we don't need to mark/restore at all.
2706  */
2707  if (IsA(outer_path, UniquePath) ||path->skip_mark_restore)
2708  rescannedtuples = 0;
2709  else
2710  {
2711  rescannedtuples = mergejointuples - inner_path_rows;
2712  /* Must clamp because of possible underestimate */
2713  if (rescannedtuples < 0)
2714  rescannedtuples = 0;
2715  }
2716  /* We'll inflate various costs this much to account for rescanning */
2717  rescanratio = 1.0 + (rescannedtuples / inner_path_rows);
2718 
2719  /*
2720  * Decide whether we want to materialize the inner input to shield it from
2721  * mark/restore and performing re-fetches. Our cost model for regular
2722  * re-fetches is that a re-fetch costs the same as an original fetch,
2723  * which is probably an overestimate; but on the other hand we ignore the
2724  * bookkeeping costs of mark/restore. Not clear if it's worth developing
2725  * a more refined model. So we just need to inflate the inner run cost by
2726  * rescanratio.
2727  */
2728  bare_inner_cost = inner_run_cost * rescanratio;
2729 
2730  /*
2731  * When we interpose a Material node the re-fetch cost is assumed to be
2732  * just cpu_operator_cost per tuple, independently of the underlying
2733  * plan's cost; and we charge an extra cpu_operator_cost per original
2734  * fetch as well. Note that we're assuming the materialize node will
2735  * never spill to disk, since it only has to remember tuples back to the
2736  * last mark. (If there are a huge number of duplicates, our other cost
2737  * factors will make the path so expensive that it probably won't get
2738  * chosen anyway.) So we don't use cost_rescan here.
2739  *
2740  * Note: keep this estimate in sync with create_mergejoin_plan's labeling
2741  * of the generated Material node.
2742  */
2743  mat_inner_cost = inner_run_cost +
2744  cpu_operator_cost * inner_path_rows * rescanratio;
2745 
2746  /*
2747  * If we don't need mark/restore at all, we don't need materialization.
2748  */
2749  if (path->skip_mark_restore)
2750  path->materialize_inner = false;
2751 
2752  /*
2753  * Prefer materializing if it looks cheaper, unless the user has asked to
2754  * suppress materialization.
2755  */
2756  else if (enable_material && mat_inner_cost < bare_inner_cost)
2757  path->materialize_inner = true;
2758 
2759  /*
2760  * Even if materializing doesn't look cheaper, we *must* do it if the
2761  * inner path is to be used directly (without sorting) and it doesn't
2762  * support mark/restore.
2763  *
2764  * Since the inner side must be ordered, and only Sorts and IndexScans can
2765  * create order to begin with, and they both support mark/restore, you
2766  * might think there's no problem --- but you'd be wrong. Nestloop and
2767  * merge joins can *preserve* the order of their inputs, so they can be
2768  * selected as the input of a mergejoin, and they don't support
2769  * mark/restore at present.
2770  *
2771  * We don't test the value of enable_material here, because
2772  * materialization is required for correctness in this case, and turning
2773  * it off does not entitle us to deliver an invalid plan.
2774  */
2775  else if (innersortkeys == NIL &&
2776  !ExecSupportsMarkRestore(inner_path))
2777  path->materialize_inner = true;
2778 
2779  /*
2780  * Also, force materializing if the inner path is to be sorted and the
2781  * sort is expected to spill to disk. This is because the final merge
2782  * pass can be done on-the-fly if it doesn't have to support mark/restore.
2783  * We don't try to adjust the cost estimates for this consideration,
2784  * though.
2785  *
2786  * Since materialization is a performance optimization in this case,
2787  * rather than necessary for correctness, we skip it if enable_material is
2788  * off.
2789  */
2790  else if (enable_material && innersortkeys != NIL &&
2791  relation_byte_size(inner_path_rows,
2792  inner_path->pathtarget->width) >
2793  (work_mem * 1024L))
2794  path->materialize_inner = true;
2795  else
2796  path->materialize_inner = false;
2797 
2798  /* Charge the right incremental cost for the chosen case */
2799  if (path->materialize_inner)
2800  run_cost += mat_inner_cost;
2801  else
2802  run_cost += bare_inner_cost;
2803 
2804  /* CPU costs */
2805 
2806  /*
2807  * The number of tuple comparisons needed is approximately number of outer
2808  * rows plus number of inner rows plus number of rescanned tuples (can we
2809  * refine this?). At each one, we need to evaluate the mergejoin quals.
2810  */
2811  startup_cost += merge_qual_cost.startup;
2812  startup_cost += merge_qual_cost.per_tuple *
2813  (outer_skip_rows + inner_skip_rows * rescanratio);
2814  run_cost += merge_qual_cost.per_tuple *
2815  ((outer_rows - outer_skip_rows) +
2816  (inner_rows - inner_skip_rows) * rescanratio);
2817 
2818  /*
2819  * For each tuple that gets through the mergejoin proper, we charge
2820  * cpu_tuple_cost plus the cost of evaluating additional restriction
2821  * clauses that are to be applied at the join. (This is pessimistic since
2822  * not all of the quals may get evaluated at each tuple.)
2823  *
2824  * Note: we could adjust for SEMI/ANTI joins skipping some qual
2825  * evaluations here, but it's probably not worth the trouble.
2826  */
2827  startup_cost += qp_qual_cost.startup;
2828  cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
2829  run_cost += cpu_per_tuple * mergejointuples;
2830 
2831  /* tlist eval costs are paid per output row, not per tuple scanned */
2832  startup_cost += path->jpath.path.pathtarget->cost.startup;
2833  run_cost += path->jpath.path.pathtarget->cost.per_tuple * path->jpath.path.rows;
2834 
2835  path->jpath.path.startup_cost = startup_cost;
2836  path->jpath.path.total_cost = startup_cost + run_cost;
2837 }
#define NIL
Definition: pg_list.h:69
List * path_mergeclauses
Definition: relation.h:1352
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
PathTarget * pathtarget
Definition: relation.h:955
bool ExecSupportsMarkRestore(Path *pathnode)
Definition: execAmi.c:400
bool materialize_inner
Definition: relation.h:1356
Path * innerjoinpath
Definition: relation.h:1297
static double approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
Definition: costsize.c:3956
int parallel_workers
Definition: relation.h:961
ParamPathInfo * param_info
Definition: relation.h:957
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
bool skip_mark_restore
Definition: relation.h:1355
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3438
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
List * joinrestrictinfo
Definition: relation.h:1299
RelOptInfo * parent
Definition: relation.h:954
static double get_parallel_divisor(Path *path)
Definition: costsize.c:5073
double cpu_operator_cost
Definition: costsize.c:108
static double relation_byte_size(double tuples, int width)
Definition: costsize.c:5052
Path * outerjoinpath
Definition: relation.h:1296
int work_mem
Definition: globals.c:113
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
double outer_skip_rows
Definition: relation.h:2216
bool enable_mergejoin
Definition: costsize.c:127
Path path
Definition: relation.h:1289
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
static int list_length(const List *l)
Definition: pg_list.h:89
List * innersortkeys
Definition: relation.h:1354
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
int width
Definition: relation.h:887
JoinType jointype
Definition: relation.h:1291
JoinPath jpath
Definition: relation.h:1351
double inner_skip_rows
Definition: relation.h:2217
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: pg_list.h:45
Definition: relation.h:948
double Cost
Definition: nodes.h:640
bool enable_material
Definition: costsize.c:126
void final_cost_nestloop ( PlannerInfo root,
NestPath path,
JoinCostWorkspace workspace,
JoinPathExtraData extra 
)

Definition at line 2162 of file costsize.c.

References clamp_row_est(), PathTarget::cost, cost_qual_eval(), cpu_tuple_cost, disable_cost, enable_nestloop, get_parallel_divisor(), has_indexed_join_quals(), JoinCostWorkspace::inner_rescan_run_cost, JoinCostWorkspace::inner_run_cost, JoinPathExtraData::inner_unique, JoinPath::innerjoinpath, JOIN_ANTI, JOIN_SEMI, JoinPath::joinrestrictinfo, JoinPath::jointype, SemiAntiJoinFactors::match_count, SemiAntiJoinFactors::outer_match_frac, JoinPath::outerjoinpath, Path::parallel_workers, Path::param_info, Path::parent, JoinPath::path, Path::pathtarget, QualCost::per_tuple, ParamPathInfo::ppi_rows, rint(), RelOptInfo::rows, Path::rows, JoinCostWorkspace::run_cost, JoinPathExtraData::semifactors, QualCost::startup, Path::startup_cost, JoinCostWorkspace::startup_cost, and Path::total_cost.

Referenced by create_nestloop_path().

2165 {
2166  Path *outer_path = path->outerjoinpath;
2167  Path *inner_path = path->innerjoinpath;
2168  double outer_path_rows = outer_path->rows;
2169  double inner_path_rows = inner_path->rows;
2170  Cost startup_cost = workspace->startup_cost;
2171  Cost run_cost = workspace->run_cost;
2172  Cost cpu_per_tuple;
2173  QualCost restrict_qual_cost;
2174  double ntuples;
2175 
2176  /* Protect some assumptions below that rowcounts aren't zero or NaN */
2177  if (outer_path_rows <= 0 || isnan(outer_path_rows))
2178  outer_path_rows = 1;
2179  if (inner_path_rows <= 0 || isnan(inner_path_rows))
2180  inner_path_rows = 1;
2181 
2182  /* Mark the path with the correct row estimate */
2183  if (path->path.param_info)
2184  path->path.rows = path->path.param_info->ppi_rows;
2185  else
2186  path->path.rows = path->path.parent->rows;
2187 
2188  /* For partial paths, scale row estimate. */
2189  if (path->path.parallel_workers > 0)
2190  {
2191  double parallel_divisor = get_parallel_divisor(&path->path);
2192 
2193  path->path.rows =
2194  clamp_row_est(path->path.rows / parallel_divisor);
2195  }
2196 
2197  /*
2198  * We could include disable_cost in the preliminary estimate, but that
2199  * would amount to optimizing for the case where the join method is
2200  * disabled, which doesn't seem like the way to bet.
2201  */
2202  if (!enable_nestloop)
2203  startup_cost += disable_cost;
2204 
2205  /* cost of inner-relation source data (we already dealt with outer rel) */
2206 
2207  if (path->jointype == JOIN_SEMI || path->jointype == JOIN_ANTI ||
2208  extra->inner_unique)
2209  {
2210  /*
2211  * With a SEMI or ANTI join, or if the innerrel is known unique, the
2212  * executor will stop after the first match.
2213  */
2214  Cost inner_run_cost = workspace->inner_run_cost;
2215  Cost inner_rescan_run_cost = workspace->inner_rescan_run_cost;
2216  double outer_matched_rows;
2217  double outer_unmatched_rows;
2218  Selectivity inner_scan_frac;
2219 
2220  /*
2221  * For an outer-rel row that has at least one match, we can expect the
2222  * inner scan to stop after a fraction 1/(match_count+1) of the inner
2223  * rows, if the matches are evenly distributed. Since they probably
2224  * aren't quite evenly distributed, we apply a fuzz factor of 2.0 to
2225  * that fraction. (If we used a larger fuzz factor, we'd have to
2226  * clamp inner_scan_frac to at most 1.0; but since match_count is at
2227  * least 1, no such clamp is needed now.)
2228  */
2229  outer_matched_rows = rint(outer_path_rows * extra->semifactors.outer_match_frac);
2230  outer_unmatched_rows = outer_path_rows - outer_matched_rows;
2231  inner_scan_frac = 2.0 / (extra->semifactors.match_count + 1.0);
2232 
2233  /*
2234  * Compute number of tuples processed (not number emitted!). First,
2235  * account for successfully-matched outer rows.
2236  */
2237  ntuples = outer_matched_rows * inner_path_rows * inner_scan_frac;
2238 
2239  /*
2240  * Now we need to estimate the actual costs of scanning the inner
2241  * relation, which may be quite a bit less than N times inner_run_cost
2242  * due to early scan stops. We consider two cases. If the inner path
2243  * is an indexscan using all the joinquals as indexquals, then an
2244  * unmatched outer row results in an indexscan returning no rows,
2245  * which is probably quite cheap. Otherwise, the executor will have
2246  * to scan the whole inner rel for an unmatched row; not so cheap.
2247  */
2248  if (has_indexed_join_quals(path))
2249  {
2250  /*
2251  * Successfully-matched outer rows will only require scanning
2252  * inner_scan_frac of the inner relation. In this case, we don't
2253  * need to charge the full inner_run_cost even when that's more
2254  * than inner_rescan_run_cost, because we can assume that none of
2255  * the inner scans ever scan the whole inner relation. So it's
2256  * okay to assume that all the inner scan executions can be
2257  * fractions of the full cost, even if materialization is reducing
2258  * the rescan cost. At this writing, it's impossible to get here
2259  * for a materialized inner scan, so inner_run_cost and
2260  * inner_rescan_run_cost will be the same anyway; but just in
2261  * case, use inner_run_cost for the first matched tuple and
2262  * inner_rescan_run_cost for additional ones.
2263  */
2264  run_cost += inner_run_cost * inner_scan_frac;
2265  if (outer_matched_rows > 1)
2266  run_cost += (outer_matched_rows - 1) * inner_rescan_run_cost * inner_scan_frac;
2267 
2268  /*
2269  * Add the cost of inner-scan executions for unmatched outer rows.
2270  * We estimate this as the same cost as returning the first tuple
2271  * of a nonempty scan. We consider that these are all rescans,
2272  * since we used inner_run_cost once already.
2273  */
2274  run_cost += outer_unmatched_rows *
2275  inner_rescan_run_cost / inner_path_rows;
2276 
2277  /*
2278  * We won't be evaluating any quals at all for unmatched rows, so
2279  * don't add them to ntuples.
2280  */
2281  }
2282  else
2283  {
2284  /*
2285  * Here, a complicating factor is that rescans may be cheaper than
2286  * first scans. If we never scan all the way to the end of the
2287  * inner rel, it might be (depending on the plan type) that we'd
2288  * never pay the whole inner first-scan run cost. However it is
2289  * difficult to estimate whether that will happen (and it could
2290  * not happen if there are any unmatched outer rows!), so be
2291  * conservative and always charge the whole first-scan cost once.
2292  * We consider this charge to correspond to the first unmatched
2293  * outer row, unless there isn't one in our estimate, in which
2294  * case blame it on the first matched row.
2295  */
2296 
2297  /* First, count all unmatched join tuples as being processed */
2298  ntuples += outer_unmatched_rows * inner_path_rows;
2299 
2300  /* Now add the forced full scan, and decrement appropriate count */
2301  run_cost += inner_run_cost;
2302  if (outer_unmatched_rows >= 1)
2303  outer_unmatched_rows -= 1;
2304  else
2305  outer_matched_rows -= 1;
2306 
2307  /* Add inner run cost for additional outer tuples having matches */
2308  if (outer_matched_rows > 0)
2309  run_cost += outer_matched_rows * inner_rescan_run_cost * inner_scan_frac;
2310 
2311  /* Add inner run cost for additional unmatched outer tuples */
2312  if (outer_unmatched_rows > 0)
2313  run_cost += outer_unmatched_rows * inner_rescan_run_cost;
2314  }
2315  }
2316  else
2317  {
2318  /* Normal-case source costs were included in preliminary estimate */
2319 
2320  /* Compute number of tuples processed (not number emitted!) */
2321  ntuples = outer_path_rows * inner_path_rows;
2322  }
2323 
2324  /* CPU costs */
2325  cost_qual_eval(&restrict_qual_cost, path->joinrestrictinfo, root);
2326  startup_cost += restrict_qual_cost.startup;
2327  cpu_per_tuple = cpu_tuple_cost + restrict_qual_cost.per_tuple;
2328  run_cost += cpu_per_tuple * ntuples;
2329 
2330  /* tlist eval costs are paid per output row, not per tuple scanned */
2331  startup_cost += path->path.pathtarget->cost.startup;
2332  run_cost += path->path.pathtarget->cost.per_tuple * path->path.rows;
2333 
2334  path->path.startup_cost = startup_cost;
2335  path->path.total_cost = startup_cost + run_cost;
2336 }
PathTarget * pathtarget
Definition: relation.h:955
SemiAntiJoinFactors semifactors
Definition: relation.h:2184
bool enable_nestloop
Definition: costsize.c:125
Selectivity outer_match_frac
Definition: relation.h:2161
Path * innerjoinpath
Definition: relation.h:1297
int parallel_workers
Definition: relation.h:961
ParamPathInfo * param_info
Definition: relation.h:957
double Selectivity
Definition: nodes.h:639
Cost inner_rescan_run_cost
Definition: relation.h:2211
Cost startup
Definition: relation.h:45
Cost per_tuple
Definition: relation.h:46
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3438
Cost startup_cost
Definition: relation.h:965
Cost disable_cost
Definition: costsize.c:114
List * joinrestrictinfo
Definition: relation.h:1299
RelOptInfo * parent
Definition: relation.h:954
static double get_parallel_divisor(Path *path)
Definition: costsize.c:5073
double rint(double x)
Definition: rint.c:22
Path * outerjoinpath
Definition: relation.h:1296
double rows
Definition: relation.h:528
Cost total_cost
Definition: relation.h:966
Path path
Definition: relation.h:1289
static bool has_indexed_join_quals(NestPath *joinpath)
Definition: costsize.c:3863
double rows
Definition: relation.h:964
QualCost cost
Definition: relation.h:886
double cpu_tuple_cost
Definition: costsize.c:106
double ppi_rows
Definition: relation.h:914
Selectivity match_count
Definition: relation.h:2162
JoinType jointype
Definition: relation.h:1291
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: relation.h:948
double Cost
Definition: nodes.h:640
static Selectivity get_foreign_key_join_selectivity ( PlannerInfo root,
Relids  outer_relids,
Relids  inner_relids,
SpecialJoinInfo sjinfo,
List **  restrictlist 
)
static

Definition at line 4314 of file costsize.c.

References bms_is_member(), bms_membership(), BMS_SINGLETON, ForeignKeyOptInfo::con_relid, ForeignKeyOptInfo::eclass, find_base_rel(), PlannerInfo::fkey_list, i, JOIN_ANTI, JOIN_SEMI, SpecialJoinInfo::jointype, lappend(), lfirst, list_concat(), list_copy(), list_delete_cell(), list_head(), list_length(), list_member_ptr(), lnext, Max, next, NIL, ForeignKeyOptInfo::nkeys, ForeignKeyOptInfo::nmatched_ec, ForeignKeyOptInfo::nmatched_ri, NULL, RestrictInfo::parent_ec, ForeignKeyOptInfo::ref_relid, ForeignKeyOptInfo::rinfos, RelOptInfo::rows, and RelOptInfo::tuples.

Referenced by calc_joinrel_size_estimate().

4319 {
4320  Selectivity fkselec = 1.0;
4321  JoinType jointype = sjinfo->jointype;
4322  List *worklist = *restrictlist;
4323  ListCell *lc;
4324 
4325  /* Consider each FK constraint that is known to match the query */
4326  foreach(lc, root->fkey_list)
4327  {
4328  ForeignKeyOptInfo *fkinfo = (ForeignKeyOptInfo *) lfirst(lc);
4329  bool ref_is_outer;
4330  List *removedlist;
4331  ListCell *cell;
4332  ListCell *prev;
4333  ListCell *next;
4334 
4335  /*
4336  * This FK is not relevant unless it connects a baserel on one side of
4337  * this join to a baserel on the other side.
4338  */
4339  if (bms_is_member(fkinfo->con_relid, outer_relids) &&
4340  bms_is_member(fkinfo->ref_relid, inner_relids))
4341  ref_is_outer = false;
4342  else if (bms_is_member(fkinfo->ref_relid, outer_relids) &&
4343  bms_is_member(fkinfo->con_relid, inner_relids))
4344  ref_is_outer = true;
4345  else
4346  continue;
4347 
4348  /*
4349  * If we're dealing with a semi/anti join, and the FK's referenced
4350  * relation is on the outside, then knowledge of the FK doesn't help
4351  * us figure out what we need to know (which is the fraction of outer
4352  * rows that have matches). On the other hand, if the referenced rel
4353  * is on the inside, then all outer rows must have matches in the
4354  * referenced table (ignoring nulls). But any restriction or join
4355  * clauses that filter that table will reduce the fraction of matches.
4356  * We can account for restriction clauses, but it's too hard to guess
4357  * how many table rows would get through a join that's inside the RHS.
4358  * Hence, if either case applies, punt and ignore the FK.
4359  */
4360  if ((jointype == JOIN_SEMI || jointype == JOIN_ANTI) &&
4361  (ref_is_outer || bms_membership(inner_relids) != BMS_SINGLETON))
4362  continue;
4363 
4364  /*
4365  * Modify the restrictlist by removing clauses that match the FK (and
4366  * putting them into removedlist instead). It seems unsafe to modify
4367  * the originally-passed List structure, so we make a shallow copy the
4368  * first time through.
4369  */
4370  if (worklist == *restrictlist)
4371  worklist = list_copy(worklist);
4372 
4373  removedlist = NIL;
4374  prev = NULL;
4375  for (cell = list_head(worklist); cell; cell = next)
4376  {
4377  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
4378  bool remove_it = false;
4379  int i;
4380 
4381  next = lnext(cell);
4382  /* Drop this clause if it matches any column of the FK */
4383  for (i = 0; i < fkinfo->nkeys; i++)
4384  {
4385  if (rinfo->parent_ec)
4386  {
4387  /*
4388  * EC-derived clauses can only match by EC. It is okay to
4389  * consider any clause derived from the same EC as
4390  * matching the FK: even if equivclass.c chose to generate
4391  * a clause equating some other pair of Vars, it could
4392  * have generated one equating the FK's Vars. So for
4393  * purposes of estimation, we can act as though it did so.
4394  *
4395  * Note: checking parent_ec is a bit of a cheat because
4396  * there are EC-derived clauses that don't have parent_ec
4397  * set; but such clauses must compare expressions that
4398  * aren't just Vars, so they cannot match the FK anyway.
4399  */
4400  if (fkinfo->eclass[i] == rinfo->parent_ec)
4401  {
4402  remove_it = true;
4403  break;
4404  }
4405  }
4406  else
4407  {
4408  /*
4409  * Otherwise, see if rinfo was previously matched to FK as
4410  * a "loose" clause.
4411  */
4412  if (list_member_ptr(fkinfo->rinfos[i], rinfo))
4413  {
4414  remove_it = true;
4415  break;
4416  }
4417  }
4418  }
4419  if (remove_it)
4420  {
4421  worklist = list_delete_cell(worklist, cell, prev);
4422  removedlist = lappend(removedlist, rinfo);
4423  }
4424  else
4425  prev = cell;
4426  }
4427 
4428  /*
4429  * If we failed to remove all the matching clauses we expected to
4430  * find, chicken out and ignore this FK; applying its selectivity
4431  * might result in double-counting. Put any clauses we did manage to
4432  * remove back into the worklist.
4433  *
4434  * Since the matching clauses are known not outerjoin-delayed, they
4435  * should certainly have appeared in the initial joinclause list. If
4436  * we didn't find them, they must have been matched to, and removed
4437  * by, some other FK in a previous iteration of this loop. (A likely
4438  * case is that two FKs are matched to the same EC; there will be only
4439  * one EC-derived clause in the initial list, so the first FK will
4440  * consume it.) Applying both FKs' selectivity independently risks
4441  * underestimating the join size; in particular, this would undo one
4442  * of the main things that ECs were invented for, namely to avoid
4443  * double-counting the selectivity of redundant equality conditions.
4444  * Later we might think of a reasonable way to combine the estimates,
4445  * but for now, just punt, since this is a fairly uncommon situation.
4446  */
4447  if (list_length(removedlist) !=
4448  (fkinfo->nmatched_ec + fkinfo->nmatched_ri))
4449  {
4450  worklist = list_concat(worklist, removedlist);
4451  continue;
4452  }
4453 
4454  /*
4455  * Finally we get to the payoff: estimate selectivity using the
4456  * knowledge that each referencing row will match exactly one row in
4457  * the referenced table.
4458  *
4459  * XXX that's not true in the presence of nulls in the referencing
4460  * column(s), so in principle we should derate the estimate for those.
4461  * However (1) if there are any strict restriction clauses for the
4462  * referencing column(s) elsewhere in the query, derating here would
4463  * be double-counting the null fraction, and (2) it's not very clear
4464  * how to combine null fractions for multiple referencing columns. So
4465  * we do nothing for now about correcting for nulls.
4466  *
4467  * XXX another point here is that if either side of an FK constraint
4468  * is an inheritance parent, we estimate as though the constraint
4469  * covers all its children as well. This is not an unreasonable
4470  * assumption for a referencing table, ie the user probably applied
4471  * identical constraints to all child tables (though perhaps we ought
4472  * to check that). But it's not possible to have done that for a
4473  * referenced table. Fortunately, precisely because that doesn't
4474  * work, it is uncommon in practice to have an FK referencing a parent
4475  * table. So, at least for now, disregard inheritance here.
4476  */
4477  if (jointype == JOIN_SEMI || jointype == JOIN_ANTI)
4478  {
4479  /*
4480  * For JOIN_SEMI and JOIN_ANTI, we only get here when the FK's
4481  * referenced table is exactly the inside of the join. The join
4482  * selectivity is defined as the fraction of LHS rows that have
4483  * matches. The FK implies that every LHS row has a match *in the
4484  * referenced table*; but any restriction clauses on it will
4485  * reduce the number of matches. Hence we take the join
4486  * selectivity as equal to the selectivity of the table's
4487  * restriction clauses, which is rows / tuples; but we must guard
4488  * against tuples == 0.
4489  */
4490  RelOptInfo *ref_rel = find_base_rel(root, fkinfo->ref_relid);
4491  double ref_tuples = Max(ref_rel->tuples, 1.0);
4492 
4493  fkselec *= ref_rel->rows / ref_tuples;
4494  }
4495  else
4496  {
4497  /*
4498  * Otherwise, selectivity is exactly 1/referenced-table-size; but
4499  * guard against tuples == 0. Note we should use the raw table
4500  * tuple count, not any estimate of its filtered or joined size.
4501  */
4502  RelOptInfo *ref_rel = find_base_rel(root, fkinfo->ref_relid);
4503  double ref_tuples = Max(ref_rel->tuples, 1.0);
4504 
4505  fkselec *= 1.0 / ref_tuples;
4506  }
4507  }
4508 
4509  *restrictlist = worklist;
4510  return fkselec;
4511 }
#define NIL
Definition: pg_list.h:69
static int32 next
Definition: blutils.c:210
double tuples
Definition: relation.h:565
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_concat(List *list1, List *list2)
Definition: list.c:321
double Selectivity
Definition: nodes.h:639
List * fkey_list
Definition: relation.h:260
JoinType
Definition: nodes.h:673
EquivalenceClass * parent_ec
Definition: relation.h:1781
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
struct EquivalenceClass * eclass[INDEX_MAX_KEYS]
Definition: relation.h:706
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
double rows
Definition: relation.h:528
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
#define Max(x, y)
Definition: c.h:800
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:1921
static int list_length(const List *l)
Definition: pg_list.h:89
int i
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
List * rinfos[INDEX_MAX_KEYS]
Definition: relation.h:708
static double get_indexpath_pages ( Path bitmapqual)
static

Definition at line 878 of file costsize.c.

References BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, elog, ERROR, IndexPath::indexinfo, IsA, lfirst, nodeTag, IndexOptInfo::pages, and result.

Referenced by compute_bitmap_pages().

879 {
880  double result = 0;
881  ListCell *l;
882 
883  if (IsA(bitmapqual, BitmapAndPath))
884  {
885  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
886 
887  foreach(l, apath->bitmapquals)
888  {
889  result += get_indexpath_pages((Path *) lfirst(l));
890  }
891  }
892  else if (IsA(bitmapqual, BitmapOrPath))
893  {
894  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
895 
896  foreach(l, opath->bitmapquals)
897  {
898  result += get_indexpath_pages((Path *) lfirst(l));
899  }
900  }
901  else if (IsA(bitmapqual, IndexPath))
902  {
903  IndexPath *ipath = (IndexPath *) bitmapqual;
904 
905  result = (double) ipath->indexinfo->pages;
906  }
907  else
908  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
909 
910  return result;
911 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
IndexOptInfo * indexinfo
Definition: relation.h:1031
return result
Definition: formatting.c:1633
List * bitmapquals
Definition: relation.h:1074
List * bitmapquals
Definition: relation.h:1087
BlockNumber pages
Definition: relation.h:636
#define ERROR
Definition: elog.h:43
#define lfirst(lc)
Definition: pg_list.h:106
static double get_indexpath_pages(Path *bitmapqual)
Definition: costsize.c:878
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define elog
Definition: elog.h:219
Definition: relation.h:948
static double get_parallel_divisor ( Path path)
static

Definition at line 5073 of file costsize.c.

References Path::parallel_workers.

Referenced by cost_bitmap_heap_scan(), cost_index(), cost_seqscan(), final_cost_hashjoin(), final_cost_mergejoin(), and final_cost_nestloop().

5074 {
5075  double parallel_divisor = path->parallel_workers;
5076  double leader_contribution;
5077 
5078  /*
5079  * Early experience with parallel query suggests that when there is only
5080  * one worker, the leader often makes a very substantial contribution to
5081  * executing the parallel portion of the plan, but as more workers are
5082  * added, it does less and less, because it's busy reading tuples from the
5083  * workers and doing whatever non-parallel post-processing is needed. By
5084  * the time we reach 4 workers, the leader no longer makes a meaningful
5085  * contribution. Thus, for now, estimate that the leader spends 30% of
5086  * its time servicing each worker, and the remainder executing the
5087  * parallel plan.
5088  */
5089  leader_contribution = 1.0 - (0.3 * path->parallel_workers);
5090  if (leader_contribution > 0)
5091  parallel_divisor += leader_contribution;
5092 
5093  return parallel_divisor;
5094 }
int parallel_workers
Definition: relation.h:961
double get_parameterized_baserel_size ( PlannerInfo root,
RelOptInfo rel,
List param_clauses 
)

Definition at line 4042 of file costsize.c.

References RelOptInfo::baserestrictinfo, clamp_row_est(), clauselist_selectivity(), JOIN_INNER, list_concat(), list_copy(), NULL, RelOptInfo::relid, RelOptInfo::rows, and RelOptInfo::tuples.

Referenced by get_baserel_parampathinfo().

4044 {
4045  List *allclauses;
4046  double nrows;
4047 
4048  /*
4049  * Estimate the number of rows returned by the parameterized scan, knowing
4050  * that it will apply all the extra join clauses as well as the rel's own
4051  * restriction clauses. Note that we force the clauses to be treated as
4052  * non-join clauses during selectivity estimation.
4053  */
4054  allclauses = list_concat(list_copy(param_clauses),
4055  rel->baserestrictinfo);
4056  nrows = rel->tuples *
4058  allclauses,
4059  rel->relid, /* do not use 0! */
4060  JOIN_INNER,
4061  NULL);
4062  nrows = clamp_row_est(nrows);
4063  /* For safety, make sure result is not more than the base estimate */
4064  if (nrows > rel->rows)
4065  nrows = rel->rows;
4066  return nrows;
4067 }
double tuples
Definition: relation.h:565
List * baserestrictinfo
Definition: relation.h:585
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_concat(List *list1, List *list2)
Definition: list.c:321
Index relid
Definition: relation.h:553
double rows
Definition: relation.h:528
#define NULL
Definition: c.h:229
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:99
double clamp_row_est(double nrows)
Definition: costsize.c:173
Definition: pg_list.h:45
double get_parameterized_joinrel_size ( PlannerInfo root,
RelOptInfo rel,
Path outer_path,
Path inner_path,
SpecialJoinInfo sjinfo,
List restrict_clauses 
)

Definition at line 4123 of file costsize.c.

References calc_joinrel_size_estimate(), Path::parent, RelOptInfo::rows, and Path::rows.

Referenced by get_joinrel_parampathinfo().

4128 {
4129  double nrows;
4130 
4131  /*
4132  * Estimate the number of rows returned by the parameterized join as the
4133