get_relid_attribute_name is dead, long live get_attname

The modern way is to use a missing_ok argument instead of two separate
almost-identical routines, so do that.

Author: Michaël Paquier
Reviewed-by: Álvaro Herrera
Discussion: https://postgr.es/m/20180201063212.GE6398@paquier.xyz
This commit is contained in:
Alvaro Herrera 2018-02-12 19:30:30 -03:00
parent 88ef48c1cc
commit 8237f27b50
11 changed files with 40 additions and 55 deletions

View File

@ -2176,7 +2176,7 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root,
* FDW option, use attribute name.
*/
if (colname == NULL)
colname = get_relid_attribute_name(rte->relid, varattno);
colname = get_attname(rte->relid, varattno, false);
if (qualify_col)
ADD_REL_QUALIFIER(buf, varno);

View File

@ -5545,7 +5545,7 @@ conversion_error_callback(void *arg)
if (var->varattno == 0)
is_wholerow = true;
else
attname = get_relid_attribute_name(rte->relid, var->varattno);
attname = get_attname(rte->relid, var->varattno, false);
relname = get_rel_name(rte->relid);
}

View File

@ -118,10 +118,7 @@ fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns)
continue;
}
attname = get_attname(parentId, attno);
if (!attname)
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
attno, parentId);
attname = get_attname(parentId, attno, false);
attno = get_attnum(childId, attname);
if (attno == InvalidAttrNumber)
elog(ERROR, "cache lookup failed for attribute %s of relation %u",

View File

@ -2405,7 +2405,8 @@ AddRelationNewConstraints(Relation rel,
if (list_length(vars) == 1)
colname = get_attname(RelationGetRelid(rel),
((Var *) linitial(vars))->varattno);
((Var *) linitial(vars))->varattno,
true);
else
colname = NULL;

View File

@ -2682,8 +2682,9 @@ getObjectDescription(const ObjectAddress *object)
getRelationDescription(&buffer, object->objectId);
if (object->objectSubId != 0)
appendStringInfo(&buffer, _(" column %s"),
get_relid_attribute_name(object->objectId,
object->objectSubId));
get_attname(object->objectId,
object->objectSubId,
false));
break;
case OCLASS_PROC:
@ -4103,8 +4104,8 @@ getObjectIdentityParts(const ObjectAddress *object,
{
char *attr;
attr = get_relid_attribute_name(object->objectId,
object->objectSubId);
attr = get_attname(object->objectId, object->objectSubId,
false);
appendStringInfo(&buffer, ".%s", quote_identifier(attr));
if (objname)
*objname = lappend(*objname, attr);

View File

@ -2687,7 +2687,7 @@ get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
* built (which can easily happen for rules).
*/
if (rte->rtekind == RTE_RELATION)
return get_relid_attribute_name(rte->relid, attnum);
return get_attname(rte->relid, attnum, false);
/*
* Otherwise use the column name from eref. There should always be one.

View File

@ -1470,7 +1470,7 @@ generateClonedIndexStmt(RangeVar *heapRel, Oid heapRelid, Relation source_idx,
/* Simple index column */
char *attname;
attname = get_relid_attribute_name(indrelid, attnum);
attname = get_attname(indrelid, attnum, false);
keycoltype = get_atttype(indrelid, attnum);
iparam->name = attname;
@ -3406,8 +3406,8 @@ transformPartitionBound(ParseState *pstate, Relation parent,
/* Get the only column's name in case we need to output an error */
if (key->partattrs[0] != 0)
colname = get_relid_attribute_name(RelationGetRelid(parent),
key->partattrs[0]);
colname = get_attname(RelationGetRelid(parent),
key->partattrs[0], false);
else
colname = deparse_expression((Node *) linitial(partexprs),
deparse_context_for(RelationGetRelationName(parent),
@ -3491,8 +3491,8 @@ transformPartitionBound(ParseState *pstate, Relation parent,
/* Get the column's name in case we need to output an error */
if (key->partattrs[i] != 0)
colname = get_relid_attribute_name(RelationGetRelid(parent),
key->partattrs[i]);
colname = get_attname(RelationGetRelid(parent),
key->partattrs[i], false);
else
{
colname = deparse_expression((Node *) list_nth(partexprs, j),

View File

@ -908,8 +908,8 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
if (i > 0)
appendStringInfoString(&buf, ", ");
attname = get_relid_attribute_name(trigrec->tgrelid,
trigrec->tgattr.values[i]);
attname = get_attname(trigrec->tgrelid,
trigrec->tgattr.values[i], false);
appendStringInfoString(&buf, quote_identifier(attname));
}
}
@ -1292,7 +1292,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
char *attname;
int32 keycoltypmod;
attname = get_relid_attribute_name(indrelid, attnum);
attname = get_attname(indrelid, attnum, false);
if (!colno || colno == keyno + 1)
appendStringInfoString(&buf, quote_identifier(attname));
get_atttypetypmodcoll(indrelid, attnum,
@ -1535,7 +1535,7 @@ pg_get_statisticsobj_worker(Oid statextid, bool missing_ok)
if (colno > 0)
appendStringInfoString(&buf, ", ");
attname = get_relid_attribute_name(statextrec->stxrelid, attnum);
attname = get_attname(statextrec->stxrelid, attnum, false);
appendStringInfoString(&buf, quote_identifier(attname));
}
@ -1692,7 +1692,7 @@ pg_get_partkeydef_worker(Oid relid, int prettyFlags,
char *attname;
int32 keycoltypmod;
attname = get_relid_attribute_name(relid, attnum);
attname = get_attname(relid, attnum, false);
appendStringInfoString(&buf, quote_identifier(attname));
get_atttypetypmodcoll(relid, attnum,
&keycoltype, &keycoltypmod,
@ -2196,7 +2196,7 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
{
char *colName;
colName = get_relid_attribute_name(relId, DatumGetInt16(keys[j]));
colName = get_attname(relId, DatumGetInt16(keys[j]), false);
if (j == 0)
appendStringInfoString(buf, quote_identifier(colName));
@ -6015,8 +6015,9 @@ get_insert_query_def(Query *query, deparse_context *context)
* tle->resname, since resname will fail to track RENAME.
*/
appendStringInfoString(buf,
quote_identifier(get_relid_attribute_name(rte->relid,
tle->resno)));
quote_identifier(get_attname(rte->relid,
tle->resno,
false)));
/*
* Print any indirection needed (subfields or subscripts), and strip
@ -6319,8 +6320,9 @@ get_update_query_targetlist_def(Query *query, List *targetList,
* tle->resname, since resname will fail to track RENAME.
*/
appendStringInfoString(buf,
quote_identifier(get_relid_attribute_name(rte->relid,
tle->resno)));
quote_identifier(get_attname(rte->relid,
tle->resno,
false)));
/*
* Print any indirection needed (subfields or subscripts), and strip
@ -10340,8 +10342,8 @@ processIndirection(Node *node, deparse_context *context)
* target lists, but this function cannot be used for that case.
*/
Assert(list_length(fstore->fieldnums) == 1);
fieldname = get_relid_attribute_name(typrelid,
linitial_int(fstore->fieldnums));
fieldname = get_attname(typrelid,
linitial_int(fstore->fieldnums), false);
appendStringInfo(buf, ".%s", quote_identifier(fieldname));
/*

View File

@ -765,19 +765,19 @@ get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
/*
* get_attname
* Given the relation id and the attribute number,
* return the "attname" field from the attribute relation.
* Given the relation id and the attribute number, return the "attname"
* field from the attribute relation as a palloc'ed string.
*
* Note: returns a palloc'd copy of the string, or NULL if no such attribute.
* If no such attribute exists and missing_ok is true, NULL is returned;
* otherwise a not-intended-for-user-consumption error is thrown.
*/
char *
get_attname(Oid relid, AttrNumber attnum)
get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
{
HeapTuple tp;
tp = SearchSysCache2(ATTNUM,
ObjectIdGetDatum(relid),
Int16GetDatum(attnum));
ObjectIdGetDatum(relid), Int16GetDatum(attnum));
if (HeapTupleIsValid(tp))
{
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
@ -787,26 +787,11 @@ get_attname(Oid relid, AttrNumber attnum)
ReleaseSysCache(tp);
return result;
}
else
return NULL;
}
/*
* get_relid_attribute_name
*
* Same as above routine get_attname(), except that error
* is handled by elog() instead of returning NULL.
*/
char *
get_relid_attribute_name(Oid relid, AttrNumber attnum)
{
char *attname;
attname = get_attname(relid, attnum);
if (attname == NULL)
if (!missing_ok)
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
attnum, relid);
return attname;
return NULL;
}
/*

View File

@ -5250,7 +5250,7 @@ errtablecol(Relation rel, int attnum)
if (attnum > 0 && attnum <= reldesc->natts)
colname = NameStr(TupleDescAttr(reldesc, attnum - 1)->attname);
else
colname = get_relid_attribute_name(RelationGetRelid(rel), attnum);
colname = get_attname(RelationGetRelid(rel), attnum, false);
return errtablecolname(rel, colname);
}

View File

@ -83,8 +83,7 @@ extern List *get_op_btree_interpretation(Oid opno);
extern bool equality_ops_are_compatible(Oid opno1, Oid opno2);
extern Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype,
int16 procnum);
extern char *get_attname(Oid relid, AttrNumber attnum);
extern char *get_relid_attribute_name(Oid relid, AttrNumber attnum);
extern char *get_attname(Oid relid, AttrNumber attnum, bool missing_ok);
extern AttrNumber get_attnum(Oid relid, const char *attname);
extern char get_attidentity(Oid relid, AttrNumber attnum);
extern Oid get_atttype(Oid relid, AttrNumber attnum);