pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Make Memoize cache keys only of Params from outer query levels
7+ messages / 1 participants
[nested] [flat]

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0fd0e501a08fe5481bb692d4ac8bf10901b88fb2

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(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
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(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_17_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_16_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/025dac1c92cfa1158e54ccd7fea74f3bb7133b24

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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Make Memoize cache keys only of Params from outer query levels
@ 2026-11-03 00:52 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-11-03 00:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/63e261b0bb37a4fc4f7d2425c64ac6cf7efc54ac

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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2026-11-03 00:52 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>
2026-11-03 00:52 pgsql: Make Memoize cache keys only of Params from outer query levels Richard Guo <rguo@postgresql.org>

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