diff options
| author | Andres Freund | 2017-08-20 18:19:07 +0000 |
|---|---|---|
| committer | Andres Freund | 2017-08-20 18:19:07 +0000 |
| commit | 2cd70845240087da205695baedab6412342d1dbe (patch) | |
| tree | 20a3b6a2231dae248218ac54983c7a854328265f /src/pl | |
| parent | b1c2d76a2fcef812af0be3343082414d401909c8 (diff) | |
Change tupledesc->attrs[n] to TupleDescAttr(tupledesc, n).
This is a mechanical change in preparation for a later commit that
will change the layout of TupleDesc. Introducing a macro to abstract
the details of where attributes are stored will allow us to change
that in separate step and revise it in future.
Author: Thomas Munro, editorialized by Andres Freund
Reviewed-By: Andres Freund
Discussion: https://postgr.es/m/CAEepm=0ZtQ-SpsgCyzzYpsXS6e=kZWqk3g5Ygn3MDV7A8dabUA@mail.gmail.com
Diffstat (limited to 'src/pl')
| -rw-r--r-- | src/pl/plperl/plperl.c | 26 | ||||
| -rw-r--r-- | src/pl/plpgsql/src/pl_comp.c | 2 | ||||
| -rw-r--r-- | src/pl/plpgsql/src/pl_exec.c | 50 | ||||
| -rw-r--r-- | src/pl/plpython/plpy_exec.c | 6 | ||||
| -rw-r--r-- | src/pl/plpython/plpy_resultobject.c | 18 | ||||
| -rw-r--r-- | src/pl/plpython/plpy_typeio.c | 41 | ||||
| -rw-r--r-- | src/pl/tcl/pltcl.c | 23 |
7 files changed, 104 insertions, 62 deletions
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index afebec910d6..5a575bdbe4e 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -1095,6 +1095,7 @@ plperl_build_tuple_result(HV *perlhash, TupleDesc td) SV *val = HeVAL(he); char *key = hek2cstr(he); int attn = SPI_fnumber(td, key); + Form_pg_attribute attr = TupleDescAttr(td, attn - 1); if (attn == SPI_ERROR_NOATTRIBUTE) ereport(ERROR, @@ -1108,8 +1109,8 @@ plperl_build_tuple_result(HV *perlhash, TupleDesc td) key))); values[attn - 1] = plperl_sv_to_datum(val, - td->attrs[attn - 1]->atttypid, - td->attrs[attn - 1]->atttypmod, + attr->atttypid, + attr->atttypmod, NULL, NULL, InvalidOid, @@ -1757,6 +1758,7 @@ plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup) char *key = hek2cstr(he); SV *val = HeVAL(he); int attn = SPI_fnumber(tupdesc, key); + Form_pg_attribute attr = TupleDescAttr(tupdesc, attn - 1); if (attn == SPI_ERROR_NOATTRIBUTE) ereport(ERROR, @@ -1770,8 +1772,8 @@ plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup) key))); modvalues[attn - 1] = plperl_sv_to_datum(val, - tupdesc->attrs[attn - 1]->atttypid, - tupdesc->attrs[attn - 1]->atttypmod, + attr->atttypid, + attr->atttypmod, NULL, NULL, InvalidOid, @@ -3014,11 +3016,12 @@ plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc) typisvarlena; char *attname; Oid typoutput; + Form_pg_attribute att = TupleDescAttr(tupdesc, i); - if (tupdesc->attrs[i]->attisdropped) + if (att->attisdropped) continue; - attname = NameStr(tupdesc->attrs[i]->attname); + attname = NameStr(att->attname); attr = heap_getattr(tuple, i + 1, tupdesc, &isnull); if (isnull) @@ -3032,7 +3035,7 @@ plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc) continue; } - if (type_is_rowtype(tupdesc->attrs[i]->atttypid)) + if (type_is_rowtype(att->atttypid)) { SV *sv = plperl_hash_from_datum(attr); @@ -3043,17 +3046,16 @@ plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc) SV *sv; Oid funcid; - if (OidIsValid(get_base_element_type(tupdesc->attrs[i]->atttypid))) - sv = plperl_ref_from_pg_array(attr, tupdesc->attrs[i]->atttypid); - else if ((funcid = get_transform_fromsql(tupdesc->attrs[i]->atttypid, current_call_data->prodesc->lang_oid, current_call_data->prodesc->trftypes))) + if (OidIsValid(get_base_element_type(att->atttypid))) + sv = plperl_ref_from_pg_array(attr, att->atttypid); + else if ((funcid = get_transform_fromsql(att->atttypid, current_call_data->prodesc->lang_oid, current_call_data->prodesc->trftypes))) sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, attr)); else { char *outputstr; /* XXX should have a way to cache these lookups */ - getTypeOutputInfo(tupdesc->attrs[i]->atttypid, - &typoutput, &typisvarlena); + getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena); outputstr = OidOutputFunctionCall(typoutput, attr); sv = cstr2sv(outputstr); diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c index 662b3c97d7f..e9d7ef55e97 100644 --- a/src/pl/plpgsql/src/pl_comp.c +++ b/src/pl/plpgsql/src/pl_comp.c @@ -2012,7 +2012,7 @@ build_row_from_class(Oid classOid) /* * Get the attribute and check for dropped column */ - attrStruct = row->rowtupdesc->attrs[i]; + attrStruct = TupleDescAttr(row->rowtupdesc, i); if (!attrStruct->attisdropped) { diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 616f5e30f81..97166972594 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -2841,6 +2841,7 @@ exec_stmt_return_next(PLpgSQL_execstate *estate, PLpgSQL_var *var = (PLpgSQL_var *) retvar; Datum retval = var->value; bool isNull = var->isnull; + Form_pg_attribute attr = TupleDescAttr(tupdesc, 0); if (natts != 1) ereport(ERROR, @@ -2858,8 +2859,8 @@ exec_stmt_return_next(PLpgSQL_execstate *estate, &isNull, var->datatype->typoid, var->datatype->atttypmod, - tupdesc->attrs[0]->atttypid, - tupdesc->attrs[0]->atttypmod); + attr->atttypid, + attr->atttypmod); tuplestore_putvalues(estate->tuple_store, tupdesc, &retval, &isNull); @@ -2968,6 +2969,8 @@ exec_stmt_return_next(PLpgSQL_execstate *estate, } else { + Form_pg_attribute attr = TupleDescAttr(tupdesc, 0); + /* Simple scalar result */ if (natts != 1) ereport(ERROR, @@ -2980,8 +2983,8 @@ exec_stmt_return_next(PLpgSQL_execstate *estate, &isNull, rettype, rettypmod, - tupdesc->attrs[0]->atttypid, - tupdesc->attrs[0]->atttypmod); + attr->atttypid, + attr->atttypmod); tuplestore_putvalues(estate->tuple_store, tupdesc, &retval, &isNull); @@ -4588,8 +4591,8 @@ exec_assign_value(PLpgSQL_execstate *estate, * Now insert the new value, being careful to cast it to the * right type. */ - atttype = rec->tupdesc->attrs[fno - 1]->atttypid; - atttypmod = rec->tupdesc->attrs[fno - 1]->atttypmod; + atttype = TupleDescAttr(rec->tupdesc, fno - 1)->atttypid; + atttypmod = TupleDescAttr(rec->tupdesc, fno - 1)->atttypmod; values[0] = exec_cast_value(estate, value, &isNull, @@ -4913,7 +4916,11 @@ exec_eval_datum(PLpgSQL_execstate *estate, rec->refname, recfield->fieldname))); *typeid = SPI_gettypeid(rec->tupdesc, fno); if (fno > 0) - *typetypmod = rec->tupdesc->attrs[fno - 1]->atttypmod; + { + Form_pg_attribute attr = TupleDescAttr(rec->tupdesc, fno - 1); + + *typetypmod = attr->atttypmod; + } else *typetypmod = -1; *value = SPI_getbinval(rec->tup, rec->tupdesc, fno, isnull); @@ -5089,11 +5096,19 @@ plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate, rec->refname, recfield->fieldname))); *typeid = SPI_gettypeid(rec->tupdesc, fno); if (fno > 0) - *typmod = rec->tupdesc->attrs[fno - 1]->atttypmod; + { + Form_pg_attribute attr = TupleDescAttr(rec->tupdesc, fno - 1); + + *typmod = attr->atttypmod; + } else *typmod = -1; if (fno > 0) - *collation = rec->tupdesc->attrs[fno - 1]->attcollation; + { + Form_pg_attribute attr = TupleDescAttr(rec->tupdesc, fno - 1); + + *collation = attr->attcollation; + } else /* no system column types have collation */ *collation = InvalidOid; break; @@ -5172,6 +5187,7 @@ exec_eval_expr(PLpgSQL_execstate *estate, { Datum result = 0; int rc; + Form_pg_attribute attr; /* * If first time through, create a plan for this expression. @@ -5211,8 +5227,9 @@ exec_eval_expr(PLpgSQL_execstate *estate, /* * ... and get the column's datatype. */ - *rettype = estate->eval_tuptable->tupdesc->attrs[0]->atttypid; - *rettypmod = estate->eval_tuptable->tupdesc->attrs[0]->atttypmod; + attr = TupleDescAttr(estate->eval_tuptable->tupdesc, 0); + *rettype = attr->atttypid; + *rettypmod = attr->atttypmod; /* * If there are no rows selected, the result is a NULL of that type. @@ -6030,7 +6047,8 @@ exec_move_row(PLpgSQL_execstate *estate, var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]); - while (anum < td_natts && tupdesc->attrs[anum]->attisdropped) + while (anum < td_natts && + TupleDescAttr(tupdesc, anum)->attisdropped) anum++; /* skip dropped column in tuple */ if (anum < td_natts) @@ -6042,8 +6060,8 @@ exec_move_row(PLpgSQL_execstate *estate, value = (Datum) 0; isnull = true; } - valtype = tupdesc->attrs[anum]->atttypid; - valtypmod = tupdesc->attrs[anum]->atttypmod; + valtype = TupleDescAttr(tupdesc, anum)->atttypid; + valtypmod = TupleDescAttr(tupdesc, anum)->atttypmod; anum++; } else @@ -6095,7 +6113,7 @@ make_tuple_from_row(PLpgSQL_execstate *estate, Oid fieldtypeid; int32 fieldtypmod; - if (tupdesc->attrs[i]->attisdropped) + if (TupleDescAttr(tupdesc, i)->attisdropped) { nulls[i] = true; /* leave the column as null */ continue; @@ -6106,7 +6124,7 @@ make_tuple_from_row(PLpgSQL_execstate *estate, exec_eval_datum(estate, estate->datums[row->varnos[i]], &fieldtypeid, &fieldtypmod, &dvalues[i], &nulls[i]); - if (fieldtypeid != tupdesc->attrs[i]->atttypid) + if (fieldtypeid != TupleDescAttr(tupdesc, i)->atttypid) return NULL; /* XXX should we insist on typmod match, too? */ } diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index c6938d00aa4..26f61dd0f37 100644 --- a/src/pl/plpython/plpy_exec.c +++ b/src/pl/plpython/plpy_exec.c @@ -950,6 +950,7 @@ PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, char *plattstr; int attn; PLyObToDatum *att; + Form_pg_attribute attr; platt = PyList_GetItem(plkeys, i); if (PyString_Check(platt)) @@ -982,11 +983,12 @@ PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, Py_INCREF(plval); + attr = TupleDescAttr(tupdesc, attn - 1); if (plval != Py_None) { modvalues[attn - 1] = (att->func) (att, - tupdesc->attrs[attn - 1]->atttypmod, + attr->atttypmod, plval, false); modnulls[attn - 1] = false; @@ -997,7 +999,7 @@ PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, InputFunctionCall(&att->typfunc, NULL, att->typioparam, - tupdesc->attrs[attn - 1]->atttypmod); + attr->atttypmod); modnulls[attn - 1] = true; } modrepls[attn - 1] = true; diff --git a/src/pl/plpython/plpy_resultobject.c b/src/pl/plpython/plpy_resultobject.c index 077bde6dc35..098a366f6fa 100644 --- a/src/pl/plpython/plpy_resultobject.c +++ b/src/pl/plpython/plpy_resultobject.c @@ -148,7 +148,11 @@ PLy_result_colnames(PyObject *self, PyObject *unused) list = PyList_New(ob->tupdesc->natts); for (i = 0; i < ob->tupdesc->natts; i++) - PyList_SET_ITEM(list, i, PyString_FromString(NameStr(ob->tupdesc->attrs[i]->attname))); + { + Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i); + + PyList_SET_ITEM(list, i, PyString_FromString(NameStr(attr->attname))); + } return list; } @@ -168,7 +172,11 @@ PLy_result_coltypes(PyObject *self, PyObject *unused) list = PyList_New(ob->tupdesc->natts); for (i = 0; i < ob->tupdesc->natts; i++) - PyList_SET_ITEM(list, i, PyInt_FromLong(ob->tupdesc->attrs[i]->atttypid)); + { + Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i); + + PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypid)); + } return list; } @@ -188,7 +196,11 @@ PLy_result_coltypmods(PyObject *self, PyObject *unused) list = PyList_New(ob->tupdesc->natts); for (i = 0; i < ob->tupdesc->natts; i++) - PyList_SET_ITEM(list, i, PyInt_FromLong(ob->tupdesc->attrs[i]->atttypmod)); + { + Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i); + + PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypmod)); + } return list; } diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c index 91ddcaa7b9c..e4af8cc9ef0 100644 --- a/src/pl/plpython/plpy_typeio.c +++ b/src/pl/plpython/plpy_typeio.c @@ -152,21 +152,21 @@ PLy_input_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc) for (i = 0; i < desc->natts; i++) { HeapTuple typeTup; + Form_pg_attribute attr = TupleDescAttr(desc, i); - if (desc->attrs[i]->attisdropped) + if (attr->attisdropped) continue; - if (arg->in.r.atts[i].typoid == desc->attrs[i]->atttypid) + if (arg->in.r.atts[i].typoid == attr->atttypid) continue; /* already set up this entry */ - typeTup = SearchSysCache1(TYPEOID, - ObjectIdGetDatum(desc->attrs[i]->atttypid)); + typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(attr->atttypid)); if (!HeapTupleIsValid(typeTup)) elog(ERROR, "cache lookup failed for type %u", - desc->attrs[i]->atttypid); + attr->atttypid); PLy_input_datum_func2(&(arg->in.r.atts[i]), arg->mcxt, - desc->attrs[i]->atttypid, + attr->atttypid, typeTup, exec_ctx->curr_proc->langid, exec_ctx->curr_proc->trftypes); @@ -224,18 +224,18 @@ PLy_output_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc) for (i = 0; i < desc->natts; i++) { HeapTuple typeTup; + Form_pg_attribute attr = TupleDescAttr(desc, i); - if (desc->attrs[i]->attisdropped) + if (attr->attisdropped) continue; - if (arg->out.r.atts[i].typoid == desc->attrs[i]->atttypid) + if (arg->out.r.atts[i].typoid == attr->atttypid) continue; /* already set up this entry */ - typeTup = SearchSysCache1(TYPEOID, - ObjectIdGetDatum(desc->attrs[i]->atttypid)); + typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(attr->atttypid)); if (!HeapTupleIsValid(typeTup)) elog(ERROR, "cache lookup failed for type %u", - desc->attrs[i]->atttypid); + attr->atttypid); PLy_output_datum_func2(&(arg->out.r.atts[i]), arg->mcxt, typeTup, exec_ctx->curr_proc->langid, @@ -306,11 +306,12 @@ PLyDict_FromTuple(PLyTypeInfo *info, HeapTuple tuple, TupleDesc desc) Datum vattr; bool is_null; PyObject *value; + Form_pg_attribute attr = TupleDescAttr(desc, i); - if (desc->attrs[i]->attisdropped) + if (attr->attisdropped) continue; - key = NameStr(desc->attrs[i]->attname); + key = NameStr(attr->attname); vattr = heap_getattr(tuple, (i + 1), desc, &is_null); if (is_null || info->in.r.atts[i].func == NULL) @@ -1183,15 +1184,16 @@ PLyMapping_ToComposite(PLyTypeInfo *info, TupleDesc desc, PyObject *mapping) char *key; PyObject *volatile value; PLyObToDatum *att; + Form_pg_attribute attr = TupleDescAttr(desc, i); - if (desc->attrs[i]->attisdropped) + if (attr->attisdropped) { values[i] = (Datum) 0; nulls[i] = true; continue; } - key = NameStr(desc->attrs[i]->attname); + key = NameStr(attr->attname); value = NULL; att = &info->out.r.atts[i]; PG_TRY(); @@ -1256,7 +1258,7 @@ PLySequence_ToComposite(PLyTypeInfo *info, TupleDesc desc, PyObject *sequence) idx = 0; for (i = 0; i < desc->natts; i++) { - if (!desc->attrs[i]->attisdropped) + if (!TupleDescAttr(desc, i)->attisdropped) idx++; } if (PySequence_Length(sequence) != idx) @@ -1277,7 +1279,7 @@ PLySequence_ToComposite(PLyTypeInfo *info, TupleDesc desc, PyObject *sequence) PyObject *volatile value; PLyObToDatum *att; - if (desc->attrs[i]->attisdropped) + if (TupleDescAttr(desc, i)->attisdropped) { values[i] = (Datum) 0; nulls[i] = true; @@ -1346,15 +1348,16 @@ PLyGenericObject_ToComposite(PLyTypeInfo *info, TupleDesc desc, PyObject *object char *key; PyObject *volatile value; PLyObToDatum *att; + Form_pg_attribute attr = TupleDescAttr(desc, i); - if (desc->attrs[i]->attisdropped) + if (attr->attisdropped) { values[i] = (Datum) 0; nulls[i] = true; continue; } - key = NameStr(desc->attrs[i]->attname); + key = NameStr(attr->attname); value = NULL; att = &info->out.r.atts[i]; PG_TRY(); diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c index ed494e12108..09f87ec7916 100644 --- a/src/pl/tcl/pltcl.c +++ b/src/pl/tcl/pltcl.c @@ -1106,11 +1106,13 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj()); for (i = 0; i < tupdesc->natts; i++) { - if (tupdesc->attrs[i]->attisdropped) + Form_pg_attribute att = TupleDescAttr(tupdesc, i); + + if (att->attisdropped) Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj()); else Tcl_ListObjAppendElement(NULL, tcl_trigtup, - Tcl_NewStringObj(utf_e2u(NameStr(tupdesc->attrs[i]->attname)), -1)); + Tcl_NewStringObj(utf_e2u(NameStr(att->attname)), -1)); } Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup); @@ -2952,15 +2954,17 @@ pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname, for (i = 0; i < tupdesc->natts; i++) { + Form_pg_attribute att = TupleDescAttr(tupdesc, i); + /* ignore dropped attributes */ - if (tupdesc->attrs[i]->attisdropped) + if (att->attisdropped) continue; /************************************************************ * Get the attribute name ************************************************************/ UTF_BEGIN; - attname = pstrdup(UTF_E2U(NameStr(tupdesc->attrs[i]->attname))); + attname = pstrdup(UTF_E2U(NameStr(att->attname))); UTF_END; /************************************************************ @@ -2978,8 +2982,7 @@ pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname, ************************************************************/ if (!isnull) { - getTypeOutputInfo(tupdesc->attrs[i]->atttypid, - &typoutput, &typisvarlena); + getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena); outputstr = OidOutputFunctionCall(typoutput, attr); UTF_BEGIN; Tcl_SetVar2Ex(interp, *arrptr, *nameptr, @@ -3013,14 +3016,16 @@ pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc) for (i = 0; i < tupdesc->natts; i++) { + Form_pg_attribute att = TupleDescAttr(tupdesc, i); + /* ignore dropped attributes */ - if (tupdesc->attrs[i]->attisdropped) + if (att->attisdropped) continue; /************************************************************ * Get the attribute name ************************************************************/ - attname = NameStr(tupdesc->attrs[i]->attname); + attname = NameStr(att->attname); /************************************************************ * Get the attributes value @@ -3037,7 +3042,7 @@ pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc) ************************************************************/ if (!isnull) { - getTypeOutputInfo(tupdesc->attrs[i]->atttypid, + getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena); outputstr = OidOutputFunctionCall(typoutput, attr); UTF_BEGIN; |
