Subject: pgsql: Avoid dangling-pointer problem with partitionwise joins under GE

From: Tom Lane <[email-masked]>
Date: Fri, 23 Feb 2024 20:22:08 +0000

Avoid dangling-pointer problem with partitionwise joins under GEQO.

build_child_join_sjinfo creates a derived SpecialJoinInfo in
the short-lived GEQO context, but afterwards the semi_rhs_exprs
from that may be used in a UniquePath for a child base relation.
This breaks the expectation that all base-relation-level structures
are in the planning-lifespan context, leading to use of a dangling
pointer with probable ensuing crash later on in create_unique_plan.
To fix, copy the expression trees when making a UniquePath.

Per bug #18360 from Alexander Lakhin.  This has been broken since
partitionwise joins were added, so back-patch to all supported
branches.

Discussion: https://postgr.es/m/[email-masked]

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/37bbe3d3acd62bad24493356a98579bd92c8f563

Modified Files
--------------
src/backend/optimizer/util/pathnode.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)