Improve NestLoopParam generation for lateral subqueries
authorDavid Rowley <drowley@postgresql.org>
Fri, 26 Jan 2024 03:18:58 +0000 (16:18 +1300)
committerDavid Rowley <drowley@postgresql.org>
Fri, 26 Jan 2024 03:18:58 +0000 (16:18 +1300)
commit2cca95e175463a7af95164498b889b1ea118583d
tree46ffb31025a4a662b3b3e7ef6763062ae4061480
parentf2743a7d70e7b2891277632121bb51e739743a47
Improve NestLoopParam generation for lateral subqueries

It was possible in cases where we had a LATERAL joined subquery that
when the same Var is mentioned in both the lateral references and in the
outer Vars of the scan clauses that the given Var wouldn't be assigned
to the same NestLoopParam.

This could cause issues in Memoize as the cache key would reference the
Var for the scan clauses but when the parameter for the lateral references
changed some code in Memoize would see that some other parameter had
changed that's not part of the cache key and end up purging the entire
cache as a result, thinking the cache had become stale.  This could
result in a Nested Loop -> Memoize plan being quite inefficient as, in
the worst case, the cache purging could result in never getting a cache
hit.  In no cases could this problem lead to incorrect query results.

Here we switch the order of operations so that we create NestLoopParam
for the lateral references first before doing replace_nestloop_params().
replace_nestloop_params() will find and reuse the existing NestLoopParam
in cases where the Var exists in both locations.

Author: Richard Guo
Reviewed-by: Tom Lane, David Rowley
Discussion: https://postgr.es/m/CAMbWs48XHJEK1Q1CzAQ7L9sTANTs9W1cepXu8%3DKc0quUL%2Btg4Q%40mail.gmail.com
src/backend/optimizer/plan/createplan.c
src/test/regress/expected/memoize.out
src/test/regress/sql/memoize.sql