diff options
Diffstat (limited to 'src/backend/optimizer/util')
-rw-r--r-- | src/backend/optimizer/util/placeholder.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/backend/optimizer/util/placeholder.c b/src/backend/optimizer/util/placeholder.c index 317a01940e3..345a0277a50 100644 --- a/src/backend/optimizer/util/placeholder.c +++ b/src/backend/optimizer/util/placeholder.c @@ -250,6 +250,8 @@ void mark_placeholder_maybe_needed(PlannerInfo *root, PlaceHolderInfo *phinfo, Relids relids) { + Relids est_eval_level; + /* Mark the PHV as possibly needed at the given syntactic level */ phinfo->ph_may_need = bms_add_members(phinfo->ph_may_need, relids); @@ -258,11 +260,18 @@ mark_placeholder_maybe_needed(PlannerInfo *root, PlaceHolderInfo *phinfo, * lower-level PHVs. We need to get those into the PlaceHolderInfo list, * but they aren't going to be needed where the outer PHV is referenced. * Rather, they'll be needed where the outer PHV is evaluated. We can - * estimate that (conservatively) as the syntactic location of the PHV's - * expression. Recurse to take care of any such PHVs. + * estimate that conservatively as the syntactic location of the PHV's + * expression, but not less than the level of any Vars it contains. + * (Normally the Vars would come from below the syntactic location anyway, + * but this might not be true if the PHV contains any LATERAL references.) */ + est_eval_level = bms_union(phinfo->ph_var->phrels, phinfo->ph_eval_at); + + /* Now recurse to take care of any such PHVs */ mark_placeholders_in_expr(root, (Node *) phinfo->ph_var->phexpr, - phinfo->ph_var->phrels); + est_eval_level); + + bms_free(est_eval_level); } /* |