]> granicus.if.org Git - postgresql/commitdiff
get_relid_attribute_name is dead, long live get_attname
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 12 Feb 2018 22:30:30 +0000 (19:30 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 12 Feb 2018 22:33:15 +0000 (19:33 -0300)
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

contrib/postgres_fdw/deparse.c
contrib/postgres_fdw/postgres_fdw.c
contrib/sepgsql/dml.c
src/backend/catalog/heap.c
src/backend/catalog/objectaddress.c
src/backend/parser/parse_relation.c
src/backend/parser/parse_utilcmd.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/lsyscache.c
src/backend/utils/cache/relcache.c
src/include/utils/lsyscache.h

index 32c7261dae587276cfc8a6d7c4543a9d87ef118b..f4b38c65acae9698b005608c97aa66d958af34b1 100644 (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);
index c1d7f8032e5a931550fc684da4a0544f703f3c86..d37180ae10b9681713f745b31ca68f715e2ed8f8 100644 (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);
                }
index 36cdb27a76861a661f7c3765b1e78c9271802aa5..c1fa320eb4bbe4bc202bb901844f777475ed0f89 100644 (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",
index 0f34f5381a299ace264b983b62625ebcf2912b75..cf36ce4add7907f76f7bf50bcce9bfb91e0f0af0 100644 (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;
 
index 570e65affb97edc50bbb88096a695ebfcc570f84..b4c2467710955aa045354a5a771cb41693b852f2 100644 (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);
index 2625da5327ad7aeb881a8deb8d1ef1b1c9156c13..053ae02c9fe04825a106e23ef90d3d36a4b1b8ca 100644 (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.
index d415d7180f2ab1d5658b3e79fb8bc7d05329220f..7c2cd4656ad6669bf114fef93add11f71a38af9e 100644 (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),
index 28767a129af30abc0d1c53373713425b2c9a9bae..3bb468bdada610847992d9209abdb2e198754b2e 100644 (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));
 
                        /*
index e8aa179347dc21078386748df2b1be52f8bd7393..51b6b4f7bbeeb1b4e4cdb0582bc2239a0ecb0f6e 100644 (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;
 }
 
 /*
index d5cc246156bf10a8c7d90f2d4608402cbccb21a2..1ebf9c4ed2bfeff069a6157d2c5d4c758d51a725 100644 (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);
 }
index 9731e6f7ae087b50c2f657b02aab683d1caaba09..1f6c04a8f3c701fcfee0179621876192c90dcbe7 100644 (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);