From: Alvaro Herrera Date: Mon, 12 Feb 2018 22:30:30 +0000 (-0300) Subject: get_relid_attribute_name is dead, long live get_attname X-Git-Tag: REL_11_BETA1~793 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8237f27b504ff1d1e2da7ae4c81a7f72ea0e0e3e;p=postgresql 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 --- diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c index 32c7261dae..f4b38c65ac 100644 --- a/contrib/postgres_fdw/deparse.c +++ b/contrib/postgres_fdw/deparse.c @@ -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); diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index c1d7f8032e..d37180ae10 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -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); } diff --git a/contrib/sepgsql/dml.c b/contrib/sepgsql/dml.c index 36cdb27a76..c1fa320eb4 100644 --- a/contrib/sepgsql/dml.c +++ b/contrib/sepgsql/dml.c @@ -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", diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 0f34f5381a..cf36ce4add 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -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; diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c index 570e65affb..b4c2467710 100644 --- a/src/backend/catalog/objectaddress.c +++ b/src/backend/catalog/objectaddress.c @@ -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); diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index 2625da5327..053ae02c9f 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -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. diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index d415d7180f..7c2cd4656a 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -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), diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 28767a129a..3bb468bdad 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -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)); /* diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c index e8aa179347..51b6b4f7bb 100644 --- a/src/backend/utils/cache/lsyscache.c +++ b/src/backend/utils/cache/lsyscache.c @@ -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; } /* diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index d5cc246156..1ebf9c4ed2 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -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); } diff --git a/src/include/utils/lsyscache.h b/src/include/utils/lsyscache.h index 9731e6f7ae..1f6c04a8f3 100644 --- a/src/include/utils/lsyscache.h +++ b/src/include/utils/lsyscache.h @@ -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);