@@ -7464,6 +7464,8 @@ genericcostestimate(PlannerInfo *root,
74647464 GenericCosts * costs )
74657465{
74667466 IndexOptInfo * index = path -> indexinfo ;
7467+ RelOptInfo * baserel = index -> rel ;
7468+ bool indexonly = (path -> path .pathtype == T_IndexOnlyScan );
74677469 List * indexQuals = get_quals_from_indexclauses (path -> indexclauses );
74687470 List * indexOrderBys = path -> indexorderbys ;
74697471 Cost indexStartupCost ;
@@ -7472,7 +7474,9 @@ genericcostestimate(PlannerInfo *root,
74727474 double indexCorrelation ;
74737475 double numIndexPages ;
74747476 double numIndexTuples ;
7477+ double spc_seq_page_cost ;
74757478 double spc_random_page_cost ;
7479+ double index_random_page_cost ;
74767480 double num_sa_scans ;
74777481 double num_outer_scans ;
74787482 double num_scans ;
@@ -7570,7 +7574,23 @@ genericcostestimate(PlannerInfo *root,
75707574 /* fetch estimated page cost for tablespace containing index */
75717575 get_tablespace_page_costs (index -> reltablespace ,
75727576 & spc_random_page_cost ,
7573- NULL );
7577+ & spc_seq_page_cost );
7578+
7579+ /* If it is an index only scan, random page cost should be incurred for
7580+ * the percentage of pages that need to be fetched from disk for the
7581+ * relation. Considering that, random page cost should be adjusted such
7582+ * that when no tuples are to be fetched from the table, we end up with a
7583+ * sequential page cost, otherwise, we hover betwen that and random
7584+ * page cost for the tablespace.
7585+ *
7586+ * Otherwise, we default to the random page cost for table space.
7587+ */
7588+ if (indexonly )
7589+ index_random_page_cost = Min (spc_seq_page_cost +
7590+ spc_random_page_cost * (1.0 - baserel -> allvisfrac ),
7591+ spc_random_page_cost );
7592+ else
7593+ index_random_page_cost = spc_random_page_cost ;
75747594
75757595 /*
75767596 * Now compute the disk access costs.
@@ -7610,16 +7630,16 @@ genericcostestimate(PlannerInfo *root,
76107630 * share for each outer scan. (Don't pro-rate for ScalarArrayOpExpr,
76117631 * since that's internal to the indexscan.)
76127632 */
7613- indexTotalCost = (pages_fetched * spc_random_page_cost )
7633+ indexTotalCost = (pages_fetched * index_random_page_cost )
76147634 / num_outer_scans ;
76157635 }
76167636 else
76177637 {
76187638 /*
7619- * For a single index scan, we just charge spc_random_page_cost per
7639+ * For a single index scan, we just charge random page cost per
76207640 * page touched.
76217641 */
7622- indexTotalCost = numIndexPages * spc_random_page_cost ;
7642+ indexTotalCost = numIndexPages * index_random_page_cost ;
76237643 }
76247644
76257645 /*
0 commit comments