Avoid mislabeling of lateral references when pulling up a subquery. If we are pulling up a subquery that's under an outer join, and the subquery's target list contains a strict expression that uses both a subquery variable and a lateral-reference variable, it's okay to pull up the expression without wrapping it in a PlaceHolderVar. That's safe because if the subquery variable is forced to NULL by the outer join, the expression result will come out as NULL too, so we don't have to force that outcome by evaluating the expression below the outer join. It'd be correct to wrap in a PHV, but that can lead to very significantly worse plans, since we'd then have to use a nestloop plan to pass down the lateral reference to where the expression will be evaluated.
However, when we do that, we should not mark the lateral reference variable as being nulled by the outer join, because it isn't after we pull up the expression in this way. So the marking logic added by cb8e50a4a was incorrect in this detail, leading to "wrong varnullingrels" errors from the consistency-checking logic in setrefs.c. It seems to be sufficient to just not mark lateral references at all in this case. (I have a nagging feeling that more complexity may be needed in cases where there are several levels of outer join, but some attempts to break it with that didn't succeed.) Per report from Bertrand Mamasam. Back-patch to v16, as the previous patch was. Discussion: https://postgr.es/m/CACZ67_UA_EVrqiFXJu9XK50baEpH=ofepjswa2kfxg6xusw...@mail.gmail.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/5c9d8636d30d0838c7432c2c75096b4cd7801971 Modified Files -------------- src/backend/optimizer/prep/prepjointree.c | 19 +++++++++++-- src/test/regress/expected/subselect.out | 47 +++++++++++++++++++++++++++++++ src/test/regress/sql/subselect.sql | 16 +++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-)