mirror of
https://github.com/postgres/postgres.git
synced 2025-07-12 21:01:52 +03:00
Add Result Cache executor node
Here we add a new executor node type named "Result Cache". The planner can include this node type in the plan to have the executor cache the results from the inner side of parameterized nested loop joins. This allows caching of tuples for sets of parameters so that in the event that the node sees the same parameter values again, it can just return the cached tuples instead of rescanning the inner side of the join all over again. Internally, result cache uses a hash table in order to quickly find tuples that have been previously cached. For certain data sets, this can significantly improve the performance of joins. The best cases for using this new node type are for join problems where a large portion of the tuples from the inner side of the join have no join partner on the outer side of the join. In such cases, hash join would have to hash values that are never looked up, thus bloating the hash table and possibly causing it to multi-batch. Merge joins would have to skip over all of the unmatched rows. If we use a nested loop join with a result cache, then we only cache tuples that have at least one join partner on the outer side of the join. The benefits of using a parameterized nested loop with a result cache increase when there are fewer distinct values being looked up and the number of lookups of each value is large. Also, hash probes to lookup the cache can be much faster than the hash probe in a hash join as it's common that the result cache's hash table is much smaller than the hash join's due to result cache only caching useful tuples rather than all tuples from the inner side of the join. This variation in hash probe performance is more significant when the hash join's hash table no longer fits into the CPU's L3 cache, but the result cache's hash table does. The apparent "random" access of hash buckets with each hash probe can cause a poor L3 cache hit ratio for large hash tables. Smaller hash tables generally perform better. The hash table used for the cache limits itself to not exceeding work_mem * hash_mem_multiplier in size. We maintain a dlist of keys for this cache and when we're adding new tuples and realize we've exceeded the memory budget, we evict cache entries starting with the least recently used ones until we have enough memory to add the new tuples to the cache. For parameterized nested loop joins, we now consider using one of these result cache nodes in between the nested loop node and its inner node. We determine when this might be useful based on cost, which is primarily driven off of what the expected cache hit ratio will be. Estimating the cache hit ratio relies on having good distinct estimates on the nested loop's parameters. For now, the planner will only consider using a result cache for parameterized nested loop joins. This works for both normal joins and also for LATERAL type joins to subqueries. It is possible to use this new node for other uses in the future. For example, to cache results from correlated subqueries. However, that's not done here due to some difficulties obtaining a distinct estimation on the outer plan to calculate the estimated cache hit ratio. Currently we plan the inner plan before planning the outer plan so there is no good way to know if a result cache would be useful or not since we can't estimate the number of times the subplan will be called until the outer plan is generated. The functionality being added here is newly introducing a dependency on the return value of estimate_num_groups() during the join search. Previously, during the join search, we only ever needed to perform selectivity estimations. With this commit, we need to use estimate_num_groups() in order to estimate what the hit ratio on the result cache will be. In simple terms, if we expect 10 distinct values and we expect 1000 outer rows, then we'll estimate the hit ratio to be 99%. Since cache hits are very cheap compared to scanning the underlying nodes on the inner side of the nested loop join, then this will significantly reduce the planner's cost for the join. However, it's fairly easy to see here that things will go bad when estimate_num_groups() incorrectly returns a value that's significantly lower than the actual number of distinct values. If this happens then that may cause us to make use of a nested loop join with a result cache instead of some other join type, such as a merge or hash join. Our distinct estimations have been known to be a source of trouble in the past, so the extra reliance on them here could cause the planner to choose slower plans than it did previous to having this feature. Distinct estimations are also fairly hard to estimate accurately when several tables have been joined already or when a WHERE clause filters out a set of values that are correlated to the expressions we're estimating the number of distinct value for. For now, the costing we perform during query planning for result caches does put quite a bit of faith in the distinct estimations being accurate. When these are accurate then we should generally see faster execution times for plans containing a result cache. However, in the real world, we may find that we need to either change the costings to put less trust in the distinct estimations being accurate or perhaps even disable this feature by default. There's always an element of risk when we teach the query planner to do new tricks that it decides to use that new trick at the wrong time and causes a regression. Users may opt to get the old behavior by turning the feature off using the enable_resultcache GUC. Currently, this is enabled by default. It remains to be seen if we'll maintain that setting for the release. Additionally, the name "Result Cache" is the best name I could think of for this new node at the time I started writing the patch. Nobody seems to strongly dislike the name. A few people did suggest other names but no other name seemed to dominate in the brief discussion that there was about names. Let's allow the beta period to see if the current name pleases enough people. If there's some consensus on a better name, then we can change it before the release. Please see the 2nd discussion link below for the discussion on the "Result Cache" name. Author: David Rowley Reviewed-by: Andy Fan, Justin Pryzby, Zhihong Yu Tested-By: Konstantin Knizhnik Discussion: https://postgr.es/m/CAApHDvrPcQyQdWERGYWx8J%2B2DLUNgXu%2BfOSbQ1UscxrunyXyrQ%40mail.gmail.com Discussion: https://postgr.es/m/CAApHDvq=yQXr5kqhRviT2RhNKwToaWr9JAN5t+5_PzhuRJ3wvg@mail.gmail.com
This commit is contained in:
@ -18,10 +18,13 @@
|
||||
|
||||
#include "executor/executor.h"
|
||||
#include "foreign/fdwapi.h"
|
||||
#include "nodes/nodeFuncs.h"
|
||||
#include "optimizer/cost.h"
|
||||
#include "optimizer/optimizer.h"
|
||||
#include "optimizer/pathnode.h"
|
||||
#include "optimizer/paths.h"
|
||||
#include "optimizer/planmain.h"
|
||||
#include "utils/typcache.h"
|
||||
|
||||
/* Hook for plugins to get control in add_paths_to_joinrel() */
|
||||
set_join_pathlist_hook_type set_join_pathlist_hook = NULL;
|
||||
@ -52,6 +55,9 @@ static void try_partial_mergejoin_path(PlannerInfo *root,
|
||||
static void sort_inner_and_outer(PlannerInfo *root, RelOptInfo *joinrel,
|
||||
RelOptInfo *outerrel, RelOptInfo *innerrel,
|
||||
JoinType jointype, JoinPathExtraData *extra);
|
||||
static inline bool clause_sides_match_join(RestrictInfo *rinfo,
|
||||
RelOptInfo *outerrel,
|
||||
RelOptInfo *innerrel);
|
||||
static void match_unsorted_outer(PlannerInfo *root, RelOptInfo *joinrel,
|
||||
RelOptInfo *outerrel, RelOptInfo *innerrel,
|
||||
JoinType jointype, JoinPathExtraData *extra);
|
||||
@ -163,6 +169,11 @@ add_paths_to_joinrel(PlannerInfo *root,
|
||||
{
|
||||
case JOIN_SEMI:
|
||||
case JOIN_ANTI:
|
||||
|
||||
/*
|
||||
* XXX it may be worth proving this to allow a ResultCache to be
|
||||
* considered for Nested Loop Semi/Anti Joins.
|
||||
*/
|
||||
extra.inner_unique = false; /* well, unproven */
|
||||
break;
|
||||
case JOIN_UNIQUE_INNER:
|
||||
@ -354,6 +365,180 @@ allow_star_schema_join(PlannerInfo *root,
|
||||
bms_nonempty_difference(inner_paramrels, outerrelids));
|
||||
}
|
||||
|
||||
/*
|
||||
* paraminfo_get_equal_hashops
|
||||
* Determine if param_info and innerrel's lateral_vars can be hashed.
|
||||
* Returns true the hashing is possible, otherwise return false.
|
||||
*
|
||||
* Additionally we also collect the outer exprs and the hash operators for
|
||||
* each parameter to innerrel. These set in 'param_exprs' and 'operators'
|
||||
* when we return true.
|
||||
*/
|
||||
static bool
|
||||
paraminfo_get_equal_hashops(PlannerInfo *root, ParamPathInfo *param_info,
|
||||
RelOptInfo *outerrel, RelOptInfo *innerrel,
|
||||
List **param_exprs, List **operators)
|
||||
|
||||
{
|
||||
ListCell *lc;
|
||||
|
||||
*param_exprs = NIL;
|
||||
*operators = NIL;
|
||||
|
||||
if (param_info != NULL)
|
||||
{
|
||||
List *clauses = param_info->ppi_clauses;
|
||||
|
||||
foreach(lc, clauses)
|
||||
{
|
||||
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
||||
OpExpr *opexpr;
|
||||
Node *expr;
|
||||
|
||||
/* can't use result cache without a valid hash equals operator */
|
||||
if (!OidIsValid(rinfo->hasheqoperator) ||
|
||||
!clause_sides_match_join(rinfo, outerrel, innerrel))
|
||||
{
|
||||
list_free(*operators);
|
||||
list_free(*param_exprs);
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* We already checked that this is an OpExpr with 2 args when
|
||||
* setting hasheqoperator.
|
||||
*/
|
||||
opexpr = (OpExpr *) rinfo->clause;
|
||||
if (rinfo->outer_is_left)
|
||||
expr = (Node *) linitial(opexpr->args);
|
||||
else
|
||||
expr = (Node *) lsecond(opexpr->args);
|
||||
|
||||
*operators = lappend_oid(*operators, rinfo->hasheqoperator);
|
||||
*param_exprs = lappend(*param_exprs, expr);
|
||||
}
|
||||
}
|
||||
|
||||
/* Now add any lateral vars to the cache key too */
|
||||
foreach(lc, innerrel->lateral_vars)
|
||||
{
|
||||
Node *expr = (Node *) lfirst(lc);
|
||||
TypeCacheEntry *typentry;
|
||||
|
||||
/* Reject if there are any volatile functions */
|
||||
if (contain_volatile_functions(expr))
|
||||
{
|
||||
list_free(*operators);
|
||||
list_free(*param_exprs);
|
||||
return false;
|
||||
}
|
||||
|
||||
typentry = lookup_type_cache(exprType(expr),
|
||||
TYPECACHE_HASH_PROC | TYPECACHE_EQ_OPR);
|
||||
|
||||
/* can't use result cache without a valid hash equals operator */
|
||||
if (!OidIsValid(typentry->hash_proc) || !OidIsValid(typentry->eq_opr))
|
||||
{
|
||||
list_free(*operators);
|
||||
list_free(*param_exprs);
|
||||
return false;
|
||||
}
|
||||
|
||||
*operators = lappend_oid(*operators, typentry->eq_opr);
|
||||
*param_exprs = lappend(*param_exprs, expr);
|
||||
}
|
||||
|
||||
/* We're okay to use result cache */
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* get_resultcache_path
|
||||
* If possible, make and return a Result Cache path atop of 'inner_path'.
|
||||
* Otherwise return NULL.
|
||||
*/
|
||||
static Path *
|
||||
get_resultcache_path(PlannerInfo *root, RelOptInfo *innerrel,
|
||||
RelOptInfo *outerrel, Path *inner_path,
|
||||
Path *outer_path, JoinType jointype,
|
||||
JoinPathExtraData *extra)
|
||||
{
|
||||
List *param_exprs;
|
||||
List *hash_operators;
|
||||
ListCell *lc;
|
||||
|
||||
/* Obviously not if it's disabled */
|
||||
if (!enable_resultcache)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* We can safely not bother with all this unless we expect to perform more
|
||||
* than one inner scan. The first scan is always going to be a cache
|
||||
* miss. This would likely fail later anyway based on costs, so this is
|
||||
* really just to save some wasted effort.
|
||||
*/
|
||||
if (outer_path->parent->rows < 2)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* We can only have a result cache when there's some kind of cache key,
|
||||
* either parameterized path clauses or lateral Vars. No cache key sounds
|
||||
* more like something a Materialize node might be more useful for.
|
||||
*/
|
||||
if ((inner_path->param_info == NULL ||
|
||||
inner_path->param_info->ppi_clauses == NIL) &&
|
||||
innerrel->lateral_vars == NIL)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* Currently we don't do this for SEMI and ANTI joins unless they're
|
||||
* marked as inner_unique. This is because nested loop SEMI/ANTI joins
|
||||
* don't scan the inner node to completion, which will mean result cache
|
||||
* cannot mark the cache entry as complete.
|
||||
*
|
||||
* XXX Currently we don't attempt to mark SEMI/ANTI joins as inner_unique
|
||||
* = true. Should we? See add_paths_to_joinrel()
|
||||
*/
|
||||
if (!extra->inner_unique && (jointype == JOIN_SEMI ||
|
||||
jointype == JOIN_ANTI))
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* We can't use a result cache if there are volatile functions in the
|
||||
* inner rel's target list or restrict list. A cache hit could reduce the
|
||||
* number of calls to these functions.
|
||||
*/
|
||||
if (contain_volatile_functions((Node *) innerrel->reltarget))
|
||||
return NULL;
|
||||
|
||||
foreach(lc, innerrel->baserestrictinfo)
|
||||
{
|
||||
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
||||
|
||||
if (contain_volatile_functions((Node *) rinfo))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Check if we have hash ops for each parameter to the path */
|
||||
if (paraminfo_get_equal_hashops(root,
|
||||
inner_path->param_info,
|
||||
outerrel,
|
||||
innerrel,
|
||||
¶m_exprs,
|
||||
&hash_operators))
|
||||
{
|
||||
return (Path *) create_resultcache_path(root,
|
||||
innerrel,
|
||||
inner_path,
|
||||
param_exprs,
|
||||
hash_operators,
|
||||
extra->inner_unique,
|
||||
outer_path->parent->rows);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* try_nestloop_path
|
||||
* Consider a nestloop join path; if it appears useful, push it into
|
||||
@ -1471,6 +1656,7 @@ match_unsorted_outer(PlannerInfo *root,
|
||||
foreach(lc2, innerrel->cheapest_parameterized_paths)
|
||||
{
|
||||
Path *innerpath = (Path *) lfirst(lc2);
|
||||
Path *rcpath;
|
||||
|
||||
try_nestloop_path(root,
|
||||
joinrel,
|
||||
@ -1479,6 +1665,22 @@ match_unsorted_outer(PlannerInfo *root,
|
||||
merge_pathkeys,
|
||||
jointype,
|
||||
extra);
|
||||
|
||||
/*
|
||||
* Try generating a result cache path and see if that makes the
|
||||
* nested loop any cheaper.
|
||||
*/
|
||||
rcpath = get_resultcache_path(root, innerrel, outerrel,
|
||||
innerpath, outerpath, jointype,
|
||||
extra);
|
||||
if (rcpath != NULL)
|
||||
try_nestloop_path(root,
|
||||
joinrel,
|
||||
outerpath,
|
||||
rcpath,
|
||||
merge_pathkeys,
|
||||
jointype,
|
||||
extra);
|
||||
}
|
||||
|
||||
/* Also consider materialized form of the cheapest inner path */
|
||||
@ -1633,6 +1835,7 @@ consider_parallel_nestloop(PlannerInfo *root,
|
||||
foreach(lc2, innerrel->cheapest_parameterized_paths)
|
||||
{
|
||||
Path *innerpath = (Path *) lfirst(lc2);
|
||||
Path *rcpath;
|
||||
|
||||
/* Can't join to an inner path that is not parallel-safe */
|
||||
if (!innerpath->parallel_safe)
|
||||
@ -1657,6 +1860,17 @@ consider_parallel_nestloop(PlannerInfo *root,
|
||||
|
||||
try_partial_nestloop_path(root, joinrel, outerpath, innerpath,
|
||||
pathkeys, jointype, extra);
|
||||
|
||||
/*
|
||||
* Try generating a result cache path and see if that makes the
|
||||
* nested loop any cheaper.
|
||||
*/
|
||||
rcpath = get_resultcache_path(root, innerrel, outerrel,
|
||||
innerpath, outerpath, jointype,
|
||||
extra);
|
||||
if (rcpath != NULL)
|
||||
try_partial_nestloop_path(root, joinrel, outerpath, rcpath,
|
||||
pathkeys, jointype, extra);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user