]> granicus.if.org Git - postgresql/blobdiff - src/backend/catalog/pg_type.c
Phase 2 of pgindent updates.
[postgresql] / src / backend / catalog / pg_type.c
index ed8259da5585cb3dae30e8a049ae96880a4669e5..6172973343a8acd03aa924d431d483f3820a207c 100644 (file)
@@ -3,7 +3,7 @@
  * pg_type.c
  *       routines to support manipulation of the pg_type relation
  *
- * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
@@ -36,7 +36,7 @@
 #include "utils/rel.h"
 #include "utils/syscache.h"
 
-/* Potentially set by contrib/pg_upgrade_support functions */
+/* Potentially set by pg_upgrade_support functions */
 Oid                    binary_upgrade_next_pg_type_oid = InvalidOid;
 
 /* ----------------------------------------------------------------
@@ -52,7 +52,7 @@ Oid                   binary_upgrade_next_pg_type_oid = InvalidOid;
  *             with correct ones, and "typisdefined" will be set to true.
  * ----------------------------------------------------------------
  */
-Oid
+ObjectAddress
 TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
 {
        Relation        pg_type_desc;
@@ -63,6 +63,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
        bool            nulls[Natts_pg_type];
        Oid                     typoid;
        NameData        name;
+       ObjectAddress address;
 
        Assert(PointerIsValid(typeName));
 
@@ -78,7 +79,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
        for (i = 0; i < Natts_pg_type; ++i)
        {
                nulls[i] = false;
-               values[i] = (Datum) NULL;               /* redundant, but safe */
+               values[i] = (Datum) NULL;       /* redundant, but safe */
        }
 
        /*
@@ -132,7 +133,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
                if (!OidIsValid(binary_upgrade_next_pg_type_oid))
                        ereport(ERROR,
                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                        errmsg("pg_type OID value not set when in binary upgrade mode")));
+                       errmsg("pg_type OID value not set when in binary upgrade mode")));
 
                HeapTupleSetOid(tup, binary_upgrade_next_pg_type_oid);
                binary_upgrade_next_pg_type_oid = InvalidOid;
@@ -141,9 +142,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
        /*
         * insert the tuple in the relation and get the tuple's oid.
         */
-       typoid = simple_heap_insert(pg_type_desc, tup);
-
-       CatalogUpdateIndexes(pg_type_desc, tup);
+       typoid = CatalogTupleInsert(pg_type_desc, tup);
 
        /*
         * Create dependencies.  We can/must skip this in bootstrap mode.
@@ -171,13 +170,15 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
        /* Post creation hook for new shell type */
        InvokeObjectPostCreateHook(TypeRelationId, typoid, 0);
 
+       ObjectAddressSet(address, TypeRelationId, typoid);
+
        /*
         * clean up and return the type-oid
         */
        heap_freetuple(tup);
        heap_close(pg_type_desc, RowExclusiveLock);
 
-       return typoid;
+       return address;
 }
 
 /* ----------------------------------------------------------------
@@ -185,12 +186,12 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
  *
  *             This does all the necessary work needed to define a new type.
  *
- *             Returns the OID assigned to the new type.  If newTypeOid is
- *             zero (the normal case), a new OID is created; otherwise we
- *             use exactly that OID.
+ *             Returns the ObjectAddress assigned to the new type.
+ *             If newTypeOid is zero (the normal case), a new OID is created;
+ *             otherwise we use exactly that OID.
  * ----------------------------------------------------------------
  */
-Oid
+ObjectAddress
 TypeCreate(Oid newTypeOid,
                   const char *typeName,
                   Oid typeNamespace,
@@ -213,7 +214,7 @@ TypeCreate(Oid newTypeOid,
                   bool isImplicitArray,
                   Oid arrayType,
                   Oid baseType,
-                  const char *defaultTypeValue,                /* human readable rep */
+                  const char *defaultTypeValue,        /* human readable rep */
                   char *defaultTypeBin,        /* cooked rep */
                   bool passedByValue,
                   char alignment,
@@ -233,6 +234,7 @@ TypeCreate(Oid newTypeOid,
        NameData        name;
        int                     i;
        Acl                *typacl = NULL;
+       ObjectAddress address;
 
        /*
         * We assume that the caller validated the arguments individually, but did
@@ -426,7 +428,7 @@ TypeCreate(Oid newTypeOid,
                                                                nulls,
                                                                replaces);
 
-               simple_heap_update(pg_type_desc, &tup->t_self, tup);
+               CatalogTupleUpdate(pg_type_desc, &tup->t_self, tup);
 
                typeObjectId = HeapTupleGetOid(tup);
 
@@ -454,12 +456,9 @@ TypeCreate(Oid newTypeOid,
                }
                /* else allow system to assign oid */
 
-               typeObjectId = simple_heap_insert(pg_type_desc, tup);
+               typeObjectId = CatalogTupleInsert(pg_type_desc, tup);
        }
 
-       /* Update indexes */
-       CatalogUpdateIndexes(pg_type_desc, tup);
-
        /*
         * Create dependencies.  We can/must skip this in bootstrap mode.
         */
@@ -488,12 +487,14 @@ TypeCreate(Oid newTypeOid,
        /* Post creation hook for new type */
        InvokeObjectPostCreateHook(TypeRelationId, typeObjectId, 0);
 
+       ObjectAddressSet(address, TypeRelationId, typeObjectId);
+
        /*
         * finish up
         */
        heap_close(pg_type_desc, RowExclusiveLock);
 
-       return typeObjectId;
+       return address;
 }
 
 /*
@@ -510,8 +511,8 @@ TypeCreate(Oid newTypeOid,
 void
 GenerateTypeDependencies(Oid typeNamespace,
                                                 Oid typeObjectId,
-                                                Oid relationOid,               /* only for relation rowtypes */
-                                                char relationKind,             /* ditto */
+                                                Oid relationOid,       /* only for relation rowtypes */
+                                                char relationKind, /* ditto */
                                                 Oid owner,
                                                 Oid inputProcedure,
                                                 Oid outputProcedure,
@@ -694,6 +695,7 @@ RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
        HeapTuple       tuple;
        Form_pg_type typ;
        Oid                     arrayOid;
+       Oid                     oldTypeOid;
 
        pg_type_desc = heap_open(TypeRelationId, RowExclusiveLock);
 
@@ -707,29 +709,45 @@ RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
 
        arrayOid = typ->typarray;
 
-       /* Just to give a more friendly error than unique-index violation */
-       if (SearchSysCacheExists2(TYPENAMENSP,
-                                                         CStringGetDatum(newTypeName),
-                                                         ObjectIdGetDatum(typeNamespace)))
-               ereport(ERROR,
-                               (errcode(ERRCODE_DUPLICATE_OBJECT),
-                                errmsg("type \"%s\" already exists", newTypeName)));
+       /* Check for a conflicting type name. */
+       oldTypeOid = GetSysCacheOid2(TYPENAMENSP,
+                                                                CStringGetDatum(newTypeName),
+                                                                ObjectIdGetDatum(typeNamespace));
+
+       /*
+        * If there is one, see if it's an autogenerated array type, and if so
+        * rename it out of the way.  (But we must skip that for a shell type
+        * because moveArrayTypeName will do the wrong thing in that case.)
+        * Otherwise, we can at least give a more friendly error than unique-index
+        * violation.
+        */
+       if (OidIsValid(oldTypeOid))
+       {
+               if (get_typisdefined(oldTypeOid) &&
+                       moveArrayTypeName(oldTypeOid, newTypeName, typeNamespace))
+                        /* successfully dodged the problem */ ;
+               else
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_DUPLICATE_OBJECT),
+                                        errmsg("type \"%s\" already exists", newTypeName)));
+       }
 
        /* OK, do the rename --- tuple is a copy, so OK to scribble on it */
        namestrcpy(&(typ->typname), newTypeName);
 
-       simple_heap_update(pg_type_desc, &tuple->t_self, tuple);
-
-       /* update the system catalog indexes */
-       CatalogUpdateIndexes(pg_type_desc, tuple);
+       CatalogTupleUpdate(pg_type_desc, &tuple->t_self, tuple);
 
        InvokeObjectPostAlterHook(TypeRelationId, typeOid, 0);
 
        heap_freetuple(tuple);
        heap_close(pg_type_desc, RowExclusiveLock);
 
-       /* If the type has an array type, recurse to handle that */
-       if (OidIsValid(arrayOid))
+       /*
+        * If the type has an array type, recurse to handle that.  But we don't
+        * need to do anything more if we already renamed that array type above
+        * (which would happen when, eg, renaming "foo" to "_foo").
+        */
+       if (OidIsValid(arrayOid) && arrayOid != oldTypeOid)
        {
                char       *arrname = makeArrayTypeName(newTypeName, typeNamespace);