• Tom Lane's avatar
    Ensure that RowExprs and whole-row Vars produce the expected column names. · bf7ca158
    Tom Lane authored
    At one time it wasn't terribly important what column names were associated
    with the fields of a composite Datum, but since the introduction of
    operations like row_to_json(), it's important that looking up the rowtype
    ID embedded in the Datum returns the column names that users would expect.
    That did not work terribly well before this patch: you could get the column
    names of the underlying table, or column aliases from any level of the
    query, depending on minor details of the plan tree.  You could even get
    totally empty field names, which is disastrous for cases like row_to_json().
    
    To fix this for whole-row Vars, look to the RTE referenced by the Var, and
    make sure its column aliases are applied to the rowtype associated with
    the result Datums.  This is a tad scary because we might have to return
    a transient RECORD type even though the Var is declared as having some
    named rowtype.  In principle it should be all right because the record
    type will still be physically compatible with the named rowtype; but
    I had to weaken one Assert in ExecEvalConvertRowtype, and there might be
    third-party code containing similar assumptions.
    
    Similarly, RowExprs have to be willing to override the column names coming
    from a named composite result type and produce a RECORD when the column
    aliases visible at the site of the RowExpr differ from the underlying
    table's column names.
    
    In passing, revert the decision made in commit 398f70ec to add
    an alias-list argument to ExecTypeFromExprList: better to provide that
    functionality in a separate function.  This also reverts most of the code
    changes in d6858148, which we don't need because we're no longer
    depending on the tupdesc found in the child plan node's result slot to be
    blessed.
    
    Back-patch to 9.4, but not earlier, since this solution changes the results
    in some cases that users might not have realized were buggy.  We'll apply a
    more restricted form of this patch in older branches.
    bf7ca158
execTuples.c 36.9 KB