Skip to content

Commit bc397e5

Browse files
committed
De-dupicate Memoize cache keys
It was possible when determining the cache keys for a Memoize path that if the same expr appeared twice in the parameterized path's ppi_clauses and/or in the Nested Loop's inner relation's lateral_vars.  If this happened the Memoize node's cache keys would contain duplicates.  This isn't a problem for correctness, all it means is that the cache lookups will be suboptimal due to having redundant work to do on every hash table lookup and insert. Here we adjust paraminfo_get_equal_hashops() to look for duplicates and ignore them when we find them. Author: David Rowley Reviewed-by: Richard Guo Discussion: https://postgr.es/m/422277.1706207562%40sss.pgh.pa.us
1 parent bd5760d commit bc397e5

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/backend/optimizer/path/joinpath.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,16 @@ paraminfo_get_equal_hashops(PlannerInfo *root, ParamPathInfo *param_info,
492492
return false;
493493
}
494494

495-
*operators = lappend_oid(*operators, hasheqoperator);
496-
*param_exprs = lappend(*param_exprs, expr);
495+
/*
496+
* 'expr' may already exist as a parameter from a previous item in
497+
* ppi_clauses. No need to include it again, however we'd better
498+
* ensure we do switch into binary mode if required. See below.
499+
*/
500+
if (!list_member(*param_exprs, expr))
501+
{
502+
*operators = lappend_oid(*operators, hasheqoperator);
503+
*param_exprs = lappend(*param_exprs, expr);
504+
}
497505

498506
/*
499507
* When the join operator is not hashable then it's possible that
@@ -536,8 +544,16 @@ paraminfo_get_equal_hashops(PlannerInfo *root, ParamPathInfo *param_info,
536544
return false;
537545
}
538546

539-
*operators = lappend_oid(*operators, typentry->eq_opr);
540-
*param_exprs = lappend(*param_exprs, expr);
547+
/*
548+
* 'expr' may already exist as a parameter from the ppi_clauses. No
549+
* need to include it again, however we'd better ensure we do switch
550+
* into binary mode.
551+
*/
552+
if (!list_member(*param_exprs, expr))
553+
{
554+
*operators = lappend_oid(*operators, typentry->eq_opr);
555+
*param_exprs = lappend(*param_exprs, expr);
556+
}
541557

542558
/*
543559
* We must go into binary mode as we don't have too much of an idea of

src/test/regress/expected/memoize.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ WHERE t1.unique1 < 10;', false);
107107
-> Index Scan using tenk1_unique1 on tenk1 t1 (actual rows=10 loops=N)
108108
Index Cond: (unique1 < 10)
109109
-> Memoize (actual rows=2 loops=N)
110-
Cache Key: t1.two, t1.two
110+
Cache Key: t1.two
111111
Cache Mode: binary
112112
Hits: 8 Misses: 2 Evictions: Zero Overflows: 0 Memory Usage: NkB
113113
-> Subquery Scan on t2 (actual rows=2 loops=N)

0 commit comments

Comments
 (0)