pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Make cache keys of other Params a Memoize subplan depends on
Date: Mon, 02 Nov 2026 01:33:58 +0000
Message-ID: <E1xNgvu-00000001jER-3zBe@gemulon.postgresql.org> (raw)

Make cache keys of other Params a Memoize subplan depends on

A Memoize node purges its whole cache whenever a Param changes which
the cache keys don't determine, such as an upper-level Param used in a
base qual of the memoized relation.  Under a correlated subquery, such
a Param changes on every invocation of the subplan, so nothing in the
cache survives from one invocation to the next.

To improve that, make cache keys of the upper-level Params found in
the memoized relation's base quals and targetlist, and on the inner
side of the join clauses, as is already done for Params buried inside
a cache key expression.  Cache entries then survive across invocations
with the same Param value.

This doesn't pay off when the Param's values don't recur across
invocations.  Cache entries for earlier values are then kept rather
than purged, and they only take up memory until the cache fills up and
evicts them.  The planner can't tell the two cases apart, as it costs
Memoize per invocation starting from an empty cache.  We accept that,
since the cache's memory is bounded by its size limit either way, and
evicting a stale entry costs about the same as purging it.

This is only an optimization, as any Param we don't find here still
purges the cache when it changes.  Hence we needn't track down every
last place a Param can appear, and a Param whose type can't be hashed
is simply left to the purging.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Yilin Zhang <jiezhilove@126.com>
Discussion: https://postgr.es/m/CA+COZaAHZT7T2G2F0-drdQB+StWJxOzqeDxp=ESp+YUiy-kOaQ@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f8da64e6ddfd5f33f1d70b3ae6f8561f1de09368

Modified Files
--------------
src/backend/optimizer/path/joinpath.c | 39 +++++++++++++++++++++++++++++------
src/test/regress/expected/memoize.out | 10 ++++-----
src/test/regress/sql/memoize.sql      |  6 +++---
3 files changed, 41 insertions(+), 14 deletions(-)



Message-ID: <E1xNgvu-00000001jER-3zBe@gemulon.postgresql.org>
Permalink:  ../E1xNgvu-00000001jER-3zBe@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xNgvu-00000001jER-3zBe@gemulon.postgresql.org

 · 

reply

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 cache keys of other Params a Memoize subplan depends on
  In-Reply-To: <E1xNgvu-00000001jER-3zBe@gemulon.postgresql.org>
  References: <E1xNgvu-00000001jER-3zBe@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