Skip to content

Commit eb210ce

Browse files
committed
Before attempting to create a composite type, check whether a type of that
name already exists, so we'd get an error message about a "type" instead of about a "relation", because the composite type code shares code with relation creation.
1 parent 60e2fdf commit eb210ce

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/backend/commands/typecmds.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.145 2010/01/02 16:57:39 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.146 2010/01/20 05:47:09 petere Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1509,6 +1509,8 @@ Oid
15091509
DefineCompositeType(const RangeVar *typevar, List *coldeflist)
15101510
{
15111511
CreateStmt *createStmt = makeNode(CreateStmt);
1512+
Oid old_type_oid;
1513+
Oid typeNamespace;
15121514

15131515
if (coldeflist == NIL)
15141516
ereport(ERROR,
@@ -1528,7 +1530,26 @@ DefineCompositeType(const RangeVar *typevar, List *coldeflist)
15281530
createStmt->tablespacename = NULL;
15291531

15301532
/*
1531-
* finally create the relation...
1533+
* Check for collision with an existing type name. If there is one
1534+
* and it's an autogenerated array, we can rename it out of the
1535+
* way. This check is here mainly to get a better error message
1536+
* about a "type" instead of below about a "relation".
1537+
*/
1538+
typeNamespace = RangeVarGetCreationNamespace(createStmt->relation);
1539+
old_type_oid = GetSysCacheOid(TYPENAMENSP,
1540+
CStringGetDatum(createStmt->relation->relname),
1541+
ObjectIdGetDatum(typeNamespace),
1542+
0, 0);
1543+
if (OidIsValid(old_type_oid))
1544+
{
1545+
if (!moveArrayTypeName(old_type_oid, createStmt->relation->relname, typeNamespace))
1546+
ereport(ERROR,
1547+
(errcode(ERRCODE_DUPLICATE_OBJECT),
1548+
errmsg("type \"%s\" already exists", createStmt->relation->relname)));
1549+
}
1550+
1551+
/*
1552+
* Finally create the relation. This also creates the type.
15321553
*/
15331554
return DefineRelation(createStmt, RELKIND_COMPOSITE_TYPE);
15341555
}

0 commit comments

Comments
 (0)