From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Make Memoize cache keys only of Params from outer query levels
Date: Tue, 03 Nov 2026 00:52:30 +0000
Message-ID: <E1xO2lK-00000001s64-2tI9@gemulon.postgresql.org> (raw)
Make Memoize cache keys only of Params from outer query levels
Commit 708eb5158 made each PARAM_EXEC Param buried inside a larger
Memoize cache key expression a cache key by itself, and, in master
only, commit f8da64e6d extended that to the Params in the memoized
relation's base quals, targetlist and join clauses. Both meant to
catch Params supplied by an outer query level, but they took every
PARAM_EXEC Param they found, including the output Params of initPlans
and SubPlans.
When an initPlan's output Param is made a cache key, it is evaluated
on every cache probe, which runs the initPlan even where the query
doesn't need it, such as in a CASE arm that is never taken, and can
raise an error the query shouldn't raise. A SubPlan's output Params
have a value only while the SubPlan evaluates its testexpr, so making
a cache key of one fails with "plan should not reference subplan's
variable".
To fix, make cache keys only of the Params listed in some outer query
level's plan_params, as those hold values supplied by outer levels and
are always safe to evaluate. Any other Param is left to purge the
cache when it changes.
Back-patch to all supported branches.
Reported-by: Fujii Masao <masao.fujii@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAHGQGwHdaau1KooFbY10=MDhKtFVgpmPrDiwNpEe6bWu7wpQYA@mail.gmail.com
Backpatch-through: 14
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/45ff0b4a317f14539567a3500eb41ec8b06cde3d
Modified Files
--------------
src/backend/optimizer/path/joinpath.c | 34 ++++++++++++++++++--
src/test/regress/expected/memoize.out | 59 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/memoize.sql | 24 ++++++++++++++
3 files changed, 115 insertions(+), 2 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: rguo@postgresql.org
Subject: Re: pgsql: Make Memoize cache keys only of Params from outer query levels
In-Reply-To: <E1xO2lK-00000001s64-2tI9@gemulon.postgresql.org>
References: <E1xO2lK-00000001s64-2tI9@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox