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