| From: | Richard Guo <guofenglinux(at)gmail(dot)com> |
|---|---|
| To: | Jacob Brazeal <jacob(dot)brazeal(at)gmail(dot)com> |
| Cc: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: bug: query returns different result with and without memoization. |
| Date: | 2026-07-27 08:46:18 |
| Message-ID: | CAMbWs49xt269Sq1Y==_1rqWKpo=xLW3sNbzjaZEePW=u_=osRQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Sat, Jul 25, 2026 at 5:04 AM Jacob Brazeal <jacob(dot)brazeal(at)gmail(dot)com> wrote:
> --setup
> CREATE TABLE tenk1 AS
> SELECT g AS unique1, g%2 AS two, g%10 AS ten, g%20 AS twenty, g%100 AS hundred
> FROM generate_series(0,9999) g;
> CREATE INDEX tenk1_unique1 ON tenk1(unique1);
> CREATE INDEX tenk1_hundred ON tenk1(hundred);
> ANALYZE tenk1;
>
> SET enable_seqscan=off; SET enable_mergejoin=off; SET work_mem='64kB';
>
> Now return the query below with each of these settings:
>
> SET enable_memoize = on; -- 82000 (incorrect)
> SET enable_memoize = off; -- 100000 (correct)
>
> SELECT sum(c) FROM (
> SELECT t0.unique1,
> (SELECT count(*) FROM tenk1 t2 JOIN tenk1 t1
> ON t1.unique1 = t2.hundred + t0.ten
> WHERE t1.twenty = t0.ten) AS c
> FROM tenk1 t0 WHERE t0.unique1 < 200) s;
Thanks for the report! ExecReScanMemoize() purges the whole cache
only when a parameter that is not part of the cache key changes. It
assumes that a change to a cache-key parameter is handled by looking
up a different cache entry. But that assumption fails when a
parameter is buried inside a larger cache-key expression and also
appears elsewhere in the subplan.
In the reported case, there is a cache key of "t2.hundred + t0.ten"
and a separate filter "t1.twenty = t0.ten". The problem is that
different (t2.hundred, t0.ten) pairs can produce the same cache-key
value while the filter selects different rows.
I think we can fix it by dropping from keyparamids any parameter that
also appears outside the cache-key expressions. See the attached
patch.
- Richard
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Flush-Memoize-cache-when-a-buried-cache-key-param.patch | application/octet-stream | 8.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Daniel Gustafsson | 2026-07-27 08:50:44 | Re: Grab bag of smaller OpenSSL fixups |
| Previous Message | Daniel Gustafsson | 2026-07-27 08:39:23 | Re: [PATCH] Rename "getdatabaseencoding()" to "pg_database_encoding()", and document |