diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-03-17 18:18:05 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-03-17 18:18:05 -0400 |
commit | 1f5ef5ae0806cb75bb0297e46e41765ffa426252 (patch) | |
tree | 724b46877be0f908c3163cc00d6b369ec501872d /src/backend/executor/execExprInterp.c | |
parent | c918f07dd47963ed4602636d5d9d1792da11515b (diff) | |
download | postgresql-1f5ef5ae0806cb75bb0297e46e41765ffa426252.tar.gz postgresql-1f5ef5ae0806cb75bb0297e46e41765ffa426252.zip |
Revert applying column aliases to the output of whole-row Vars.
In commit bf7ca1587, I had the bright idea that we could make the
result of a whole-row Var (that is, foo.*) track any column aliases
that had been applied to the FROM entry the Var refers to. However,
that's not terribly logically consistent, because now the output of
the Var is no longer of the named composite type that the Var claims
to emit. bf7ca1587 tried to handle that by changing the output
tuple values to be labeled with a blessed RECORD type, but that's
really pretty disastrous: we can wind up storing such tuples onto
disk, whereupon they're not readable by other sessions.
The only practical fix I can see is to give up on what bf7ca1587
tried to do, and say that the column names of tuples produced by
a whole-row Var are always those of the underlying named composite
type, query aliases or no. While this introduces some inconsistencies,
it removes others, so it's not that awful in the abstract. What *is*
kind of awful is to make such a behavioral change in a back-patched
bug fix. But corrupt data is worse, so back-patched it will be.
(A workaround available to anyone who's unhappy about this is to
introduce an extra level of sub-SELECT, so that the whole-row Var is
referring to the sub-SELECT's output and not to a named table type.
Then the Var is of type RECORD to begin with and there's no issue.)
Per report from Miles Delahunty. The faulty commit dates to 9.5,
so back-patch to all supported branches.
Discussion: https://postgr.es/m/2950001.1638729947@sss.pgh.pa.us
Diffstat (limited to 'src/backend/executor/execExprInterp.c')
-rw-r--r-- | src/backend/executor/execExprInterp.c | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c index dac514548ca..0999bba4217 100644 --- a/src/backend/executor/execExprInterp.c +++ b/src/backend/executor/execExprInterp.c @@ -3917,12 +3917,8 @@ ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext) * generates an INT4 NULL regardless of the dropped column type). * If we find a dropped column and cannot verify that case (1) * holds, we have to use the slow path to check (2) for each row. - * - * If vartype is a domain over composite, just look through that - * to the base composite type. */ - var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype, - -1, false); + var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1); slot_tupdesc = slot->tts_tupleDescriptor; @@ -3959,9 +3955,8 @@ ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext) /* * Use the variable's declared rowtype as the descriptor for the - * output values, modulo possibly assigning new column names - * below. In particular, we *must* absorb any attisdropped - * markings. + * output values. In particular, we *must* absorb any + * attisdropped markings. */ oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); output_tupdesc = CreateTupleDescCopy(var_tupdesc); @@ -3979,39 +3974,38 @@ ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext) oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor); MemoryContextSwitchTo(oldcontext); - } - /* - * Construct a tuple descriptor for the composite values we'll - * produce, and make sure its record type is "blessed". The main - * reason to do this is to be sure that operations such as - * row_to_json() will see the desired column names when they look up - * the descriptor from the type information embedded in the composite - * values. - * - * We already got the correct physical datatype info above, but now we - * should try to find the source RTE and adopt its column aliases, in - * case they are different from the original rowtype's names. For - * example, in "SELECT foo(t) FROM tab t(x,y)", the first two columns - * in the composite output should be named "x" and "y" regardless of - * tab's column names. - * - * If we can't locate the RTE, assume the column names we've got are - * OK. (As of this writing, the only cases where we can't locate the - * RTE are in execution of trigger WHEN clauses, and then the Var will - * have the trigger's relation's rowtype, so its names are fine.) - * Also, if the creator of the RTE didn't bother to fill in an eref - * field, assume our column names are OK. (This happens in COPY, and - * perhaps other places.) - */ - if (econtext->ecxt_estate && - variable->varno <= econtext->ecxt_estate->es_range_table_size) - { - RangeTblEntry *rte = exec_rt_fetch(variable->varno, - econtext->ecxt_estate); + /* + * It's possible that the input slot is a relation scan slot and + * so is marked with that relation's rowtype. But we're supposed + * to be returning RECORD, so reset to that. + */ + output_tupdesc->tdtypeid = RECORDOID; + output_tupdesc->tdtypmod = -1; - if (rte->eref) - ExecTypeSetColNames(output_tupdesc, rte->eref->colnames); + /* + * We already got the correct physical datatype info above, but + * now we should try to find the source RTE and adopt its column + * aliases, since it's unlikely that the input slot has the + * desired names. + * + * If we can't locate the RTE, assume the column names we've got + * are OK. (As of this writing, the only cases where we can't + * locate the RTE are in execution of trigger WHEN clauses, and + * then the Var will have the trigger's relation's rowtype, so its + * names are fine.) Also, if the creator of the RTE didn't bother + * to fill in an eref field, assume our column names are OK. (This + * happens in COPY, and perhaps other places.) + */ + if (econtext->ecxt_estate && + variable->varno <= econtext->ecxt_estate->es_range_table_size) + { + RangeTblEntry *rte = exec_rt_fetch(variable->varno, + econtext->ecxt_estate); + + if (rte->eref) + ExecTypeSetColNames(output_tupdesc, rte->eref->colnames); + } } /* Bless the tupdesc if needed, and save it in the execution state */ |