Re: Memoize: stale entry pointer after cache_store_tuple()

From: cca5507 <cca5507(at)qq(dot)com>
To: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Memoize: stale entry pointer after cache_store_tuple()
Date: 2026-11-11 17:24:47
Message-ID: tencent_38F1320E9AECB043830B6D6BBF52A6647009@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> Hi hackers,
>
> I found a case where Memoize rescans its inner plan even though the complete result for the requested key is already cached.
>
> When the cache exceeds its memory limit, Memoize removes older entries. Removing an entry from the hash table can move other entries to different buckets, including the entry currently being filled.
>
> The code that stores a tuple accounts for this movement and updates node->entry. However, its caller then marks the entry as complete using the old local pointer:
>
> ```
> entry->complete = node->singlerow;
> ```
>
> This writes to the old bucket and leaves the actual entry marked incomplete. When the same key is requested again, Memoize discards the cached tuples and rescans the inner plan.

Good catch! Your analysis is right for me.

> Using the updated pointer fixes this:
>
> - entry->complete = node->singlerow;
> + node->entry->complete = node->singlerow;
>
> Thoughts?

Patch LGTM and we need backpatch to all supported version.

--
Regards,
ChangAo Chen

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Bharath Rupireddy 2026-11-11 17:13:35 Re: Pass heap tuple counts to parallel vacuum as double