pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Fix wrong results from Memoize with a Param inside a cache key 7+ messages / 1 participants [nested] [flat]
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/708eb515812bf23ebc36a2c436eabe701b3c2f5d Modified Files -------------- src/backend/optimizer/path/joinpath.c | 112 ++++++++++++++++++++++-------- src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 2 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 292 insertions(+), 33 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/f39f3f97118c94edc57f473404e069ceaca4c9e0 Modified Files -------------- src/backend/optimizer/path/joinpath.c | 112 ++++++++++++++++++++++-------- src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 2 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 292 insertions(+), 33 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/dbe6cf5588da84ac7dd7268b3e95261475291f27 Modified Files -------------- src/backend/optimizer/path/joinpath.c | 112 ++++++++++++++++++++++-------- src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 2 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 292 insertions(+), 33 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ REL_15_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/a7a2cab5744741ccfd8124394be1ba31ab005f64 Modified Files -------------- src/backend/optimizer/path/joinpath.c | 97 ++++++++++++++++++++------ src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 3 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 286 insertions(+), 25 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ REL_14_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/de7d7d4df80acceca1195d549bd2887d5bc8ab26 Modified Files -------------- src/backend/optimizer/path/joinpath.c | 97 ++++++++++++++++++++------ src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 3 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 286 insertions(+), 25 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ REL_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/d6f60a031f21d57c6e574d5166d114b7d510f5e6 Modified Files -------------- src/backend/optimizer/path/joinpath.c | 112 ++++++++++++++++++++++-------- src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 2 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 292 insertions(+), 33 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix wrong results from Memoize with a Param inside a cache key @ 2026-11-02 01:33 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Richard Guo @ 2026-11-02 01:33 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix wrong results from Memoize with a Param inside a cache key ExecReScanMemoize() purges the whole cache only when a Param changes which is not part of the cache key; a change to a cache-key Param is assumed to be handled by looking up a different cache entry. To that end, create_memoize_plan() put every Param appearing in the cache key expressions into keyparamids. That assumption holds only when the cache key values determine the Param's value. It fails for an upper-level Param which is buried inside a larger cache-key expression and also appears elsewhere in the subplan. With a cache key of "t2.hundred + t0.ten" and a filter "t1.twenty = t0.ten", different (t2.hundred, t0.ten) pairs produce the same cache-key value while the filter selects different rows, so one cache entry wrongly serves both. Since t0.ten was included in keyparamids, the cache was never purged when it changed. Also, pull_paramids() collected PARAM_EXTERN Params too, whose numbers are unrelated to those of PARAM_EXEC Params. An external Param in a cache key could therefore keep a change to an unrelated executor Param from purging the cache. To fix, put into keyparamids only the Params whose values the cache keys determine. These are the nestloop Params which replace the outer rel's Vars in the cache keys, as the inner scan can only reference the outer rel through the cache keys, and, in binary mode only, a Param which is a cache key by itself. In logical mode the hash equality operator may not distinguish values that affect the results differently. Any other Param now purges the cache when it changes. Also make pull_paramids() collect PARAM_EXEC Params only. Purging alone would empty the cache on every rescan of the enclosing plan whenever a buried Param changes, even when the Param is used nowhere else and the old code was correct and kept its cache entries across rescans. So also make each buried Param a cache key by itself, which puts Memoize into binary mode, so that cache entries survive across rescans with the same Param value. A buried Param whose type can't be hashed is left to the purging, rather than giving up on Memoize. Back-patch to all supported branches. Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com> 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 Backpatch-through: 14 Branch ------ REL_16_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/b5ad785beb71f2c8581915648826aebc9839f946 Modified Files -------------- src/backend/optimizer/path/joinpath.c | 97 ++++++++++++++++++++------ src/backend/optimizer/plan/createplan.c | 27 +++++++- src/backend/optimizer/util/clauses.c | 6 +- src/include/nodes/execnodes.h | 3 +- src/include/nodes/plannodes.h | 2 +- src/test/regress/expected/memoize.out | 116 ++++++++++++++++++++++++++++++++ src/test/regress/sql/memoize.sql | 59 ++++++++++++++++ 7 files changed, 285 insertions(+), 25 deletions(-) ^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-11-02 01:33 UTC | newest] Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key Richard Guo <rguo@postgresql.org> 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key Richard Guo <rguo@postgresql.org> 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key Richard Guo <rguo@postgresql.org> 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key Richard Guo <rguo@postgresql.org> 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key Richard Guo <rguo@postgresql.org> 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key Richard Guo <rguo@postgresql.org> 2026-11-02 01:33 pgsql: Fix wrong results from Memoize with a Param inside a cache key 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