• Tom Lane's avatar
    Remove unnecessary output expressions from unflattened subqueries. · 55d5b3c0
    Tom Lane authored
    If a sub-select-in-FROM gets flattened into the upper query, then we
    naturally get rid of any output columns that are defined in the sub-select
    text but not actually used in the upper query.  However, this doesn't
    happen when it's not possible to flatten the subquery, for example because
    it contains GROUP BY, LIMIT, etc.  Allowing the subquery to compute useless
    output columns is often fairly harmless, but sometimes it has significant
    performance cost: the unused output might be an expensive expression,
    or it might be a Var from a relation that we could remove entirely (via
    the join-removal logic) if only we realized that we didn't really need
    that Var.  Situations like this are common when expanding views, so it
    seems worth taking the trouble to detect and remove unused outputs.
    
    Because the upper query's Var numbering for subquery references depends on
    positions in the subquery targetlist, we don't want to renumber the items
    we leave behind.  Instead, we can implement "removal" by replacing the
    unwanted expressions with simple NULL constants.  This wastes a few cycles
    at runtime, but not enough to justify more work in the planner.
    55d5b3c0
allpaths.c 68.6 KB