diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2014-07-11 19:12:35 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2014-07-11 19:12:35 -0400 |
commit | d68581483564ec0fbfdc255e493c4f5bd165e55e (patch) | |
tree | 9ecf9da266f71de63515e2623f035c76f9bebe18 /src/backend/executor | |
parent | 80ddd04b4d66ad6564b6d77ffc2bd8fb5e0dcdfa (diff) | |
download | postgresql-d68581483564ec0fbfdc255e493c4f5bd165e55e.tar.gz postgresql-d68581483564ec0fbfdc255e493c4f5bd165e55e.zip |
Fix bug with whole-row references to append subplans.
ExecEvalWholeRowVar incorrectly supposed that it could "bless" the source
TupleTableSlot just once per query. But if the input is coming from an
Append (or, perhaps, other cases?) more than one slot might be returned
over the query run. This led to "record type has not been registered"
errors when a composite datum was extracted from a non-blessed slot.
This bug has been there a long time; I guess it escaped notice because when
dealing with subqueries the planner tends to expand whole-row Vars into
RowExprs, which don't have the same problem. It is possible to trigger
the problem in all active branches, though, as illustrated by the added
regression test.
Diffstat (limited to 'src/backend/executor')
-rw-r--r-- | src/backend/executor/execQual.c | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c index bc79e3aa00c..7cfa63f37dc 100644 --- a/src/backend/executor/execQual.c +++ b/src/backend/executor/execQual.c @@ -712,7 +712,6 @@ ExecEvalWholeRowVar(WholeRowVarExprState *wrvstate, ExprContext *econtext, { Var *variable = (Var *) wrvstate->xprstate.expr; TupleTableSlot *slot; - TupleDesc slot_tupdesc; bool needslow = false; if (isDone) @@ -804,25 +803,14 @@ ExecEvalWholeRowVar(WholeRowVarExprState *wrvstate, ExprContext *econtext, if (wrvstate->wrv_junkFilter != NULL) slot = ExecFilterJunk(wrvstate->wrv_junkFilter, slot); - slot_tupdesc = slot->tts_tupleDescriptor; - /* - * If it's a RECORD Var, we'll use the slot's type ID info. It's likely - * that the slot's type is also RECORD; if so, make sure it's been - * "blessed", so that the Datum can be interpreted later. - * * If the Var identifies a named composite type, we must check that the * actual tuple type is compatible with it. */ - if (variable->vartype == RECORDOID) - { - if (slot_tupdesc->tdtypeid == RECORDOID && - slot_tupdesc->tdtypmod < 0) - assign_record_type_typmod(slot_tupdesc); - } - else + if (variable->vartype != RECORDOID) { TupleDesc var_tupdesc; + TupleDesc slot_tupdesc; int i; /* @@ -839,6 +827,8 @@ ExecEvalWholeRowVar(WholeRowVarExprState *wrvstate, ExprContext *econtext, */ var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1); + slot_tupdesc = slot->tts_tupleDescriptor; + if (var_tupdesc->natts != slot_tupdesc->natts) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), @@ -896,6 +886,7 @@ ExecEvalWholeRowFast(WholeRowVarExprState *wrvstate, ExprContext *econtext, { Var *variable = (Var *) wrvstate->xprstate.expr; TupleTableSlot *slot; + TupleDesc slot_tupdesc; HeapTupleHeader dtuple; if (isDone) @@ -926,6 +917,20 @@ ExecEvalWholeRowFast(WholeRowVarExprState *wrvstate, ExprContext *econtext, slot = ExecFilterJunk(wrvstate->wrv_junkFilter, slot); /* + * If it's a RECORD Var, we'll use the slot's type ID info. It's likely + * that the slot's type is also RECORD; if so, make sure it's been + * "blessed", so that the Datum can be interpreted later. (Note: we must + * do this here, not in ExecEvalWholeRowVar, because some plan trees may + * return different slots at different times. We have to be ready to + * bless additional slots during the run.) + */ + slot_tupdesc = slot->tts_tupleDescriptor; + if (variable->vartype == RECORDOID && + slot_tupdesc->tdtypeid == RECORDOID && + slot_tupdesc->tdtypmod < 0) + assign_record_type_typmod(slot_tupdesc); + + /* * Copy the slot tuple and make sure any toasted fields get detoasted. */ dtuple = DatumGetHeapTupleHeader(ExecFetchSlotTupleDatum(slot)); |