diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 05686d0194..efb562c9f7 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -123,6 +123,7 @@ double cpu_index_tuple_cost = DEFAULT_CPU_INDEX_TUPLE_COST; double cpu_operator_cost = DEFAULT_CPU_OPERATOR_COST; double parallel_tuple_cost = DEFAULT_PARALLEL_TUPLE_COST; double parallel_setup_cost = DEFAULT_PARALLEL_SETUP_COST; +double resultcache_cost_factor = DEFAULT_RESULTCACHE_COST_FACTOR; int effective_cache_size = DEFAULT_EFFECTIVE_CACHE_SIZE; @@ -2541,6 +2542,13 @@ cost_resultcache_rescan(PlannerInfo *root, ResultCachePath *rcpath, */ startup_cost += cpu_tuple_cost; + /* + * We multiply the costs by resultcache_cost_factor to control the + * aggressiveness of result cache. + */ + startup_cost *= resultcache_cost_factor; + total_cost *= resultcache_cost_factor; + *rescan_startup_cost = startup_cost; *rescan_total_cost = total_cost; } diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index b248b038e0..1dfcc820bf 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -1618,9 +1618,14 @@ create_resultcache_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, * Add a small additional charge for caching the first entry. All the * harder calculations for rescans are performed in * cost_resultcache_rescan(). + * + * We multiply the costs by resultcache_cost_factor to control the + * aggressiveness of result cache. */ - pathnode->path.startup_cost = subpath->startup_cost + cpu_tuple_cost; - pathnode->path.total_cost = subpath->total_cost + cpu_tuple_cost; + pathnode->path.startup_cost = + (subpath->startup_cost + cpu_tuple_cost) * resultcache_cost_factor; + pathnode->path.total_cost = + (subpath->total_cost + cpu_tuple_cost) * resultcache_cost_factor; pathnode->path.rows = subpath->rows; return pathnode; diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index c9c9da85f3..c319551a2d 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -3787,6 +3787,17 @@ static struct config_real ConfigureNamesReal[] = NULL, NULL, NULL }, + { + {"resultcache_cost_factor", PGC_SUSET, QUERY_TUNING_COST, + gettext_noop("Controls the aggressiveness of result cache."), + gettext_noop("This factor will be multiplied by the cost of " + "the Result Cache node.") + }, + &resultcache_cost_factor, + DEFAULT_RESULTCACHE_COST_FACTOR, 0.0, DBL_MAX, + NULL, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL, NULL diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index 0fe60d82e4..903d1fb607 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -28,6 +28,7 @@ #define DEFAULT_CPU_OPERATOR_COST 0.0025 #define DEFAULT_PARALLEL_TUPLE_COST 0.1 #define DEFAULT_PARALLEL_SETUP_COST 1000.0 +#define DEFAULT_RESULTCACHE_COST_FACTOR 1.0 #define DEFAULT_EFFECTIVE_CACHE_SIZE 524288 /* measured in pages */ @@ -47,6 +48,7 @@ typedef enum /* parameter variables and flags (see also optimizer.h) */ extern PGDLLIMPORT Cost disable_cost; extern PGDLLIMPORT int max_parallel_workers_per_gather; +extern PGDLLIMPORT double resultcache_cost_factor; extern PGDLLIMPORT bool enable_seqscan; extern PGDLLIMPORT bool enable_indexscan; extern PGDLLIMPORT bool enable_indexonlyscan;