postgres.git / summary / log / commit / refs

commit    76c0df7381e5e4d3fba06abddcb51216821c02bc
Author:   David Rowley <drowley@postgresql.org>
Date:     Wed Nov 11 02:19:11 2026 +0000

    Remove needless initialization work in ExecInitMemoize
    
    When a Memoize node is in binary mode, cache keys are hashed with
    datum_image_hash() and compared with datum_image_eq().  Despite that,
    ExecInitMemoize() always looked up the hash functions for each cache
    key and built cache_eq_expr, neither of which binary mode uses.
    
    Only do that work when not in binary mode.  This saves some executor
    startup work and memory for Memoize nodes using binary mode.
    
    Discovered while studying code for bug #19853.
    
    In passing, adjust comment that mentioned "type's hash equality operator",
    per changes done in 8cdf3c921.
    
    Author: David Rowley <dgrowleyml@gmail.com>
    Discussion: https://postgr.es/m/CAApHDvq+atDgBEYnxMBbQyGji0y3sBtpa4zhYnXqb77UfyxzMA@mail.gmail.com


src/backend/executor/nodeMemoize.c | 70 +++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/backend/executor/nodeMemoize.c b/src/backend/executor/nodeMemoize.c index 023468e056c..74f79260fc3 100644 --- a/src/backend/executor/nodeMemoize.c +++ b/src/backend/executor/nodeMemoize.c @@ -1037,40 +1037,60 @@ ExecInitMemoize(Memoize *node, EState *estate, int eflags) &TTSOpsMinimalTuple); mstate->probeslot = MakeSingleTupleTableSlot(mstate->hashkeydesc, &TTSOpsVirtual); - mstate->param_exprs = palloc_array(ExprState *, nkeys); + mstate->cache_eq_expr = NULL; /* May be adjusted below */ + mstate->hashfunctions = NULL; /* May be adjusted below */ mstate->collations = node->collations; /* Just point directly to the plan * data */ - mstate->hashfunctions = palloc_array(FmgrInfo, nkeys); - - eqfuncoids = palloc_array(Oid, nkeys); for (i = 0; i < nkeys; i++) { - Oid hashop = node->hashOperators[i]; - Oid left_hashfn; - Oid right_hashfn; Expr *param_expr = (Expr *) list_nth(node->param_exprs, i); - if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn)) - elog(ERROR, "could not find hash function for hash operator %u", - hashop); - - fmgr_info(left_hashfn, &mstate->hashfunctions[i]); - mstate->param_exprs[i] = ExecInitExpr(param_expr, (PlanState *) mstate); - eqfuncoids[i] = get_opcode(hashop); } - mstate->cache_eq_expr = ExecBuildParamSetEqual(mstate->hashkeydesc, - &TTSOpsMinimalTuple, - &TTSOpsVirtual, - eqfuncoids, - node->collations, - node->param_exprs, - (PlanState *) mstate); + /* + * Record if the cache keys should be compared bit by bit, or logically + * using the hash equality operator + */ + mstate->binary_mode = node->binary_mode; + + /* + * In binary mode, the cache keys are hashed and compared using + * datum_image_hash() and datum_image_eq(), so the hash functions and + * equality expression are only needed when not in binary mode. + */ + if (!mstate->binary_mode) + { + mstate->hashfunctions = palloc_array(FmgrInfo, nkeys); + eqfuncoids = palloc_array(Oid, nkeys); + + for (i = 0; i < nkeys; i++) + { + Oid hashop = node->hashOperators[i]; + Oid left_hashfn; + Oid right_hashfn; + + if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn)) + elog(ERROR, "could not find hash function for hash operator %u", + hashop); + + fmgr_info(left_hashfn, &mstate->hashfunctions[i]); + eqfuncoids[i] = get_opcode(hashop); + } + + mstate->cache_eq_expr = ExecBuildParamSetEqual(mstate->hashkeydesc, + &TTSOpsMinimalTuple, + &TTSOpsVirtual, + eqfuncoids, + node->collations, + node->param_exprs, + (PlanState *) mstate); + + pfree(eqfuncoids); + } - pfree(eqfuncoids); mstate->mem_used = 0; /* Limit the total memory consumed by the cache to this */ @@ -1096,12 +1116,6 @@ ExecInitMemoize(Memoize *node, EState *estate, int eflags) mstate->singlerow = node->singlerow; mstate->keyparamids = node->keyparamids; - /* - * Record if the cache keys should be compared bit by bit, or logically - * using the type's hash equality operator - */ - mstate->binary_mode = node->binary_mode; - /* Zero the statistics counters */ memset(&mstate->stats, 0, sizeof(MemoizeInstrumentation)); [parent: 35def3385998]