When remove_useless_result_rtes() removes an RTE_RESULT, any PlaceHolderVars that were to be evaluated there must be evaluated somewhere else. If the RTE_RESULT is the nullable side of an outer join, there is no other place that would preserve the PHVs' nulling behavior, so find_dependent_phvs() refuses the removal if any such PHV exists anywhere in the query. If the RTE_RESULT is a child of an inner join, its PHVs can instead be evaluated at the join of all its sibling rels, unless a sibling needs them before that join is formed. find_dependent_phvs_in_jointree() checks for that by looking for such PHVs in the join quals and LATERAL RTEs of the siblings. That misses a PHV that is evaluated at some but not all of the sibling rels and contains one of the RTE_RESULT's PHVs. Such a PHV can appear anywhere in the query, such as in the targetlist. This can happen when pulled-up expressions are wrapped in PHVs because of outer joins or grouping sets. Once the RTE_RESULT is removed, the outer PHV needs a value that is only available at a join above its own evaluation level. The outer PHV's rel thus becomes laterally dependent on the other sibling rels. If one of those rels laterally references it in turn, we end up with a rel that is laterally dependent on itself, causing an assertion failure in create_lateral_join_info(). To fix, also search the whole query for PHVs that are evaluated at some but not all of the sibling rels, and refuse to remove the RTE_RESULT if any of them contains one of its PHVs. An outer PHV that is evaluated at all of the sibling rels is not a problem, since the moved PHV is available there, so we don't block removal in that common case. Back-patch to all supported branches. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com> Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com Backpatch-through: 14
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/18/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/18/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.