Commit 8237f27b authored by Alvaro Herrera's avatar Alvaro Herrera

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
parent 88ef48c1
...@@ -2176,7 +2176,7 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, ...@@ -2176,7 +2176,7 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root,
* FDW option, use attribute name. * FDW option, use attribute name.
*/ */
if (colname == NULL) if (colname == NULL)
colname = get_relid_attribute_name(rte->relid, varattno); colname = get_attname(rte->relid, varattno, false);
if (qualify_col) if (qualify_col)
ADD_REL_QUALIFIER(buf, varno); ADD_REL_QUALIFIER(buf, varno);
......
...@@ -5545,7 +5545,7 @@ conversion_error_callback(void *arg) ...@@ -5545,7 +5545,7 @@ conversion_error_callback(void *arg)
if (var->varattno == 0) if (var->varattno == 0)
is_wholerow = true; is_wholerow = true;
else else
attname = get_relid_attribute_name(rte->relid, var->varattno); attname = get_attname(rte->relid, var->varattno, false);
relname = get_rel_name(rte->relid); relname = get_rel_name(rte->relid);
} }
......
...@@ -118,10 +118,7 @@ fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns) ...@@ -118,10 +118,7 @@ fixup_inherited_columns(Oid parentId, Oid childId, Bitmapset *columns)
continue; continue;
} }
attname = get_attname(parentId, attno); attname = get_attname(parentId, attno, false);
if (!attname)
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
attno, parentId);
attno = get_attnum(childId, attname); attno = get_attnum(childId, attname);
if (attno == InvalidAttrNumber) if (attno == InvalidAttrNumber)
elog(ERROR, "cache lookup failed for attribute %s of relation %u", elog(ERROR, "cache lookup failed for attribute %s of relation %u",
......
...@@ -2405,7 +2405,8 @@ AddRelationNewConstraints(Relation rel, ...@@ -2405,7 +2405,8 @@ AddRelationNewConstraints(Relation rel,
if (list_length(vars) == 1) if (list_length(vars) == 1)
colname = get_attname(RelationGetRelid(rel), colname = get_attname(RelationGetRelid(rel),
((Var *) linitial(vars))->varattno); ((Var *) linitial(vars))->varattno,
true);
else else
colname = NULL; colname = NULL;
......
...@@ -2682,8 +2682,9 @@ getObjectDescription(const ObjectAddress *object) ...@@ -2682,8 +2682,9 @@ getObjectDescription(const ObjectAddress *object)
getRelationDescription(&buffer, object->objectId); getRelationDescription(&buffer, object->objectId);
if (object->objectSubId != 0) if (object->objectSubId != 0)
appendStringInfo(&buffer, _(" column %s"), appendStringInfo(&buffer, _(" column %s"),
get_relid_attribute_name(object->objectId, get_attname(object->objectId,
object->objectSubId)); object->objectSubId,
false));
break; break;
case OCLASS_PROC: case OCLASS_PROC:
...@@ -4103,8 +4104,8 @@ getObjectIdentityParts(const ObjectAddress *object, ...@@ -4103,8 +4104,8 @@ getObjectIdentityParts(const ObjectAddress *object,
{ {
char *attr; char *attr;
attr = get_relid_attribute_name(object->objectId, attr = get_attname(object->objectId, object->objectSubId,
object->objectSubId); false);
appendStringInfo(&buffer, ".%s", quote_identifier(attr)); appendStringInfo(&buffer, ".%s", quote_identifier(attr));
if (objname) if (objname)
*objname = lappend(*objname, attr); *objname = lappend(*objname, attr);
......
...@@ -2687,7 +2687,7 @@ get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum) ...@@ -2687,7 +2687,7 @@ get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
* built (which can easily happen for rules). * built (which can easily happen for rules).
*/ */
if (rte->rtekind == RTE_RELATION) 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. * Otherwise use the column name from eref. There should always be one.
......
...@@ -1470,7 +1470,7 @@ generateClonedIndexStmt(RangeVar *heapRel, Oid heapRelid, Relation source_idx, ...@@ -1470,7 +1470,7 @@ generateClonedIndexStmt(RangeVar *heapRel, Oid heapRelid, Relation source_idx,
/* Simple index column */ /* Simple index column */
char *attname; char *attname;
attname = get_relid_attribute_name(indrelid, attnum); attname = get_attname(indrelid, attnum, false);
keycoltype = get_atttype(indrelid, attnum); keycoltype = get_atttype(indrelid, attnum);
iparam->name = attname; iparam->name = attname;
...@@ -3406,8 +3406,8 @@ transformPartitionBound(ParseState *pstate, Relation parent, ...@@ -3406,8 +3406,8 @@ transformPartitionBound(ParseState *pstate, Relation parent,
/* Get the only column's name in case we need to output an error */ /* Get the only column's name in case we need to output an error */
if (key->partattrs[0] != 0) if (key->partattrs[0] != 0)
colname = get_relid_attribute_name(RelationGetRelid(parent), colname = get_attname(RelationGetRelid(parent),
key->partattrs[0]); key->partattrs[0], false);
else else
colname = deparse_expression((Node *) linitial(partexprs), colname = deparse_expression((Node *) linitial(partexprs),
deparse_context_for(RelationGetRelationName(parent), deparse_context_for(RelationGetRelationName(parent),
...@@ -3491,8 +3491,8 @@ transformPartitionBound(ParseState *pstate, Relation parent, ...@@ -3491,8 +3491,8 @@ transformPartitionBound(ParseState *pstate, Relation parent,
/* Get the column's name in case we need to output an error */ /* Get the column's name in case we need to output an error */
if (key->partattrs[i] != 0) if (key->partattrs[i] != 0)
colname = get_relid_attribute_name(RelationGetRelid(parent), colname = get_attname(RelationGetRelid(parent),
key->partattrs[i]); key->partattrs[i], false);
else else
{ {
colname = deparse_expression((Node *) list_nth(partexprs, j), colname = deparse_expression((Node *) list_nth(partexprs, j),
......
...@@ -908,8 +908,8 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty) ...@@ -908,8 +908,8 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
if (i > 0) if (i > 0)
appendStringInfoString(&buf, ", "); appendStringInfoString(&buf, ", ");
attname = get_relid_attribute_name(trigrec->tgrelid, attname = get_attname(trigrec->tgrelid,
trigrec->tgattr.values[i]); trigrec->tgattr.values[i], false);
appendStringInfoString(&buf, quote_identifier(attname)); appendStringInfoString(&buf, quote_identifier(attname));
} }
} }
...@@ -1292,7 +1292,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, ...@@ -1292,7 +1292,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
char *attname; char *attname;
int32 keycoltypmod; int32 keycoltypmod;
attname = get_relid_attribute_name(indrelid, attnum); attname = get_attname(indrelid, attnum, false);
if (!colno || colno == keyno + 1) if (!colno || colno == keyno + 1)
appendStringInfoString(&buf, quote_identifier(attname)); appendStringInfoString(&buf, quote_identifier(attname));
get_atttypetypmodcoll(indrelid, attnum, get_atttypetypmodcoll(indrelid, attnum,
...@@ -1535,7 +1535,7 @@ pg_get_statisticsobj_worker(Oid statextid, bool missing_ok) ...@@ -1535,7 +1535,7 @@ pg_get_statisticsobj_worker(Oid statextid, bool missing_ok)
if (colno > 0) if (colno > 0)
appendStringInfoString(&buf, ", "); appendStringInfoString(&buf, ", ");
attname = get_relid_attribute_name(statextrec->stxrelid, attnum); attname = get_attname(statextrec->stxrelid, attnum, false);
appendStringInfoString(&buf, quote_identifier(attname)); appendStringInfoString(&buf, quote_identifier(attname));
} }
...@@ -1692,7 +1692,7 @@ pg_get_partkeydef_worker(Oid relid, int prettyFlags, ...@@ -1692,7 +1692,7 @@ pg_get_partkeydef_worker(Oid relid, int prettyFlags,
char *attname; char *attname;
int32 keycoltypmod; int32 keycoltypmod;
attname = get_relid_attribute_name(relid, attnum); attname = get_attname(relid, attnum, false);
appendStringInfoString(&buf, quote_identifier(attname)); appendStringInfoString(&buf, quote_identifier(attname));
get_atttypetypmodcoll(relid, attnum, get_atttypetypmodcoll(relid, attnum,
&keycoltype, &keycoltypmod, &keycoltype, &keycoltypmod,
...@@ -2196,7 +2196,7 @@ decompile_column_index_array(Datum column_index_array, Oid relId, ...@@ -2196,7 +2196,7 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
{ {
char *colName; char *colName;
colName = get_relid_attribute_name(relId, DatumGetInt16(keys[j])); colName = get_attname(relId, DatumGetInt16(keys[j]), false);
if (j == 0) if (j == 0)
appendStringInfoString(buf, quote_identifier(colName)); appendStringInfoString(buf, quote_identifier(colName));
...@@ -6015,8 +6015,9 @@ get_insert_query_def(Query *query, deparse_context *context) ...@@ -6015,8 +6015,9 @@ get_insert_query_def(Query *query, deparse_context *context)
* tle->resname, since resname will fail to track RENAME. * tle->resname, since resname will fail to track RENAME.
*/ */
appendStringInfoString(buf, appendStringInfoString(buf,
quote_identifier(get_relid_attribute_name(rte->relid, quote_identifier(get_attname(rte->relid,
tle->resno))); tle->resno,
false)));
/* /*
* Print any indirection needed (subfields or subscripts), and strip * Print any indirection needed (subfields or subscripts), and strip
...@@ -6319,8 +6320,9 @@ get_update_query_targetlist_def(Query *query, List *targetList, ...@@ -6319,8 +6320,9 @@ get_update_query_targetlist_def(Query *query, List *targetList,
* tle->resname, since resname will fail to track RENAME. * tle->resname, since resname will fail to track RENAME.
*/ */
appendStringInfoString(buf, appendStringInfoString(buf,
quote_identifier(get_relid_attribute_name(rte->relid, quote_identifier(get_attname(rte->relid,
tle->resno))); tle->resno,
false)));
/* /*
* Print any indirection needed (subfields or subscripts), and strip * Print any indirection needed (subfields or subscripts), and strip
...@@ -10340,8 +10342,8 @@ processIndirection(Node *node, deparse_context *context) ...@@ -10340,8 +10342,8 @@ processIndirection(Node *node, deparse_context *context)
* target lists, but this function cannot be used for that case. * target lists, but this function cannot be used for that case.
*/ */
Assert(list_length(fstore->fieldnums) == 1); Assert(list_length(fstore->fieldnums) == 1);
fieldname = get_relid_attribute_name(typrelid, fieldname = get_attname(typrelid,
linitial_int(fstore->fieldnums)); linitial_int(fstore->fieldnums), false);
appendStringInfo(buf, ".%s", quote_identifier(fieldname)); appendStringInfo(buf, ".%s", quote_identifier(fieldname));
/* /*
......
...@@ -765,19 +765,19 @@ get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum) ...@@ -765,19 +765,19 @@ get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
/* /*
* get_attname * get_attname
* Given the relation id and the attribute number, * Given the relation id and the attribute number, return the "attname"
* return the "attname" field from the attribute relation. * 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 * char *
get_attname(Oid relid, AttrNumber attnum) get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
{ {
HeapTuple tp; HeapTuple tp;
tp = SearchSysCache2(ATTNUM, tp = SearchSysCache2(ATTNUM,
ObjectIdGetDatum(relid), ObjectIdGetDatum(relid), Int16GetDatum(attnum));
Int16GetDatum(attnum));
if (HeapTupleIsValid(tp)) if (HeapTupleIsValid(tp))
{ {
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp); Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
...@@ -787,26 +787,11 @@ get_attname(Oid relid, AttrNumber attnum) ...@@ -787,26 +787,11 @@ get_attname(Oid relid, AttrNumber attnum)
ReleaseSysCache(tp); ReleaseSysCache(tp);
return result; 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 (!missing_ok)
if (attname == NULL)
elog(ERROR, "cache lookup failed for attribute %d of relation %u", elog(ERROR, "cache lookup failed for attribute %d of relation %u",
attnum, relid); attnum, relid);
return attname; return NULL;
} }
/* /*
......
...@@ -5250,7 +5250,7 @@ errtablecol(Relation rel, int attnum) ...@@ -5250,7 +5250,7 @@ errtablecol(Relation rel, int attnum)
if (attnum > 0 && attnum <= reldesc->natts) if (attnum > 0 && attnum <= reldesc->natts)
colname = NameStr(TupleDescAttr(reldesc, attnum - 1)->attname); colname = NameStr(TupleDescAttr(reldesc, attnum - 1)->attname);
else else
colname = get_relid_attribute_name(RelationGetRelid(rel), attnum); colname = get_attname(RelationGetRelid(rel), attnum, false);
return errtablecolname(rel, colname); return errtablecolname(rel, colname);
} }
......
...@@ -83,8 +83,7 @@ extern List *get_op_btree_interpretation(Oid opno); ...@@ -83,8 +83,7 @@ extern List *get_op_btree_interpretation(Oid opno);
extern bool equality_ops_are_compatible(Oid opno1, Oid opno2); extern bool equality_ops_are_compatible(Oid opno1, Oid opno2);
extern Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, extern Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype,
int16 procnum); int16 procnum);
extern char *get_attname(Oid relid, AttrNumber attnum); extern char *get_attname(Oid relid, AttrNumber attnum, bool missing_ok);
extern char *get_relid_attribute_name(Oid relid, AttrNumber attnum);
extern AttrNumber get_attnum(Oid relid, const char *attname); extern AttrNumber get_attnum(Oid relid, const char *attname);
extern char get_attidentity(Oid relid, AttrNumber attnum); extern char get_attidentity(Oid relid, AttrNumber attnum);
extern Oid get_atttype(Oid relid, AttrNumber attnum); extern Oid get_atttype(Oid relid, AttrNumber attnum);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment