pgsql: Make Memoize cache keys only of Params from outer query levels

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Make Memoize cache keys only of Params from outer query levels
Date: 2026-11-03 00:52:30
Message-ID: E1xO2lK-00000001s68-361D@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(dot)fujii(at)gmail(dot)com>
Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHGQGwHdaau1KooFbY10=MDhKtFVgpmPrDiwNpEe6bWu7wpQYA@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/2bcf2fa49b27b19177cc082e0a952ad4816bb2ff

Modified Files
--------------
src/backend/optimizer/path/joinpath.c | 34 ++++++++++++++++++--
src/test/regress/expected/memoize.out | 60 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/memoize.sql | 24 ++++++++++++++
3 files changed, 116 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-11-03 01:53:27 pgsql: Fix lost wakeups in physical WAL senders
Previous Message Heikki Linnakangas 2026-11-03 00:11:26 pgsql: Delay checkpoint start while creating a multixact