PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
costsize.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * costsize.c
4  * Routines to compute (and set) relation sizes and path costs
5  *
6  * Path costs are measured in arbitrary units established by these basic
7  * parameters:
8  *
9  * seq_page_cost Cost of a sequential page fetch
10  * random_page_cost Cost of a non-sequential page fetch
11  * cpu_tuple_cost Cost of typical CPU time to process a tuple
12  * cpu_index_tuple_cost Cost of typical CPU time to process an index tuple
13  * cpu_operator_cost Cost of CPU time to execute an operator or function
14  * parallel_tuple_cost Cost of CPU time to pass a tuple from worker to master backend
15  * parallel_setup_cost Cost of setting up shared memory for parallelism
16  *
17  * We expect that the kernel will typically do some amount of read-ahead
18  * optimization; this in conjunction with seek costs means that seq_page_cost
19  * is normally considerably less than random_page_cost. (However, if the
20  * database is fully cached in RAM, it is reasonable to set them equal.)
21  *
22  * We also use a rough estimate "effective_cache_size" of the number of
23  * disk pages in Postgres + OS-level disk cache. (We can't simply use
24  * NBuffers for this purpose because that would ignore the effects of
25  * the kernel's disk cache.)
26  *
27  * Obviously, taking constants for these values is an oversimplification,
28  * but it's tough enough to get any useful estimates even at this level of
29  * detail. Note that all of these parameters are user-settable, in case
30  * the default values are drastically off for a particular platform.
31  *
32  * seq_page_cost and random_page_cost can also be overridden for an individual
33  * tablespace, in case some data is on a fast disk and other data is on a slow
34  * disk. Per-tablespace overrides never apply to temporary work files such as
35  * an external sort or a materialize node that overflows work_mem.
36  *
37  * We compute two separate costs for each path:
38  * total_cost: total estimated cost to fetch all tuples
39  * startup_cost: cost that is expended before first tuple is fetched
40  * In some scenarios, such as when there is a LIMIT or we are implementing
41  * an EXISTS(...) sub-select, it is not necessary to fetch all tuples of the
42  * path's result. A caller can estimate the cost of fetching a partial
43  * result by interpolating between startup_cost and total_cost. In detail:
44  * actual_cost = startup_cost +
45  * (total_cost - startup_cost) * tuples_to_fetch / path->rows;
46  * Note that a base relation's rows count (and, by extension, plan_rows for
47  * plan nodes below the LIMIT node) are set without regard to any LIMIT, so
48  * that this equation works properly. (Note: while path->rows is never zero
49  * for ordinary relations, it is zero for paths for provably-empty relations,
50  * so beware of division-by-zero.) The LIMIT is applied as a top-level
51  * plan node.
52  *
53  * For largely historical reasons, most of the routines in this module use
54  * the passed result Path only to store their results (rows, startup_cost and
55  * total_cost) into. All the input data they need is passed as separate
56  * parameters, even though much of it could be extracted from the Path.
57  * An exception is made for the cost_XXXjoin() routines, which expect all
58  * the other fields of the passed XXXPath to be filled in, and similarly
59  * cost_index() assumes the passed IndexPath is valid except for its output
60  * values.
61  *
62  *
63  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
64  * Portions Copyright (c) 1994, Regents of the University of California
65  *
66  * IDENTIFICATION
67  * src/backend/optimizer/path/costsize.c
68  *
69  *-------------------------------------------------------------------------
70  */
71 
72 #include "postgres.h"
73 
74 #ifdef _MSC_VER
75 #include <float.h> /* for _isnan */
76 #endif
77 #include <math.h>
78 
79 #include "access/amapi.h"
80 #include "access/htup_details.h"
81 #include "access/tsmapi.h"
82 #include "executor/executor.h"
83 #include "executor/nodeHash.h"
84 #include "miscadmin.h"
85 #include "nodes/nodeFuncs.h"
86 #include "optimizer/clauses.h"
87 #include "optimizer/cost.h"
88 #include "optimizer/pathnode.h"
89 #include "optimizer/paths.h"
90 #include "optimizer/placeholder.h"
91 #include "optimizer/plancat.h"
92 #include "optimizer/planmain.h"
93 #include "optimizer/restrictinfo.h"
94 #include "parser/parsetree.h"
95 #include "utils/lsyscache.h"
96 #include "utils/selfuncs.h"
97 #include "utils/spccache.h"
98 #include "utils/tuplesort.h"
99 
100 
101 #define LOG2(x) (log(x) / 0.693147180559945)
102 
103 
111 
113 
115 
117 
118 bool enable_seqscan = true;
119 bool enable_indexscan = true;
121 bool enable_bitmapscan = true;
122 bool enable_tidscan = true;
123 bool enable_sort = true;
124 bool enable_hashagg = true;
125 bool enable_nestloop = true;
126 bool enable_material = true;
127 bool enable_mergejoin = true;
128 bool enable_hashjoin = true;
129 
130 typedef struct
131 {
135 
136 static List *extract_nonindex_conditions(List *qual_clauses, List *indexquals);
138  RestrictInfo *rinfo,
139  PathKey *pathkey);
140 static void cost_rescan(PlannerInfo *root, Path *path,
141  Cost *rescan_startup_cost, Cost *rescan_total_cost);
142 static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context);
143 static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel,
144  ParamPathInfo *param_info,
145  QualCost *qpqual_cost);
146 static bool has_indexed_join_quals(NestPath *joinpath);
147 static double approx_tuple_count(PlannerInfo *root, JoinPath *path,
148  List *quals);
149 static double calc_joinrel_size_estimate(PlannerInfo *root,
150  RelOptInfo *outer_rel,
151  RelOptInfo *inner_rel,
152  double outer_rows,
153  double inner_rows,
154  SpecialJoinInfo *sjinfo,
155  List *restrictlist);
157  Relids outer_relids,
158  Relids inner_relids,
159  SpecialJoinInfo *sjinfo,
160  List **restrictlist);
161 static void set_rel_width(PlannerInfo *root, RelOptInfo *rel);
162 static double relation_byte_size(double tuples, int width);
163 static double page_size(double tuples, int width);
164 static double get_parallel_divisor(Path *path);
165 
166 
167 /*
168  * clamp_row_est
169  * Force a row-count estimate to a sane value.
170  */
171 double
172 clamp_row_est(double nrows)
173 {
174  /*
175  * Force estimate to be at least one row, to make explain output look
176  * better and to avoid possible divide-by-zero when interpolating costs.
177  * Make it an integer, too.
178  */
179  if (nrows <= 1.0)
180  nrows = 1.0;
181  else
182  nrows = rint(nrows);
183 
184  return nrows;
185 }
186 
187 
188 /*
189  * cost_seqscan
190  * Determines and returns the cost of scanning a relation sequentially.
191  *
192  * 'baserel' is the relation to be scanned
193  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
194  */
195 void
197  RelOptInfo *baserel, ParamPathInfo *param_info)
198 {
199  Cost startup_cost = 0;
200  Cost cpu_run_cost;
201  Cost disk_run_cost;
202  double spc_seq_page_cost;
203  QualCost qpqual_cost;
204  Cost cpu_per_tuple;
205 
206  /* Should only be applied to base relations */
207  Assert(baserel->relid > 0);
208  Assert(baserel->rtekind == RTE_RELATION);
209 
210  /* Mark the path with the correct row estimate */
211  if (param_info)
212  path->rows = param_info->ppi_rows;
213  else
214  path->rows = baserel->rows;
215 
216  if (!enable_seqscan)
217  startup_cost += disable_cost;
218 
219  /* fetch estimated page cost for tablespace containing table */
221  NULL,
222  &spc_seq_page_cost);
223 
224  /*
225  * disk costs
226  */
227  disk_run_cost = spc_seq_page_cost * baserel->pages;
228 
229  /* CPU costs */
230  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
231 
232  startup_cost += qpqual_cost.startup;
233  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
234  cpu_run_cost = cpu_per_tuple * baserel->tuples;
235  /* tlist eval costs are paid per output row, not per tuple scanned */
236  startup_cost += path->pathtarget->cost.startup;
237  cpu_run_cost += path->pathtarget->cost.per_tuple * path->rows;
238 
239  /* Adjust costing for parallelism, if used. */
240  if (path->parallel_workers > 0)
241  {
242  double parallel_divisor = get_parallel_divisor(path);
243 
244  /* The CPU cost is divided among all the workers. */
245  cpu_run_cost /= parallel_divisor;
246 
247  /*
248  * It may be possible to amortize some of the I/O cost, but probably
249  * not very much, because most operating systems already do aggressive
250  * prefetching. For now, we assume that the disk run cost can't be
251  * amortized at all.
252  */
253 
254  /*
255  * In the case of a parallel plan, the row count needs to represent
256  * the number of tuples processed per worker.
257  */
258  path->rows = clamp_row_est(path->rows / parallel_divisor);
259  }
260 
261  path->startup_cost = startup_cost;
262  path->total_cost = startup_cost + cpu_run_cost + disk_run_cost;
263 }
264 
265 /*
266  * cost_samplescan
267  * Determines and returns the cost of scanning a relation using sampling.
268  *
269  * 'baserel' is the relation to be scanned
270  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
271  */
272 void
274  RelOptInfo *baserel, ParamPathInfo *param_info)
275 {
276  Cost startup_cost = 0;
277  Cost run_cost = 0;
278  RangeTblEntry *rte;
279  TableSampleClause *tsc;
280  TsmRoutine *tsm;
281  double spc_seq_page_cost,
282  spc_random_page_cost,
283  spc_page_cost;
284  QualCost qpqual_cost;
285  Cost cpu_per_tuple;
286 
287  /* Should only be applied to base relations with tablesample clauses */
288  Assert(baserel->relid > 0);
289  rte = planner_rt_fetch(baserel->relid, root);
290  Assert(rte->rtekind == RTE_RELATION);
291  tsc = rte->tablesample;
292  Assert(tsc != NULL);
293  tsm = GetTsmRoutine(tsc->tsmhandler);
294 
295  /* Mark the path with the correct row estimate */
296  if (param_info)
297  path->rows = param_info->ppi_rows;
298  else
299  path->rows = baserel->rows;
300 
301  /* fetch estimated page cost for tablespace containing table */
303  &spc_random_page_cost,
304  &spc_seq_page_cost);
305 
306  /* if NextSampleBlock is used, assume random access, else sequential */
307  spc_page_cost = (tsm->NextSampleBlock != NULL) ?
308  spc_random_page_cost : spc_seq_page_cost;
309 
310  /*
311  * disk costs (recall that baserel->pages has already been set to the
312  * number of pages the sampling method will visit)
313  */
314  run_cost += spc_page_cost * baserel->pages;
315 
316  /*
317  * CPU costs (recall that baserel->tuples has already been set to the
318  * number of tuples the sampling method will select). Note that we ignore
319  * execution cost of the TABLESAMPLE parameter expressions; they will be
320  * evaluated only once per scan, and in most usages they'll likely be
321  * simple constants anyway. We also don't charge anything for the
322  * calculations the sampling method might do internally.
323  */
324  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
325 
326  startup_cost += qpqual_cost.startup;
327  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
328  run_cost += cpu_per_tuple * baserel->tuples;
329  /* tlist eval costs are paid per output row, not per tuple scanned */
330  startup_cost += path->pathtarget->cost.startup;
331  run_cost += path->pathtarget->cost.per_tuple * path->rows;
332 
333  path->startup_cost = startup_cost;
334  path->total_cost = startup_cost + run_cost;
335 }
336 
337 /*
338  * cost_gather
339  * Determines and returns the cost of gather path.
340  *
341  * 'rel' is the relation to be operated upon
342  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
343  * 'rows' may be used to point to a row estimate; if non-NULL, it overrides
344  * both 'rel' and 'param_info'. This is useful when the path doesn't exactly
345  * correspond to any particular RelOptInfo.
346  */
347 void
349  RelOptInfo *rel, ParamPathInfo *param_info,
350  double *rows)
351 {
352  Cost startup_cost = 0;
353  Cost run_cost = 0;
354 
355  /* Mark the path with the correct row estimate */
356  if (rows)
357  path->path.rows = *rows;
358  else if (param_info)
359  path->path.rows = param_info->ppi_rows;
360  else
361  path->path.rows = rel->rows;
362 
363  startup_cost = path->subpath->startup_cost;
364 
365  run_cost = path->subpath->total_cost - path->subpath->startup_cost;
366 
367  /* Parallel setup and communication cost. */
368  startup_cost += parallel_setup_cost;
369  run_cost += parallel_tuple_cost * path->path.rows;
370 
371  path->path.startup_cost = startup_cost;
372  path->path.total_cost = (startup_cost + run_cost);
373 }
374 
375 /*
376  * cost_index
377  * Determines and returns the cost of scanning a relation using an index.
378  *
379  * 'path' describes the indexscan under consideration, and is complete
380  * except for the fields to be set by this routine
381  * 'loop_count' is the number of repetitions of the indexscan to factor into
382  * estimates of caching behavior
383  *
384  * In addition to rows, startup_cost and total_cost, cost_index() sets the
385  * path's indextotalcost and indexselectivity fields. These values will be
386  * needed if the IndexPath is used in a BitmapIndexScan.
387  *
388  * NOTE: path->indexquals must contain only clauses usable as index
389  * restrictions. Any additional quals evaluated as qpquals may reduce the
390  * number of returned tuples, but they won't reduce the number of tuples
391  * we have to fetch from the table, so they don't reduce the scan cost.
392  */
393 void
394 cost_index(IndexPath *path, PlannerInfo *root, double loop_count,
395  bool partial_path)
396 {
397  IndexOptInfo *index = path->indexinfo;
398  RelOptInfo *baserel = index->rel;
399  bool indexonly = (path->path.pathtype == T_IndexOnlyScan);
400  amcostestimate_function amcostestimate;
401  List *qpquals;
402  Cost startup_cost = 0;
403  Cost run_cost = 0;
404  Cost cpu_run_cost = 0;
405  Cost indexStartupCost;
406  Cost indexTotalCost;
407  Selectivity indexSelectivity;
408  double indexCorrelation,
409  csquared;
410  double spc_seq_page_cost,
411  spc_random_page_cost;
412  Cost min_IO_cost,
413  max_IO_cost;
414  QualCost qpqual_cost;
415  Cost cpu_per_tuple;
416  double tuples_fetched;
417  double pages_fetched;
418  double rand_heap_pages;
419  double index_pages;
420 
421  /* Should only be applied to base relations */
422  Assert(IsA(baserel, RelOptInfo) &&
423  IsA(index, IndexOptInfo));
424  Assert(baserel->relid > 0);
425  Assert(baserel->rtekind == RTE_RELATION);
426 
427  /*
428  * Mark the path with the correct row estimate, and identify which quals
429  * will need to be enforced as qpquals. We need not check any quals that
430  * are implied by the index's predicate, so we can use indrestrictinfo not
431  * baserestrictinfo as the list of relevant restriction clauses for the
432  * rel.
433  */
434  if (path->path.param_info)
435  {
436  path->path.rows = path->path.param_info->ppi_rows;
437  /* qpquals come from the rel's restriction clauses and ppi_clauses */
438  qpquals = list_concat(
440  path->indexquals),
442  path->indexquals));
443  }
444  else
445  {
446  path->path.rows = baserel->rows;
447  /* qpquals come from just the rel's restriction clauses */
449  path->indexquals);
450  }
451 
452  if (!enable_indexscan)
453  startup_cost += disable_cost;
454  /* we don't need to check enable_indexonlyscan; indxpath.c does that */
455 
456  /*
457  * Call index-access-method-specific code to estimate the processing cost
458  * for scanning the index, as well as the selectivity of the index (ie,
459  * the fraction of main-table tuples we will have to retrieve) and its
460  * correlation to the main-table tuple order. We need a cast here because
461  * relation.h uses a weak function type to avoid including amapi.h.
462  */
463  amcostestimate = (amcostestimate_function) index->amcostestimate;
464  amcostestimate(root, path, loop_count,
465  &indexStartupCost, &indexTotalCost,
466  &indexSelectivity, &indexCorrelation,
467  &index_pages);
468 
469  /*
470  * Save amcostestimate's results for possible use in bitmap scan planning.
471  * We don't bother to save indexStartupCost or indexCorrelation, because a
472  * bitmap scan doesn't care about either.
473  */
474  path->indextotalcost = indexTotalCost;
475  path->indexselectivity = indexSelectivity;
476 
477  /* all costs for touching index itself included here */
478  startup_cost += indexStartupCost;
479  run_cost += indexTotalCost - indexStartupCost;
480 
481  /* estimate number of main-table tuples fetched */
482  tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
483 
484  /* fetch estimated page costs for tablespace containing table */
486  &spc_random_page_cost,
487  &spc_seq_page_cost);
488 
489  /*----------
490  * Estimate number of main-table pages fetched, and compute I/O cost.
491  *
492  * When the index ordering is uncorrelated with the table ordering,
493  * we use an approximation proposed by Mackert and Lohman (see
494  * index_pages_fetched() for details) to compute the number of pages
495  * fetched, and then charge spc_random_page_cost per page fetched.
496  *
497  * When the index ordering is exactly correlated with the table ordering
498  * (just after a CLUSTER, for example), the number of pages fetched should
499  * be exactly selectivity * table_size. What's more, all but the first
500  * will be sequential fetches, not the random fetches that occur in the
501  * uncorrelated case. So if the number of pages is more than 1, we
502  * ought to charge
503  * spc_random_page_cost + (pages_fetched - 1) * spc_seq_page_cost
504  * For partially-correlated indexes, we ought to charge somewhere between
505  * these two estimates. We currently interpolate linearly between the
506  * estimates based on the correlation squared (XXX is that appropriate?).
507  *
508  * If it's an index-only scan, then we will not need to fetch any heap
509  * pages for which the visibility map shows all tuples are visible.
510  * Hence, reduce the estimated number of heap fetches accordingly.
511  * We use the measured fraction of the entire heap that is all-visible,
512  * which might not be particularly relevant to the subset of the heap
513  * that this query will fetch; but it's not clear how to do better.
514  *----------
515  */
516  if (loop_count > 1)
517  {
518  /*
519  * For repeated indexscans, the appropriate estimate for the
520  * uncorrelated case is to scale up the number of tuples fetched in
521  * the Mackert and Lohman formula by the number of scans, so that we
522  * estimate the number of pages fetched by all the scans; then
523  * pro-rate the costs for one scan. In this case we assume all the
524  * fetches are random accesses.
525  */
526  pages_fetched = index_pages_fetched(tuples_fetched * loop_count,
527  baserel->pages,
528  (double) index->pages,
529  root);
530 
531  if (indexonly)
532  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
533 
534  rand_heap_pages = pages_fetched;
535 
536  max_IO_cost = (pages_fetched * spc_random_page_cost) / loop_count;
537 
538  /*
539  * In the perfectly correlated case, the number of pages touched by
540  * each scan is selectivity * table_size, and we can use the Mackert
541  * and Lohman formula at the page level to estimate how much work is
542  * saved by caching across scans. We still assume all the fetches are
543  * random, though, which is an overestimate that's hard to correct for
544  * without double-counting the cache effects. (But in most cases
545  * where such a plan is actually interesting, only one page would get
546  * fetched per scan anyway, so it shouldn't matter much.)
547  */
548  pages_fetched = ceil(indexSelectivity * (double) baserel->pages);
549 
550  pages_fetched = index_pages_fetched(pages_fetched * loop_count,
551  baserel->pages,
552  (double) index->pages,
553  root);
554 
555  if (indexonly)
556  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
557 
558  min_IO_cost = (pages_fetched * spc_random_page_cost) / loop_count;
559  }
560  else
561  {
562  /*
563  * Normal case: apply the Mackert and Lohman formula, and then
564  * interpolate between that and the correlation-derived result.
565  */
566  pages_fetched = index_pages_fetched(tuples_fetched,
567  baserel->pages,
568  (double) index->pages,
569  root);
570 
571  if (indexonly)
572  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
573 
574  rand_heap_pages = pages_fetched;
575 
576  /* max_IO_cost is for the perfectly uncorrelated case (csquared=0) */
577  max_IO_cost = pages_fetched * spc_random_page_cost;
578 
579  /* min_IO_cost is for the perfectly correlated case (csquared=1) */
580  pages_fetched = ceil(indexSelectivity * (double) baserel->pages);
581 
582  if (indexonly)
583  pages_fetched = ceil(pages_fetched * (1.0 - baserel->allvisfrac));
584 
585  if (pages_fetched > 0)
586  {
587  min_IO_cost = spc_random_page_cost;
588  if (pages_fetched > 1)
589  min_IO_cost += (pages_fetched - 1) * spc_seq_page_cost;
590  }
591  else
592  min_IO_cost = 0;
593  }
594 
595  if (partial_path)
596  {
597  /*
598  * Estimate the number of parallel workers required to scan index. Use
599  * the number of heap pages computed considering heap fetches won't be
600  * sequential as for parallel scans the pages are accessed in random
601  * order.
602  */
604  (BlockNumber) rand_heap_pages,
605  (BlockNumber) index_pages);
606 
607  /*
608  * Fall out if workers can't be assigned for parallel scan, because in
609  * such a case this path will be rejected. So there is no benefit in
610  * doing extra computation.
611  */
612  if (path->path.parallel_workers <= 0)
613  return;
614 
615  path->path.parallel_aware = true;
616  }
617 
618  /*
619  * Now interpolate based on estimated index order correlation to get total
620  * disk I/O cost for main table accesses.
621  */
622  csquared = indexCorrelation * indexCorrelation;
623 
624  run_cost += max_IO_cost + csquared * (min_IO_cost - max_IO_cost);
625 
626  /*
627  * Estimate CPU costs per tuple.
628  *
629  * What we want here is cpu_tuple_cost plus the evaluation costs of any
630  * qual clauses that we have to evaluate as qpquals.
631  */
632  cost_qual_eval(&qpqual_cost, qpquals, root);
633 
634  startup_cost += qpqual_cost.startup;
635  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
636 
637  cpu_run_cost += cpu_per_tuple * tuples_fetched;
638 
639  /* tlist eval costs are paid per output row, not per tuple scanned */
640  startup_cost += path->path.pathtarget->cost.startup;
641  cpu_run_cost += path->path.pathtarget->cost.per_tuple * path->path.rows;
642 
643  /* Adjust costing for parallelism, if used. */
644  if (path->path.parallel_workers > 0)
645  {
646  double parallel_divisor = get_parallel_divisor(&path->path);
647 
648  path->path.rows = clamp_row_est(path->path.rows / parallel_divisor);
649 
650  /* The CPU cost is divided among all the workers. */
651  cpu_run_cost /= parallel_divisor;
652  }
653 
654  run_cost += cpu_run_cost;
655 
656  path->path.startup_cost = startup_cost;
657  path->path.total_cost = startup_cost + run_cost;
658 }
659 
660 /*
661  * extract_nonindex_conditions
662  *
663  * Given a list of quals to be enforced in an indexscan, extract the ones that
664  * will have to be applied as qpquals (ie, the index machinery won't handle
665  * them). The actual rules for this appear in create_indexscan_plan() in
666  * createplan.c, but the full rules are fairly expensive and we don't want to
667  * go to that much effort for index paths that don't get selected for the
668  * final plan. So we approximate it as quals that don't appear directly in
669  * indexquals and also are not redundant children of the same EquivalenceClass
670  * as some indexqual. This method neglects some infrequently-relevant
671  * considerations, specifically clauses that needn't be checked because they
672  * are implied by an indexqual. It does not seem worth the cycles to try to
673  * factor that in at this stage, even though createplan.c will take pains to
674  * remove such unnecessary clauses from the qpquals list if this path is
675  * selected for use.
676  */
677 static List *
678 extract_nonindex_conditions(List *qual_clauses, List *indexquals)
679 {
680  List *result = NIL;
681  ListCell *lc;
682 
683  foreach(lc, qual_clauses)
684  {
685  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lc));
686 
687  if (rinfo->pseudoconstant)
688  continue; /* we may drop pseudoconstants here */
689  if (list_member_ptr(indexquals, rinfo))
690  continue; /* simple duplicate */
691  if (is_redundant_derived_clause(rinfo, indexquals))
692  continue; /* derived from same EquivalenceClass */
693  /* ... skip the predicate proof attempt createplan.c will try ... */
694  result = lappend(result, rinfo);
695  }
696  return result;
697 }
698 
699 /*
700  * index_pages_fetched
701  * Estimate the number of pages actually fetched after accounting for
702  * cache effects.
703  *
704  * We use an approximation proposed by Mackert and Lohman, "Index Scans
705  * Using a Finite LRU Buffer: A Validated I/O Model", ACM Transactions
706  * on Database Systems, Vol. 14, No. 3, September 1989, Pages 401-424.
707  * The Mackert and Lohman approximation is that the number of pages
708  * fetched is
709  * PF =
710  * min(2TNs/(2T+Ns), T) when T <= b
711  * 2TNs/(2T+Ns) when T > b and Ns <= 2Tb/(2T-b)
712  * b + (Ns - 2Tb/(2T-b))*(T-b)/T when T > b and Ns > 2Tb/(2T-b)
713  * where
714  * T = # pages in table
715  * N = # tuples in table
716  * s = selectivity = fraction of table to be scanned
717  * b = # buffer pages available (we include kernel space here)
718  *
719  * We assume that effective_cache_size is the total number of buffer pages
720  * available for the whole query, and pro-rate that space across all the
721  * tables in the query and the index currently under consideration. (This
722  * ignores space needed for other indexes used by the query, but since we
723  * don't know which indexes will get used, we can't estimate that very well;
724  * and in any case counting all the tables may well be an overestimate, since
725  * depending on the join plan not all the tables may be scanned concurrently.)
726  *
727  * The product Ns is the number of tuples fetched; we pass in that
728  * product rather than calculating it here. "pages" is the number of pages
729  * in the object under consideration (either an index or a table).
730  * "index_pages" is the amount to add to the total table space, which was
731  * computed for us by query_planner.
732  *
733  * Caller is expected to have ensured that tuples_fetched is greater than zero
734  * and rounded to integer (see clamp_row_est). The result will likewise be
735  * greater than zero and integral.
736  */
737 double
738 index_pages_fetched(double tuples_fetched, BlockNumber pages,
739  double index_pages, PlannerInfo *root)
740 {
741  double pages_fetched;
742  double total_pages;
743  double T,
744  b;
745 
746  /* T is # pages in table, but don't allow it to be zero */
747  T = (pages > 1) ? (double) pages : 1.0;
748 
749  /* Compute number of pages assumed to be competing for cache space */
750  total_pages = root->total_table_pages + index_pages;
751  total_pages = Max(total_pages, 1.0);
752  Assert(T <= total_pages);
753 
754  /* b is pro-rated share of effective_cache_size */
755  b = (double) effective_cache_size *T / total_pages;
756 
757  /* force it positive and integral */
758  if (b <= 1.0)
759  b = 1.0;
760  else
761  b = ceil(b);
762 
763  /* This part is the Mackert and Lohman formula */
764  if (T <= b)
765  {
766  pages_fetched =
767  (2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
768  if (pages_fetched >= T)
769  pages_fetched = T;
770  else
771  pages_fetched = ceil(pages_fetched);
772  }
773  else
774  {
775  double lim;
776 
777  lim = (2.0 * T * b) / (2.0 * T - b);
778  if (tuples_fetched <= lim)
779  {
780  pages_fetched =
781  (2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
782  }
783  else
784  {
785  pages_fetched =
786  b + (tuples_fetched - lim) * (T - b) / T;
787  }
788  pages_fetched = ceil(pages_fetched);
789  }
790  return pages_fetched;
791 }
792 
793 /*
794  * get_indexpath_pages
795  * Determine the total size of the indexes used in a bitmap index path.
796  *
797  * Note: if the same index is used more than once in a bitmap tree, we will
798  * count it multiple times, which perhaps is the wrong thing ... but it's
799  * not completely clear, and detecting duplicates is difficult, so ignore it
800  * for now.
801  */
802 static double
804 {
805  double result = 0;
806  ListCell *l;
807 
808  if (IsA(bitmapqual, BitmapAndPath))
809  {
810  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
811 
812  foreach(l, apath->bitmapquals)
813  {
814  result += get_indexpath_pages((Path *) lfirst(l));
815  }
816  }
817  else if (IsA(bitmapqual, BitmapOrPath))
818  {
819  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
820 
821  foreach(l, opath->bitmapquals)
822  {
823  result += get_indexpath_pages((Path *) lfirst(l));
824  }
825  }
826  else if (IsA(bitmapqual, IndexPath))
827  {
828  IndexPath *ipath = (IndexPath *) bitmapqual;
829 
830  result = (double) ipath->indexinfo->pages;
831  }
832  else
833  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
834 
835  return result;
836 }
837 
838 /*
839  * cost_bitmap_heap_scan
840  * Determines and returns the cost of scanning a relation using a bitmap
841  * index-then-heap plan.
842  *
843  * 'baserel' is the relation to be scanned
844  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
845  * 'bitmapqual' is a tree of IndexPaths, BitmapAndPaths, and BitmapOrPaths
846  * 'loop_count' is the number of repetitions of the indexscan to factor into
847  * estimates of caching behavior
848  *
849  * Note: the component IndexPaths in bitmapqual should have been costed
850  * using the same loop_count.
851  */
852 void
854  ParamPathInfo *param_info,
855  Path *bitmapqual, double loop_count)
856 {
857  Cost startup_cost = 0;
858  Cost run_cost = 0;
859  Cost indexTotalCost;
860  QualCost qpqual_cost;
861  Cost cpu_per_tuple;
862  Cost cost_per_page;
863  double tuples_fetched;
864  double pages_fetched;
865  double spc_seq_page_cost,
866  spc_random_page_cost;
867  double T;
868 
869  /* Should only be applied to base relations */
870  Assert(IsA(baserel, RelOptInfo));
871  Assert(baserel->relid > 0);
872  Assert(baserel->rtekind == RTE_RELATION);
873 
874  /* Mark the path with the correct row estimate */
875  if (param_info)
876  path->rows = param_info->ppi_rows;
877  else
878  path->rows = baserel->rows;
879 
880  if (!enable_bitmapscan)
881  startup_cost += disable_cost;
882 
883  pages_fetched = compute_bitmap_pages(root, baserel, bitmapqual,
884  loop_count, &indexTotalCost,
885  &tuples_fetched);
886 
887  startup_cost += indexTotalCost;
888  T = (baserel->pages > 1) ? (double) baserel->pages : 1.0;
889 
890  /* Fetch estimated page costs for tablespace containing table. */
892  &spc_random_page_cost,
893  &spc_seq_page_cost);
894 
895  /*
896  * For small numbers of pages we should charge spc_random_page_cost
897  * apiece, while if nearly all the table's pages are being read, it's more
898  * appropriate to charge spc_seq_page_cost apiece. The effect is
899  * nonlinear, too. For lack of a better idea, interpolate like this to
900  * determine the cost per page.
901  */
902  if (pages_fetched >= 2.0)
903  cost_per_page = spc_random_page_cost -
904  (spc_random_page_cost - spc_seq_page_cost)
905  * sqrt(pages_fetched / T);
906  else
907  cost_per_page = spc_random_page_cost;
908 
909  run_cost += pages_fetched * cost_per_page;
910 
911  /*
912  * Estimate CPU costs per tuple.
913  *
914  * Often the indexquals don't need to be rechecked at each tuple ... but
915  * not always, especially not if there are enough tuples involved that the
916  * bitmaps become lossy. For the moment, just assume they will be
917  * rechecked always. This means we charge the full freight for all the
918  * scan clauses.
919  */
920  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
921 
922  startup_cost += qpqual_cost.startup;
923  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
924 
925  run_cost += cpu_per_tuple * tuples_fetched;
926 
927  /* tlist eval costs are paid per output row, not per tuple scanned */
928  startup_cost += path->pathtarget->cost.startup;
929  run_cost += path->pathtarget->cost.per_tuple * path->rows;
930 
931  path->startup_cost = startup_cost;
932  path->total_cost = startup_cost + run_cost;
933 }
934 
935 /*
936  * cost_bitmap_tree_node
937  * Extract cost and selectivity from a bitmap tree node (index/and/or)
938  */
939 void
941 {
942  if (IsA(path, IndexPath))
943  {
944  *cost = ((IndexPath *) path)->indextotalcost;
945  *selec = ((IndexPath *) path)->indexselectivity;
946 
947  /*
948  * Charge a small amount per retrieved tuple to reflect the costs of
949  * manipulating the bitmap. This is mostly to make sure that a bitmap
950  * scan doesn't look to be the same cost as an indexscan to retrieve a
951  * single tuple.
952  */
953  *cost += 0.1 * cpu_operator_cost * path->rows;
954  }
955  else if (IsA(path, BitmapAndPath))
956  {
957  *cost = path->total_cost;
958  *selec = ((BitmapAndPath *) path)->bitmapselectivity;
959  }
960  else if (IsA(path, BitmapOrPath))
961  {
962  *cost = path->total_cost;
963  *selec = ((BitmapOrPath *) path)->bitmapselectivity;
964  }
965  else
966  {
967  elog(ERROR, "unrecognized node type: %d", nodeTag(path));
968  *cost = *selec = 0; /* keep compiler quiet */
969  }
970 }
971 
972 /*
973  * cost_bitmap_and_node
974  * Estimate the cost of a BitmapAnd node
975  *
976  * Note that this considers only the costs of index scanning and bitmap
977  * creation, not the eventual heap access. In that sense the object isn't
978  * truly a Path, but it has enough path-like properties (costs in particular)
979  * to warrant treating it as one. We don't bother to set the path rows field,
980  * however.
981  */
982 void
984 {
985  Cost totalCost;
986  Selectivity selec;
987  ListCell *l;
988 
989  /*
990  * We estimate AND selectivity on the assumption that the inputs are
991  * independent. This is probably often wrong, but we don't have the info
992  * to do better.
993  *
994  * The runtime cost of the BitmapAnd itself is estimated at 100x
995  * cpu_operator_cost for each tbm_intersect needed. Probably too small,
996  * definitely too simplistic?
997  */
998  totalCost = 0.0;
999  selec = 1.0;
1000  foreach(l, path->bitmapquals)
1001  {
1002  Path *subpath = (Path *) lfirst(l);
1003  Cost subCost;
1004  Selectivity subselec;
1005 
1006  cost_bitmap_tree_node(subpath, &subCost, &subselec);
1007 
1008  selec *= subselec;
1009 
1010  totalCost += subCost;
1011  if (l != list_head(path->bitmapquals))
1012  totalCost += 100.0 * cpu_operator_cost;
1013  }
1014  path->bitmapselectivity = selec;
1015  path->path.rows = 0; /* per above, not used */
1016  path->path.startup_cost = totalCost;
1017  path->path.total_cost = totalCost;
1018 }
1019 
1020 /*
1021  * cost_bitmap_or_node
1022  * Estimate the cost of a BitmapOr node
1023  *
1024  * See comments for cost_bitmap_and_node.
1025  */
1026 void
1028 {
1029  Cost totalCost;
1030  Selectivity selec;
1031  ListCell *l;
1032 
1033  /*
1034  * We estimate OR selectivity on the assumption that the inputs are
1035  * non-overlapping, since that's often the case in "x IN (list)" type
1036  * situations. Of course, we clamp to 1.0 at the end.
1037  *
1038  * The runtime cost of the BitmapOr itself is estimated at 100x
1039  * cpu_operator_cost for each tbm_union needed. Probably too small,
1040  * definitely too simplistic? We are aware that the tbm_unions are
1041  * optimized out when the inputs are BitmapIndexScans.
1042  */
1043  totalCost = 0.0;
1044  selec = 0.0;
1045  foreach(l, path->bitmapquals)
1046  {
1047  Path *subpath = (Path *) lfirst(l);
1048  Cost subCost;
1049  Selectivity subselec;
1050 
1051  cost_bitmap_tree_node(subpath, &subCost, &subselec);
1052 
1053  selec += subselec;
1054 
1055  totalCost += subCost;
1056  if (l != list_head(path->bitmapquals) &&
1057  !IsA(subpath, IndexPath))
1058  totalCost += 100.0 * cpu_operator_cost;
1059  }
1060  path->bitmapselectivity = Min(selec, 1.0);
1061  path->path.rows = 0; /* per above, not used */
1062  path->path.startup_cost = totalCost;
1063  path->path.total_cost = totalCost;
1064 }
1065 
1066 /*
1067  * cost_tidscan
1068  * Determines and returns the cost of scanning a relation using TIDs.
1069  *
1070  * 'baserel' is the relation to be scanned
1071  * 'tidquals' is the list of TID-checkable quals
1072  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
1073  */
1074 void
1076  RelOptInfo *baserel, List *tidquals, ParamPathInfo *param_info)
1077 {
1078  Cost startup_cost = 0;
1079  Cost run_cost = 0;
1080  bool isCurrentOf = false;
1081  QualCost qpqual_cost;
1082  Cost cpu_per_tuple;
1083  QualCost tid_qual_cost;
1084  int ntuples;
1085  ListCell *l;
1086  double spc_random_page_cost;
1087 
1088  /* Should only be applied to base relations */
1089  Assert(baserel->relid > 0);
1090  Assert(baserel->rtekind == RTE_RELATION);
1091 
1092  /* Mark the path with the correct row estimate */
1093  if (param_info)
1094  path->rows = param_info->ppi_rows;
1095  else
1096  path->rows = baserel->rows;
1097 
1098  /* Count how many tuples we expect to retrieve */
1099  ntuples = 0;
1100  foreach(l, tidquals)
1101  {
1102  if (IsA(lfirst(l), ScalarArrayOpExpr))
1103  {
1104  /* Each element of the array yields 1 tuple */
1106  Node *arraynode = (Node *) lsecond(saop->args);
1107 
1108  ntuples += estimate_array_length(arraynode);
1109  }
1110  else if (IsA(lfirst(l), CurrentOfExpr))
1111  {
1112  /* CURRENT OF yields 1 tuple */
1113  isCurrentOf = true;
1114  ntuples++;
1115  }
1116  else
1117  {
1118  /* It's just CTID = something, count 1 tuple */
1119  ntuples++;
1120  }
1121  }
1122 
1123  /*
1124  * We must force TID scan for WHERE CURRENT OF, because only nodeTidscan.c
1125  * understands how to do it correctly. Therefore, honor enable_tidscan
1126  * only when CURRENT OF isn't present. Also note that cost_qual_eval
1127  * counts a CurrentOfExpr as having startup cost disable_cost, which we
1128  * subtract off here; that's to prevent other plan types such as seqscan
1129  * from winning.
1130  */
1131  if (isCurrentOf)
1132  {
1134  startup_cost -= disable_cost;
1135  }
1136  else if (!enable_tidscan)
1137  startup_cost += disable_cost;
1138 
1139  /*
1140  * The TID qual expressions will be computed once, any other baserestrict
1141  * quals once per retrieved tuple.
1142  */
1143  cost_qual_eval(&tid_qual_cost, tidquals, root);
1144 
1145  /* fetch estimated page cost for tablespace containing table */
1147  &spc_random_page_cost,
1148  NULL);
1149 
1150  /* disk costs --- assume each tuple on a different page */
1151  run_cost += spc_random_page_cost * ntuples;
1152 
1153  /* Add scanning CPU costs */
1154  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1155 
1156  /* XXX currently we assume TID quals are a subset of qpquals */
1157  startup_cost += qpqual_cost.startup + tid_qual_cost.per_tuple;
1158  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple -
1159  tid_qual_cost.per_tuple;
1160  run_cost += cpu_per_tuple * ntuples;
1161 
1162  /* tlist eval costs are paid per output row, not per tuple scanned */
1163  startup_cost += path->pathtarget->cost.startup;
1164  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1165 
1166  path->startup_cost = startup_cost;
1167  path->total_cost = startup_cost + run_cost;
1168 }
1169 
1170 /*
1171  * cost_subqueryscan
1172  * Determines and returns the cost of scanning a subquery RTE.
1173  *
1174  * 'baserel' is the relation to be scanned
1175  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
1176  */
1177 void
1179  RelOptInfo *baserel, ParamPathInfo *param_info)
1180 {
1181  Cost startup_cost;
1182  Cost run_cost;
1183  QualCost qpqual_cost;
1184  Cost cpu_per_tuple;
1185 
1186  /* Should only be applied to base relations that are subqueries */
1187  Assert(baserel->relid > 0);
1188  Assert(baserel->rtekind == RTE_SUBQUERY);
1189 
1190  /* Mark the path with the correct row estimate */
1191  if (param_info)
1192  path->path.rows = param_info->ppi_rows;
1193  else
1194  path->path.rows = baserel->rows;
1195 
1196  /*
1197  * Cost of path is cost of evaluating the subplan, plus cost of evaluating
1198  * any restriction clauses and tlist that will be attached to the
1199  * SubqueryScan node, plus cpu_tuple_cost to account for selection and
1200  * projection overhead.
1201  */
1202  path->path.startup_cost = path->subpath->startup_cost;
1203  path->path.total_cost = path->subpath->total_cost;
1204 
1205  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1206 
1207  startup_cost = qpqual_cost.startup;
1208  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
1209  run_cost = cpu_per_tuple * baserel->tuples;
1210 
1211  /* tlist eval costs are paid per output row, not per tuple scanned */
1212  startup_cost += path->path.pathtarget->cost.startup;
1213  run_cost += path->path.pathtarget->cost.per_tuple * path->path.rows;
1214 
1215  path->path.startup_cost += startup_cost;
1216  path->path.total_cost += startup_cost + run_cost;
1217 }
1218 
1219 /*
1220  * cost_functionscan
1221  * Determines and returns the cost of scanning a function RTE.
1222  *
1223  * 'baserel' is the relation to be scanned
1224  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
1225  */
1226 void
1228  RelOptInfo *baserel, ParamPathInfo *param_info)
1229 {
1230  Cost startup_cost = 0;
1231  Cost run_cost = 0;
1232  QualCost qpqual_cost;
1233  Cost cpu_per_tuple;
1234  RangeTblEntry *rte;
1235  QualCost exprcost;
1236 
1237  /* Should only be applied to base relations that are functions */
1238  Assert(baserel->relid > 0);
1239  rte = planner_rt_fetch(baserel->relid, root);
1240  Assert(rte->rtekind == RTE_FUNCTION);
1241 
1242  /* Mark the path with the correct row estimate */
1243  if (param_info)
1244  path->rows = param_info->ppi_rows;
1245  else
1246  path->rows = baserel->rows;
1247 
1248  /*
1249  * Estimate costs of executing the function expression(s).
1250  *
1251  * Currently, nodeFunctionscan.c always executes the functions to
1252  * completion before returning any rows, and caches the results in a
1253  * tuplestore. So the function eval cost is all startup cost, and per-row
1254  * costs are minimal.
1255  *
1256  * XXX in principle we ought to charge tuplestore spill costs if the
1257  * number of rows is large. However, given how phony our rowcount
1258  * estimates for functions tend to be, there's not a lot of point in that
1259  * refinement right now.
1260  */
1261  cost_qual_eval_node(&exprcost, (Node *) rte->functions, root);
1262 
1263  startup_cost += exprcost.startup + exprcost.per_tuple;
1264 
1265  /* Add scanning CPU costs */
1266  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1267 
1268  startup_cost += qpqual_cost.startup;
1269  cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
1270  run_cost += cpu_per_tuple * baserel->tuples;
1271 
1272  /* tlist eval costs are paid per output row, not per tuple scanned */
1273  startup_cost += path->pathtarget->cost.startup;
1274  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1275 
1276  path->startup_cost = startup_cost;
1277  path->total_cost = startup_cost + run_cost;
1278 }
1279 
1280 /*
1281  * cost_valuesscan
1282  * Determines and returns the cost of scanning a VALUES RTE.
1283  *
1284  * 'baserel' is the relation to be scanned
1285  * 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
1286  */
1287 void
1289  RelOptInfo *baserel, ParamPathInfo *param_info)
1290 {
1291  Cost startup_cost = 0;
1292  Cost run_cost = 0;
1293  QualCost qpqual_cost;
1294  Cost cpu_per_tuple;
1295 
1296  /* Should only be applied to base relations that are values lists */
1297  Assert(baserel->relid > 0);
1298  Assert(baserel->rtekind == RTE_VALUES);
1299 
1300  /* Mark the path with the correct row estimate */
1301  if (param_info)
1302  path->rows = param_info->ppi_rows;
1303  else
1304  path->rows = baserel->rows;
1305 
1306  /*
1307  * For now, estimate list evaluation cost at one operator eval per list
1308  * (probably pretty bogus, but is it worth being smarter?)
1309  */
1310  cpu_per_tuple = cpu_operator_cost;
1311 
1312  /* Add scanning CPU costs */
1313  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1314 
1315  startup_cost += qpqual_cost.startup;
1316  cpu_per_tuple += cpu_tuple_cost + qpqual_cost.per_tuple;
1317  run_cost += cpu_per_tuple * baserel->tuples;
1318 
1319  /* tlist eval costs are paid per output row, not per tuple scanned */
1320  startup_cost += path->pathtarget->cost.startup;
1321  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1322 
1323  path->startup_cost = startup_cost;
1324  path->total_cost = startup_cost + run_cost;
1325 }
1326 
1327 /*
1328  * cost_ctescan
1329  * Determines and returns the cost of scanning a CTE RTE.
1330  *
1331  * Note: this is used for both self-reference and regular CTEs; the
1332  * possible cost differences are below the threshold of what we could
1333  * estimate accurately anyway. Note that the costs of evaluating the
1334  * referenced CTE query are added into the final plan as initplan costs,
1335  * and should NOT be counted here.
1336  */
1337 void
1339  RelOptInfo *baserel, ParamPathInfo *param_info)
1340 {
1341  Cost startup_cost = 0;
1342  Cost run_cost = 0;
1343  QualCost qpqual_cost;
1344  Cost cpu_per_tuple;
1345 
1346  /* Should only be applied to base relations that are CTEs */
1347  Assert(baserel->relid > 0);
1348  Assert(baserel->rtekind == RTE_CTE);
1349 
1350  /* Mark the path with the correct row estimate */
1351  if (param_info)
1352  path->rows = param_info->ppi_rows;
1353  else
1354  path->rows = baserel->rows;
1355 
1356  /* Charge one CPU tuple cost per row for tuplestore manipulation */
1357  cpu_per_tuple = cpu_tuple_cost;
1358 
1359  /* Add scanning CPU costs */
1360  get_restriction_qual_cost(root, baserel, param_info, &qpqual_cost);
1361 
1362  startup_cost += qpqual_cost.startup;
1363  cpu_per_tuple += cpu_tuple_cost + qpqual_cost.per_tuple;
1364  run_cost += cpu_per_tuple * baserel->tuples;
1365 
1366  /* tlist eval costs are paid per output row, not per tuple scanned */
1367  startup_cost += path->pathtarget->cost.startup;
1368  run_cost += path->pathtarget->cost.per_tuple * path->rows;
1369 
1370  path->startup_cost = startup_cost;
1371  path->total_cost = startup_cost + run_cost;
1372 }
1373 
1374 /*
1375  * cost_recursive_union
1376  * Determines and returns the cost of performing a recursive union,
1377  * and also the estimated output size.
1378  *
1379  * We are given Paths for the nonrecursive and recursive terms.
1380  */
1381 void
1382 cost_recursive_union(Path *runion, Path *nrterm, Path *rterm)
1383 {
1384  Cost startup_cost;
1385  Cost total_cost;
1386  double total_rows;
1387 
1388  /* We probably have decent estimates for the non-recursive term */
1389  startup_cost = nrterm->startup_cost;
1390  total_cost = nrterm->total_cost;
1391  total_rows = nrterm->rows;
1392 
1393  /*
1394  * We arbitrarily assume that about 10 recursive iterations will be
1395  * needed, and that we've managed to get a good fix on the cost and output
1396  * size of each one of them. These are mighty shaky assumptions but it's
1397  * hard to see how to do better.
1398  */
1399  total_cost += 10 * rterm->total_cost;
1400  total_rows += 10 * rterm->rows;
1401 
1402  /*
1403  * Also charge cpu_tuple_cost per row to account for the costs of
1404  * manipulating the tuplestores. (We don't worry about possible
1405  * spill-to-disk costs.)
1406  */
1407  total_cost += cpu_tuple_cost * total_rows;
1408 
1409  runion->startup_cost = startup_cost;
1410  runion->total_cost = total_cost;
1411  runion->rows = total_rows;
1412  runion->pathtarget->width = Max(nrterm->pathtarget->width,
1413  rterm->pathtarget->width);
1414 }
1415 
1416 /*
1417  * cost_sort
1418  * Determines and returns the cost of sorting a relation, including
1419  * the cost of reading the input data.
1420  *
1421  * If the total volume of data to sort is less than sort_mem, we will do
1422  * an in-memory sort, which requires no I/O and about t*log2(t) tuple
1423  * comparisons for t tuples.
1424  *
1425  * If the total volume exceeds sort_mem, we switch to a tape-style merge
1426  * algorithm. There will still be about t*log2(t) tuple comparisons in
1427  * total, but we will also need to write and read each tuple once per
1428  * merge pass. We expect about ceil(logM(r)) merge passes where r is the
1429  * number of initial runs formed and M is the merge order used by tuplesort.c.
1430  * Since the average initial run should be about sort_mem, we have
1431  * disk traffic = 2 * relsize * ceil(logM(p / sort_mem))
1432  * cpu = comparison_cost * t * log2(t)
1433  *
1434  * If the sort is bounded (i.e., only the first k result tuples are needed)
1435  * and k tuples can fit into sort_mem, we use a heap method that keeps only
1436  * k tuples in the heap; this will require about t*log2(k) tuple comparisons.
1437  *
1438  * The disk traffic is assumed to be 3/4ths sequential and 1/4th random
1439  * accesses (XXX can't we refine that guess?)
1440  *
1441  * By default, we charge two operator evals per tuple comparison, which should
1442  * be in the right ballpark in most cases. The caller can tweak this by
1443  * specifying nonzero comparison_cost; typically that's used for any extra
1444  * work that has to be done to prepare the inputs to the comparison operators.
1445  *
1446  * 'pathkeys' is a list of sort keys
1447  * 'input_cost' is the total cost for reading the input data
1448  * 'tuples' is the number of tuples in the relation
1449  * 'width' is the average tuple width in bytes
1450  * 'comparison_cost' is the extra cost per comparison, if any
1451  * 'sort_mem' is the number of kilobytes of work memory allowed for the sort
1452  * 'limit_tuples' is the bound on the number of output tuples; -1 if no bound
1453  *
1454  * NOTE: some callers currently pass NIL for pathkeys because they
1455  * can't conveniently supply the sort keys. Since this routine doesn't
1456  * currently do anything with pathkeys anyway, that doesn't matter...
1457  * but if it ever does, it should react gracefully to lack of key data.
1458  * (Actually, the thing we'd most likely be interested in is just the number
1459  * of sort keys, which all callers *could* supply.)
1460  */
1461 void
1463  List *pathkeys, Cost input_cost, double tuples, int width,
1464  Cost comparison_cost, int sort_mem,
1465  double limit_tuples)
1466 {
1467  Cost startup_cost = input_cost;
1468  Cost run_cost = 0;
1469  double input_bytes = relation_byte_size(tuples, width);
1470  double output_bytes;
1471  double output_tuples;
1472  long sort_mem_bytes = sort_mem * 1024L;
1473 
1474  if (!enable_sort)
1475  startup_cost += disable_cost;
1476 
1477  path->rows = tuples;
1478 
1479  /*
1480  * We want to be sure the cost of a sort is never estimated as zero, even
1481  * if passed-in tuple count is zero. Besides, mustn't do log(0)...
1482  */
1483  if (tuples < 2.0)
1484  tuples = 2.0;
1485 
1486  /* Include the default cost-per-comparison */
1487  comparison_cost += 2.0 * cpu_operator_cost;
1488 
1489  /* Do we have a useful LIMIT? */
1490  if (limit_tuples > 0 && limit_tuples < tuples)
1491  {
1492  output_tuples = limit_tuples;
1493  output_bytes = relation_byte_size(output_tuples, width);
1494  }
1495  else
1496  {
1497  output_tuples = tuples;
1498  output_bytes = input_bytes;
1499  }
1500 
1501  if (output_bytes > sort_mem_bytes)
1502  {
1503  /*
1504  * We'll have to use a disk-based sort of all the tuples
1505  */
1506  double npages = ceil(input_bytes / BLCKSZ);
1507  double nruns = input_bytes / sort_mem_bytes;
1508  double mergeorder = tuplesort_merge_order(sort_mem_bytes);
1509  double log_runs;
1510  double npageaccesses;
1511 
1512  /*
1513  * CPU costs
1514  *
1515  * Assume about N log2 N comparisons
1516  */
1517  startup_cost += comparison_cost * tuples * LOG2(tuples);
1518 
1519  /* Disk costs */
1520 
1521  /* Compute logM(r) as log(r) / log(M) */
1522  if (nruns > mergeorder)
1523  log_runs = ceil(log(nruns) / log(mergeorder));
1524  else
1525  log_runs = 1.0;
1526  npageaccesses = 2.0 * npages * log_runs;
1527  /* Assume 3/4ths of accesses are sequential, 1/4th are not */
1528  startup_cost += npageaccesses *
1529  (seq_page_cost * 0.75 + random_page_cost * 0.25);
1530  }
1531  else if (tuples > 2 * output_tuples || input_bytes > sort_mem_bytes)
1532  {
1533  /*
1534  * We'll use a bounded heap-sort keeping just K tuples in memory, for
1535  * a total number of tuple comparisons of N log2 K; but the constant
1536  * factor is a bit higher than for quicksort. Tweak it so that the
1537  * cost curve is continuous at the crossover point.
1538  */
1539  startup_cost += comparison_cost * tuples * LOG2(2.0 * output_tuples);
1540  }
1541  else
1542  {
1543  /* We'll use plain quicksort on all the input tuples */
1544  startup_cost += comparison_cost * tuples * LOG2(tuples);
1545  }
1546 
1547  /*
1548  * Also charge a small amount (arbitrarily set equal to operator cost) per
1549  * extracted tuple. We don't charge cpu_tuple_cost because a Sort node
1550  * doesn't do qual-checking or projection, so it has less overhead than
1551  * most plan nodes. Note it's correct to use tuples not output_tuples
1552  * here --- the upper LIMIT will pro-rate the run cost so we'd be double
1553  * counting the LIMIT otherwise.
1554  */
1555  run_cost += cpu_operator_cost * tuples;
1556 
1557  path->startup_cost = startup_cost;
1558  path->total_cost = startup_cost + run_cost;
1559 }
1560 
1561 /*
1562  * cost_merge_append
1563  * Determines and returns the cost of a MergeAppend node.
1564  *
1565  * MergeAppend merges several pre-sorted input streams, using a heap that
1566  * at any given instant holds the next tuple from each stream. If there
1567  * are N streams, we need about N*log2(N) tuple comparisons to construct
1568  * the heap at startup, and then for each output tuple, about log2(N)
1569  * comparisons to replace the top entry.
1570  *
1571  * (The effective value of N will drop once some of the input streams are
1572  * exhausted, but it seems unlikely to be worth trying to account for that.)
1573  *
1574  * The heap is never spilled to disk, since we assume N is not very large.
1575  * So this is much simpler than cost_sort.
1576  *
1577  * As in cost_sort, we charge two operator evals per tuple comparison.
1578  *
1579  * 'pathkeys' is a list of sort keys
1580  * 'n_streams' is the number of input streams
1581  * 'input_startup_cost' is the sum of the input streams' startup costs
1582  * 'input_total_cost' is the sum of the input streams' total costs
1583  * 'tuples' is the number of tuples in all the streams
1584  */
1585 void
1587  List *pathkeys, int n_streams,
1588  Cost input_startup_cost, Cost input_total_cost,
1589  double tuples)
1590 {
1591  Cost startup_cost = 0;
1592  Cost run_cost = 0;
1593  Cost comparison_cost;
1594  double N;
1595  double logN;
1596 
1597  /*
1598  * Avoid log(0)...
1599  */
1600  N = (n_streams < 2) ? 2.0 : (double) n_streams;
1601  logN = LOG2(N);
1602 
1603  /* Assumed cost per tuple comparison */
1604  comparison_cost = 2.0 * cpu_operator_cost;
1605 
1606  /* Heap creation cost */
1607  startup_cost += comparison_cost * N * logN;
1608 
1609  /* Per-tuple heap maintenance cost */
1610  run_cost += tuples * comparison_cost * logN;
1611 
1612  /*
1613  * Also charge a small amount (arbitrarily set equal to operator cost) per
1614  * extracted tuple. We don't charge cpu_tuple_cost because a MergeAppend
1615  * node doesn't do qual-checking or projection, so it has less overhead
1616  * than most plan nodes.
1617  */
1618  run_cost += cpu_operator_cost * tuples;
1619 
1620  path->startup_cost = startup_cost + input_startup_cost;
1621  path->total_cost = startup_cost + run_cost + input_total_cost;
1622 }
1623 
1624 /*
1625  * cost_material
1626  * Determines and returns the cost of materializing a relation, including
1627  * the cost of reading the input data.
1628  *
1629  * If the total volume of data to materialize exceeds work_mem, we will need
1630  * to write it to disk, so the cost is much higher in that case.
1631  *
1632  * Note that here we are estimating the costs for the first scan of the
1633  * relation, so the materialization is all overhead --- any savings will
1634  * occur only on rescan, which is estimated in cost_rescan.
1635  */
1636 void
1638  Cost input_startup_cost, Cost input_total_cost,
1639  double tuples, int width)
1640 {
1641  Cost startup_cost = input_startup_cost;
1642  Cost run_cost = input_total_cost - input_startup_cost;
1643  double nbytes = relation_byte_size(tuples, width);
1644  long work_mem_bytes = work_mem * 1024L;
1645 
1646  path->rows = tuples;
1647 
1648  /*
1649  * Whether spilling or not, charge 2x cpu_operator_cost per tuple to
1650  * reflect bookkeeping overhead. (This rate must be more than what
1651  * cost_rescan charges for materialize, ie, cpu_operator_cost per tuple;
1652  * if it is exactly the same then there will be a cost tie between
1653  * nestloop with A outer, materialized B inner and nestloop with B outer,
1654  * materialized A inner. The extra cost ensures we'll prefer
1655  * materializing the smaller rel.) Note that this is normally a good deal
1656  * less than cpu_tuple_cost; which is OK because a Material plan node
1657  * doesn't do qual-checking or projection, so it's got less overhead than
1658  * most plan nodes.
1659  */
1660  run_cost += 2 * cpu_operator_cost * tuples;
1661 
1662  /*
1663  * If we will spill to disk, charge at the rate of seq_page_cost per page.
1664  * This cost is assumed to be evenly spread through the plan run phase,
1665  * which isn't exactly accurate but our cost model doesn't allow for
1666  * nonuniform costs within the run phase.
1667  */
1668  if (nbytes > work_mem_bytes)
1669  {
1670  double npages = ceil(nbytes / BLCKSZ);
1671 
1672  run_cost += seq_page_cost * npages;
1673  }
1674 
1675  path->startup_cost = startup_cost;
1676  path->total_cost = startup_cost + run_cost;
1677 }
1678 
1679 /*
1680  * cost_agg
1681  * Determines and returns the cost of performing an Agg plan node,
1682  * including the cost of its input.
1683  *
1684  * aggcosts can be NULL when there are no actual aggregate functions (i.e.,
1685  * we are using a hashed Agg node just to do grouping).
1686  *
1687  * Note: when aggstrategy == AGG_SORTED, caller must ensure that input costs
1688  * are for appropriately-sorted input.
1689  */
1690 void
1692  AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
1693  int numGroupCols, double numGroups,
1694  Cost input_startup_cost, Cost input_total_cost,
1695  double input_tuples)
1696 {
1697  double output_tuples;
1698  Cost startup_cost;
1699  Cost total_cost;
1700  AggClauseCosts dummy_aggcosts;
1701 
1702  /* Use all-zero per-aggregate costs if NULL is passed */
1703  if (aggcosts == NULL)
1704  {
1705  Assert(aggstrategy == AGG_HASHED);
1706  MemSet(&dummy_aggcosts, 0, sizeof(AggClauseCosts));
1707  aggcosts = &dummy_aggcosts;
1708  }
1709 
1710  /*
1711  * The transCost.per_tuple component of aggcosts should be charged once
1712  * per input tuple, corresponding to the costs of evaluating the aggregate
1713  * transfns and their input expressions (with any startup cost of course
1714  * charged but once). The finalCost component is charged once per output
1715  * tuple, corresponding to the costs of evaluating the finalfns.
1716  *
1717  * If we are grouping, we charge an additional cpu_operator_cost per
1718  * grouping column per input tuple for grouping comparisons.
1719  *
1720  * We will produce a single output tuple if not grouping, and a tuple per
1721  * group otherwise. We charge cpu_tuple_cost for each output tuple.
1722  *
1723  * Note: in this cost model, AGG_SORTED and AGG_HASHED have exactly the
1724  * same total CPU cost, but AGG_SORTED has lower startup cost. If the
1725  * input path is already sorted appropriately, AGG_SORTED should be
1726  * preferred (since it has no risk of memory overflow). This will happen
1727  * as long as the computed total costs are indeed exactly equal --- but if
1728  * there's roundoff error we might do the wrong thing. So be sure that
1729  * the computations below form the same intermediate values in the same
1730  * order.
1731  */
1732  if (aggstrategy == AGG_PLAIN)
1733  {
1734  startup_cost = input_total_cost;
1735  startup_cost += aggcosts->transCost.startup;
1736  startup_cost += aggcosts->transCost.per_tuple * input_tuples;
1737  startup_cost += aggcosts->finalCost;
1738  /* we aren't grouping */
1739  total_cost = startup_cost + cpu_tuple_cost;
1740  output_tuples = 1;
1741  }
1742  else if (aggstrategy == AGG_SORTED)
1743  {
1744  /* Here we are able to deliver output on-the-fly */
1745  startup_cost = input_startup_cost;
1746  total_cost = input_total_cost;
1747  /* calcs phrased this way to match HASHED case, see note above */
1748  total_cost += aggcosts->transCost.startup;
1749  total_cost += aggcosts->transCost.per_tuple * input_tuples;
1750  total_cost += (cpu_operator_cost * numGroupCols) * input_tuples;
1751  total_cost += aggcosts->finalCost * numGroups;
1752  total_cost += cpu_tuple_cost * numGroups;
1753  output_tuples = numGroups;
1754  }
1755  else
1756  {
1757  /* must be AGG_HASHED */
1758  startup_cost = input_total_cost;
1759  if (!enable_hashagg)
1760  startup_cost += disable_cost;
1761  startup_cost += aggcosts->transCost.startup;
1762  startup_cost += aggcosts->transCost.per_tuple * input_tuples;
1763  startup_cost += (cpu_operator_cost * numGroupCols) * input_tuples;
1764  total_cost = startup_cost;
1765  total_cost += aggcosts->finalCost * numGroups;
1766  total_cost += cpu_tuple_cost * numGroups;
1767  output_tuples = numGroups;
1768  }
1769 
1770  path->rows = output_tuples;
1771  path->startup_cost = startup_cost;
1772  path->total_cost = total_cost;
1773 }
1774 
1775 /*
1776  * cost_windowagg
1777  * Determines and returns the cost of performing a WindowAgg plan node,
1778  * including the cost of its input.
1779  *
1780  * Input is assumed already properly sorted.
1781  */
1782 void
1784  List *windowFuncs, int numPartCols, int numOrderCols,
1785  Cost input_startup_cost, Cost input_total_cost,
1786  double input_tuples)
1787 {
1788  Cost startup_cost;
1789  Cost total_cost;
1790  ListCell *lc;
1791 
1792  startup_cost = input_startup_cost;
1793  total_cost = input_total_cost;
1794 
1795  /*
1796  * Window functions are assumed to cost their stated execution cost, plus
1797  * the cost of evaluating their input expressions, per tuple. Since they
1798  * may in fact evaluate their inputs at multiple rows during each cycle,
1799  * this could be a drastic underestimate; but without a way to know how
1800  * many rows the window function will fetch, it's hard to do better. In
1801  * any case, it's a good estimate for all the built-in window functions,
1802  * so we'll just do this for now.
1803  */
1804  foreach(lc, windowFuncs)
1805  {
1806  WindowFunc *wfunc = castNode(WindowFunc, lfirst(lc));
1807  Cost wfunccost;
1808  QualCost argcosts;
1809 
1810  wfunccost = get_func_cost(wfunc->winfnoid) * cpu_operator_cost;
1811 
1812  /* also add the input expressions' cost to per-input-row costs */
1813  cost_qual_eval_node(&argcosts, (Node *) wfunc->args, root);
1814  startup_cost += argcosts.startup;
1815  wfunccost += argcosts.per_tuple;
1816 
1817  /*
1818  * Add the filter's cost to per-input-row costs. XXX We should reduce
1819  * input expression costs according to filter selectivity.
1820  */
1821  cost_qual_eval_node(&argcosts, (Node *) wfunc->aggfilter, root);
1822  startup_cost += argcosts.startup;
1823  wfunccost += argcosts.per_tuple;
1824 
1825  total_cost += wfunccost * input_tuples;
1826  }
1827 
1828  /*
1829  * We also charge cpu_operator_cost per grouping column per tuple for
1830  * grouping comparisons, plus cpu_tuple_cost per tuple for general
1831  * overhead.
1832  *
1833  * XXX this neglects costs of spooling the data to disk when it overflows
1834  * work_mem. Sooner or later that should get accounted for.
1835  */
1836  total_cost += cpu_operator_cost * (numPartCols + numOrderCols) * input_tuples;
1837  total_cost += cpu_tuple_cost * input_tuples;
1838 
1839  path->rows = input_tuples;
1840  path->startup_cost = startup_cost;
1841  path->total_cost = total_cost;
1842 }
1843 
1844 /*
1845  * cost_group
1846  * Determines and returns the cost of performing a Group plan node,
1847  * including the cost of its input.
1848  *
1849  * Note: caller must ensure that input costs are for appropriately-sorted
1850  * input.
1851  */
1852 void
1854  int numGroupCols, double numGroups,
1855  Cost input_startup_cost, Cost input_total_cost,
1856  double input_tuples)
1857 {
1858  Cost startup_cost;
1859  Cost total_cost;
1860 
1861  startup_cost = input_startup_cost;
1862  total_cost = input_total_cost;
1863 
1864  /*
1865  * Charge one cpu_operator_cost per comparison per input tuple. We assume
1866  * all columns get compared at most of the tuples.
1867  */
1868  total_cost += cpu_operator_cost * input_tuples * numGroupCols;
1869 
1870  path->rows = numGroups;
1871  path->startup_cost = startup_cost;
1872  path->total_cost = total_cost;
1873 }
1874 
1875 /*
1876  * initial_cost_nestloop
1877  * Preliminary estimate of the cost of a nestloop join path.
1878  *
1879  * This must quickly produce lower-bound estimates of the path's startup and
1880  * total costs. If we are unable to eliminate the proposed path from
1881  * consideration using the lower bounds, final_cost_nestloop will be called
1882  * to obtain the final estimates.
1883  *
1884  * The exact division of labor between this function and final_cost_nestloop
1885  * is private to them, and represents a tradeoff between speed of the initial
1886  * estimate and getting a tight lower bound. We choose to not examine the
1887  * join quals here, since that's by far the most expensive part of the
1888  * calculations. The end result is that CPU-cost considerations must be
1889  * left for the second phase; and for SEMI/ANTI joins, we must also postpone
1890  * incorporation of the inner path's run cost.
1891  *
1892  * 'workspace' is to be filled with startup_cost, total_cost, and perhaps
1893  * other data to be used by final_cost_nestloop
1894  * 'jointype' is the type of join to be performed
1895  * 'outer_path' is the outer input to the join
1896  * 'inner_path' is the inner input to the join
1897  * 'sjinfo' is extra info about the join for selectivity estimation
1898  * 'semifactors' contains valid data if jointype is SEMI or ANTI
1899  */
1900 void
1902  JoinType jointype,
1903  Path *outer_path, Path *inner_path,
1904  SpecialJoinInfo *sjinfo,
1905  SemiAntiJoinFactors *semifactors)
1906 {
1907  Cost startup_cost = 0;
1908  Cost run_cost = 0;
1909  double outer_path_rows = outer_path->rows;
1910  Cost inner_rescan_start_cost;
1911  Cost inner_rescan_total_cost;
1912  Cost inner_run_cost;
1913  Cost inner_rescan_run_cost;
1914 
1915  /* estimate costs to rescan the inner relation */
1916  cost_rescan(root, inner_path,
1917  &inner_rescan_start_cost,
1918  &inner_rescan_total_cost);
1919 
1920  /* cost of source data */
1921 
1922  /*
1923  * NOTE: clearly, we must pay both outer and inner paths' startup_cost
1924  * before we can start returning tuples, so the join's startup cost is
1925  * their sum. We'll also pay the inner path's rescan startup cost
1926  * multiple times.
1927  */
1928  startup_cost += outer_path->startup_cost + inner_path->startup_cost;
1929  run_cost += outer_path->total_cost - outer_path->startup_cost;
1930  if (outer_path_rows > 1)
1931  run_cost += (outer_path_rows - 1) * inner_rescan_start_cost;
1932 
1933  inner_run_cost = inner_path->total_cost - inner_path->startup_cost;
1934  inner_rescan_run_cost = inner_rescan_total_cost - inner_rescan_start_cost;
1935 
1936  if (jointype == JOIN_SEMI || jointype == JOIN_ANTI)
1937  {
1938  /*
1939  * SEMI or ANTI join: executor will stop after first match.
1940  *
1941  * Getting decent estimates requires inspection of the join quals,
1942  * which we choose to postpone to final_cost_nestloop.
1943  */
1944 
1945  /* Save private data for final_cost_nestloop */
1946  workspace->inner_run_cost = inner_run_cost;
1947  workspace->inner_rescan_run_cost = inner_rescan_run_cost;
1948  }
1949  else
1950  {
1951  /* Normal case; we'll scan whole input rel for each outer row */
1952  run_cost += inner_run_cost;
1953  if (outer_path_rows > 1)
1954  run_cost += (outer_path_rows - 1) * inner_rescan_run_cost;
1955  }
1956 
1957  /* CPU costs left for later */
1958 
1959  /* Public result fields */
1960  workspace->startup_cost = startup_cost;
1961  workspace->total_cost = startup_cost + run_cost;
1962  /* Save private data for final_cost_nestloop */
1963  workspace->run_cost = run_cost;
1964 }
1965 
1966 /*
1967  * final_cost_nestloop
1968  * Final estimate of the cost and result size of a nestloop join path.
1969  *
1970  * 'path' is already filled in except for the rows and cost fields
1971  * 'workspace' is the result from initial_cost_nestloop
1972  * 'sjinfo' is extra info about the join for selectivity estimation
1973  * 'semifactors' contains valid data if path->jointype is SEMI or ANTI
1974  */
1975 void
1977  JoinCostWorkspace *workspace,
1978  SpecialJoinInfo *sjinfo,
1979  SemiAntiJoinFactors *semifactors)
1980 {
1981  Path *outer_path = path->outerjoinpath;
1982  Path *inner_path = path->innerjoinpath;
1983  double outer_path_rows = outer_path->rows;
1984  double inner_path_rows = inner_path->rows;
1985  Cost startup_cost = workspace->startup_cost;
1986  Cost run_cost = workspace->run_cost;
1987  Cost cpu_per_tuple;
1988  QualCost restrict_qual_cost;
1989  double ntuples;
1990 
1991  /* Protect some assumptions below that rowcounts aren't zero or NaN */
1992  if (outer_path_rows <= 0 || isnan(outer_path_rows))
1993  outer_path_rows = 1;
1994  if (inner_path_rows <= 0 || isnan(inner_path_rows))
1995  inner_path_rows = 1;
1996 
1997  /* Mark the path with the correct row estimate */
1998  if (path->path.param_info)
1999  path->path.rows = path->path.param_info->ppi_rows;
2000  else
2001  path->path.rows = path->path.parent->rows;
2002 
2003  /* For partial paths, scale row estimate. */
2004  if (path->path.parallel_workers > 0)
2005  path->path.rows /= get_parallel_divisor(&path->path);
2006 
2007  /*
2008  * We could include disable_cost in the preliminary estimate, but that
2009  * would amount to optimizing for the case where the join method is
2010  * disabled, which doesn't seem like the way to bet.
2011  */
2012  if (!enable_nestloop)
2013  startup_cost += disable_cost;
2014 
2015  /* cost of inner-relation source data (we already dealt with outer rel) */
2016 
2017  if (path->jointype == JOIN_SEMI || path->jointype == JOIN_ANTI)
2018  {
2019  /*
2020  * SEMI or ANTI join: executor will stop after first match.
2021  */
2022  Cost inner_run_cost = workspace->inner_run_cost;
2023  Cost inner_rescan_run_cost = workspace->inner_rescan_run_cost;
2024  double outer_matched_rows;
2025  Selectivity inner_scan_frac;
2026 
2027  /*
2028  * For an outer-rel row that has at least one match, we can expect the
2029  * inner scan to stop after a fraction 1/(match_count+1) of the inner
2030  * rows, if the matches are evenly distributed. Since they probably
2031  * aren't quite evenly distributed, we apply a fuzz factor of 2.0 to
2032  * that fraction. (If we used a larger fuzz factor, we'd have to
2033  * clamp inner_scan_frac to at most 1.0; but since match_count is at
2034  * least 1, no such clamp is needed now.)
2035  */
2036  outer_matched_rows = rint(outer_path_rows * semifactors->outer_match_frac);
2037  inner_scan_frac = 2.0 / (semifactors->match_count + 1.0);
2038 
2039  /*
2040  * Compute number of tuples processed (not number emitted!). First,
2041  * account for successfully-matched outer rows.
2042  */
2043  ntuples = outer_matched_rows * inner_path_rows * inner_scan_frac;
2044 
2045  /*
2046  * Now we need to estimate the actual costs of scanning the inner
2047  * relation, which may be quite a bit less than N times inner_run_cost
2048  * due to early scan stops. We consider two cases. If the inner path
2049  * is an indexscan using all the joinquals as indexquals, then an
2050  * unmatched outer row results in an indexscan returning no rows,
2051  * which is probably quite cheap. Otherwise, the executor will have
2052  * to scan the whole inner rel for an unmatched row; not so cheap.
2053  */
2054  if (has_indexed_join_quals(path))
2055  {
2056  /*
2057  * Successfully-matched outer rows will only require scanning
2058  * inner_scan_frac of the inner relation. In this case, we don't
2059  * need to charge the full inner_run_cost even when that's more
2060  * than inner_rescan_run_cost, because we can assume that none of
2061  * the inner scans ever scan the whole inner relation. So it's
2062  * okay to assume that all the inner scan executions can be
2063  * fractions of the full cost, even if materialization is reducing
2064  * the rescan cost. At this writing, it's impossible to get here
2065  * for a materialized inner scan, so inner_run_cost and
2066  * inner_rescan_run_cost will be the same anyway; but just in
2067  * case, use inner_run_cost for the first matched tuple and
2068  * inner_rescan_run_cost for additional ones.
2069  */
2070  run_cost += inner_run_cost * inner_scan_frac;
2071  if (outer_matched_rows > 1)
2072  run_cost += (outer_matched_rows - 1) * inner_rescan_run_cost * inner_scan_frac;
2073 
2074  /*
2075  * Add the cost of inner-scan executions for unmatched outer rows.
2076  * We estimate this as the same cost as returning the first tuple
2077  * of a nonempty scan. We consider that these are all rescans,
2078  * since we used inner_run_cost once already.
2079  */
2080  run_cost += (outer_path_rows - outer_matched_rows) *
2081  inner_rescan_run_cost / inner_path_rows;
2082 
2083  /*
2084  * We won't be evaluating any quals at all for unmatched rows, so
2085  * don't add them to ntuples.
2086  */
2087  }
2088  else
2089  {
2090  /*
2091  * Here, a complicating factor is that rescans may be cheaper than
2092  * first scans. If we never scan all the way to the end of the
2093  * inner rel, it might be (depending on the plan type) that we'd
2094  * never pay the whole inner first-scan run cost. However it is
2095  * difficult to estimate whether that will happen (and it could
2096  * not happen if there are any unmatched outer rows!), so be
2097  * conservative and always charge the whole first-scan cost once.
2098  */
2099  run_cost += inner_run_cost;
2100 
2101  /* Add inner run cost for additional outer tuples having matches */
2102  if (outer_matched_rows > 1)
2103  run_cost += (outer_matched_rows - 1) * inner_rescan_run_cost * inner_scan_frac;
2104 
2105  /* Add inner run cost for unmatched outer tuples */
2106  run_cost += (outer_path_rows - outer_matched_rows) *
2107  inner_rescan_run_cost;
2108 
2109  /* And count the unmatched join tuples as being processed */
2110  ntuples += (outer_path_rows - outer_matched_rows) *
2111  inner_path_rows;
2112  }
2113  }
2114  else
2115  {
2116  /* Normal-case source costs were included in preliminary estimate */
2117 
2118  /* Compute number of tuples processed (not number emitted!) */
2119  ntuples = outer_path_rows * inner_path_rows;
2120  }
2121 
2122  /* CPU costs */
2123  cost_qual_eval(&restrict_qual_cost, path->joinrestrictinfo, root);
2124  startup_cost += restrict_qual_cost.startup;
2125  cpu_per_tuple = cpu_tuple_cost + restrict_qual_cost.per_tuple;
2126  run_cost += cpu_per_tuple * ntuples;
2127 
2128  /* tlist eval costs are paid per output row, not per tuple scanned */
2129  startup_cost += path->path.pathtarget->cost.startup;
2130  run_cost += path->path.pathtarget->cost.per_tuple * path->path.rows;
2131 
2132  path->path.startup_cost = startup_cost;
2133  path->path.total_cost = startup_cost + run_cost;
2134 }
2135 
2136 /*
2137  * initial_cost_mergejoin
2138  * Preliminary estimate of the cost of a mergejoin path.
2139  *
2140  * This must quickly produce lower-bound estimates of the path's startup and
2141  * total costs. If we are unable to eliminate the proposed path from
2142  * consideration using the lower bounds, final_cost_mergejoin will be called
2143  * to obtain the final estimates.
2144  *
2145  * The exact division of labor between this function and final_cost_mergejoin
2146  * is private to them, and represents a tradeoff between speed of the initial
2147  * estimate and getting a tight lower bound. We choose to not examine the
2148  * join quals here, except for obtaining the scan selectivity estimate which
2149  * is really essential (but fortunately, use of caching keeps the cost of
2150  * getting that down to something reasonable).
2151  * We also assume that cost_sort is cheap enough to use here.
2152  *
2153  * 'workspace' is to be filled with startup_cost, total_cost, and perhaps
2154  * other data to be used by final_cost_mergejoin
2155  * 'jointype' is the type of join to be performed
2156  * 'mergeclauses' is the list of joinclauses to be used as merge clauses
2157  * 'outer_path' is the outer input to the join
2158  * 'inner_path' is the inner input to the join
2159  * 'outersortkeys' is the list of sort keys for the outer path
2160  * 'innersortkeys' is the list of sort keys for the inner path
2161  * 'sjinfo' is extra info about the join for selectivity estimation
2162  *
2163  * Note: outersortkeys and innersortkeys should be NIL if no explicit
2164  * sort is needed because the respective source path is already ordered.
2165  */
2166 void
2168  JoinType jointype,
2169  List *mergeclauses,
2170  Path *outer_path, Path *inner_path,
2171  List *outersortkeys, List *innersortkeys,
2172  SpecialJoinInfo *sjinfo)
2173 {
2174  Cost startup_cost = 0;
2175  Cost run_cost = 0;
2176  double outer_path_rows = outer_path->rows;
2177  double inner_path_rows = inner_path->rows;
2178  Cost inner_run_cost;
2179  double outer_rows,
2180  inner_rows,
2181  outer_skip_rows,
2182  inner_skip_rows;
2183  Selectivity outerstartsel,
2184  outerendsel,
2185  innerstartsel,
2186  innerendsel;
2187  Path sort_path; /* dummy for result of cost_sort */
2188 
2189  /* Protect some assumptions below that rowcounts aren't zero or NaN */
2190  if (outer_path_rows <= 0 || isnan(outer_path_rows))
2191  outer_path_rows = 1;
2192  if (inner_path_rows <= 0 || isnan(inner_path_rows))
2193  inner_path_rows = 1;
2194 
2195  /*
2196  * A merge join will stop as soon as it exhausts either input stream
2197  * (unless it's an outer join, in which case the outer side has to be
2198  * scanned all the way anyway). Estimate fraction of the left and right
2199  * inputs that will actually need to be scanned. Likewise, we can
2200  * estimate the number of rows that will be skipped before the first join
2201  * pair is found, which should be factored into startup cost. We use only
2202  * the first (most significant) merge clause for this purpose. Since
2203  * mergejoinscansel() is a fairly expensive computation, we cache the
2204  * results in the merge clause RestrictInfo.
2205  */
2206  if (mergeclauses && jointype != JOIN_FULL)
2207  {
2208  RestrictInfo *firstclause = (RestrictInfo *) linitial(mergeclauses);
2209  List *opathkeys;
2210  List *ipathkeys;
2211  PathKey *opathkey;
2212  PathKey *ipathkey;
2213  MergeScanSelCache *cache;
2214 
2215  /* Get the input pathkeys to determine the sort-order details */
2216  opathkeys = outersortkeys ? outersortkeys : outer_path->pathkeys;
2217  ipathkeys = innersortkeys ? innersortkeys : inner_path->pathkeys;
2218  Assert(opathkeys);
2219  Assert(ipathkeys);
2220  opathkey = (PathKey *) linitial(opathkeys);
2221  ipathkey = (PathKey *) linitial(ipathkeys);
2222  /* debugging check */
2223  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2224  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2225  opathkey->pk_strategy != ipathkey->pk_strategy ||
2226  opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2227  elog(ERROR, "left and right pathkeys do not match in mergejoin");
2228 
2229  /* Get the selectivity with caching */
2230  cache = cached_scansel(root, firstclause, opathkey);
2231 
2232  if (bms_is_subset(firstclause->left_relids,
2233  outer_path->parent->relids))
2234  {
2235  /* left side of clause is outer */
2236  outerstartsel = cache->leftstartsel;
2237  outerendsel = cache->leftendsel;
2238  innerstartsel = cache->rightstartsel;
2239  innerendsel = cache->rightendsel;
2240  }
2241  else
2242  {
2243  /* left side of clause is inner */
2244  outerstartsel = cache->rightstartsel;
2245  outerendsel = cache->rightendsel;
2246  innerstartsel = cache->leftstartsel;
2247  innerendsel = cache->leftendsel;
2248  }
2249  if (jointype == JOIN_LEFT ||
2250  jointype == JOIN_ANTI)
2251  {
2252  outerstartsel = 0.0;
2253  outerendsel = 1.0;
2254  }
2255  else if (jointype == JOIN_RIGHT)
2256  {
2257  innerstartsel = 0.0;
2258  innerendsel = 1.0;
2259  }
2260  }
2261  else
2262  {
2263  /* cope with clauseless or full mergejoin */
2264  outerstartsel = innerstartsel = 0.0;
2265  outerendsel = innerendsel = 1.0;
2266  }
2267 
2268  /*
2269  * Convert selectivities to row counts. We force outer_rows and
2270  * inner_rows to be at least 1, but the skip_rows estimates can be zero.
2271  */
2272  outer_skip_rows = rint(outer_path_rows * outerstartsel);
2273  inner_skip_rows = rint(inner_path_rows * innerstartsel);
2274  outer_rows = clamp_row_est(outer_path_rows * outerendsel);
2275  inner_rows = clamp_row_est(inner_path_rows * innerendsel);
2276 
2277  Assert(outer_skip_rows <= outer_rows);
2278  Assert(inner_skip_rows <= inner_rows);
2279 
2280  /*
2281  * Readjust scan selectivities to account for above rounding. This is
2282  * normally an insignificant effect, but when there are only a few rows in
2283  * the inputs, failing to do this makes for a large percentage error.
2284  */
2285  outerstartsel = outer_skip_rows / outer_path_rows;
2286  innerstartsel = inner_skip_rows / inner_path_rows;
2287  outerendsel = outer_rows / outer_path_rows;
2288  innerendsel = inner_rows / inner_path_rows;
2289 
2290  Assert(outerstartsel <= outerendsel);
2291  Assert(innerstartsel <= innerendsel);
2292 
2293  /* cost of source data */
2294 
2295  if (outersortkeys) /* do we need to sort outer? */
2296  {
2297  cost_sort(&sort_path,
2298  root,
2299  outersortkeys,
2300  outer_path->total_cost,
2301  outer_path_rows,
2302  outer_path->pathtarget->width,
2303  0.0,
2304  work_mem,
2305  -1.0);
2306  startup_cost += sort_path.startup_cost;
2307  startup_cost += (sort_path.total_cost - sort_path.startup_cost)
2308  * outerstartsel;
2309  run_cost += (sort_path.total_cost - sort_path.startup_cost)
2310  * (outerendsel - outerstartsel);
2311  }
2312  else
2313  {
2314  startup_cost += outer_path->startup_cost;
2315  startup_cost += (outer_path->total_cost - outer_path->startup_cost)
2316  * outerstartsel;
2317  run_cost += (outer_path->total_cost - outer_path->startup_cost)
2318  * (outerendsel - outerstartsel);
2319  }
2320 
2321  if (innersortkeys) /* do we need to sort inner? */
2322  {
2323  cost_sort(&sort_path,
2324  root,
2325  innersortkeys,
2326  inner_path->total_cost,
2327  inner_path_rows,
2328  inner_path->pathtarget->width,
2329  0.0,
2330  work_mem,
2331  -1.0);
2332  startup_cost += sort_path.startup_cost;
2333  startup_cost += (sort_path.total_cost - sort_path.startup_cost)
2334  * innerstartsel;
2335  inner_run_cost = (sort_path.total_cost - sort_path.startup_cost)
2336  * (innerendsel - innerstartsel);
2337  }
2338  else
2339  {
2340  startup_cost += inner_path->startup_cost;
2341  startup_cost += (inner_path->total_cost - inner_path->startup_cost)
2342  * innerstartsel;
2343  inner_run_cost = (inner_path->total_cost - inner_path->startup_cost)
2344  * (innerendsel - innerstartsel);
2345  }
2346 
2347  /*
2348  * We can't yet determine whether rescanning occurs, or whether
2349  * materialization of the inner input should be done. The minimum
2350  * possible inner input cost, regardless of rescan and materialization
2351  * considerations, is inner_run_cost. We include that in
2352  * workspace->total_cost, but not yet in run_cost.
2353  */
2354 
2355  /* CPU costs left for later */
2356 
2357  /* Public result fields */
2358  workspace->startup_cost = startup_cost;
2359  workspace->total_cost = startup_cost + run_cost + inner_run_cost;
2360  /* Save private data for final_cost_mergejoin */
2361  workspace->run_cost = run_cost;
2362  workspace->inner_run_cost = inner_run_cost;
2363  workspace->outer_rows = outer_rows;
2364  workspace->inner_rows = inner_rows;
2365  workspace->outer_skip_rows = outer_skip_rows;
2366  workspace->inner_skip_rows = inner_skip_rows;
2367 }
2368 
2369 /*
2370  * final_cost_mergejoin
2371  * Final estimate of the cost and result size of a mergejoin path.
2372  *
2373  * Unlike other costsize functions, this routine makes one actual decision:
2374  * whether we should materialize the inner path. We do that either because
2375  * the inner path can't support mark/restore, or because it's cheaper to
2376  * use an interposed Material node to handle mark/restore. When the decision
2377  * is cost-based it would be logically cleaner to build and cost two separate
2378  * paths with and without that flag set; but that would require repeating most
2379  * of the cost calculations, which are not all that cheap. Since the choice
2380  * will not affect output pathkeys or startup cost, only total cost, there is
2381  * no possibility of wanting to keep both paths. So it seems best to make
2382  * the decision here and record it in the path's materialize_inner field.
2383  *
2384  * 'path' is already filled in except for the rows and cost fields and
2385  * materialize_inner
2386  * 'workspace' is the result from initial_cost_mergejoin
2387  * 'sjinfo' is extra info about the join for selectivity estimation
2388  */
2389 void
2391  JoinCostWorkspace *workspace,
2392  SpecialJoinInfo *sjinfo)
2393 {
2394  Path *outer_path = path->jpath.outerjoinpath;
2395  Path *inner_path = path->jpath.innerjoinpath;
2396  double inner_path_rows = inner_path->rows;
2397  List *mergeclauses = path->path_mergeclauses;
2398  List *innersortkeys = path->innersortkeys;
2399  Cost startup_cost = workspace->startup_cost;
2400  Cost run_cost = workspace->run_cost;
2401  Cost inner_run_cost = workspace->inner_run_cost;
2402  double outer_rows = workspace->outer_rows;
2403  double inner_rows = workspace->inner_rows;
2404  double outer_skip_rows = workspace->outer_skip_rows;
2405  double inner_skip_rows = workspace->inner_skip_rows;
2406  Cost cpu_per_tuple,
2407  bare_inner_cost,
2408  mat_inner_cost;
2409  QualCost merge_qual_cost;
2410  QualCost qp_qual_cost;
2411  double mergejointuples,
2412  rescannedtuples;
2413  double rescanratio;
2414 
2415  /* Protect some assumptions below that rowcounts aren't zero or NaN */
2416  if (inner_path_rows <= 0 || isnan(inner_path_rows))
2417  inner_path_rows = 1;
2418 
2419  /* Mark the path with the correct row estimate */
2420  if (path->jpath.path.param_info)
2421  path->jpath.path.rows = path->jpath.path.param_info->ppi_rows;
2422  else
2423  path->jpath.path.rows = path->jpath.path.parent->rows;
2424 
2425  /* For partial paths, scale row estimate. */
2426  if (path->jpath.path.parallel_workers > 0)
2427  path->jpath.path.rows /= get_parallel_divisor(&path->jpath.path);
2428 
2429  /*
2430  * We could include disable_cost in the preliminary estimate, but that
2431  * would amount to optimizing for the case where the join method is
2432  * disabled, which doesn't seem like the way to bet.
2433  */
2434  if (!enable_mergejoin)
2435  startup_cost += disable_cost;
2436 
2437  /*
2438  * Compute cost of the mergequals and qpquals (other restriction clauses)
2439  * separately.
2440  */
2441  cost_qual_eval(&merge_qual_cost, mergeclauses, root);
2442  cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
2443  qp_qual_cost.startup -= merge_qual_cost.startup;
2444  qp_qual_cost.per_tuple -= merge_qual_cost.per_tuple;
2445 
2446  /*
2447  * Get approx # tuples passing the mergequals. We use approx_tuple_count
2448  * here because we need an estimate done with JOIN_INNER semantics.
2449  */
2450  mergejointuples = approx_tuple_count(root, &path->jpath, mergeclauses);
2451 
2452  /*
2453  * When there are equal merge keys in the outer relation, the mergejoin
2454  * must rescan any matching tuples in the inner relation. This means
2455  * re-fetching inner tuples; we have to estimate how often that happens.
2456  *
2457  * For regular inner and outer joins, the number of re-fetches can be
2458  * estimated approximately as size of merge join output minus size of
2459  * inner relation. Assume that the distinct key values are 1, 2, ..., and
2460  * denote the number of values of each key in the outer relation as m1,
2461  * m2, ...; in the inner relation, n1, n2, ... Then we have
2462  *
2463  * size of join = m1 * n1 + m2 * n2 + ...
2464  *
2465  * number of rescanned tuples = (m1 - 1) * n1 + (m2 - 1) * n2 + ... = m1 *
2466  * n1 + m2 * n2 + ... - (n1 + n2 + ...) = size of join - size of inner
2467  * relation
2468  *
2469  * This equation works correctly for outer tuples having no inner match
2470  * (nk = 0), but not for inner tuples having no outer match (mk = 0); we
2471  * are effectively subtracting those from the number of rescanned tuples,
2472  * when we should not. Can we do better without expensive selectivity
2473  * computations?
2474  *
2475  * The whole issue is moot if we are working from a unique-ified outer
2476  * input.
2477  */
2478  if (IsA(outer_path, UniquePath))
2479  rescannedtuples = 0;
2480  else
2481  {
2482  rescannedtuples = mergejointuples - inner_path_rows;
2483  /* Must clamp because of possible underestimate */
2484  if (rescannedtuples < 0)
2485  rescannedtuples = 0;
2486  }
2487  /* We'll inflate various costs this much to account for rescanning */
2488  rescanratio = 1.0 + (rescannedtuples / inner_path_rows);
2489 
2490  /*
2491  * Decide whether we want to materialize the inner input to shield it from
2492  * mark/restore and performing re-fetches. Our cost model for regular
2493  * re-fetches is that a re-fetch costs the same as an original fetch,
2494  * which is probably an overestimate; but on the other hand we ignore the
2495  * bookkeeping costs of mark/restore. Not clear if it's worth developing
2496  * a more refined model. So we just need to inflate the inner run cost by
2497  * rescanratio.
2498  */
2499  bare_inner_cost = inner_run_cost * rescanratio;
2500 
2501  /*
2502  * When we interpose a Material node the re-fetch cost is assumed to be
2503  * just cpu_operator_cost per tuple, independently of the underlying
2504  * plan's cost; and we charge an extra cpu_operator_cost per original
2505  * fetch as well. Note that we're assuming the materialize node will
2506  * never spill to disk, since it only has to remember tuples back to the
2507  * last mark. (If there are a huge number of duplicates, our other cost
2508  * factors will make the path so expensive that it probably won't get
2509  * chosen anyway.) So we don't use cost_rescan here.
2510  *
2511  * Note: keep this estimate in sync with create_mergejoin_plan's labeling
2512  * of the generated Material node.
2513  */
2514  mat_inner_cost = inner_run_cost +
2515  cpu_operator_cost * inner_path_rows * rescanratio;
2516 
2517  /*
2518  * Prefer materializing if it looks cheaper, unless the user has asked to
2519  * suppress materialization.
2520  */
2521  if (enable_material && mat_inner_cost < bare_inner_cost)
2522  path->materialize_inner = true;
2523 
2524  /*
2525  * Even if materializing doesn't look cheaper, we *must* do it if the
2526  * inner path is to be used directly (without sorting) and it doesn't
2527  * support mark/restore.
2528  *
2529  * Since the inner side must be ordered, and only Sorts and IndexScans can
2530  * create order to begin with, and they both support mark/restore, you
2531  * might think there's no problem --- but you'd be wrong. Nestloop and
2532  * merge joins can *preserve* the order of their inputs, so they can be
2533  * selected as the input of a mergejoin, and they don't support
2534  * mark/restore at present.
2535  *
2536  * We don't test the value of enable_material here, because
2537  * materialization is required for correctness in this case, and turning
2538  * it off does not entitle us to deliver an invalid plan.
2539  */
2540  else if (innersortkeys == NIL &&
2541  !ExecSupportsMarkRestore(inner_path))
2542  path->materialize_inner = true;
2543 
2544  /*
2545  * Also, force materializing if the inner path is to be sorted and the
2546  * sort is expected to spill to disk. This is because the final merge
2547  * pass can be done on-the-fly if it doesn't have to support mark/restore.
2548  * We don't try to adjust the cost estimates for this consideration,
2549  * though.
2550  *
2551  * Since materialization is a performance optimization in this case,
2552  * rather than necessary for correctness, we skip it if enable_material is
2553  * off.
2554  */
2555  else if (enable_material && innersortkeys != NIL &&
2556  relation_byte_size(inner_path_rows,
2557  inner_path->pathtarget->width) >
2558  (work_mem * 1024L))
2559  path->materialize_inner = true;
2560  else
2561  path->materialize_inner = false;
2562 
2563  /* Charge the right incremental cost for the chosen case */
2564  if (path->materialize_inner)
2565  run_cost += mat_inner_cost;
2566  else
2567  run_cost += bare_inner_cost;
2568 
2569  /* CPU costs */
2570 
2571  /*
2572  * The number of tuple comparisons needed is approximately number of outer
2573  * rows plus number of inner rows plus number of rescanned tuples (can we
2574  * refine this?). At each one, we need to evaluate the mergejoin quals.
2575  */
2576  startup_cost += merge_qual_cost.startup;
2577  startup_cost += merge_qual_cost.per_tuple *
2578  (outer_skip_rows + inner_skip_rows * rescanratio);
2579  run_cost += merge_qual_cost.per_tuple *
2580  ((outer_rows - outer_skip_rows) +
2581  (inner_rows - inner_skip_rows) * rescanratio);
2582 
2583  /*
2584  * For each tuple that gets through the mergejoin proper, we charge
2585  * cpu_tuple_cost plus the cost of evaluating additional restriction
2586  * clauses that are to be applied at the join. (This is pessimistic since
2587  * not all of the quals may get evaluated at each tuple.)
2588  *
2589  * Note: we could adjust for SEMI/ANTI joins skipping some qual
2590  * evaluations here, but it's probably not worth the trouble.
2591  */
2592  startup_cost += qp_qual_cost.startup;
2593  cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
2594  run_cost += cpu_per_tuple * mergejointuples;
2595 
2596  /* tlist eval costs are paid per output row, not per tuple scanned */
2597  startup_cost += path->jpath.path.pathtarget->cost.startup;
2598  run_cost += path->jpath.path.pathtarget->cost.per_tuple * path->jpath.path.rows;
2599 
2600  path->jpath.path.startup_cost = startup_cost;
2601  path->jpath.path.total_cost = startup_cost + run_cost;
2602 }
2603 
2604 /*
2605  * run mergejoinscansel() with caching
2606  */
2607 static MergeScanSelCache *
2609 {
2610  MergeScanSelCache *cache;
2611  ListCell *lc;
2612  Selectivity leftstartsel,
2613  leftendsel,
2614  rightstartsel,
2615  rightendsel;
2616  MemoryContext oldcontext;
2617 
2618  /* Do we have this result already? */
2619  foreach(lc, rinfo->scansel_cache)
2620  {
2621  cache = (MergeScanSelCache *) lfirst(lc);
2622  if (cache->opfamily == pathkey->pk_opfamily &&
2623  cache->collation == pathkey->pk_eclass->ec_collation &&
2624  cache->strategy == pathkey->pk_strategy &&
2625  cache->nulls_first == pathkey->pk_nulls_first)
2626  return cache;
2627  }
2628 
2629  /* Nope, do the computation */
2630  mergejoinscansel(root,
2631  (Node *) rinfo->clause,
2632  pathkey->pk_opfamily,
2633  pathkey->pk_strategy,
2634  pathkey->pk_nulls_first,
2635  &leftstartsel,
2636  &leftendsel,
2637  &rightstartsel,
2638  &rightendsel);
2639 
2640  /* Cache the result in suitably long-lived workspace */
2641  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2642 
2643  cache = (MergeScanSelCache *) palloc(sizeof(MergeScanSelCache));
2644  cache->opfamily = pathkey->pk_opfamily;
2645  cache->collation = pathkey->pk_eclass->ec_collation;
2646  cache->strategy = pathkey->pk_strategy;
2647  cache->nulls_first = pathkey->pk_nulls_first;
2648  cache->leftstartsel = leftstartsel;
2649  cache->leftendsel = leftendsel;
2650  cache->rightstartsel = rightstartsel;
2651  cache->rightendsel = rightendsel;
2652 
2653  rinfo->scansel_cache = lappend(rinfo->scansel_cache, cache);
2654 
2655  MemoryContextSwitchTo(oldcontext);
2656 
2657  return cache;
2658 }
2659 
2660 /*
2661  * initial_cost_hashjoin
2662  * Preliminary estimate of the cost of a hashjoin path.
2663  *
2664  * This must quickly produce lower-bound estimates of the path's startup and
2665  * total costs. If we are unable to eliminate the proposed path from
2666  * consideration using the lower bounds, final_cost_hashjoin will be called
2667  * to obtain the final estimates.
2668  *
2669  * The exact division of labor between this function and final_cost_hashjoin
2670  * is private to them, and represents a tradeoff between speed of the initial
2671  * estimate and getting a tight lower bound. We choose to not examine the
2672  * join quals here (other than by counting the number of hash clauses),
2673  * so we can't do much with CPU costs. We do assume that
2674  * ExecChooseHashTableSize is cheap enough to use here.
2675  *
2676  * 'workspace' is to be filled with startup_cost, total_cost, and perhaps
2677  * other data to be used by final_cost_hashjoin
2678  * 'jointype' is the type of join to be performed
2679  * 'hashclauses' is the list of joinclauses to be used as hash clauses
2680  * 'outer_path' is the outer input to the join
2681  * 'inner_path' is the inner input to the join
2682  * 'sjinfo' is extra info about the join for selectivity estimation
2683  * 'semifactors' contains valid data if jointype is SEMI or ANTI
2684  */
2685 void
2687  JoinType jointype,
2688  List *hashclauses,
2689  Path *outer_path, Path *inner_path,
2690  SpecialJoinInfo *sjinfo,
2691  SemiAntiJoinFactors *semifactors)
2692 {
2693  Cost startup_cost = 0;
2694  Cost run_cost = 0;
2695  double outer_path_rows = outer_path->rows;
2696  double inner_path_rows = inner_path->rows;
2697  int num_hashclauses = list_length(hashclauses);
2698  int numbuckets;
2699  int numbatches;
2700  int num_skew_mcvs;
2701 
2702  /* cost of source data */
2703  startup_cost += outer_path->startup_cost;
2704  run_cost += outer_path->total_cost - outer_path->startup_cost;
2705  startup_cost += inner_path->total_cost;
2706 
2707  /*
2708  * Cost of computing hash function: must do it once per input tuple. We
2709  * charge one cpu_operator_cost for each column's hash function. Also,
2710  * tack on one cpu_tuple_cost per inner row, to model the costs of
2711  * inserting the row into the hashtable.
2712  *
2713  * XXX when a hashclause is more complex than a single operator, we really
2714  * should charge the extra eval costs of the left or right side, as
2715  * appropriate, here. This seems more work than it's worth at the moment.
2716  */
2717  startup_cost += (cpu_operator_cost * num_hashclauses + cpu_tuple_cost)
2718  * inner_path_rows;
2719  run_cost += cpu_operator_cost * num_hashclauses * outer_path_rows;
2720 
2721  /*
2722  * Get hash table size that executor would use for inner relation.
2723  *
2724  * XXX for the moment, always assume that skew optimization will be
2725  * performed. As long as SKEW_WORK_MEM_PERCENT is small, it's not worth
2726  * trying to determine that for sure.
2727  *
2728  * XXX at some point it might be interesting to try to account for skew
2729  * optimization in the cost estimate, but for now, we don't.
2730  */
2731  ExecChooseHashTableSize(inner_path_rows,
2732  inner_path->pathtarget->width,
2733  true, /* useskew */
2734  &numbuckets,
2735  &numbatches,
2736  &num_skew_mcvs);
2737 
2738  /*
2739  * If inner relation is too big then we will need to "batch" the join,
2740  * which implies writing and reading most of the tuples to disk an extra
2741  * time. Charge seq_page_cost per page, since the I/O should be nice and
2742  * sequential. Writing the inner rel counts as startup cost, all the rest
2743  * as run cost.
2744  */
2745  if (numbatches > 1)
2746  {
2747  double outerpages = page_size(outer_path_rows,
2748  outer_path->pathtarget->width);
2749  double innerpages = page_size(inner_path_rows,
2750  inner_path->pathtarget->width);
2751 
2752  startup_cost += seq_page_cost * innerpages;
2753  run_cost += seq_page_cost * (innerpages + 2 * outerpages);
2754  }
2755 
2756  /* CPU costs left for later */
2757 
2758  /* Public result fields */
2759  workspace->startup_cost = startup_cost;
2760  workspace->total_cost = startup_cost + run_cost;
2761  /* Save private data for final_cost_hashjoin */
2762  workspace->run_cost = run_cost;
2763  workspace->numbuckets = numbuckets;
2764  workspace->numbatches = numbatches;
2765 }
2766 
2767 /*
2768  * final_cost_hashjoin
2769  * Final estimate of the cost and result size of a hashjoin path.
2770  *
2771  * Note: the numbatches estimate is also saved into 'path' for use later
2772  *
2773  * 'path' is already filled in except for the rows and cost fields and
2774  * num_batches
2775  * 'workspace' is the result from initial_cost_hashjoin
2776  * 'sjinfo' is extra info about the join for selectivity estimation
2777  * 'semifactors' contains valid data if path->jointype is SEMI or ANTI
2778  */
2779 void
2781  JoinCostWorkspace *workspace,
2782  SpecialJoinInfo *sjinfo,
2783  SemiAntiJoinFactors *semifactors)
2784 {
2785  Path *outer_path = path->jpath.outerjoinpath;
2786  Path *inner_path = path->jpath.innerjoinpath;
2787  double outer_path_rows = outer_path->rows;
2788  double inner_path_rows = inner_path->rows;
2789  List *hashclauses = path->path_hashclauses;
2790  Cost startup_cost = workspace->startup_cost;
2791  Cost run_cost = workspace->run_cost;
2792  int numbuckets = workspace->numbuckets;
2793  int numbatches = workspace->numbatches;
2794  Cost cpu_per_tuple;
2795  QualCost hash_qual_cost;
2796  QualCost qp_qual_cost;
2797  double hashjointuples;
2798  double virtualbuckets;
2799  Selectivity innerbucketsize;
2800  ListCell *hcl;
2801 
2802  /* Mark the path with the correct row estimate */
2803  if (path->jpath.path.param_info)
2804  path->jpath.path.rows = path->jpath.path.param_info->ppi_rows;
2805  else
2806  path->jpath.path.rows = path->jpath.path.parent->rows;
2807 
2808  /* For partial paths, scale row estimate. */
2809  if (path->jpath.path.parallel_workers > 0)
2810  path->jpath.path.rows /= get_parallel_divisor(&path->jpath.path);
2811 
2812  /*
2813  * We could include disable_cost in the preliminary estimate, but that
2814  * would amount to optimizing for the case where the join method is
2815  * disabled, which doesn't seem like the way to bet.
2816  */
2817  if (!enable_hashjoin)
2818  startup_cost += disable_cost;
2819 
2820  /* mark the path with estimated # of batches */
2821  path->num_batches = numbatches;
2822 
2823  /* and compute the number of "virtual" buckets in the whole join */
2824  virtualbuckets = (double) numbuckets *(double) numbatches;
2825 
2826  /*
2827  * Determine bucketsize fraction for inner relation. We use the smallest
2828  * bucketsize estimated for any individual hashclause; this is undoubtedly
2829  * conservative.
2830  *
2831  * BUT: if inner relation has been unique-ified, we can assume it's good
2832  * for hashing. This is important both because it's the right answer, and
2833  * because we avoid contaminating the cache with a value that's wrong for
2834  * non-unique-ified paths.
2835  */
2836  if (IsA(inner_path, UniquePath))
2837  innerbucketsize = 1.0 / virtualbuckets;
2838  else
2839  {
2840  innerbucketsize = 1.0;
2841  foreach(hcl, hashclauses)
2842  {
2843  RestrictInfo *restrictinfo = castNode(RestrictInfo, lfirst(hcl));
2844  Selectivity thisbucketsize;
2845 
2846  /*
2847  * First we have to figure out which side of the hashjoin clause
2848  * is the inner side.
2849  *
2850  * Since we tend to visit the same clauses over and over when
2851  * planning a large query, we cache the bucketsize estimate in the
2852  * RestrictInfo node to avoid repeated lookups of statistics.
2853  */
2854  if (bms_is_subset(restrictinfo->right_relids,
2855  inner_path->parent->relids))
2856  {
2857  /* righthand side is inner */
2858  thisbucketsize = restrictinfo->right_bucketsize;
2859  if (thisbucketsize < 0)
2860  {
2861  /* not cached yet */
2862  thisbucketsize =
2864  get_rightop(restrictinfo->clause),
2865  virtualbuckets);
2866  restrictinfo->right_bucketsize = thisbucketsize;
2867  }
2868  }
2869  else
2870  {
2871  Assert(bms_is_subset(restrictinfo->left_relids,
2872  inner_path->parent->relids));
2873  /* lefthand side is inner */
2874  thisbucketsize = restrictinfo->left_bucketsize;
2875  if (thisbucketsize < 0)
2876  {
2877  /* not cached yet */
2878  thisbucketsize =
2880  get_leftop(restrictinfo->clause),
2881  virtualbuckets);
2882  restrictinfo->left_bucketsize = thisbucketsize;
2883  }
2884  }
2885 
2886  if (innerbucketsize > thisbucketsize)
2887  innerbucketsize = thisbucketsize;
2888  }
2889  }
2890 
2891  /*
2892  * Compute cost of the hashquals and qpquals (other restriction clauses)
2893  * separately.
2894  */
2895  cost_qual_eval(&hash_qual_cost, hashclauses, root);
2896  cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
2897  qp_qual_cost.startup -= hash_qual_cost.startup;
2898  qp_qual_cost.per_tuple -= hash_qual_cost.per_tuple;
2899 
2900  /* CPU costs */
2901 
2902  if (path->jpath.jointype == JOIN_SEMI || path->jpath.jointype == JOIN_ANTI)
2903  {
2904  double outer_matched_rows;
2905  Selectivity inner_scan_frac;
2906 
2907  /*
2908  * SEMI or ANTI join: executor will stop after first match.
2909  *
2910  * For an outer-rel row that has at least one match, we can expect the
2911  * bucket scan to stop after a fraction 1/(match_count+1) of the
2912  * bucket's rows, if the matches are evenly distributed. Since they
2913  * probably aren't quite evenly distributed, we apply a fuzz factor of
2914  * 2.0 to that fraction. (If we used a larger fuzz factor, we'd have
2915  * to clamp inner_scan_frac to at most 1.0; but since match_count is
2916  * at least 1, no such clamp is needed now.)
2917  */
2918  outer_matched_rows = rint(outer_path_rows * semifactors->outer_match_frac);
2919  inner_scan_frac = 2.0 / (semifactors->match_count + 1.0);
2920 
2921  startup_cost += hash_qual_cost.startup;
2922  run_cost += hash_qual_cost.per_tuple * outer_matched_rows *
2923  clamp_row_est(inner_path_rows * innerbucketsize * inner_scan_frac) * 0.5;
2924 
2925  /*
2926  * For unmatched outer-rel rows, the picture is quite a lot different.
2927  * In the first place, there is no reason to assume that these rows
2928  * preferentially hit heavily-populated buckets; instead assume they
2929  * are uncorrelated with the inner distribution and so they see an
2930  * average bucket size of inner_path_rows / virtualbuckets. In the
2931  * second place, it seems likely that they will have few if any exact
2932  * hash-code matches and so very few of the tuples in the bucket will
2933  * actually require eval of the hash quals. We don't have any good
2934  * way to estimate how many will, but for the moment assume that the
2935  * effective cost per bucket entry is one-tenth what it is for
2936  * matchable tuples.
2937  */
2938  run_cost += hash_qual_cost.per_tuple *
2939  (outer_path_rows - outer_matched_rows) *
2940  clamp_row_est(inner_path_rows / virtualbuckets) * 0.05;
2941 
2942  /* Get # of tuples that will pass the basic join */
2943  if (path->jpath.jointype == JOIN_SEMI)
2944  hashjointuples = outer_matched_rows;
2945  else
2946  hashjointuples = outer_path_rows - outer_matched_rows;
2947  }
2948  else
2949  {
2950  /*
2951  * The number of tuple comparisons needed is the number of outer
2952  * tuples times the typical number of tuples in a hash bucket, which
2953  * is the inner relation size times its bucketsize fraction. At each
2954  * one, we need to evaluate the hashjoin quals. But actually,
2955  * charging the full qual eval cost at each tuple is pessimistic,
2956  * since we don't evaluate the quals unless the hash values match
2957  * exactly. For lack of a better idea, halve the cost estimate to
2958  * allow for that.
2959  */
2960  startup_cost += hash_qual_cost.startup;
2961  run_cost += hash_qual_cost.per_tuple * outer_path_rows *
2962  clamp_row_est(inner_path_rows * innerbucketsize) * 0.5;
2963 
2964  /*
2965  * Get approx # tuples passing the hashquals. We use
2966  * approx_tuple_count here because we need an estimate done with
2967  * JOIN_INNER semantics.
2968  */
2969  hashjointuples = approx_tuple_count(root, &path->jpath, hashclauses);
2970  }
2971 
2972  /*
2973  * For each tuple that gets through the hashjoin proper, we charge
2974  * cpu_tuple_cost plus the cost of evaluating additional restriction
2975  * clauses that are to be applied at the join. (This is pessimistic since
2976  * not all of the quals may get evaluated at each tuple.)
2977  */
2978  startup_cost += qp_qual_cost.startup;
2979  cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
2980  run_cost += cpu_per_tuple * hashjointuples;
2981 
2982  /* tlist eval costs are paid per output row, not per tuple scanned */
2983  startup_cost += path->jpath.path.pathtarget->cost.startup;
2984  run_cost += path->jpath.path.pathtarget->cost.per_tuple * path->jpath.path.rows;
2985 
2986  path->jpath.path.startup_cost = startup_cost;
2987  path->jpath.path.total_cost = startup_cost + run_cost;
2988 }
2989 
2990 
2991 /*
2992  * cost_subplan
2993  * Figure the costs for a SubPlan (or initplan).
2994  *
2995  * Note: we could dig the subplan's Plan out of the root list, but in practice
2996  * all callers have it handy already, so we make them pass it.
2997  */
2998 void
2999 cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
3000 {
3001  QualCost sp_cost;
3002 
3003  /* Figure any cost for evaluating the testexpr */
3004  cost_qual_eval(&sp_cost,
3005  make_ands_implicit((Expr *) subplan->testexpr),
3006  root);
3007 
3008  if (subplan->useHashTable)
3009  {
3010  /*
3011  * If we are using a hash table for the subquery outputs, then the
3012  * cost of evaluating the query is a one-time cost. We charge one
3013  * cpu_operator_cost per tuple for the work of loading the hashtable,
3014  * too.
3015  */
3016  sp_cost.startup += plan->total_cost +
3017  cpu_operator_cost * plan->plan_rows;
3018 
3019  /*
3020  * The per-tuple costs include the cost of evaluating the lefthand
3021  * expressions, plus the cost of probing the hashtable. We already
3022  * accounted for the lefthand expressions as part of the testexpr, and
3023  * will also have counted one cpu_operator_cost for each comparison
3024  * operator. That is probably too low for the probing cost, but it's
3025  * hard to make a better estimate, so live with it for now.
3026  */
3027  }
3028  else
3029  {
3030  /*
3031  * Otherwise we will be rescanning the subplan output on each
3032  * evaluation. We need to estimate how much of the output we will
3033  * actually need to scan. NOTE: this logic should agree with the
3034  * tuple_fraction estimates used by make_subplan() in
3035  * plan/subselect.c.
3036  */
3037  Cost plan_run_cost = plan->total_cost - plan->startup_cost;
3038 
3039  if (subplan->subLinkType == EXISTS_SUBLINK)
3040  {
3041  /* we only need to fetch 1 tuple; clamp to avoid zero divide */
3042  sp_cost.per_tuple += plan_run_cost / clamp_row_est(plan->plan_rows);
3043  }
3044  else if (subplan->subLinkType == ALL_SUBLINK ||
3045  subplan->subLinkType == ANY_SUBLINK)
3046  {
3047  /* assume we need 50% of the tuples */
3048  sp_cost.per_tuple += 0.50 * plan_run_cost;
3049  /* also charge a cpu_operator_cost per row examined */
3050  sp_cost.per_tuple += 0.50 * plan->plan_rows * cpu_operator_cost;
3051  }
3052  else
3053  {
3054  /* assume we need all tuples */
3055  sp_cost.per_tuple += plan_run_cost;
3056  }
3057 
3058  /*
3059  * Also account for subplan's startup cost. If the subplan is
3060  * uncorrelated or undirect correlated, AND its topmost node is one
3061  * that materializes its output, assume that we'll only need to pay
3062  * its startup cost once; otherwise assume we pay the startup cost
3063  * every time.
3064  */
3065  if (subplan->parParam == NIL &&
3067  sp_cost.startup += plan->startup_cost;
3068  else
3069  sp_cost.per_tuple += plan->startup_cost;
3070  }
3071 
3072  subplan->startup_cost = sp_cost.startup;
3073  subplan->per_call_cost = sp_cost.per_tuple;
3074 }
3075 
3076 
3077 /*
3078  * cost_rescan
3079  * Given a finished Path, estimate the costs of rescanning it after
3080  * having done so the first time. For some Path types a rescan is
3081  * cheaper than an original scan (if no parameters change), and this
3082  * function embodies knowledge about that. The default is to return
3083  * the same costs stored in the Path. (Note that the cost estimates
3084  * actually stored in Paths are always for first scans.)
3085  *
3086  * This function is not currently intended to model effects such as rescans
3087  * being cheaper due to disk block caching; what we are concerned with is
3088  * plan types wherein the executor caches results explicitly, or doesn't
3089  * redo startup calculations, etc.
3090  */
3091 static void
3093  Cost *rescan_startup_cost, /* output parameters */
3094  Cost *rescan_total_cost)
3095 {
3096  switch (path->pathtype)
3097  {
3098  case T_FunctionScan:
3099 
3100  /*
3101  * Currently, nodeFunctionscan.c always executes the function to
3102  * completion before returning any rows, and caches the results in
3103  * a tuplestore. So the function eval cost is all startup cost
3104  * and isn't paid over again on rescans. However, all run costs
3105  * will be paid over again.
3106  */
3107  *rescan_startup_cost = 0;
3108  *rescan_total_cost = path->total_cost - path->startup_cost;
3109  break;
3110  case T_HashJoin:
3111 
3112  /*
3113  * If it's a single-batch join, we don't need to rebuild the hash
3114  * table during a rescan.
3115  */
3116  if (((HashPath *) path)->num_batches == 1)
3117  {
3118  /* Startup cost is exactly the cost of hash table building */
3119  *rescan_startup_cost = 0;
3120  *rescan_total_cost = path->total_cost - path->startup_cost;
3121  }
3122  else
3123  {
3124  /* Otherwise, no special treatment */
3125  *rescan_startup_cost = path->startup_cost;
3126  *rescan_total_cost = path->total_cost;
3127  }
3128  break;
3129  case T_CteScan:
3130  case T_WorkTableScan:
3131  {
3132  /*
3133  * These plan types materialize their final result in a
3134  * tuplestore or tuplesort object. So the rescan cost is only
3135  * cpu_tuple_cost per tuple, unless the result is large enough
3136  * to spill to disk.
3137  */
3138  Cost run_cost = cpu_tuple_cost * path->rows;
3139  double nbytes = relation_byte_size(path->rows,
3140  path->pathtarget->width);
3141  long work_mem_bytes = work_mem * 1024L;
3142 
3143  if (nbytes > work_mem_bytes)
3144  {
3145  /* It will spill, so account for re-read cost */
3146  double npages = ceil(nbytes / BLCKSZ);
3147 
3148  run_cost += seq_page_cost * npages;
3149  }
3150  *rescan_startup_cost = 0;
3151  *rescan_total_cost = run_cost;
3152  }
3153  break;
3154  case T_Material:
3155  case T_Sort:
3156  {
3157  /*
3158  * These plan types not only materialize their results, but do
3159  * not implement qual filtering or projection. So they are
3160  * even cheaper to rescan than the ones above. We charge only
3161  * cpu_operator_cost per tuple. (Note: keep that in sync with
3162  * the run_cost charge in cost_sort, and also see comments in
3163  * cost_material before you change it.)
3164  */
3165  Cost run_cost = cpu_operator_cost * path->rows;
3166  double nbytes = relation_byte_size(path->rows,
3167  path->pathtarget->width);
3168  long work_mem_bytes = work_mem * 1024L;
3169 
3170  if (nbytes > work_mem_bytes)
3171  {
3172  /* It will spill, so account for re-read cost */
3173  double npages = ceil(nbytes / BLCKSZ);
3174 
3175  run_cost += seq_page_cost * npages;
3176  }
3177  *rescan_startup_cost = 0;
3178  *rescan_total_cost = run_cost;
3179  }
3180  break;
3181  default:
3182  *rescan_startup_cost = path->startup_cost;
3183  *rescan_total_cost = path->total_cost;
3184  break;
3185  }
3186 }
3187 
3188 
3189 /*
3190  * cost_qual_eval
3191  * Estimate the CPU costs of evaluating a WHERE clause.
3192  * The input can be either an implicitly-ANDed list of boolean
3193  * expressions, or a list of RestrictInfo nodes. (The latter is
3194  * preferred since it allows caching of the results.)
3195  * The result includes both a one-time (startup) component,
3196  * and a per-evaluation component.
3197  */
3198 void
3200 {
3201  cost_qual_eval_context context;
3202  ListCell *l;
3203 
3204  context.root = root;
3205  context.total.startup = 0;
3206  context.total.per_tuple = 0;
3207 
3208  /* We don't charge any cost for the implicit ANDing at top level ... */
3209 
3210  foreach(l, quals)
3211  {
3212  Node *qual = (Node *) lfirst(l);
3213 
3214  cost_qual_eval_walker(qual, &context);
3215  }
3216 
3217  *cost = context.total;
3218 }
3219 
3220 /*
3221  * cost_qual_eval_node
3222  * As above, for a single RestrictInfo or expression.
3223  */
3224 void
3226 {
3227  cost_qual_eval_context context;
3228 
3229  context.root = root;
3230  context.total.startup = 0;
3231  context.total.per_tuple = 0;
3232 
3233  cost_qual_eval_walker(qual, &context);
3234 
3235  *cost = context.total;
3236 }
3237 
3238 static bool
3240 {
3241  if (node == NULL)
3242  return false;
3243 
3244  /*
3245  * RestrictInfo nodes contain an eval_cost field reserved for this
3246  * routine's use, so that it's not necessary to evaluate the qual clause's
3247  * cost more than once. If the clause's cost hasn't been computed yet,
3248  * the field's startup value will contain -1.
3249  */
3250  if (IsA(node, RestrictInfo))
3251  {
3252  RestrictInfo *rinfo = (RestrictInfo *) node;
3253 
3254  if (rinfo->eval_cost.startup < 0)
3255  {
3256  cost_qual_eval_context locContext;
3257 
3258  locContext.root = context->root;
3259  locContext.total.startup = 0;
3260  locContext.total.per_tuple = 0;
3261 
3262  /*
3263  * For an OR clause, recurse into the marked-up tree so that we
3264  * set the eval_cost for contained RestrictInfos too.
3265  */
3266  if (rinfo->orclause)
3267  cost_qual_eval_walker((Node *) rinfo->orclause, &locContext);
3268  else
3269  cost_qual_eval_walker((Node *) rinfo->clause, &locContext);
3270 
3271  /*
3272  * If the RestrictInfo is marked pseudoconstant, it will be tested
3273  * only once, so treat its cost as all startup cost.
3274  */
3275  if (rinfo->pseudoconstant)
3276  {
3277  /* count one execution during startup */
3278  locContext.total.startup += locContext.total.per_tuple;
3279  locContext.total.per_tuple = 0;
3280  }
3281  rinfo->eval_cost = locContext.total;
3282  }
3283  context->total.startup += rinfo->eval_cost.startup;
3284  context->total.per_tuple += rinfo->eval_cost.per_tuple;
3285  /* do NOT recurse into children */
3286  return false;
3287  }
3288 
3289  /*
3290  * For each operator or function node in the given tree, we charge the
3291  * estimated execution cost given by pg_proc.procost (remember to multiply
3292  * this by cpu_operator_cost).
3293  *
3294  * Vars and Consts are charged zero, and so are boolean operators (AND,
3295  * OR, NOT). Simplistic, but a lot better than no model at all.
3296  *
3297  * Should we try to account for the possibility of short-circuit
3298  * evaluation of AND/OR? Probably *not*, because that would make the
3299  * results depend on the clause ordering, and we are not in any position
3300  * to expect that the current ordering of the clauses is the one that's
3301  * going to end up being used. The above per-RestrictInfo caching would
3302  * not mix well with trying to re-order clauses anyway.
3303  *
3304  * Another issue that is entirely ignored here is that if a set-returning
3305  * function is below top level in the tree, the functions/operators above
3306  * it will need to be evaluated multiple times. In practical use, such
3307  * cases arise so seldom as to not be worth the added complexity needed;
3308  * moreover, since our rowcount estimates for functions tend to be pretty
3309  * phony, the results would also be pretty phony.
3310  */
3311  if (IsA(node, FuncExpr))
3312  {
3313  context->total.per_tuple +=
3314  get_func_cost(((FuncExpr *) node)->funcid) * cpu_operator_cost;
3315  }
3316  else if (IsA(node, OpExpr) ||
3317  IsA(node, DistinctExpr) ||
3318  IsA(node, NullIfExpr))
3319  {
3320  /* rely on struct equivalence to treat these all alike */
3321  set_opfuncid((OpExpr *) node);
3322  context->total.per_tuple +=
3323  get_func_cost(((OpExpr *) node)->opfuncid) * cpu_operator_cost;
3324  }
3325  else if (IsA(node, ScalarArrayOpExpr))
3326  {
3327  /*
3328  * Estimate that the operator will be applied to about half of the
3329  * array elements before the answer is determined.
3330  */
3331  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
3332  Node *arraynode = (Node *) lsecond(saop->args);
3333 
3334  set_sa_opfuncid(saop);
3335  context->total.per_tuple += get_func_cost(saop->opfuncid) *
3336  cpu_operator_cost * estimate_array_length(arraynode) * 0.5;
3337  }
3338  else if (IsA(node, Aggref) ||
3339  IsA(node, WindowFunc))
3340  {
3341  /*
3342  * Aggref and WindowFunc nodes are (and should be) treated like Vars,
3343  * ie, zero execution cost in the current model, because they behave
3344  * essentially like Vars in execQual.c. We disregard the costs of
3345  * their input expressions for the same reason. The actual execution
3346  * costs of the aggregate/window functions and their arguments have to
3347  * be factored into plan-node-specific costing of the Agg or WindowAgg
3348  * plan node.
3349  */
3350  return false; /* don't recurse into children */
3351  }
3352  else if (IsA(node, CoerceViaIO))
3353  {
3354  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3355  Oid iofunc;
3356  Oid typioparam;
3357  bool typisvarlena;
3358 
3359  /* check the result type's input function */
3360  getTypeInputInfo(iocoerce->resulttype,
3361  &iofunc, &typioparam);
3362  context->total.per_tuple += get_func_cost(iofunc) * cpu_operator_cost;
3363  /* check the input type's output function */
3364  getTypeOutputInfo(exprType((Node *) iocoerce->arg),
3365  &iofunc, &typisvarlena);
3366  context->total.per_tuple += get_func_cost(iofunc) * cpu_operator_cost;
3367  }
3368  else if (IsA(node, ArrayCoerceExpr))
3369  {
3370  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
3371  Node *arraynode = (Node *) acoerce->arg;
3372 
3373  if (OidIsValid(acoerce->elemfuncid))
3374  context->total.per_tuple += get_func_cost(acoerce->elemfuncid) *
3376  }
3377  else if (IsA(node, RowCompareExpr))
3378  {
3379  /* Conservatively assume we will check all the columns */
3380  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3381  ListCell *lc;
3382 
3383  foreach(lc, rcexpr->opnos)
3384  {
3385  Oid opid = lfirst_oid(lc);
3386 
3387  context->total.per_tuple += get_func_cost(get_opcode(opid)) *
3389  }
3390  }
3391  else if (IsA(node, CurrentOfExpr))
3392  {
3393  /* Report high cost to prevent selection of anything but TID scan */
3394  context->total.startup += disable_cost;
3395  }
3396  else if (IsA(node, SubLink))
3397  {
3398  /* This routine should not be applied to un-planned expressions */
3399  elog(ERROR, "cannot handle unplanned sub-select");
3400  }
3401  else if (IsA(node, SubPlan))
3402  {
3403  /*
3404  * A subplan node in an expression typically indicates that the
3405  * subplan will be executed on each evaluation, so charge accordingly.
3406  * (Sub-selects that can be executed as InitPlans have already been
3407  * removed from the expression.)
3408  */
3409  SubPlan *subplan = (SubPlan *) node;
3410 
3411  context->total.startup += subplan->startup_cost;
3412  context->total.per_tuple += subplan->per_call_cost;
3413 
3414  /*
3415  * We don't want to recurse into the testexpr, because it was already
3416  * counted in the SubPlan node's costs. So we're done.
3417  */
3418  return false;
3419  }
3420  else if (IsA(node, AlternativeSubPlan))
3421  {
3422  /*
3423  * Arbitrarily use the first alternative plan for costing. (We should
3424  * certainly only include one alternative, and we don't yet have
3425  * enough information to know which one the executor is most likely to
3426  * use.)
3427  */
3428  AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
3429 
3430  return cost_qual_eval_walker((Node *) linitial(asplan->subplans),
3431  context);
3432  }
3433  else if (IsA(node, PlaceHolderVar))
3434  {
3435  /*
3436  * A PlaceHolderVar should be given cost zero when considering general
3437  * expression evaluation costs. The expense of doing the contained
3438  * expression is charged as part of the tlist eval costs of the scan
3439  * or join where the PHV is first computed (see set_rel_width and
3440  * add_placeholders_to_joinrel). If we charged it again here, we'd be
3441  * double-counting the cost for each level of plan that the PHV
3442  * bubbles up through. Hence, return without recursing into the
3443  * phexpr.
3444  */
3445  return false;
3446  }
3447 
3448  /* recurse into children */
3450  (void *) context);
3451 }
3452 
3453 /*
3454  * get_restriction_qual_cost
3455  * Compute evaluation costs of a baserel's restriction quals, plus any
3456  * movable join quals that have been pushed down to the scan.
3457  * Results are returned into *qpqual_cost.
3458  *
3459  * This is a convenience subroutine that works for seqscans and other cases
3460  * where all the given quals will be evaluated the hard way. It's not useful
3461  * for cost_index(), for example, where the index machinery takes care of
3462  * some of the quals. We assume baserestrictcost was previously set by
3463  * set_baserel_size_estimates().
3464  */
3465 static void
3467  ParamPathInfo *param_info,
3468  QualCost *qpqual_cost)
3469 {
3470  if (param_info)
3471  {
3472  /* Include costs of pushed-down clauses */
3473  cost_qual_eval(qpqual_cost, param_info->ppi_clauses, root);
3474 
3475  qpqual_cost->startup += baserel->baserestrictcost.startup;
3476  qpqual_cost->per_tuple += baserel->baserestrictcost.per_tuple;
3477  }
3478  else
3479  *qpqual_cost = baserel->baserestrictcost;
3480 }
3481 
3482 
3483 /*
3484  * compute_semi_anti_join_factors
3485  * Estimate how much of the inner input a SEMI or ANTI join
3486  * can be expected to scan.
3487  *
3488  * In a hash or nestloop SEMI/ANTI join, the executor will stop scanning
3489  * inner rows as soon as it finds a match to the current outer row.
3490  * We should therefore adjust some of the cost components for this effect.
3491  * This function computes some estimates needed for these adjustments.
3492  * These estimates will be the same regardless of the particular paths used
3493  * for the outer and inner relation, so we compute these once and then pass
3494  * them to all the join cost estimation functions.
3495  *
3496  * Input parameters:
3497  * outerrel: outer relation under consideration
3498  * innerrel: inner relation under consideration
3499  * jointype: must be JOIN_SEMI or JOIN_ANTI
3500  * sjinfo: SpecialJoinInfo relevant to this join
3501  * restrictlist: join quals
3502  * Output parameters:
3503  * *semifactors is filled in (see relation.h for field definitions)
3504  */
3505 void
3507  RelOptInfo *outerrel,
3508  RelOptInfo *innerrel,
3509  JoinType jointype,
3510  SpecialJoinInfo *sjinfo,
3511  List *restrictlist,
3512  SemiAntiJoinFactors *semifactors)
3513 {
3514  Selectivity jselec;
3515  Selectivity nselec;
3516  Selectivity avgmatch;
3517  SpecialJoinInfo norm_sjinfo;
3518  List *joinquals;
3519  ListCell *l;
3520 
3521  /* Should only be called in these cases */
3522  Assert(jointype == JOIN_SEMI || jointype == JOIN_ANTI);
3523 
3524  /*
3525  * In an ANTI join, we must ignore clauses that are "pushed down", since
3526  * those won't affect the match logic. In a SEMI join, we do not
3527  * distinguish joinquals from "pushed down" quals, so just use the whole
3528  * restrictinfo list.
3529  */
3530  if (jointype == JOIN_ANTI)
3531  {
3532  joinquals = NIL;
3533  foreach(l, restrictlist)
3534  {
3535  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(l));
3536 
3537  if (!rinfo->is_pushed_down)
3538  joinquals = lappend(joinquals, rinfo);
3539  }
3540  }
3541  else
3542  joinquals = restrictlist;
3543 
3544  /*
3545  * Get the JOIN_SEMI or JOIN_ANTI selectivity of the join clauses.
3546  */
3547  jselec = clauselist_selectivity(root,
3548  joinquals,
3549  0,
3550  jointype,
3551  sjinfo);
3552 
3553  /*
3554  * Also get the normal inner-join selectivity of the join clauses.
3555  */
3556  norm_sjinfo.type = T_SpecialJoinInfo;
3557  norm_sjinfo.min_lefthand = outerrel->relids;
3558  norm_sjinfo.min_righthand = innerrel->relids;
3559  norm_sjinfo.syn_lefthand = outerrel->relids;
3560  norm_sjinfo.syn_righthand = innerrel->relids;
3561  norm_sjinfo.jointype = JOIN_INNER;
3562  /* we don't bother trying to make the remaining fields valid */
3563  norm_sjinfo.lhs_strict = false;
3564  norm_sjinfo.delay_upper_joins = false;
3565  norm_sjinfo.semi_can_btree = false;
3566  norm_sjinfo.semi_can_hash = false;
3567  norm_sjinfo.semi_operators = NIL;
3568  norm_sjinfo.semi_rhs_exprs = NIL;
3569 
3570  nselec = clauselist_selectivity(root,
3571  joinquals,
3572  0,
3573  JOIN_INNER,
3574  &norm_sjinfo);
3575 
3576  /* Avoid leaking a lot of ListCells */
3577  if (jointype == JOIN_ANTI)
3578  list_free(joinquals);
3579 
3580  /*
3581  * jselec can be interpreted as the fraction of outer-rel rows that have
3582  * any matches (this is true for both SEMI and ANTI cases). And nselec is
3583  * the fraction of the Cartesian product that matches. So, the average
3584  * number of matches for each outer-rel row that has at least one match is
3585  * nselec * inner_rows / jselec.
3586  *
3587  * Note: it is correct to use the inner rel's "rows" count here, even
3588  * though we might later be considering a parameterized inner path with
3589  * fewer rows. This is because we have included all the join clauses in
3590  * the selectivity estimate.
3591  */
3592  if (jselec > 0) /* protect against zero divide */
3593  {
3594  avgmatch = nselec * innerrel->rows / jselec;
3595  /* Clamp to sane range */
3596  avgmatch = Max(1.0, avgmatch);
3597  }
3598  else
3599  avgmatch = 1.0;
3600 
3601  semifactors->outer_match_frac = jselec;
3602  semifactors->match_count = avgmatch;
3603 }
3604 
3605 /*
3606  * has_indexed_join_quals
3607  * Check whether all the joinquals of a nestloop join are used as
3608  * inner index quals.
3609  *
3610  * If the inner path of a SEMI/ANTI join is an indexscan (including bitmap
3611  * indexscan) that uses all the joinquals as indexquals, we can assume that an
3612  * unmatched outer tuple is cheap to process, whereas otherwise it's probably
3613  * expensive.
3614  */
3615 static bool
3617 {
3618  Relids joinrelids = joinpath->path.parent->relids;
3619  Path *innerpath = joinpath->innerjoinpath;
3620  List *indexclauses;
3621  bool found_one;
3622  ListCell *lc;
3623 
3624  /* If join still has quals to evaluate, it's not fast */
3625  if (joinpath->joinrestrictinfo != NIL)
3626  return false;
3627  /* Nor if the inner path isn't parameterized at all */
3628  if (innerpath->param_info == NULL)
3629  return false;
3630 
3631  /* Find the indexclauses list for the inner scan */
3632  switch (innerpath->pathtype)
3633  {
3634  case T_IndexScan:
3635  case T_IndexOnlyScan:
3636  indexclauses = ((IndexPath *) innerpath)->indexclauses;
3637  break;
3638  case T_BitmapHeapScan:
3639  {
3640  /* Accept only a simple bitmap scan, not AND/OR cases */
3641  Path *bmqual = ((BitmapHeapPath *) innerpath)->bitmapqual;
3642 
3643  if (IsA(bmqual, IndexPath))
3644  indexclauses = ((IndexPath *) bmqual)->indexclauses;
3645  else
3646  return false;
3647  break;
3648  }
3649  default:
3650 
3651  /*
3652  * If it's not a simple indexscan, it probably doesn't run quickly
3653  * for zero rows out, even if it's a parameterized path using all
3654  * the joinquals.
3655  */
3656  return false;
3657  }
3658 
3659  /*
3660  * Examine the inner path's param clauses. Any that are from the outer
3661  * path must be found in the indexclauses list, either exactly or in an
3662  * equivalent form generated by equivclass.c. Also, we must find at least
3663  * one such clause, else it's a clauseless join which isn't fast.
3664  */
3665  found_one = false;
3666  foreach(lc, innerpath->param_info->ppi_clauses)
3667  {
3668  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3669 
3670  if (join_clause_is_movable_into(rinfo,
3671  innerpath->parent->relids,
3672  joinrelids))
3673  {
3674  if (!(list_member_ptr(indexclauses, rinfo) ||
3675  is_redundant_derived_clause(rinfo, indexclauses)))
3676  return false;
3677  found_one = true;
3678  }
3679  }
3680  return found_one;
3681 }
3682 
3683 
3684 /*
3685  * approx_tuple_count
3686  * Quick-and-dirty estimation of the number of join rows passing
3687  * a set of qual conditions.
3688  *
3689  * The quals can be either an implicitly-ANDed list of boolean expressions,
3690  * or a list of RestrictInfo nodes (typically the latter).
3691  *
3692  * We intentionally compute the selectivity under JOIN_INNER rules, even
3693  * if it's some type of outer join. This is appropriate because we are
3694  * trying to figure out how many tuples pass the initial merge or hash
3695  * join step.
3696  *
3697  * This is quick-and-dirty because we bypass clauselist_selectivity, and
3698  * simply multiply the independent clause selectivities together. Now
3699  * clauselist_selectivity often can't do any better than that anyhow, but
3700  * for some situations (such as range constraints) it is smarter. However,
3701  * we can't effectively cache the results of clauselist_selectivity, whereas
3702  * the individual clause selectivities can be and are cached.
3703  *
3704  * Since we are only using the results to estimate how many potential
3705  * output tuples are generated and passed through qpqual checking, it
3706  * seems OK to live with the approximation.
3707  */
3708 static double
3710 {
3711  double tuples;
3712  double outer_tuples = path->outerjoinpath->rows;
3713  double inner_tuples = path->innerjoinpath->rows;
3714  SpecialJoinInfo sjinfo;
3715  Selectivity selec = 1.0;
3716  ListCell *l;
3717 
3718  /*
3719  * Make up a SpecialJoinInfo for JOIN_INNER semantics.
3720  */
3721  sjinfo.type = T_SpecialJoinInfo;
3722  sjinfo.min_lefthand = path->outerjoinpath->parent->relids;
3723  sjinfo.min_righthand = path->innerjoinpath->parent->relids;
3724  sjinfo.syn_lefthand = path->outerjoinpath->parent->relids;
3725  sjinfo.syn_righthand = path->innerjoinpath->parent->relids;
3726  sjinfo.jointype = JOIN_INNER;
3727  /* we don't bother trying to make the remaining fields valid */
3728  sjinfo.lhs_strict = false;
3729  sjinfo.delay_upper_joins = false;
3730  sjinfo.semi_can_btree = false;
3731  sjinfo.semi_can_hash = false;
3732  sjinfo.semi_operators = NIL;
3733  sjinfo.semi_rhs_exprs = NIL;
3734 
3735  /* Get the approximate selectivity */
3736  foreach(l, quals)
3737  {
3738  Node *qual = (Node *) lfirst(l);
3739 
3740  /* Note that clause_selectivity will be able to cache its result */
3741  selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo);
3742  }
3743 
3744  /* Apply it to the input relation sizes */
3745  tuples = selec * outer_tuples * inner_tuples;
3746 
3747  return clamp_row_est(tuples);
3748 }
3749 
3750 
3751 /*
3752  * set_baserel_size_estimates
3753  * Set the size estimates for the given base relation.
3754  *
3755  * The rel's targetlist and restrictinfo list must have been constructed
3756  * already, and rel->tuples must be set.
3757  *
3758  * We set the following fields of the rel node:
3759  * rows: the estimated number of output tuples (after applying
3760  * restriction clauses).
3761  * width: the estimated average output tuple width in bytes.
3762  * baserestrictcost: estimated cost of evaluating baserestrictinfo clauses.
3763  */
3764 void
3766 {
3767  double nrows;
3768 
3769  /* Should only be applied to base relations */
3770  Assert(rel->relid > 0);
3771 
3772  nrows = rel->tuples *
3774  rel->baserestrictinfo,
3775  0,
3776  JOIN_INNER,
3777  NULL);
3778 
3779  rel->rows = clamp_row_est(nrows);
3780 
3782 
3783  set_rel_width(root, rel);
3784 }
3785 
3786 /*
3787  * get_parameterized_baserel_size
3788  * Make a size estimate for a parameterized scan of a base relation.
3789  *
3790  * 'param_clauses' lists the additional join clauses to be used.
3791  *
3792  * set_baserel_size_estimates must have been applied already.
3793  */
3794 double
3796  List *param_clauses)
3797 {
3798  List *allclauses;
3799  double nrows;
3800 
3801  /*
3802  * Estimate the number of rows returned by the parameterized scan, knowing
3803  * that it will apply all the extra join clauses as well as the rel's own
3804  * restriction clauses. Note that we force the clauses to be treated as
3805  * non-join clauses during selectivity estimation.
3806  */
3807  allclauses = list_concat(list_copy(param_clauses),
3808  rel->baserestrictinfo);
3809  nrows = rel->tuples *
3811  allclauses,
3812  rel->relid, /* do not use 0! */
3813  JOIN_INNER,
3814  NULL);
3815  nrows = clamp_row_est(nrows);
3816  /* For safety, make sure result is not more than the base estimate */
3817  if (nrows > rel->rows)
3818  nrows = rel->rows;
3819  return nrows;
3820 }
3821 
3822 /*
3823  * set_joinrel_size_estimates
3824  * Set the size estimates for the given join relation.
3825  *
3826  * The rel's targetlist must have been constructed already, and a
3827  * restriction clause list that matches the given component rels must
3828  * be provided.
3829  *
3830  * Since there is more than one way to make a joinrel for more than two
3831  * base relations, the results we get here could depend on which component
3832  * rel pair is provided. In theory we should get the same answers no matter
3833  * which pair is provided; in practice, since the selectivity estimation
3834  * routines don't handle all cases equally well, we might not. But there's
3835  * not much to be done about it. (Would it make sense to repeat the
3836  * calculations for each pair of input rels that's encountered, and somehow
3837  * average the results? Probably way more trouble than it's worth, and
3838  * anyway we must keep the rowcount estimate the same for all paths for the
3839  * joinrel.)
3840  *
3841  * We set only the rows field here. The reltarget field was already set by
3842  * build_joinrel_tlist, and baserestrictcost is not used for join rels.
3843  */
3844 void
3846  RelOptInfo *outer_rel,
3847  RelOptInfo *inner_rel,
3848  SpecialJoinInfo *sjinfo,
3849  List *restrictlist)
3850 {
3851  rel->rows = calc_joinrel_size_estimate(root,
3852  outer_rel,
3853  inner_rel,
3854  outer_rel->rows,
3855  inner_rel->rows,
3856  sjinfo,
3857  restrictlist);
3858 }
3859 
3860 /*
3861  * get_parameterized_joinrel_size
3862  * Make a size estimate for a parameterized scan of a join relation.
3863  *
3864  * 'rel' is the joinrel under consideration.
3865  * 'outer_path', 'inner_path' are (probably also parameterized) Paths that
3866  * produce the relations being joined.
3867  * 'sjinfo' is any SpecialJoinInfo relevant to this join.
3868  * 'restrict_clauses' lists the join clauses that need to be applied at the
3869  * join node (including any movable clauses that were moved down to this join,
3870  * and not including any movable clauses that were pushed down into the
3871  * child paths).
3872  *
3873  * set_joinrel_size_estimates must have been applied already.
3874  */
3875 double
3877  Path *outer_path,
3878  Path *inner_path,
3879  SpecialJoinInfo *sjinfo,
3880  List *restrict_clauses)
3881 {
3882  double nrows;
3883 
3884  /*
3885  * Estimate the number of rows returned by the parameterized join as the
3886  * sizes of the input paths times the selectivity of the clauses that have
3887  * ended up at this join node.
3888  *
3889  * As with set_joinrel_size_estimates, the rowcount estimate could depend
3890  * on the pair of input paths provided, though ideally we'd get the same
3891  * estimate for any pair with the same parameterization.
3892  */
3893  nrows = calc_joinrel_size_estimate(root,
3894  outer_path->parent,
3895  inner_path->parent,
3896  outer_path->rows,
3897  inner_path->rows,
3898  sjinfo,
3899  restrict_clauses);
3900  /* For safety, make sure result is not more than the base estimate */
3901  if (nrows > rel->rows)
3902  nrows = rel->rows;
3903  return nrows;
3904 }
3905 
3906 /*
3907  * calc_joinrel_size_estimate
3908  * Workhorse for set_joinrel_size_estimates and
3909  * get_parameterized_joinrel_size.
3910  *
3911  * outer_rel/inner_rel are the relations being joined, but they should be
3912  * assumed to have sizes outer_rows/inner_rows; those numbers might be less
3913  * than what rel->rows says, when we are considering parameterized paths.
3914  */
3915 static double
3917  RelOptInfo *outer_rel,
3918  RelOptInfo *inner_rel,
3919  double outer_rows,
3920  double inner_rows,
3921  SpecialJoinInfo *sjinfo,
3922  List *restrictlist_in)
3923 {
3924  /* This apparently-useless variable dodges a compiler bug in VS2013: */
3925  List *restrictlist = restrictlist_in;
3926  JoinType jointype = sjinfo->jointype;
3927  Selectivity fkselec;
3928  Selectivity jselec;
3929  Selectivity pselec;
3930  double nrows;
3931 
3932  /*
3933  * Compute joinclause selectivity. Note that we are only considering
3934  * clauses that become restriction clauses at this join level; we are not
3935  * double-counting them because they were not considered in estimating the
3936  * sizes of the component rels.
3937  *
3938  * First, see whether any of the joinclauses can be matched to known FK
3939  * constraints. If so, drop those clauses from the restrictlist, and
3940  * instead estimate their selectivity using FK semantics. (We do this
3941  * without regard to whether said clauses are local or "pushed down".
3942  * Probably, an FK-matching clause could never be seen as pushed down at
3943  * an outer join, since it would be strict and hence would be grounds for
3944  * join strength reduction.) fkselec gets the net selectivity for
3945  * FK-matching clauses, or 1.0 if there are none.
3946  */
3947  fkselec = get_foreign_key_join_selectivity(root,
3948  outer_rel->relids,
3949  inner_rel->relids,
3950  sjinfo,
3951  &restrictlist);
3952 
3953  /*
3954  * For an outer join, we have to distinguish the selectivity of the join's
3955  * own clauses (JOIN/ON conditions) from any clauses that were "pushed
3956  * down". For inner joins we just count them all as joinclauses.
3957  */
3958  if (IS_OUTER_JOIN(jointype))
3959  {
3960  List *joinquals = NIL;
3961  List *pushedquals = NIL;
3962  ListCell *l;
3963 
3964  /* Grovel through the clauses to separate into two lists */
3965  foreach(l, restrictlist)
3966  {
3967  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(l));
3968 
3969  if (rinfo->is_pushed_down)
3970  pushedquals = lappend(pushedquals, rinfo);
3971  else
3972  joinquals = lappend(joinquals, rinfo);
3973  }
3974 
3975  /* Get the separate selectivities */
3976  jselec = clauselist_selectivity(root,
3977  joinquals,
3978  0,
3979  jointype,
3980  sjinfo);
3981  pselec = clauselist_selectivity(root,
3982  pushedquals,
3983  0,
3984  jointype,
3985  sjinfo);
3986 
3987  /* Avoid leaking a lot of ListCells */
3988  list_free(joinquals);
3989  list_free(pushedquals);
3990  }
3991  else
3992  {
3993  jselec = clauselist_selectivity(root,
3994  restrictlist,
3995  0,
3996  jointype,
3997  sjinfo);
3998  pselec = 0.0; /* not used, keep compiler quiet */
3999  }
4000 
4001  /*
4002  * Basically, we multiply size of Cartesian product by selectivity.
4003  *
4004  * If we are doing an outer join, take that into account: the joinqual
4005  * selectivity has to be clamped using the knowledge that the output must
4006  * be at least as large as the non-nullable input. However, any
4007  * pushed-down quals are applied after the outer join, so their
4008  * selectivity applies fully.
4009  *
4010  * For JOIN_SEMI and JOIN_ANTI, the selectivity is defined as the fraction
4011  * of LHS rows that have matches, and we apply that straightforwardly.
4012  */
4013  switch (jointype)
4014  {
4015  case JOIN_INNER:
4016  nrows = outer_rows * inner_rows * fkselec * jselec;
4017  /* pselec not used */
4018  break;
4019  case JOIN_LEFT:
4020  nrows = outer_rows * inner_rows * fkselec * jselec;
4021  if (nrows < outer_rows)
4022  nrows = outer_rows;
4023  nrows *= pselec;
4024  break;
4025  case JOIN_FULL:
4026  nrows = outer_rows * inner_rows * fkselec * jselec;
4027  if (nrows < outer_rows)
4028  nrows = outer_rows;
4029  if (nrows < inner_rows)
4030  nrows = inner_rows;
4031  nrows *= pselec;
4032  break;
4033  case JOIN_SEMI:
4034  nrows = outer_rows * fkselec * jselec;
4035  /* pselec not used */
4036  break;
4037  case JOIN_ANTI:
4038  nrows = outer_rows * (1.0 - fkselec * jselec);
4039  nrows *= pselec;
4040  break;
4041  default:
4042  /* other values not expected here */
4043  elog(ERROR, "unrecognized join type: %d", (int) jointype);
4044  nrows = 0; /* keep compiler quiet */
4045  break;
4046  }
4047 
4048  return clamp_row_est(nrows);
4049 }
4050 
4051 /*
4052  * get_foreign_key_join_selectivity
4053  * Estimate join selectivity for foreign-key-related clauses.
4054  *
4055  * Remove any clauses that can be matched to FK constraints from *restrictlist,
4056  * and return a substitute estimate of their selectivity. 1.0 is returned
4057  * when there are no such clauses.
4058  *
4059  * The reason for treating such clauses specially is that we can get better
4060  * estimates this way than by relying on clauselist_selectivity(), especially
4061  * for multi-column FKs where that function's assumption that the clauses are
4062  * independent falls down badly. But even with single-column FKs, we may be
4063  * able to get a better answer when the pg_statistic stats are missing or out
4064  * of date.
4065  */
4066 static Selectivity
4068  Relids outer_relids,
4069  Relids inner_relids,
4070  SpecialJoinInfo *sjinfo,
4071  List **restrictlist)
4072 {
4073  Selectivity fkselec = 1.0;
4074  JoinType jointype = sjinfo->jointype;
4075  List *worklist = *restrictlist;
4076  ListCell *lc;
4077 
4078  /* Consider each FK constraint that is known to match the query */
4079  foreach(lc, root->fkey_list)
4080  {
4081  ForeignKeyOptInfo *fkinfo = (ForeignKeyOptInfo *) lfirst(lc);
4082  bool ref_is_outer;
4083  bool use_smallest_selectivity = false;
4084  List *removedlist;
4085  ListCell *cell;
4086  ListCell *prev;
4087  ListCell *next;
4088 
4089  /*
4090  * This FK is not relevant unless it connects a baserel on one side of
4091  * this join to a baserel on the other side.
4092  */
4093  if (bms_is_member(fkinfo->con_relid, outer_relids) &&
4094  bms_is_member(fkinfo->ref_relid, inner_relids))
4095  ref_is_outer = false;
4096  else if (bms_is_member(fkinfo->ref_relid, outer_relids) &&
4097  bms_is_member(fkinfo->con_relid, inner_relids))
4098  ref_is_outer = true;
4099  else
4100  continue;
4101 
4102  /*
4103  * Modify the restrictlist by removing clauses that match the FK (and
4104  * putting them into removedlist instead). It seems unsafe to modify
4105  * the originally-passed List structure, so we make a shallow copy the
4106  * first time through.
4107  */
4108  if (worklist == *restrictlist)
4109  worklist = list_copy(worklist);
4110 
4111  removedlist = NIL;
4112  prev = NULL;
4113  for (cell = list_head(worklist); cell; cell = next)
4114  {
4115  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
4116  bool remove_it = false;
4117  int i;
4118 
4119  next = lnext(cell);
4120  /* Drop this clause if it matches any column of the FK */
4121  for (i = 0; i < fkinfo->nkeys; i++)
4122  {
4123  if (rinfo->parent_ec)
4124  {
4125  /*
4126  * EC-derived clauses can only match by EC. It is okay to
4127  * consider any clause derived from the same EC as
4128  * matching the FK: even if equivclass.c chose to generate
4129  * a clause equating some other pair of Vars, it could
4130  * have generated one equating the FK's Vars. So for
4131  * purposes of estimation, we can act as though it did so.
4132  *
4133  * Note: checking parent_ec is a bit of a cheat because
4134  * there are EC-derived clauses that don't have parent_ec
4135  * set; but such clauses must compare expressions that
4136  * aren't just Vars, so they cannot match the FK anyway.
4137  */
4138  if (fkinfo->eclass[i] == rinfo->parent_ec)
4139  {
4140  remove_it = true;
4141  break;
4142  }
4143  }
4144  else
4145  {
4146  /*
4147  * Otherwise, see if rinfo was previously matched to FK as
4148  * a "loose" clause.
4149  */
4150  if (list_member_ptr(fkinfo->rinfos[i], rinfo))
4151  {
4152  remove_it = true;
4153  break;
4154  }
4155  }
4156  }
4157  if (remove_it)
4158  {
4159  worklist = list_delete_cell(worklist, cell, prev);
4160  removedlist = lappend(removedlist, rinfo);
4161  }
4162  else
4163  prev = cell;
4164  }
4165 
4166  /*
4167  * If we failed to remove all the matching clauses we expected to
4168  * find, chicken out and ignore this FK; applying its selectivity
4169  * might result in double-counting. Put any clauses we did manage to
4170  * remove back into the worklist.
4171  *
4172  * Since the matching clauses are known not outerjoin-delayed, they
4173  * should certainly have appeared in the initial joinclause list. If
4174  * we didn't find them, they must have been matched to, and removed
4175  * by, some other FK in a previous iteration of this loop. (A likely
4176  * case is that two FKs are matched to the same EC; there will be only
4177  * one EC-derived clause in the initial list, so the first FK will
4178  * consume it.) Applying both FKs' selectivity independently risks
4179  * underestimating the join size; in particular, this would undo one
4180  * of the main things that ECs were invented for, namely to avoid
4181  * double-counting the selectivity of redundant equality conditions.
4182  * Later we might think of a reasonable way to combine the estimates,
4183  * but for now, just punt, since this is a fairly uncommon situation.
4184  */
4185  if (list_length(removedlist) !=
4186  (fkinfo->nmatched_ec + fkinfo->nmatched_ri))
4187  {
4188  worklist = list_concat(worklist, removedlist);
4189  continue;
4190  }
4191 
4192  /*
4193  * Finally we get to the payoff: estimate selectivity using the
4194  * knowledge that each referencing row will match exactly one row in
4195  * the referenced table.
4196  *
4197  * XXX that's not true in the presence of nulls in the referencing
4198  * column(s), so in principle we should derate the estimate for those.
4199  * However (1) if there are any strict restriction clauses for the
4200  * referencing column(s) elsewhere in the query, derating here would
4201  * be double-counting the null fraction, and (2) it's not very clear
4202  * how to combine null fractions for multiple referencing columns.
4203  *
4204  * In the use_smallest_selectivity code below, null derating is done
4205  * implicitly by relying on clause_selectivity(); in the other cases,
4206  * we do nothing for now about correcting for nulls.
4207  *
4208  * XXX another point here is that if either side of an FK constraint
4209  * is an inheritance parent, we estimate as though the constraint
4210  * covers all its children as well. This is not an unreasonable
4211  * assumption for a referencing table, ie the user probably applied
4212  * identical constraints to all child tables (though perhaps we ought
4213  * to check that). But it's not possible to have done that for a
4214  * referenced table. Fortunately, precisely because that doesn't
4215  * work, it is uncommon in practice to have an FK referencing a parent
4216  * table. So, at least for now, disregard inheritance here.
4217  */
4218  if (ref_is_outer && jointype != JOIN_INNER)
4219  {
4220  /*
4221  * When the referenced table is on the outer side of a non-inner
4222  * join, knowing that each inner row has exactly one match is not
4223  * as useful as one could wish, since we really need to know the
4224  * fraction of outer rows with a match. Still, we can avoid the
4225  * folly of multiplying the per-column estimates together. Take
4226  * the smallest per-column selectivity, instead. (This should
4227  * correspond to the FK column with the most nulls.)
4228  */
4229  use_smallest_selectivity = true;
4230  }
4231  else if (jointype == JOIN_SEMI || jointype == JOIN_ANTI)
4232  {
4233  /*
4234  * For JOIN_SEMI and JOIN_ANTI, the selectivity is defined as the
4235  * fraction of LHS rows that have matches. The referenced table
4236  * is on the inner side (we already handled the other case above),
4237  * so the FK implies that every LHS row has a match *in the
4238  * referenced table*. But any restriction or join clauses below
4239  * here will reduce the number of matches.
4240  */
4241  if (bms_membership(inner_relids) == BMS_SINGLETON)
4242  {
4243  /*
4244  * When the inner side of the semi/anti join is just the
4245  * referenced table, we may take the FK selectivity as equal
4246  * to the selectivity of the table's restriction clauses.
4247  */
4248  RelOptInfo *ref_rel = find_base_rel(root, fkinfo->ref_relid);
4249  double ref_tuples = Max(ref_rel->tuples, 1.0);
4250 
4251  fkselec *= ref_rel->rows / ref_tuples;
4252  }
4253  else
4254  {
4255  /*
4256  * When the inner side of the semi/anti join is itself a join,
4257  * it's hard to guess what fraction of the referenced table
4258  * will get through the join. But we still don't want to
4259  * multiply per-column estimates together. Take the smallest
4260  * per-column selectivity, instead.
4261  */
4262  use_smallest_selectivity = true;
4263  }
4264  }
4265  else
4266  {
4267  /*
4268  * Otherwise, selectivity is exactly 1/referenced-table-size; but
4269  * guard against tuples == 0. Note we should use the raw table
4270  * tuple count, not any estimate of its filtered or joined size.
4271  */
4272  RelOptInfo *ref_rel = find_base_rel(root, fkinfo->ref_relid);
4273  double ref_tuples = Max(ref_rel->tuples, 1.0);
4274 
4275  fkselec *= 1.0 / ref_tuples;
4276  }
4277 
4278  /*
4279  * Common code for cases where we should use the smallest selectivity
4280  * that would be computed for any one of the FK's clauses.
4281  */
4282  if (use_smallest_selectivity)
4283  {
4284  Selectivity thisfksel = 1.0;
4285 
4286  foreach(cell, removedlist)
4287  {
4288  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
4289  Selectivity csel;
4290 
4291  csel = clause_selectivity(root, (Node *) rinfo,
4292  0, jointype, sjinfo);
4293  thisfksel = Min(thisfksel, csel);
4294  }
4295  fkselec *= thisfksel;
4296  }
4297  }
4298 
4299  *restrictlist = worklist;
4300  return fkselec;
4301 }
4302 
4303 /*
4304  * set_subquery_size_estimates
4305  * Set the size estimates for a base relation that is a subquery.
4306  *
4307  * The rel's targetlist and restrictinfo list must have been constructed
4308  * already, and the Paths for the subquery must have been completed.
4309  * We look at the subquery's PlannerInfo to extract data.
4310  *
4311  * We set the same fields as set_baserel_size_estimates.
4312  */
4313 void
4315 {
4316  PlannerInfo *subroot = rel->subroot;
4317  RelOptInfo *sub_final_rel;
4319  ListCell *lc;
4320 
4321  /* Should only be applied to base relations that are subqueries */
4322  Assert(rel->relid > 0);
4323  rte = planner_rt_fetch(rel->relid, root);
4324  Assert(rte->rtekind == RTE_SUBQUERY);
4325 
4326  /*
4327  * Copy raw number of output rows from subquery. All of its paths should
4328  * have the same output rowcount, so just look at cheapest-total.
4329  */
4330  sub_final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
4331  rel->tuples = sub_final_rel->cheapest_total_path->rows;
4332 
4333  /*
4334  * Compute per-output-column width estimates by examining the subquery's
4335  * targetlist. For any output that is a plain Var, get the width estimate
4336  * that was made while planning the subquery. Otherwise, we leave it to
4337  * set_rel_width to fill in a datatype-based default estimate.
4338  */
4339  foreach(lc, subroot->parse->targetList)
4340  {
4341  TargetEntry *te = castNode(TargetEntry, lfirst(lc));
4342  Node *texpr = (Node *) te->expr;
4343  int32 item_width = 0;
4344 
4345  /* junk columns aren't visible to upper query */
4346  if (te->resjunk)
4347  continue;
4348 
4349  /*
4350  * The subquery could be an expansion of a view that's had columns
4351  * added to it since the current query was parsed, so that there are
4352  * non-junk tlist columns in it that don't correspond to any column
4353  * visible at our query level. Ignore such columns.
4354  */
4355  if (te->resno < rel->min_attr || te->resno > rel->max_attr)
4356  continue;
4357 
4358  /*
4359  * XXX This currently doesn't work for subqueries containing set
4360  * operations, because the Vars in their tlists are bogus references
4361  * to the first leaf subquery, which wouldn't give the right answer
4362  * even if we could still get to its PlannerInfo.
4363  *
4364  * Also, the subquery could be an appendrel for which all branches are
4365  * known empty due to constraint exclusion, in which case
4366  * set_append_rel_pathlist will have left the attr_widths set to zero.
4367  *
4368  * In either case, we just leave the width estimate zero until
4369  * set_rel_width fixes it.
4370  */
4371  if (IsA(texpr, Var) &&
4372  subroot->parse->setOperations == NULL)
4373  {
4374  Var *var = (Var *) texpr;
4375  RelOptInfo *subrel = find_base_rel(subroot, var->varno);
4376 
4377  item_width = subrel->attr_widths[var->varattno - subrel->min_attr];
4378  }
4379  rel->attr_widths[te->resno - rel->min_attr] = item_width;
4380  }
4381 
4382  /* Now estimate number of output rows, etc */
4383  set_baserel_size_estimates(root, rel);
4384 }
4385 
4386 /*
4387  * set_function_size_estimates
4388  * Set the size estimates for a base relation that is a function call.
4389  *
4390  * The rel's targetlist and restrictinfo list must have been constructed
4391  * already.
4392  *
4393  * We set the same fields as set_baserel_size_estimates.
4394  */
4395 void
4397 {
4398  RangeTblEntry *rte;
4399  ListCell *lc;
4400 
4401  /* Should only be applied to base relations that are functions */
4402  Assert(rel->relid > 0);
4403  rte = planner_rt_fetch(rel->relid, root);
4404  Assert(rte->rtekind == RTE_FUNCTION);
4405 
4406  /*
4407  * Estimate number of rows the functions will return. The rowcount of the
4408  * node is that of the largest function result.
4409  */
4410  rel->tuples = 0;
4411  foreach(lc, rte->functions)
4412  {
4413  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
4414  double ntup = expression_returns_set_rows(rtfunc->funcexpr);
4415 
4416  if (ntup > rel->tuples)
4417  rel->tuples = ntup;
4418  }
4419 
4420  /* Now estimate number of output rows, etc */
4421  set_baserel_size_estimates(root, rel);
4422 }
4423 
4424 /*
4425  * set_values_size_estimates
4426  * Set the size estimates for a base relation that is a values list.
4427  *
4428  * The rel's targetlist and restrictinfo list must have been constructed
4429  * already.
4430  *
4431  * We set the same fields as set_baserel_size_estimates.
4432  */
4433 void
4435 {
4436  RangeTblEntry *rte;
4437 
4438  /* Should only be applied to base relations that are values lists */
4439  Assert(rel->relid > 0);
4440  rte = planner_rt_fetch(rel->relid, root);
4441  Assert(rte->rtekind == RTE_VALUES);
4442 
4443  /*
4444  * Estimate number of rows the values list will return. We know this
4445  * precisely based on the list length (well, barring set-returning
4446  * functions in list items, but that's a refinement not catered for
4447  * anywhere else either).
4448  */
4449  rel->tuples = list_length(rte->values_lists);
4450 
4451  /* Now estimate number of output rows, etc */
4452  set_baserel_size_estimates(root, rel);
4453 }
4454 
4455 /*
4456  * set_cte_size_estimates
4457  * Set the size estimates for a base relation that is a CTE reference.
4458  *
4459  * The rel's targetlist and restrictinfo list must have been constructed
4460  * already, and we need an estimate of the number of rows returned by the CTE
4461  * (if a regular CTE) or the non-recursive term (if a self-reference).
4462  *
4463  * We set the same fields as set_baserel_size_estimates.
4464  */
4465 void
4466 set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
4467 {
4468  RangeTblEntry *rte;
4469 
4470  /* Should only be applied to base relations that are CTE references */
4471  Assert(rel->relid > 0);
4472  rte = planner_rt_fetch(rel->relid, root);
4473  Assert(rte->rtekind == RTE_CTE);
4474 
4475  if (rte->self_reference)
4476  {
4477  /*
4478  * In a self-reference, arbitrarily assume the average worktable size
4479  * is about 10 times the nonrecursive term's size.
4480  */
4481  rel->tuples = 10 * cte_rows;
4482  }
4483  else
4484  {
4485  /* Otherwise just believe the CTE's rowcount estimate */
4486  rel->tuples = cte_rows;
4487  }
4488 
4489  /* Now estimate number of output rows, etc */
4490  set_baserel_size_estimates(root, rel);
4491 }
4492 
4493 /*
4494  * set_foreign_size_estimates
4495  * Set the size estimates for a base relation that is a foreign table.
4496  *
4497  * There is not a whole lot that we can do here; the foreign-data wrapper
4498  * is responsible for producing useful estimates. We can do a decent job
4499  * of estimating baserestrictcost, so we set that, and we also set up width
4500  * using what will be purely datatype-driven estimates from the targetlist.
4501  * There is no way to do anything sane with the rows value, so we just put
4502  * a default estimate and hope that the wrapper can improve on it. The
4503  * wrapper's GetForeignRelSize function will be called momentarily.
4504  *
4505  * The rel's targetlist and restrictinfo list must have been constructed
4506  * already.
4507  */
4508 void
4510 {
4511  /* Should only be applied to base relations */
4512  Assert(rel->relid > 0);
4513 
4514  rel->rows = 1000; /* entirely bogus default estimate */
4515 
4517 
4518  set_rel_width(root, rel);
4519 }
4520 
4521 
4522 /*
4523  * set_rel_width
4524  * Set the estimated output width of a base relation.
4525  *
4526  * The estimated output width is the sum of the per-attribute width estimates
4527  * for the actually-referenced columns, plus any PHVs or other expressions
4528  * that have to be calculated at this relation. This is the amount of data
4529  * we'd need to pass upwards in case of a sort, hash, etc.
4530  *
4531  * This function also sets reltarget->cost, so it's a bit misnamed now.
4532  *
4533  * NB: this works best on plain relations because it prefers to look at
4534  * real Vars. For subqueries, set_subquery_size_estimates will already have
4535  * copied up whatever per-column estimates were made within the subquery,
4536  * and for other types of rels there isn't much we can do anyway. We fall
4537  * back on (fairly stupid) datatype-based width estimates if we can't get
4538  * any better number.
4539  *
4540  * The per-attribute width estimates are cached for possible re-use while
4541  * building join relations or post-scan/join pathtargets.
4542  */
4543 static void
4545 {
4546  Oid reloid = planner_rt_fetch(rel->relid, root)->relid;
4547  int32 tuple_width = 0;
4548  bool have_wholerow_var = false;
4549  ListCell *lc;
4550 
4551  /* Vars are assumed to have cost zero, but other exprs do not */
4552  rel->reltarget->cost.startup = 0;
4553  rel->reltarget->cost.per_tuple = 0;
4554 
4555  foreach(lc, rel->reltarget->exprs)
4556  {
4557  Node *node = (Node *) lfirst(lc);
4558 
4559  /*
4560  * Ordinarily, a Var in a rel's targetlist must belong to that rel;
4561  * but there are corner cases involving LATERAL references where that
4562  * isn't so. If the Var has the wrong varno, fall through to the
4563  * generic case (it doesn't seem worth the trouble to be any smarter).
4564  */
4565  if (IsA(node, Var) &&
4566  ((Var *) node)->varno == rel->relid)
4567  {
4568  Var *var = (Var *) node;
4569  int ndx;
4570  int32 item_width;
4571 
4572  Assert(var->varattno >= rel->min_attr);
4573  Assert(var->varattno <= rel->max_attr);
4574 
4575  ndx = var->varattno - rel->min_attr;
4576 
4577  /*
4578  * If it's a whole-row Var, we'll deal with it below after we have
4579  * already cached as many attr widths as possible.
4580  */
4581  if (var->varattno == 0)
4582  {
4583  have_wholerow_var = true;
4584  continue;
4585  }
4586 
4587  /*
4588  * The width may have been cached already (especially if it's a
4589  * subquery), so don't duplicate effort.
4590  */
4591  if (rel->attr_widths[ndx] > 0)
4592  {
4593  tuple_width += rel->attr_widths[ndx];
4594  continue;
4595  }
4596 
4597  /* Try to get column width from statistics */
4598  if (reloid != InvalidOid && var->varattno > 0)
4599  {
4600  item_width = get_attavgwidth(reloid, var->varattno);
4601  if (item_width > 0)
4602  {
4603  rel->attr_widths[ndx] = item_width;
4604  tuple_width += item_width;
4605  continue;
4606  }
4607  }
4608 
4609  /*
4610  * Not a plain relation, or can't find statistics for it. Estimate
4611  * using just the type info.
4612  */
4613  item_width = get_typavgwidth(var->vartype, var->vartypmod);
4614  Assert(item_width > 0);
4615  rel->attr_widths[ndx] = item_width;
4616  tuple_width += item_width;
4617  }
4618  else if (IsA(node, PlaceHolderVar))
4619  {
4620  /*
4621  * We will need to evaluate the PHV's contained expression while
4622  * scanning this rel, so be sure to include it in reltarget->cost.
4623  */
4624  PlaceHolderVar *phv = (PlaceHolderVar *) node;
4625  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv, false);
4626  QualCost cost;
4627 
4628  tuple_width += phinfo->ph_width;
4629  cost_qual_eval_node(&cost, (Node *) phv->phexpr, root);
4630  rel->reltarget->cost.startup += cost.startup;
4631  rel->reltarget->cost.per_tuple += cost.per_tuple;
4632  }
4633  else
4634  {
4635  /*
4636  * We could be looking at an expression pulled up from a subquery,
4637  * or a ROW() representing a whole-row child Var, etc. Do what we
4638  * can using the expression type information.
4639  */
4640  int32 item_width;
4641  QualCost cost;
4642 
4643  item_width = get_typavgwidth(exprType(node), exprTypmod(node));
4644  Assert(item_width > 0);
4645  tuple_width += item_width;
4646  /* Not entirely clear if we need to account for cost, but do so */
4647  cost_qual_eval_node(&cost, node, root);
4648  rel->reltarget->cost.startup += cost.startup;
4649  rel->reltarget->cost.per_tuple += cost.per_tuple;
4650  }
4651  }
4652 
4653  /*
4654  * If we have a whole-row reference, estimate its width as the sum of
4655  * per-column widths plus heap tuple header overhead.
4656  */
4657  if (have_wholerow_var)
4658  {
4659  int32 wholerow_width = MAXALIGN(SizeofHeapTupleHeader);
4660 
4661  if (reloid != InvalidOid)
4662  {
4663  /* Real relation, so estimate true tuple width */
4664  wholerow_width += get_relation_data_width(reloid,
4665  rel->attr_widths - rel->min_attr);
4666  }
4667  else
4668  {
4669  /* Do what we can with info for a phony rel */
4670  AttrNumber i;
4671 
4672  for (i = 1; i <= rel->max_attr; i++)
4673  wholerow_width += rel->attr_widths[i - rel->min_attr];
4674  }
4675 
4676  rel->attr_widths[0 - rel->min_attr] = wholerow_width;
4677 
4678  /*
4679  * Include the whole-row Var as part of the output tuple. Yes, that
4680  * really is what happens at runtime.
4681  */
4682  tuple_width += wholerow_width;
4683  }
4684 
4685  Assert(tuple_width >= 0);
4686  rel->reltarget->width = tuple_width;
4687 }
4688 
4689 /*
4690  * set_pathtarget_cost_width
4691  * Set the estimated eval cost and output width of a PathTarget tlist.
4692  *
4693  * As a notational convenience, returns the same PathTarget pointer passed in.
4694  *
4695  * Most, though not quite all, uses of this function occur after we've run
4696  * set_rel_width() for base relations; so we can usually obtain cached width
4697  * estimates for Vars. If we can't, fall back on datatype-based width
4698  * estimates. Present early-planning uses of PathTargets don't need accurate
4699  * widths badly enough to justify going to the catalogs for better data.
4700  */
4701 PathTarget *
4703 {
4704  int32 tuple_width = 0;
4705  ListCell *lc;
4706 
4707  /* Vars are assumed to have cost zero, but other exprs do not */
4708  target->cost.startup = 0;
4709  target->cost.per_tuple = 0;
4710 
4711  foreach(lc, target->exprs)
4712  {
4713  Node *node = (Node *) lfirst(lc);
4714 
4715  if (IsA(node, Var))
4716  {
4717  Var *var = (Var *) node;
4718  int32 item_width;
4719 
4720  /* We should not see any upper-level Vars here */
4721  Assert(var->varlevelsup == 0);
4722 
4723  /* Try to get data from RelOptInfo cache */
4724  if (var->varno < root->simple_rel_array_size)
4725  {
4726  RelOptInfo *rel = root->simple_rel_array[var->varno];
4727 
4728  if (rel != NULL &&
4729  var->varattno >= rel->min_attr &&
4730  var->varattno <= rel->max_attr)
4731  {
4732  int ndx = var->varattno - rel->min_attr;
4733 
4734  if (rel->attr_widths[ndx] > 0)
4735  {
4736  tuple_width += rel->attr_widths[ndx];
4737  continue;
4738  }
4739  }
4740  }
4741 
4742  /*
4743  * No cached data available, so estimate using just the type info.
4744  */
4745  item_width = get_typavgwidth(var->vartype, var->vartypmod);
4746  Assert(item_width > 0);
4747  tuple_width += item_width;
4748  }
4749  else
4750  {
4751  /*
4752  * Handle general expressions using type info.
4753  */
4754  int32 item_width;
4755  QualCost cost;
4756 
4757  item_width = get_typavgwidth(exprType(node), exprTypmod(node));
4758  Assert(item_width > 0);
4759  tuple_width += item_width;
4760 
4761  /* Account for cost, too */
4762  cost_qual_eval_node(&cost, node, root);
4763  target->cost.startup += cost.startup;
4764  target->cost.per_tuple += cost.per_tuple;
4765  }
4766  }
4767 
4768  Assert(tuple_width >= 0);
4769  target->width = tuple_width;
4770 
4771  return target;
4772 }
4773 
4774 /*
4775  * relation_byte_size
4776  * Estimate the storage space in bytes for a given number of tuples
4777  * of a given width (size in bytes).
4778  */
4779 static double
4780 relation_byte_size(double tuples, int width)
4781 {
4782  return tuples * (MAXALIGN(width) + MAXALIGN(SizeofHeapTupleHeader));
4783 }
4784 
4785 /*
4786  * page_size
4787  * Returns an estimate of the number of pages covered by a given
4788  * number of tuples of a given width (size in bytes).
4789  */
4790 static double
4791 page_size(double tuples, int width)
4792 {
4793  return ceil(relation_byte_size(tuples, width) / BLCKSZ);
4794 }
4795 
4796 /*
4797  * Estimate the fraction of the work that each worker will do given the
4798  * number of workers budgeted for the path.
4799  */
4800 static double
4802 {
4803  double parallel_divisor = path->parallel_workers;
4804  double leader_contribution;
4805 
4806  /*
4807  * Early experience with parallel query suggests that when there is only
4808  * one worker, the leader often makes a very substantial contribution to
4809  * executing the parallel portion of the plan, but as more workers are
4810  * added, it does less and less, because it's busy reading tuples from the
4811  * workers and doing whatever non-parallel post-processing is needed. By
4812  * the time we reach 4 workers, the leader no longer makes a meaningful
4813  * contribution. Thus, for now, estimate that the leader spends 30% of
4814  * its time servicing each worker, and the remainder executing the
4815  * parallel plan.
4816  */
4817  leader_contribution = 1.0 - (0.3 * path->parallel_workers);
4818  if (leader_contribution > 0)
4819  parallel_divisor += leader_contribution;
4820 
4821  return parallel_divisor;
4822 }
4823 
4824 /*
4825  * compute_bitmap_pages
4826  *
4827  * compute number of pages fetched from heap in bitmap heap scan.
4828  */
4829 double
4830 compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual,
4831  int loop_count, Cost *cost, double *tuple)
4832 {
4833  Cost indexTotalCost;
4834  Selectivity indexSelectivity;
4835  double T;
4836  double pages_fetched;
4837  double tuples_fetched;
4838 
4839  /*
4840  * Fetch total cost of obtaining the bitmap, as well as its total
4841  * selectivity.
4842  */
4843  cost_bitmap_tree_node(bitmapqual, &indexTotalCost, &indexSelectivity);
4844 
4845  /*
4846  * Estimate number of main-table pages fetched.
4847  */
4848  tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
4849 
4850  T = (baserel->pages > 1) ? (double) baserel->pages : 1.0;
4851 
4852  if (loop_count > 1)
4853  {
4854  /*
4855  * For repeated bitmap scans, scale up the number of tuples fetched in
4856  * the Mackert and Lohman formula by the number of scans, so that we
4857  * estimate the number of pages fetched by all the scans. Then
4858  * pro-rate for one scan.
4859  */
4860  pages_fetched = index_pages_fetched(tuples_fetched * loop_count,
4861  baserel->pages,
4862  get_indexpath_pages(bitmapqual),
4863  root);
4864  pages_fetched /= loop_count;
4865  }
4866  else
4867  {
4868  /*
4869  * For a single scan, the number of heap pages that need to be fetched
4870  * is the same as the Mackert and Lohman formula for the case T <= b
4871  * (ie, no re-reads needed).
4872  */
4873  pages_fetched =
4874  (2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
4875  }
4876 
4877  if (pages_fetched >= T)
4878  pages_fetched = T;
4879  else
4880  pages_fetched = ceil(pages_fetched);
4881 
4882  if (cost)
4883  *cost = indexTotalCost;
4884  if (tuple)
4885  *tuple = tuples_fetched;
4886 
4887  return pages_fetched;
4888 }
QualCost eval_cost
Definition: relation.h:1674
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4314
void cost_group(Path *path, PlannerInfo *root, int numGroupCols, double numGroups, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
Definition: costsize.c:1853
static void cost_rescan(PlannerInfo *root, Path *path, Cost *rescan_startup_cost, Cost *rescan_total_cost)
Definition: costsize.c:3092
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3225
#define NIL
Definition: pg_list.h:69
void initial_cost_nestloop(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:1901
bool semi_can_btree
Definition: relation.h:1815
Selectivity leftendsel
Definition: relation.h:1718
#define SizeofHeapTupleHeader
Definition: htup_details.h:170
List * path_mergeclauses
Definition: relation.h:1265
double plan_rows
Definition: plannodes.h:117
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
JoinPath jpath
Definition: relation.h:1282
PlannerInfo * root
Definition: costsize.c:132
PathTarget * pathtarget
Definition: relation.h:895
Query * parse
Definition: relation.h:152
bool enable_tidscan
Definition: costsize.c:122
bool ExecSupportsMarkRestore(Path *pathnode)
Definition: execAmi.c:390
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:853
Index varlevelsup
Definition: primnodes.h:151
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
Path path
Definition: relation.h:971
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2448
IndexOptInfo * indexinfo
Definition: relation.h:972
void cost_tidscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, List *tidquals, ParamPathInfo *param_info)
Definition: costsize.c:1075
static int32 next
Definition: blutils.c:210
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: costsize.c:1783
bool enable_nestloop
Definition: costsize.c:125
int num_batches
Definition: relation.h:1284
PathTarget * set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target)
Definition: costsize.c:4702
List * args
Definition: primnodes.h:337
Relids min_righthand
Definition: relation.h:1808
bool materialize_inner
Definition: relation.h:1268
#define DEFAULT_CPU_TUPLE_COST
Definition: cost.h:26
void mergejoinscansel(PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
Definition: selfuncs.c:2848
double expression_returns_set_rows(Node *clause)
Definition: clauses.c:801
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
Expr * orclause
Definition: relation.h:1668
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
Selectivity outer_match_frac
Definition: relation.h:2032
Path * innerjoinpath
Definition: relation.h:1217
static MergeScanSelCache * cached_scansel(PlannerInfo *root, RestrictInfo *rinfo, PathKey *pathkey)
Definition: costsize.c:2608
static double approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
Definition: costsize.c:3709
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:3845
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:4466
void final_cost_nestloop(PlannerInfo *root, NestPath *path, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:1976
int compute_parallel_worker(RelOptInfo *rel, BlockNumber heap_pages, BlockNumber index_pages)
Definition: allpaths.c:2887
double tuples
Definition: relation.h:529
Oid reltablespace
Definition: relation.h:519
List * baserestrictinfo
Definition: relation.h:544
Oid resulttype
Definition: primnodes.h:788
NodeTag type
Definition: relation.h:1806
#define DEFAULT_PARALLEL_SETUP_COST
Definition: cost.h:30
#define Min(x, y)
Definition: c.h:802
int parallel_workers
Definition: relation.h:901
bool pseudoconstant
Definition: relation.h:1645
void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
Definition: costsize.c:983
int effective_cache_size
Definition: costsize.c:112
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SubLinkType subLinkType
Definition: primnodes.h:661
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:714
ParamPathInfo * param_info
Definition: relation.h:897
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:508
#define MemSet(start, val, len)
Definition: c.h:853
Relids left_relids
Definition: relation.h:1664
AttrNumber varattno
Definition: primnodes.h:146
void cost_valuesscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1288
static double page_size(double tuples, int width)
Definition: costsize.c:4791
void cost_ctescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1338
List * list_concat(List *list1, List *list2)
Definition: list.c:321
double parallel_setup_cost
Definition: costsize.c:110
uint32 BlockNumber
Definition: block.h:31
static List * extract_nonindex_conditions(List *qual_clauses, List *indexquals)
Definition: costsize.c:678
Definition: nodes.h:73
double Selectivity
Definition: nodes.h:631
void ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, int *numbuckets, int *numbatches, int *num_skew_mcvs)
Definition: nodeHash.c:404
QualCost transCost
Definition: relation.h:62
float4 get_func_cost(Oid funcid)
Definition: lsyscache.c:1609
Selectivity bitmapselectivity
Definition: relation.h:1016
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
bool enable_seqscan
Definition: costsize.c:118
List * fkey_list
Definition: relation.h:255
#define DEFAULT_EFFECTIVE_CACHE_SIZE
Definition: cost.h:32
#define OidIsValid(objectId)
Definition: c.h:534
#define DEFAULT_CPU_OPERATOR_COST
Definition: cost.h:28
Cost inner_rescan_run_cost
Definition: relation.h:2079
List * values_lists
Definition: parsenodes.h:937
#define lsecond(l)
Definition: pg_list.h:114
Relids syn_lefthand
Definition: relation.h:1809
int pk_strategy
Definition: relation.h:793
Cost startup
Definition: relation.h:45
double allvisfrac
Definition: relation.h:530
signed int int32
Definition: c.h:253
List * bitmapquals
Definition: relation.h:1015
JoinType
Definition: nodes.h:665
List * targetList
Definition: parsenodes.h:131
struct RelOptInfo ** simple_rel_array
Definition: relation.h:176
List * bitmapquals
Definition: relation.h:1028
Definition: type.h:90
BlockNumber pages
Definition: relation.h:593
NodeTag pathtype
Definition: relation.h:892
Relids syn_righthand
Definition: relation.h:1810
PlannerInfo * subroot
Definition: relation.h:531
void final_cost_mergejoin(PlannerInfo *root, MergePath *path, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo)
Definition: costsize.c:2390
bool enable_sort
Definition: costsize.c:123
double random_page_cost
Definition: costsize.c:105
void cost_seqscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:196
static Selectivity get_foreign_key_join_selectivity(PlannerInfo *root, Relids outer_relids, Relids inner_relids, SpecialJoinInfo *sjinfo, List **restrictlist)
Definition: costsize.c:4067
Cost per_tuple
Definition: relation.h:46
List * indexquals
Definition: relation.h:974
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2078
static void set_rel_width(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4544
RelOptInfo * rel
Definition: relation.h:590
bool resjunk
Definition: primnodes.h:1337
#define linitial(l)
Definition: pg_list.h:110
#define planner_rt_fetch(rti, root)
Definition: relation.h:320
List * make_ands_implicit(Expr *clause)
Definition: clauses.c:377
bool pk_nulls_first
Definition: relation.h:794
#define ERROR
Definition: elog.h:43
Expr * phexpr
Definition: relation.h:1740
Oid vartype
Definition: primnodes.h:148
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3199
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
Cost indextotalcost
Definition: relation.h:979
Cost startup_cost
Definition: relation.h:906
Cost disable_cost
Definition: costsize.c:114
List * semi_rhs_exprs
Definition: relation.h:1818
void cost_subqueryscan(SubqueryScanPath *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1178
#define DEFAULT_RANDOM_PAGE_COST
Definition: cost.h:25
bool semi_can_hash
Definition: relation.h:1816
List * joinrestrictinfo
Definition: relation.h:1219
EquivalenceClass * parent_ec
Definition: relation.h:1671
RelOptInfo * parent
Definition: relation.h:894
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:870
static const uint32 T[65]
Definition: md5.c:101
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:3876
struct Path * cheapest_total_path
Definition: relation.h:508
Selectivity rightstartsel
Definition: relation.h:1719
Selectivity indexselectivity
Definition: relation.h:980
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:483
Cost startup_cost
Definition: plannodes.h:111
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
Definition: placeholder.c:69
AttrNumber resno
Definition: primnodes.h:1331
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: costsize.c:1691
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
static double get_parallel_divisor(Path *path)
Definition: costsize.c:4801
Relids relids
Definition: relation.h:490
double cpu_operator_cost
Definition: costsize.c:108
Path * subpath
Definition: relation.h:1202
Oid winfnoid
Definition: primnodes.h:333
Expr * arg
Definition: primnodes.h:787
void initial_cost_hashjoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *hashclauses, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:2686
double total_table_pages
Definition: relation.h:284
Selectivity bitmapselectivity
Definition: relation.h:1029
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:72
int simple_rel_array_size
Definition: relation.h:177
double rint(double x)
Definition: rint.c:22
void cost_samplescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:273
#define lnext(lc)
Definition: pg_list.h:105
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2567
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:508
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:178
Index relid
Definition: relation.h:518
int32 get_relation_data_width(Oid relid, int32 *attr_widths)
Definition: plancat.c:1103
List * lappend(List *list, void *datum)
Definition: list.c:128
bool enable_bitmapscan
Definition: costsize.c:121
Expr * clause
Definition: relation.h:1637
struct EquivalenceClass * eclass[INDEX_MAX_KEYS]
Definition: relation.h:662
Index varno
Definition: primnodes.h:144
static double relation_byte_size(double tuples, int width)
Definition: costsize.c:4780
List * exprs
Definition: relation.h:824
List * indrestrictinfo
Definition: relation.h:615
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Path * outerjoinpath
Definition: relation.h:1216
void cost_index(IndexPath *path, PlannerInfo *root, double loop_count, bool partial_path)
Definition: costsize.c:394
void initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *mergeclauses, Path *outer_path, Path *inner_path, List *outersortkeys, List *innersortkeys, SpecialJoinInfo *sjinfo)
Definition: costsize.c:2167
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:604
bool delay_upper_joins
Definition: relation.h:1813
void final_cost_hashjoin(PlannerInfo *root, HashPath *path, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:2780
void cost_recursive_union(Path *runion, Path *nrterm, Path *rterm)
Definition: costsize.c:1382
bool self_reference
Definition: parsenodes.h:944
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:3765
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:3795
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: costsize.c:1462
Node * testexpr
Definition: primnodes.h:663
int work_mem
Definition: globals.c:112
RTEKind rtekind
Definition: relation.h:520
Cost per_call_cost
Definition: primnodes.h:689
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2296
double rows
Definition: relation.h:493
#define InvalidOid
Definition: postgres_ext.h:36
Cost finalCost
Definition: relation.h:63
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
Definition: costsize.c:3239
bool is_pushed_down
Definition: relation.h:1639
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
Cost total_cost
Definition: relation.h:907
void cost_material(Path *path, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition: costsize.c:1637
Selectivity left_bucketsize
Definition: relation.h:1698
double outer_skip_rows
Definition: relation.h:2084
List * pathkeys
Definition: relation.h:909
static void get_restriction_qual_cost(PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, QualCost *qpqual_cost)
Definition: costsize.c:3466
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
#define Max(x, y)
Definition: c.h:796
bool enable_mergejoin
Definition: costsize.c:127
void cost_merge_append(Path *path, PlannerInfo *root, List *pathkeys, int n_streams, Cost input_startup_cost, Cost input_total_cost, double tuples)
Definition: costsize.c:1586
Relids right_relids
Definition: relation.h:1665
#define LOG2(x)
Definition: costsize.c:101
BlockNumber pages
Definition: relation.h:528
Path path
Definition: relation.h:1212
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
static bool has_indexed_join_quals(NestPath *joinpath)
Definition: costsize.c:3616
List * functions
Definition: parsenodes.h:931
Expr * aggfilter
Definition: primnodes.h:338
void compute_semi_anti_join_factors(PlannerInfo *root, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:3506
double rows
Definition: relation.h:905
Expr * expr
Definition: primnodes.h:1330
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:2999
static double get_indexpath_pages(Path *bitmapqual)
Definition: costsize.c:803
JoinType jointype
Definition: relation.h:1811
EquivalenceClass * pk_eclass
Definition: relation.h:791
List * ppi_clauses
Definition: relation.h:855
QualCost cost
Definition: relation.h:826
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1852
static int list_length(const List *l)
Definition: pg_list.h:89
int tuplesort_merge_order(int64 allowedMem)
Definition: tuplesort.c:2289
#define MAXALIGN(LEN)
Definition: c.h:584
List * parParam
Definition: primnodes.h:685
#define DEFAULT_CPU_INDEX_TUPLE_COST
Definition: cost.h:27
List * innersortkeys
Definition: relation.h:1267
double cpu_tuple_cost
Definition: costsize.c:106
Oid pk_opfamily
Definition: relation.h:792
void cost_gather(GatherPath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, double *rows)
Definition: costsize.c:348
#define nodeTag(nodeptr)
Definition: nodes.h:513
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
double ppi_rows
Definition: relation.h:854
bool enable_hashjoin
Definition: costsize.c:128
RTEKind rtekind
Definition: parsenodes.h:882
bool enable_hashagg
Definition: costsize.c:124
Node * setOperations
Definition: parsenodes.h:154
int width
Definition: relation.h:827
Selectivity estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
Definition: selfuncs.c:3554
AggStrategy
Definition: nodes.h:727
void cost_functionscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1227
AttrNumber max_attr
Definition: relation.h:522
Selectivity match_count
Definition: relation.h:2033
Selectivity right_bucketsize
Definition: relation.h:1699
void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4434
void * palloc(Size size)
Definition: mcxt.c:891
JoinType jointype
Definition: relation.h:1214
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1622
List * semi_operators
Definition: relation.h:1817
#define DEFAULT_PARALLEL_TUPLE_COST
Definition: cost.h:29
void list_free(List *list)
Definition: list.c:1133
static double calc_joinrel_size_estimate(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, double outer_rows, double inner_rows, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:3916
int i
Cost total_cost
Definition: plannodes.h:112
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:561
void cost_bitmap_or_node(BitmapOrPath *path, PlannerInfo *root)
Definition: costsize.c:1027
JoinPath jpath
Definition: relation.h:1264
bool useHashTable
Definition: primnodes.h:675
bool parallel_aware
Definition: relation.h:899
List * path_hashclauses
Definition: relation.h:1283
Cost startup_cost
Definition: primnodes.h:688
MemoryContext planner_cxt
Definition: relation.h:282
void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4396
#define elog
Definition: elog.h:219
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:92
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:219
void(* amcostestimate)()
Definition: relation.h:634
int32 get_attavgwidth(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:2796
#define DEFAULT_SEQ_PAGE_COST
Definition: cost.h:24
Path path
Definition: relation.h:1201
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:986
double inner_skip_rows
Definition: relation.h:2085
int32 * attr_widths
Definition: relation.h:524
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4509
double clamp_row_est(double nrows)
Definition: costsize.c:172
double seq_page_cost
Definition: costsize.c:104
int max_parallel_workers_per_gather
Definition: costsize.c:116
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
struct PathTarget * reltarget
Definition: relation.h:501
Relids min_lefthand
Definition: relation.h:1807
struct TableSampleClause * tablesample
Definition: parsenodes.h:895
int16 AttrNumber
Definition: attnum.h:21
bool enable_indexscan
Definition: costsize.c:119
QualCost baserestrictcost
Definition: relation.h:546
List * rinfos[INDEX_MAX_KEYS]
Definition: relation.h:664
double cpu_index_tuple_cost
Definition: costsize.c:107
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1633
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
bool enable_indexonlyscan
Definition: costsize.c:120
Definition: relation.h:888
Selectivity rightendsel
Definition: relation.h:1720
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:738
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:4830
double Cost
Definition: nodes.h:632
List * scansel_cache
Definition: relation.h:1689
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
Selectivity leftstartsel
Definition: relation.h:1717
#define lfirst_oid(lc)
Definition: pg_list.h:108
bool enable_material
Definition: costsize.c:126
double parallel_tuple_cost
Definition: costsize.c:109
int32 vartypmod
Definition: primnodes.h:149
AttrNumber min_attr
Definition: relation.h:521
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:940