1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-18 04:29:09 +03:00

Refactor addition of PlaceHolderVars to joinrel targetlists.

Make build_joinrel_tlist() responsible for adding PHVs that were
already computed in one or the other input relation, and therefore
change add_placeholders_to_joinrel() to only add PHVs that will be
newly computed in this joinrel's output.  This makes the handling
of PHVs in build_joinrel_tlist() more like its handling of plain
Vars, which seems like a good thing on intelligibility grounds
and will simplify planned future changes.  There is a purely
cosmetic side-effect that the order of entries in the joinrel's
tlist may change; but since it becomes more like the order of
entries in the input tlists, that's not bad.

The reason it wasn't done like this originally was the potential
cost of looking up PlaceHolderInfo entries to consult ph_needed.
Now that that's O(1) it shouldn't hurt.

Discussion: https://postgr.es/m/1405792.1660677844@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2022-08-17 16:12:23 -04:00
parent b3ff6c742f
commit afa0ec30bf
3 changed files with 36 additions and 19 deletions

View File

@@ -679,8 +679,9 @@ build_join_rel(PlannerInfo *root,
set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
/*
* Create a new tlist containing just the vars that need to be output from
* this join (ie, are needed for higher joinclauses or final output).
* Fill the joinrel's tlist with just the Vars and PHVs that need to be
* output from this join (ie, are needed for higher joinclauses or final
* output).
*
* NOTE: the tlist order for a join rel will depend on which pair of outer
* and inner rels we first try to build it from. But the contents should
@@ -966,6 +967,7 @@ min_join_parameterization(PlannerInfo *root,
* The join's targetlist includes all Vars of its member relations that
* will still be needed above the join. This subroutine adds all such
* Vars from the specified input rel's tlist to the join rel's tlist.
* Likewise for any PlaceHolderVars emitted by the input rel.
*
* We also compute the expected width of the join's output, making use
* of data that was cached at the baserel level by set_rel_width().
@@ -982,11 +984,24 @@ build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel,
Var *var = (Var *) lfirst(vars);
/*
* Ignore PlaceHolderVars in the input tlists; we'll make our own
* decisions about whether to copy them.
* For a PlaceHolderVar, we have to look up the PlaceHolderInfo.
*/
if (IsA(var, PlaceHolderVar))
{
PlaceHolderVar *phv = (PlaceHolderVar *) var;
PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
/* Is it still needed above this joinrel? */
if (bms_nonempty_difference(phinfo->ph_needed, relids))
{
/* Yup, add it to the output */
joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs,
phv);
/* Bubbling up the precomputed result has cost zero */
joinrel->reltarget->width += phinfo->ph_width;
}
continue;
}
/*
* Otherwise, anything in a baserel or joinrel targetlist ought to be