Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR7EM-00000000h7Y-0R3P for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Nov 2026 12:15:10 +0000 Received: from localhost ([127.0.0.1]:46626 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xR7EK-0000000Abyx-0l6X for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Nov 2026 12:15:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:41842) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR7EJ-0000000Abyp-3M7T for pgsql-hackers@lists.postgresql.org; Wed, 11 Nov 2026 12:15:08 +0000 Received: from forward101d.mail.yandex.net ([2a02:6b8:c41:1300:1:45:d181:d101]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR7EH-00000000SLz-1Og7 for pgsql-hackers@lists.postgresql.org; Wed, 11 Nov 2026 12:15:07 +0000 Received: from mail-nwsmtp-smtp-production-main-62 (mail-nwsmtp-smtp-production-main-62.yp-c.yandex.net) by forward101d.mail.yandex.net (postfix) with ESMTPS id 66C5AC00D0 for ; Wed, 11 Nov 2026 15:15:03 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-62 with ESMTPSA id 1FS5qZPdAOs0-v2c158w0; Wed, 11 Nov 2026 15:15:02 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1794399302; bh=oAp56x5oWm8egOfU5VGukb1+6CO1AbCV+O1amhxDL8E=; h=Subject:To:Message-ID:Date:From; b=camm2/unYJk3aCK+mkAGy27BvXdwpkzKfWTprqNcAcusdO3sCGkdR0gJlE6M905Qe 1RUqNcjT2JCX9HyFQ+uwOPzgnaUDg7jZIriiveORYNBLTFK93W40aMpqh8C9Ul5/I3 iVnNX+b6dPpcukYchdfeu5q00j0JN3RKIM8sKQo8= Authentication-Results: mail-nwsmtp-smtp-production-main-62; dkim=pass header.i=@tantorlabs.com Content-Type: multipart/mixed; boundary="------------0PeML7jwTT9D3Gb5fCEv0eRg" Message-ID: <14118dd4-eefa-42e4-ada7-900d6845d704@tantorlabs.com> Date: Wed, 11 Nov 2026 15:15:01 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: pgsql-hackers From: Ilia Evdokimov Subject: Memoize: stale entry pointer after cache_store_tuple() List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------0PeML7jwTT9D3Gb5fCEv0eRg Content-Type: multipart/alternative; boundary="------------nVpliLC3R1bXKdRFxDv9ixXQ" --------------nVpliLC3R1bXKdRFxDv9ixXQ Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. Example ``` -- Force a nested loop join  with Memoize and keep the cache small enough to trigger evictions SET enable_hashjoin = off; SET enable_mergejoin = off; SET work_mem = '64kB'; SET hash_mem_multiplier = 1; CREATE TEMP TABLE a AS SELECT i / 2 AS k FROM generate_series(0, 1999) i; ALTER TABLE a ALTER k SET (n_distinct = 10); ANALYZE a; CREATE TEMP TABLE b (k int PRIMARY KEY, v text); -- Underestimate the number of distinct keys to make the planner choose Memoize despite the small cache. INSERT INTO b SELECT i, repeat('x', 1200) FROM generate_series(0, 999) i; ANALYZE b; EXPLAIN ANALYZE SELECT sum(length(v)) FROM a JOIN b USING (k); --- master                                                              QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------  Aggregate  (cost=102.96..102.97 rows=1 width=8) (actual time=6.359..6.360 rows=1.00 loops=1)    Buffers: local hit=2554 dirtied=2 written=5    ->  Nested Loop  (cost=0.29..92.96 rows=2000 width=1204) (actual time=0.246..2.549 rows=2000.00 loops=1)          Buffers: local hit=2554 dirtied=2 written=5          ->  Seq Scan on a  (cost=0.00..36.00 rows=2000 width=4) (actual time=0.172..0.258 rows=2000.00 loops=1)                Buffers: local hit=23 dirtied=1 written=4          ->  Memoize  (cost=0.29..0.65 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=2000)                Cache Key: a.k                Cache Mode: logical                Estimates: capacity=10 distinct keys=10 lookups=2000 hit percent=99.50% *Hits: 901  Misses: 1099*  Evictions: 950 Overflows: 0  Memory Usage: 65kB                Buffers: local hit=2531 dirtied=1 written=1                ->  Index Scan using b_pkey on b (cost=0.28..0.64 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=1099)                      Index Cond: (k = a.k)                      Index Searches: 1099                      Buffers: local hit=2531 dirtied=1 written=1  Planning:    Buffers: shared hit=37 read=1, local hit=1  Planning Time: 0.211 ms  Execution Time: 6.412 ms (20 rows) --- v1 patch                                                              QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------  Aggregate  (cost=102.96..102.97 rows=1 width=8) (actual time=6.138..6.139 rows=1.00 loops=1)    Buffers: local hit=2356 dirtied=2 written=5    ->  Nested Loop  (cost=0.29..92.96 rows=2000 width=1204) (actual time=0.277..2.442 rows=2000.00 loops=1)          Buffers: local hit=2356 dirtied=2 written=5          ->  Seq Scan on a  (cost=0.00..36.00 rows=2000 width=4) (actual time=0.192..0.276 rows=2000.00 loops=1)                Buffers: local hit=23 dirtied=1 written=4          ->  Memoize  (cost=0.29..0.65 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=2000)                Cache Key: a.k                Cache Mode: logical                Estimates: capacity=10 distinct keys=10 lookups=2000 hit percent=99.50% *Hits: 1000  Misses: 1000*  Evictions: 950 Overflows: 0  Memory Usage: 65kB                Buffers: local hit=2333 dirtied=1 written=1                ->  Index Scan using b_pkey on b (cost=0.28..0.64 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=1000)                      Index Cond: (k = a.k)                      Index Searches: 1000                      Buffers: local hit=2333 dirtied=1 written=1  Planning:    Buffers: shared hit=32 read=5, local hit=1  Planning Time: 0.272 ms  Execution Time: 6.222 ms (20 rows) ```                   Hits    Misses Before the fix    901   1099 After the fix    1000    1000 Using the updated pointer fixes this: - entry->complete = node->singlerow; + node->entry->complete = node->singlerow; Thoughts? -- Best regards, Ilia Evdokimov, Tantor Labs LLC, https://tantorlabs.com/ --------------nVpliLC3R1bXKdRFxDv9ixXQ Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

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.

Example
```
-- Force a nested loop join  with Memoize and keep the cache small enough to trigger evictions
SET enable_hashjoin = off;
SET enable_mergejoin = off;
SET work_mem = '64kB';
SET hash_mem_multiplier = 1;

CREATE TEMP TABLE a AS SELECT i / 2 AS k FROM generate_series(0, 1999) i;

ALTER TABLE a ALTER k SET (n_distinct = 10);
ANALYZE a;

CREATE TEMP TABLE b (k int PRIMARY KEY, v text);

-- Underestimate the number of distinct keys to make the planner choose Memoize despite the small cache.
INSERT INTO b SELECT i, repeat('x', 1200) FROM generate_series(0, 999) i;
ANALYZE b;

EXPLAIN ANALYZE                                     
SELECT sum(length(v)) FROM a JOIN b USING (k);

--- master
                                                             QUERY PLAN                                                              
-------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=102.96..102.97 rows=1 width=8) (actual time=6.359..6.360 rows=1.00 loops=1)
   Buffers: local hit=2554 dirtied=2 written=5
   ->  Nested Loop  (cost=0.29..92.96 rows=2000 width=1204) (actual time=0.246..2.549 rows=2000.00 loops=1)
         Buffers: local hit=2554 dirtied=2 written=5
         ->  Seq Scan on a  (cost=0.00..36.00 rows=2000 width=4) (actual time=0.172..0.258 rows=2000.00 loops=1)
               Buffers: local hit=23 dirtied=1 written=4
         ->  Memoize  (cost=0.29..0.65 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=2000)
               Cache Key: a.k
               Cache Mode: logical
               Estimates: capacity=10 distinct keys=10 lookups=2000 hit percent=99.50%
               Hits: 901  Misses: 1099  Evictions: 950  Overflows: 0  Memory Usage: 65kB
               Buffers: local hit=2531 dirtied=1 written=1
               ->  Index Scan using b_pkey on b  (cost=0.28..0.64 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=1099)
                     Index Cond: (k = a.k)
                     Index Searches: 1099
                     Buffers: local hit=2531 dirtied=1 written=1
 Planning:
   Buffers: shared hit=37 read=1, local hit=1
 Planning Time: 0.211 ms
 Execution Time: 6.412 ms
(20 rows)

--- v1 patch
                                                             QUERY PLAN                                                              
-------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=102.96..102.97 rows=1 width=8) (actual time=6.138..6.139 rows=1.00 loops=1)
   Buffers: local hit=2356 dirtied=2 written=5
   ->  Nested Loop  (cost=0.29..92.96 rows=2000 width=1204) (actual time=0.277..2.442 rows=2000.00 loops=1)
         Buffers: local hit=2356 dirtied=2 written=5
         ->  Seq Scan on a  (cost=0.00..36.00 rows=2000 width=4) (actual time=0.192..0.276 rows=2000.00 loops=1)
               Buffers: local hit=23 dirtied=1 written=4
         ->  Memoize  (cost=0.29..0.65 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=2000)
               Cache Key: a.k
               Cache Mode: logical
               Estimates: capacity=10 distinct keys=10 lookups=2000 hit percent=99.50%
               Hits: 1000  Misses: 1000  Evictions: 950  Overflows: 0  Memory Usage: 65kB
               Buffers: local hit=2333 dirtied=1 written=1
               ->  Index Scan using b_pkey on b  (cost=0.28..0.64 rows=1 width=1208) (actual time=0.001..0.001 rows=1.00 loops=1000)
                     Index Cond: (k = a.k)
                     Index Searches: 1000
                     Buffers: local hit=2333 dirtied=1 written=1
 Planning:
   Buffers: shared hit=32 read=5, local hit=1
 Planning Time: 0.272 ms
 Execution Time: 6.222 ms
(20 rows)
```

                  Hits    Misses
Before the fix    901   1099
After the fix    1000    1000

Using the updated pointer fixes this:

- entry->complete = node->singlerow;
+ node->entry->complete = node->singlerow;

Thoughts?

--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/

--------------nVpliLC3R1bXKdRFxDv9ixXQ-- --------------0PeML7jwTT9D3Gb5fCEv0eRg Content-Type: text/x-patch; charset=UTF-8; name="v1-0001-Fix-stale-entry-pointer-in-Memoize-after-cache-ev.patch" Content-Disposition: attachment; filename*0="v1-0001-Fix-stale-entry-pointer-in-Memoize-after-cache-ev.pa"; filename*1="tch" Content-Transfer-Encoding: base64 RnJvbSAyMDMxZDU2YmQ1ZmNjNWY1MjE3Y2E4MzYxMTkyOTgyODg3YjYyMGFiIE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBFdmRva2ltb3YgSWxpYSA8aWx5YS5ldmRva2ltb3ZA dGFudG9ybGFicy5jb20+CkRhdGU6IFdlZCwgMTEgTm92IDIwMjYgMTU6MTI6MjQgKzAzMDAK U3ViamVjdDogW1BBVENIIHYxXSBGaXggc3RhbGUgZW50cnkgcG9pbnRlciBpbiBNZW1vaXpl IGFmdGVyIGNhY2hlIGV2aWN0aW9uCgp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