postgres.git / summary / log / commit / refs

commit    aa2cfd0c5ccb8a256d98b6cd85d5332cc61c0ca2
Author:   David Rowley <drowley@postgresql.org>
Date:     Thu Nov 12 00:36:38 2026 +0000
First released in: 19.0

    Fix possible bogus cache miss bug in Memoize nodes
    
    When Memoize got a cache miss, it would create a new hash table entry
    and execute its subnode to fetch the tuple to store in the cache.  When
    storing that tuple, the cache may have needed to make space for the new
    tuple, and that could have resulted in older, less recently used entries
    to be removed from the cache.  The simplehash code, since it uses linear
    probing to handle hash collisions, would shift subsequent elements back
    towards their most optimal hash bucket when deleting items from the table.
    This could result in the just-added entry being moved.  The Memoize code
    would then accidentally proceed to assign the entry's "complete" flag, but
    the entry being modified there could have become stale as a result of the
    simplehash code moving things around.
    
    This could happen regardless of singlerow mode.  In singlerow mode, it
    could result in the entry being populated not correctly being marked as
    complete, which could result in cache misses later that could have been
    cache hits.  When not in singlerow mode, an unrelated entry could have
    been unmarked as complete accidentally, which could also have resulted
    in cache misses that could have been hits.  No wrong results were
    possible from this.
    
    Fix by using the entry pointer that's set in MemoizeState, which
    cache_store_tuple() had so carefully rediscovered for that purpose.
    
    Reported-by: Ilia Evdokimov <ilya.evdokimov@tantorlabs.com>
    Author: Ilia Evdokimov <ilya.evdokimov@tantorlabs.com>
    Reviewed-by: cca5507 <cca5507@qq.com>
    Reviewed-by: David Rowley <dgrowleyml@gmail.com>
    Discussion: https://postgr.es/m/14118dd4-eefa-42e4-ada7-900d6845d704@tantorlabs.com
    Backpatch-through: 15


src/backend/executor/nodeMemoize.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/executor/nodeMemoize.c b/src/backend/executor/nodeMemoize.c index 1f5cb9715c2..394d2344dfd 100644 --- a/src/backend/executor/nodeMemoize.c +++ b/src/backend/executor/nodeMemoize.c @@ -820,7 +820,7 @@ ExecMemoize(PlanState *pstate) * cache lookups to work even when the scan has not been * executed to completion. */ - entry->complete = node->singlerow; + node->entry->complete = node->singlerow; node->mstatus = MEMO_FILLING_CACHE; } [parent: be913fabe79d]