103 #define LOG2(x) (log(x) / 0.693147180559945)
110 #define APPEND_CPU_COST_MULTIPLIER 0.5
118 #define MAXIMUM_ROWCOUNT 1e100
168 Cost *rescan_startup_cost,
Cost *rescan_total_cost);
188 List **restrictlist);
190 int parallel_workers);
194 static double page_size(
double tuples,
int width);
213 else if (nrows <= 1.0)
244 return (
x < (
double) LONG_MAX) ? (long)
x : LONG_MAX;
259 Cost startup_cost = 0;
262 double spc_seq_page_cost;
287 disk_run_cost = spc_seq_page_cost * baserel->
pages;
292 startup_cost += qpqual_cost.
startup;
294 cpu_run_cost = cpu_per_tuple * baserel->
tuples;
296 startup_cost += path->pathtarget->cost.startup;
297 cpu_run_cost += path->pathtarget->cost.per_tuple * path->
rows;
305 cpu_run_cost /= parallel_divisor;
322 path->
total_cost = startup_cost + cpu_run_cost + disk_run_cost;
336 Cost startup_cost = 0;
341 double spc_seq_page_cost,
342 spc_random_page_cost,
363 &spc_random_page_cost,
368 spc_random_page_cost : spc_seq_page_cost;
374 run_cost += spc_page_cost * baserel->
pages;
386 startup_cost += qpqual_cost.
startup;
388 run_cost += cpu_per_tuple * baserel->
tuples;
390 startup_cost += path->pathtarget->cost.startup;
391 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
412 Cost startup_cost = 0;
448 Cost input_startup_cost,
Cost input_total_cost,
451 Cost startup_cost = 0;
453 Cost comparison_cost;
481 startup_cost += comparison_cost * N * logN;
484 run_cost += path->
path.
rows * comparison_cost * logN;
499 path->
path.
total_cost = (startup_cost + run_cost + input_total_cost);
526 bool indexonly = (path->
path.
pathtype == T_IndexOnlyScan);
529 Cost startup_cost = 0;
531 Cost cpu_run_cost = 0;
532 Cost indexStartupCost;
535 double indexCorrelation,
537 double spc_seq_page_cost,
538 spc_random_page_cost;
543 double tuples_fetched;
544 double pages_fetched;
545 double rand_heap_pages;
561 if (path->
path.param_info)
590 amcostestimate(root, path, loop_count,
591 &indexStartupCost, &indexTotalCost,
592 &indexSelectivity, &indexCorrelation,
604 startup_cost += indexStartupCost;
605 run_cost += indexTotalCost - indexStartupCost;
612 &spc_random_page_cost,
654 (
double)
index->pages,
658 pages_fetched = ceil(pages_fetched * (1.0 - baserel->
allvisfrac));
660 rand_heap_pages = pages_fetched;
662 max_IO_cost = (pages_fetched * spc_random_page_cost) / loop_count;
674 pages_fetched = ceil(indexSelectivity * (
double) baserel->
pages);
678 (
double)
index->pages,
682 pages_fetched = ceil(pages_fetched * (1.0 - baserel->
allvisfrac));
684 min_IO_cost = (pages_fetched * spc_random_page_cost) / loop_count;
694 (
double)
index->pages,
698 pages_fetched = ceil(pages_fetched * (1.0 - baserel->
allvisfrac));
700 rand_heap_pages = pages_fetched;
703 max_IO_cost = pages_fetched * spc_random_page_cost;
706 pages_fetched = ceil(indexSelectivity * (
double) baserel->
pages);
709 pages_fetched = ceil(pages_fetched * (1.0 - baserel->
allvisfrac));
711 if (pages_fetched > 0)
713 min_IO_cost = spc_random_page_cost;
714 if (pages_fetched > 1)
715 min_IO_cost += (pages_fetched - 1) * spc_seq_page_cost;
729 rand_heap_pages = -1;
757 csquared = indexCorrelation * indexCorrelation;
759 run_cost += max_IO_cost + csquared * (min_IO_cost - max_IO_cost);
769 startup_cost += qpqual_cost.
startup;
772 cpu_run_cost += cpu_per_tuple * tuples_fetched;
775 startup_cost += path->
path.pathtarget->cost.startup;
776 cpu_run_cost += path->
path.pathtarget->cost.per_tuple * path->
path.
rows;
786 cpu_run_cost /= parallel_divisor;
789 run_cost += cpu_run_cost;
817 foreach(lc, qual_clauses)
821 if (rinfo->pseudoconstant)
826 result =
lappend(result, rinfo);
873 double pages_fetched;
879 T = (pages > 1) ? (
double) pages : 1.0;
883 total_pages =
Max(total_pages, 1.0);
899 (2.0 *
T * tuples_fetched) / (2.0 *
T + tuples_fetched);
900 if (pages_fetched >=
T)
903 pages_fetched = ceil(pages_fetched);
909 lim = (2.0 *
T *
b) / (2.0 *
T -
b);
910 if (tuples_fetched <= lim)
913 (2.0 *
T * tuples_fetched) / (2.0 *
T + tuples_fetched);
918 b + (tuples_fetched - lim) * (
T -
b) /
T;
920 pages_fetched = ceil(pages_fetched);
922 return pages_fetched;
987 Path *bitmapqual,
double loop_count)
989 Cost startup_cost = 0;
996 double tuples_fetched;
997 double pages_fetched;
998 double spc_seq_page_cost,
999 spc_random_page_cost;
1017 loop_count, &indexTotalCost,
1020 startup_cost += indexTotalCost;
1021 T = (baserel->
pages > 1) ? (
double) baserel->
pages : 1.0;
1025 &spc_random_page_cost,
1026 &spc_seq_page_cost);
1035 if (pages_fetched >= 2.0)
1036 cost_per_page = spc_random_page_cost -
1037 (spc_random_page_cost - spc_seq_page_cost)
1038 * sqrt(pages_fetched /
T);
1040 cost_per_page = spc_random_page_cost;
1042 run_cost += pages_fetched * cost_per_page;
1055 startup_cost += qpqual_cost.
startup;
1057 cpu_run_cost = cpu_per_tuple * tuples_fetched;
1065 cpu_run_cost /= parallel_divisor;
1071 run_cost += cpu_run_cost;
1074 startup_cost += path->pathtarget->cost.startup;
1075 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1090 *cost = ((
IndexPath *) path)->indextotalcost;
1091 *selec = ((
IndexPath *) path)->indexselectivity;
1156 totalCost += subCost;
1201 totalCost += subCost;
1224 Cost startup_cost = 0;
1226 bool isCurrentOf =
false;
1232 double spc_random_page_cost;
1246 foreach(l, tidquals)
1296 &spc_random_page_cost,
1300 run_cost += spc_random_page_cost * ntuples;
1309 run_cost += cpu_per_tuple * ntuples;
1312 startup_cost += path->pathtarget->cost.startup;
1313 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1335 Cost startup_cost = 0;
1342 double spc_random_page_cost;
1343 double spc_seq_page_cost;
1358 pages = ceil(selectivity * baserel->
pages);
1371 ntuples = selectivity * baserel->
tuples;
1372 nseqpages = pages - 1.0;
1385 &spc_random_page_cost,
1386 &spc_seq_page_cost);
1389 run_cost += spc_random_page_cost + spc_seq_page_cost * nseqpages;
1404 run_cost += cpu_per_tuple * ntuples;
1407 startup_cost += path->pathtarget->cost.startup;
1408 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1425 bool trivial_pathtarget)
1478 if (qpquals ==
NIL && trivial_pathtarget)
1483 startup_cost = qpqual_cost.
startup;
1488 startup_cost += path->
path.pathtarget->cost.startup;
1489 run_cost += path->
path.pathtarget->cost.per_tuple * path->
path.
rows;
1506 Cost startup_cost = 0;
1544 startup_cost += qpqual_cost.
startup;
1546 run_cost += cpu_per_tuple * baserel->
tuples;
1549 startup_cost += path->pathtarget->cost.startup;
1550 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1567 Cost startup_cost = 0;
1600 startup_cost += qpqual_cost.
startup;
1602 run_cost += cpu_per_tuple * baserel->
tuples;
1605 startup_cost += path->pathtarget->cost.startup;
1606 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1623 Cost startup_cost = 0;
1647 startup_cost += qpqual_cost.
startup;
1649 run_cost += cpu_per_tuple * baserel->
tuples;
1652 startup_cost += path->pathtarget->cost.startup;
1653 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1673 Cost startup_cost = 0;
1694 startup_cost += qpqual_cost.
startup;
1696 run_cost += cpu_per_tuple * baserel->
tuples;
1699 startup_cost += path->pathtarget->cost.startup;
1700 run_cost += path->pathtarget->cost.per_tuple * path->
rows;
1714 Cost startup_cost = 0;
1735 startup_cost += qpqual_cost.
startup;
1737 run_cost += cpu_per_tuple * baserel->
tuples;
1751 Cost startup_cost = 0;
1769 startup_cost += qpqual_cost.
startup;
1771 run_cost += cpu_per_tuple * baserel->
tuples;
1794 total_rows = nrterm->
rows;
1803 total_rows += 10 * rterm->
rows;
1814 runion->
rows = total_rows;
1815 runion->pathtarget->width =
Max(nrterm->pathtarget->width,
1816 rterm->pathtarget->width);
1857 double tuples,
int width,
1858 Cost comparison_cost,
int sort_mem,
1859 double limit_tuples)
1862 double output_bytes;
1863 double output_tuples;
1864 long sort_mem_bytes = sort_mem * 1024L;
1877 if (limit_tuples > 0 && limit_tuples < tuples)
1879 output_tuples = limit_tuples;
1884 output_tuples = tuples;
1885 output_bytes = input_bytes;
1888 if (output_bytes > sort_mem_bytes)
1893 double npages = ceil(input_bytes / BLCKSZ);
1894 double nruns = input_bytes / sort_mem_bytes;
1897 double npageaccesses;
1904 *startup_cost = comparison_cost * tuples *
LOG2(tuples);
1909 if (nruns > mergeorder)
1910 log_runs = ceil(log(nruns) / log(mergeorder));
1913 npageaccesses = 2.0 * npages * log_runs;
1915 *startup_cost += npageaccesses *
1918 else if (tuples > 2 * output_tuples || input_bytes > sort_mem_bytes)
1926 *startup_cost = comparison_cost * tuples *
LOG2(2.0 * output_tuples);
1931 *startup_cost = comparison_cost * tuples *
LOG2(tuples);
1960 Cost input_startup_cost,
Cost input_total_cost,
1961 double input_tuples,
int width,
Cost comparison_cost,
int sort_mem,
1962 double limit_tuples)
1966 input_run_cost = input_total_cost - input_startup_cost;
1967 double group_tuples,
1969 Cost group_startup_cost,
1971 group_input_run_cost;
1974 bool unknown_varno =
false;
1982 if (input_tuples < 2.0)
2010 foreach(l, pathkeys)
2022 unknown_varno =
true;
2038 group_tuples = input_tuples / input_groups;
2039 group_input_run_cost = input_run_cost / input_groups;
2046 group_tuples, width, comparison_cost, sort_mem,
2053 startup_cost = group_startup_cost + input_startup_cost +
2054 group_input_run_cost;
2062 run_cost = group_run_cost + (group_run_cost + group_startup_cost) *
2063 (input_groups - 1) + group_input_run_cost * (input_groups - 1);
2078 path->
rows = input_tuples;
2097 List *pathkeys,
Cost input_cost,
double tuples,
int width,
2098 Cost comparison_cost,
int sort_mem,
2099 double limit_tuples)
2107 comparison_cost, sort_mem,
2113 startup_cost += input_cost;
2115 path->
rows = tuples;
2144 arrlen =
Min(parallel_workers, numpaths);
2149 foreach(cell, subpaths)
2153 if (path_index == arrlen)
2155 costarr[path_index++] =
subpath->total_cost;
2162 min_index = arrlen - 1;
2173 if (path_index++ == numpaths)
2176 costarr[min_index] +=
subpath->total_cost;
2180 for (
int i = 0;
i < arrlen;
i++)
2182 if (costarr[
i] < costarr[min_index])
2189 for (
int i = 0;
i < arrlen;
i++)
2191 if (costarr[
i] > costarr[max_index])
2195 return costarr[max_index];
2218 if (pathkeys ==
NIL)
2308 else if (i < apath->path.parallel_workers)
2319 if (i < apath->first_partial_path)
2323 double subpath_parallel_divisor;
2377 List *pathkeys,
int n_streams,
2378 Cost input_startup_cost,
Cost input_total_cost,
2381 Cost startup_cost = 0;
2383 Cost comparison_cost;
2390 N = (n_streams < 2) ? 2.0 : (
double) n_streams;
2397 startup_cost += comparison_cost * N * logN;
2400 run_cost += tuples * comparison_cost * logN;
2408 path->
startup_cost = startup_cost + input_startup_cost;
2409 path->
total_cost = startup_cost + run_cost + input_total_cost;
2426 Cost input_startup_cost,
Cost input_total_cost,
2427 double tuples,
int width)
2429 Cost startup_cost = input_startup_cost;
2430 Cost run_cost = input_total_cost - input_startup_cost;
2432 long work_mem_bytes =
work_mem * 1024L;
2434 path->
rows = tuples;
2456 if (nbytes > work_mem_bytes)
2458 double npages = ceil(nbytes / BLCKSZ);
2482 Cost *rescan_startup_cost,
Cost *rescan_total_cost)
2489 double calls = mpath->
calls;
2490 int width = mpath->
subpath->pathtarget->width;
2492 double hash_mem_bytes;
2493 double est_entry_bytes;
2494 double est_cache_entries;
2518 est_cache_entries = floor(hash_mem_bytes / est_entry_bytes);
2553 evict_ratio = 1.0 -
Min(est_cache_entries, ndistinct) / ndistinct;
2561 hit_ratio = ((calls - ndistinct) / calls) *
2562 (est_cache_entries /
Max(ndistinct, est_cache_entries));
2564 Assert(hit_ratio >= 0 && hit_ratio <= 1.0);
2598 startup_cost = input_startup_cost * (1.0 - hit_ratio);
2606 *rescan_startup_cost = startup_cost;
2607 *rescan_total_cost = total_cost;
2624 int numGroupCols,
double numGroups,
2626 Cost input_startup_cost,
Cost input_total_cost,
2627 double input_tuples,
double input_width)
2629 double output_tuples;
2635 if (aggcosts == NULL)
2639 aggcosts = &dummy_aggcosts;
2666 startup_cost = input_total_cost;
2678 startup_cost = input_startup_cost;
2679 total_cost = input_total_cost;
2692 output_tuples = numGroups;
2697 startup_cost = input_total_cost;
2706 total_cost = startup_cost;
2710 output_tuples = numGroups;
2729 double pages_written = 0.0;
2730 double pages_read = 0.0;
2732 double hashentrysize;
2735 uint64 ngroups_limit;
2748 &ngroups_limit, &num_partitions);
2750 nbatches =
Max((numGroups * hashentrysize) / mem_limit,
2751 numGroups / ngroups_limit);
2753 nbatches =
Max(ceil(nbatches), 1.0);
2754 num_partitions =
Max(num_partitions, 2);
2761 depth = ceil(log(nbatches) / log(num_partitions));
2768 pages_written = pages_read = pages * depth;
2775 pages_written *= 2.0;
2783 startup_cost += spill_cost;
2784 total_cost += spill_cost;
2796 startup_cost += qual_cost.
startup;
2807 path->
rows = output_tuples;
2821 List *windowFuncs,
int numPartCols,
int numOrderCols,
2822 Cost input_startup_cost,
Cost input_total_cost,
2823 double input_tuples)
2829 startup_cost = input_startup_cost;
2830 total_cost = input_total_cost;
2841 foreach(lc, windowFuncs)
2850 startup_cost += argcosts.
startup;
2855 startup_cost += argcosts.
startup;
2863 startup_cost += argcosts.
startup;
2866 total_cost += wfunccost * input_tuples;
2880 path->
rows = input_tuples;
2895 int numGroupCols,
double numGroups,
2897 Cost input_startup_cost,
Cost input_total_cost,
2898 double input_tuples)
2900 double output_tuples;
2904 output_tuples = numGroups;
2905 startup_cost = input_startup_cost;
2906 total_cost = input_total_cost;
2923 startup_cost += qual_cost.
startup;
2934 path->
rows = output_tuples;
2966 Path *outer_path,
Path *inner_path,
2969 Cost startup_cost = 0;
2971 double outer_path_rows = outer_path->
rows;
2972 Cost inner_rescan_start_cost;
2973 Cost inner_rescan_total_cost;
2974 Cost inner_run_cost;
2975 Cost inner_rescan_run_cost;
2979 &inner_rescan_start_cost,
2980 &inner_rescan_total_cost);
2992 if (outer_path_rows > 1)
2993 run_cost += (outer_path_rows - 1) * inner_rescan_start_cost;
2996 inner_rescan_run_cost = inner_rescan_total_cost - inner_rescan_start_cost;
3016 run_cost += inner_run_cost;
3017 if (outer_path_rows > 1)
3018 run_cost += (outer_path_rows - 1) * inner_rescan_run_cost;
3025 workspace->
total_cost = startup_cost + run_cost;
3045 double outer_path_rows = outer_path->
rows;
3046 double inner_path_rows = inner_path->
rows;
3054 if (outer_path_rows <= 0)
3055 outer_path_rows = 1;
3056 if (inner_path_rows <= 0)
3057 inner_path_rows = 1;
3059 if (path->
jpath.path.param_info)
3060 path->
jpath.path.rows = path->
jpath.path.param_info->ppi_rows;
3062 path->
jpath.path.rows = path->
jpath.path.parent->rows;
3065 if (path->
jpath.path.parallel_workers > 0)
3069 path->
jpath.path.rows =
3092 double outer_matched_rows;
3093 double outer_unmatched_rows;
3106 outer_unmatched_rows = outer_path_rows - outer_matched_rows;
3113 ntuples = outer_matched_rows * inner_path_rows * inner_scan_frac;
3140 run_cost += inner_run_cost * inner_scan_frac;
3141 if (outer_matched_rows > 1)
3142 run_cost += (outer_matched_rows - 1) * inner_rescan_run_cost * inner_scan_frac;
3150 run_cost += outer_unmatched_rows *
3151 inner_rescan_run_cost / inner_path_rows;
3174 ntuples += outer_unmatched_rows * inner_path_rows;
3177 run_cost += inner_run_cost;
3178 if (outer_unmatched_rows >= 1)
3179 outer_unmatched_rows -= 1;
3181 outer_matched_rows -= 1;
3184 if (outer_matched_rows > 0)
3185 run_cost += outer_matched_rows * inner_rescan_run_cost * inner_scan_frac;
3188 if (outer_unmatched_rows > 0)
3189 run_cost += outer_unmatched_rows * inner_rescan_run_cost;
3197 ntuples = outer_path_rows * inner_path_rows;
3202 startup_cost += restrict_qual_cost.
startup;
3204 run_cost += cpu_per_tuple * ntuples;
3207 startup_cost += path->
jpath.path.pathtarget->cost.startup;
3208 run_cost += path->
jpath.path.pathtarget->cost.per_tuple * path->
jpath.path.rows;
3210 path->
jpath.path.startup_cost = startup_cost;
3211 path->
jpath.path.total_cost = startup_cost + run_cost;
3248 Path *outer_path,
Path *inner_path,
3249 List *outersortkeys,
List *innersortkeys,
3252 Cost startup_cost = 0;
3254 double outer_path_rows = outer_path->
rows;
3255 double inner_path_rows = inner_path->
rows;
3256 Cost inner_run_cost;
3268 if (outer_path_rows <= 0)
3269 outer_path_rows = 1;
3270 if (inner_path_rows <= 0)
3271 inner_path_rows = 1;
3284 if (mergeclauses && jointype !=
JOIN_FULL)
3294 opathkeys = outersortkeys ? outersortkeys : outer_path->
pathkeys;
3295 ipathkeys = innersortkeys ? innersortkeys : inner_path->
pathkeys;
3302 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
3305 elog(
ERROR,
"left and right pathkeys do not match in mergejoin");
3311 outer_path->parent->relids))
3330 outerstartsel = 0.0;
3335 innerstartsel = 0.0;
3342 outerstartsel = innerstartsel = 0.0;
3343 outerendsel = innerendsel = 1.0;
3350 outer_skip_rows = rint(outer_path_rows * outerstartsel);
3351 inner_skip_rows = rint(inner_path_rows * innerstartsel);
3355 Assert(outer_skip_rows <= outer_rows);
3356 Assert(inner_skip_rows <= inner_rows);
3363 outerstartsel = outer_skip_rows / outer_path_rows;
3364 innerstartsel = inner_skip_rows / inner_path_rows;
3365 outerendsel = outer_rows / outer_path_rows;
3366 innerendsel = inner_rows / inner_path_rows;
3368 Assert(outerstartsel <= outerendsel);
3369 Assert(innerstartsel <= innerendsel);
3380 outer_path->pathtarget->width,
3388 * (outerendsel - outerstartsel);
3396 * (outerendsel - outerstartsel);
3406 inner_path->pathtarget->width,
3414 * (innerendsel - innerstartsel);
3422 * (innerendsel - innerstartsel);
3437 workspace->
total_cost = startup_cost + run_cost + inner_run_cost;
3481 double inner_path_rows = inner_path->
rows;
3496 double mergejointuples,
3501 if (inner_path_rows <= 0)
3502 inner_path_rows = 1;
3505 if (path->
jpath.path.param_info)
3506 path->
jpath.path.rows = path->
jpath.path.param_info->ppi_rows;
3508 path->
jpath.path.rows = path->
jpath.path.parent->rows;
3511 if (path->
jpath.path.parallel_workers > 0)
3515 path->
jpath.path.rows =
3584 rescannedtuples = 0;
3587 rescannedtuples = mergejointuples - inner_path_rows;
3589 if (rescannedtuples < 0)
3590 rescannedtuples = 0;
3598 rescanratio = 1.0 + (rescannedtuples / inner_rows);
3609 bare_inner_cost = inner_run_cost * rescanratio;
3624 mat_inner_cost = inner_run_cost +
3656 else if (innersortkeys ==
NIL &&
3673 inner_path->pathtarget->width) >
3681 run_cost += mat_inner_cost;
3683 run_cost += bare_inner_cost;
3692 startup_cost += merge_qual_cost.
startup;
3693 startup_cost += merge_qual_cost.
per_tuple *
3694 (outer_skip_rows + inner_skip_rows * rescanratio);
3696 ((outer_rows - outer_skip_rows) +
3697 (inner_rows - inner_skip_rows) * rescanratio);
3708 startup_cost += qp_qual_cost.
startup;
3710 run_cost += cpu_per_tuple * mergejointuples;
3713 startup_cost += path->
jpath.path.pathtarget->cost.startup;
3714 run_cost += path->
jpath.path.pathtarget->cost.per_tuple * path->
jpath.path.rows;
3716 path->
jpath.path.startup_cost = startup_cost;
3717 path->
jpath.path.total_cost = startup_cost + run_cost;
3735 foreach(lc, rinfo->scansel_cache)
3739 cache->
collation == pathkey->pk_eclass->ec_collation &&
3761 cache->
collation = pathkey->pk_eclass->ec_collation;
3769 rinfo->scansel_cache =
lappend(rinfo->scansel_cache, cache);
3806 Path *outer_path,
Path *inner_path,
3810 Cost startup_cost = 0;
3812 double outer_path_rows = outer_path->
rows;
3813 double inner_path_rows = inner_path->
rows;
3814 double inner_path_rows_total = inner_path_rows;
3819 size_t space_allowed;
3860 inner_path->pathtarget->width,
3878 double outerpages =
page_size(outer_path_rows,
3879 outer_path->pathtarget->width);
3880 double innerpages =
page_size(inner_path_rows,
3881 inner_path->pathtarget->width);
3891 workspace->
total_cost = startup_cost + run_cost;
3917 double outer_path_rows = outer_path->
rows;
3918 double inner_path_rows = inner_path->
rows;
3928 double hashjointuples;
3929 double virtualbuckets;
3935 if (path->
jpath.path.param_info)
3936 path->
jpath.path.rows = path->
jpath.path.param_info->ppi_rows;
3938 path->
jpath.path.rows = path->
jpath.path.parent->rows;
3941 if (path->
jpath.path.parallel_workers > 0)
3945 path->
jpath.path.rows =
3964 virtualbuckets = (double) numbuckets * (
double) numbatches;
3978 innerbucketsize = 1.0 / virtualbuckets;
3983 innerbucketsize = 1.0;
3985 foreach(hcl, hashclauses)
4000 inner_path->parent->relids))
4003 thisbucketsize = restrictinfo->right_bucketsize;
4004 if (thisbucketsize < 0)
4010 &restrictinfo->right_mcvfreq,
4011 &restrictinfo->right_bucketsize);
4012 thisbucketsize = restrictinfo->right_bucketsize;
4014 thismcvfreq = restrictinfo->right_mcvfreq;
4019 inner_path->parent->relids));
4021 thisbucketsize = restrictinfo->left_bucketsize;
4022 if (thisbucketsize < 0)
4028 &restrictinfo->left_mcvfreq,
4029 &restrictinfo->left_bucketsize);
4030 thisbucketsize = restrictinfo->left_bucketsize;
4032 thismcvfreq = restrictinfo->left_mcvfreq;
4035 if (innerbucketsize > thisbucketsize)
4036 innerbucketsize = thisbucketsize;
4037 if (innermcvfreq > thismcvfreq)
4038 innermcvfreq = thismcvfreq;
4069 double outer_matched_rows;
4087 startup_cost += hash_qual_cost.
startup;
4088 run_cost += hash_qual_cost.
per_tuple * outer_matched_rows *
4089 clamp_row_est(inner_path_rows * innerbucketsize * inner_scan_frac) * 0.5;
4105 (outer_path_rows - outer_matched_rows) *
4110 hashjointuples = outer_path_rows - outer_matched_rows;
4112 hashjointuples = outer_matched_rows;
4126 startup_cost += hash_qual_cost.
startup;
4127 run_cost += hash_qual_cost.
per_tuple * outer_path_rows *
4144 startup_cost += qp_qual_cost.
startup;
4146 run_cost += cpu_per_tuple * hashjointuples;
4149 startup_cost += path->
jpath.path.pathtarget->cost.startup;
4150 run_cost += path->
jpath.path.pathtarget->cost.per_tuple * path->
jpath.path.rows;
4152 path->
jpath.path.startup_cost = startup_cost;
4153 path->
jpath.path.total_cost = startup_cost + run_cost;
4203 Cost plan_run_cost =
plan->total_cost -
plan->startup_cost;
4214 sp_cost.
per_tuple += 0.50 * plan_run_cost;
4259 Cost *rescan_startup_cost,
4260 Cost *rescan_total_cost)
4264 case T_FunctionScan:
4273 *rescan_startup_cost = 0;
4282 if (((
HashPath *) path)->num_batches == 1)
4285 *rescan_startup_cost = 0;
4286 *rescan_total_cost = path->total_cost - path->startup_cost;
4291 *rescan_startup_cost = path->startup_cost;
4292 *rescan_total_cost = path->total_cost;
4296 case T_WorkTableScan:
4306 path->pathtarget->width);
4307 long work_mem_bytes =
work_mem * 1024L;
4309 if (nbytes > work_mem_bytes)
4312 double npages = ceil(nbytes / BLCKSZ);
4316 *rescan_startup_cost = 0;
4317 *rescan_total_cost = run_cost;
4333 path->pathtarget->width);
4334 long work_mem_bytes =
work_mem * 1024L;
4336 if (nbytes > work_mem_bytes)
4339 double npages = ceil(nbytes / BLCKSZ);
4343 *rescan_startup_cost = 0;
4344 *rescan_total_cost = run_cost;
4350 rescan_startup_cost, rescan_total_cost);
4353 *rescan_startup_cost = path->startup_cost;
4354 *rescan_total_cost = path->total_cost;
4375 context.
root = root;
4388 *cost = context.
total;
4400 context.
root = root;
4406 *cost = context.
total;
4425 if (rinfo->eval_cost.startup < 0)
4437 if (rinfo->orclause)
4446 if (rinfo->pseudoconstant)
4452 rinfo->eval_cost = locContext.
total;
4572 &iofunc, &typioparam);
4577 &iofunc, &typisvarlena);
4599 foreach(lc, rcexpr->opnos)
4623 elog(
ERROR,
"cannot handle unplanned sub-select");
4758 foreach(l, restrictlist)
4763 joinquals =
lappend(joinquals, rinfo);
4767 joinquals = restrictlist;
4781 norm_sjinfo.type = T_SpecialJoinInfo;
4822 avgmatch = nselec * innerrel->
rows / jselec;
4824 avgmatch =
Max(1.0, avgmatch);
4847 Relids joinrelids = joinpath->path.parent->relids;
4857 if (innerpath->param_info == NULL)
4864 case T_IndexOnlyScan:
4865 indexclauses = ((
IndexPath *) innerpath)->indexclauses;
4867 case T_BitmapHeapScan:
4873 indexclauses = ((
IndexPath *) bmqual)->indexclauses;
4895 foreach(lc, innerpath->param_info->ppi_clauses)
4900 innerpath->parent->relids,
4949 sjinfo.type = T_SpecialJoinInfo;
4976 tuples = selec * outer_tuples * inner_tuples;
5027 List *param_clauses)
5047 if (nrows > rel->
rows)
5111 List *restrict_clauses)
5133 if (nrows > rel->
rows)
5196 foreach(l, restrictlist)
5201 pushedquals =
lappend(pushedquals, rinfo);
5203 joinquals =
lappend(joinquals, rinfo);
5247 nrows = outer_rows * inner_rows * fkselec * jselec;
5251 nrows = outer_rows * inner_rows * fkselec * jselec;
5252 if (nrows < outer_rows)
5257 nrows = outer_rows * inner_rows * fkselec * jselec;
5258 if (nrows < outer_rows)
5260 if (nrows < inner_rows)
5265 nrows = outer_rows * fkselec * jselec;
5269 nrows = outer_rows * (1.0 - fkselec * jselec);
5274 elog(
ERROR,
"unrecognized join type: %d", (
int) jointype);
5302 List **restrictlist)
5306 List *worklist = *restrictlist;
5323 ref_is_outer =
false;
5326 ref_is_outer =
true;
5352 if (worklist == *restrictlist)
5356 foreach(cell, worklist)
5359 bool remove_it =
false;
5363 for (
i = 0;
i < fkinfo->
nkeys;
i++)
5365 if (rinfo->parent_ec)
5380 if (fkinfo->
eclass[
i] == rinfo->parent_ec)
5402 removedlist =
lappend(removedlist, rinfo);
5432 if (removedlist ==
NIL ||
5477 double ref_tuples =
Max(ref_rel->
tuples, 1.0);
5479 fkselec *= ref_rel->
rows / ref_tuples;
5489 double ref_tuples =
Max(ref_rel->
tuples, 1.0);
5491 fkselec *= 1.0 / ref_tuples;
5506 for (
int i = 0;
i < fkinfo->
nkeys;
i++)
5533 *restrictlist = worklist;
5576 int32 item_width = 0;
5607 Var *var = (
Var *) texpr;
5859 int32 tuple_width = 0;
5860 bool have_wholerow_var =
false;
5878 ((
Var *) node)->varno == rel->
relid)
5895 have_wholerow_var =
true;
5903 if (rel->attr_widths[ndx] > 0)
5905 tuple_width += rel->attr_widths[ndx];
5915 rel->attr_widths[ndx] = item_width;
5916 tuple_width += item_width;
5927 rel->attr_widths[ndx] = item_width;
5928 tuple_width += item_width;
5957 tuple_width += item_width;
5969 if (have_wholerow_var)
5985 wholerow_width += rel->attr_widths[
i - rel->
min_attr];
5988 rel->attr_widths[0 - rel->
min_attr] = wholerow_width;
5994 tuple_width += wholerow_width;
5997 Assert(tuple_width >= 0);
6016 int32 tuple_width = 0;