Ensure that RowExprs and whole-row Vars produce the expected column names

Enterprise / PostgreSQL - Tom Lane [sss.pgh.pa.us] - 10 November 2014 14:21 UTC

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 398f70ec070fe601 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 d68581483564ec0f, 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.

bf7ca15 Ensure that RowExprs and whole-row Vars produce the expected column names.
src/backend/executor/execQual.c | 115 ++++++++++++++--------
src/backend/executor/execTuples.c | 65 +++++++++++--
src/backend/executor/nodeFunctionscan.c | 19 ----
src/backend/executor/nodeValuesscan.c | 6 +-
src/include/executor/executor.h | 3 +-
src/include/nodes/execnodes.h | 1 +
src/test/regress/expected/rowtypes.out | 160 +++++++++++++++++++++++++++++++
src/test/regress/sql/rowtypes.sql | 44 +++++++++
8 files changed, 340 insertions(+), 73 deletions(-)

Upstream: git.postgresql.org


  • Share