* 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);
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);
}
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",
if (list_length(vars) == 1)
colname = get_attname(RelationGetRelid(rel),
- ((Var *) linitial(vars))->varattno);
+ ((Var *) linitial(vars))->varattno,
+ true);
else
colname = NULL;
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:
{
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);
* 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.
/* 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;
/* 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),
/* 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),
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));
}
}
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,
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));
}
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,
{
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));
* 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
* 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
* 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));
/*
/*
* 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);
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;
}
/*
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);
}
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);