When you create a composite type that already exists, the error message
you get is about 'relation "foo" already exists'.  While true, this can
be confusing, as you didn't plan to create a relation.  Therefore, I
propose that we insert a snippet of code that is already used by the
other forms of type creation to check first whether a *type* with that
name exists.
Index: src/backend/commands/typecmds.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/commands/typecmds.c,v
retrieving revision 1.145
diff -u -3 -p -r1.145 typecmds.c
--- src/backend/commands/typecmds.c	2 Jan 2010 16:57:39 -0000	1.145
+++ src/backend/commands/typecmds.c	18 Jan 2010 23:46:45 -0000
@@ -1509,6 +1509,8 @@ Oid
 DefineCompositeType(const RangeVar *typevar, List *coldeflist)
 {
 	CreateStmt *createStmt = makeNode(CreateStmt);
+	Oid			old_type_oid;
+	Oid			typeNamespace;
 
 	if (coldeflist == NIL)
 		ereport(ERROR,
@@ -1528,6 +1530,23 @@ DefineCompositeType(const RangeVar *type
 	createStmt->tablespacename = NULL;
 
 	/*
+	 * Check for collision with an existing type name.	If there is one and
+	 * it's an autogenerated array, we can rename it out of the way.
+	 */
+	typeNamespace = RangeVarGetCreationNamespace(createStmt->relation);
+	old_type_oid = GetSysCacheOid(TYPENAMENSP,
+								  CStringGetDatum(createStmt->relation->relname),
+								  ObjectIdGetDatum(typeNamespace),
+								  0, 0);
+	if (OidIsValid(old_type_oid))
+	{
+		if (!moveArrayTypeName(old_type_oid, createStmt->relation->relname, typeNamespace))
+			ereport(ERROR,
+					(errcode(ERRCODE_DUPLICATE_OBJECT),
+					 errmsg("type \"%s\" already exists", createStmt->relation->relname)));
+	}
+
+	/*
 	 * finally create the relation...
 	 */
 	return DefineRelation(createStmt, RELKIND_COMPOSITE_TYPE);
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to