Index: src/backend/catalog/pg_constraint.c =================================================================== RCS file: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v retrieving revision 1.37 diff -c -r1.37 pg_constraint.c *** src/backend/catalog/pg_constraint.c 1 Jan 2008 19:45:48 -0000 1.37 --- src/backend/catalog/pg_constraint.c 17 Jan 2008 01:14:33 -0000 *************** *** 25,30 **** --- 25,31 ---- #include "utils/array.h" #include "utils/builtins.h" #include "utils/fmgroids.h" + #include "utils/lsyscache.h" #include "utils/syscache.h" *************** *** 569,574 **** --- 570,636 ---- } /* + * RenameConstraintById + * Rename a constraint. + * + * Note: this isn't intended to be a user-exposed function; it doesn't check + * permissions etc. Currently this is only invoked when renaming an index + * that is associated with a constraint, but it's made a little more general + * than that with the expectation of someday having ALTER TABLE RENAME + * CONSTRAINT. + */ + void + RenameConstraintById(Oid conId, const char *newname) + { + Relation conDesc; + HeapTuple tuple; + Form_pg_constraint con; + + conDesc = heap_open(ConstraintRelationId, RowExclusiveLock); + + tuple = SearchSysCacheCopy(CONSTROID, + ObjectIdGetDatum(conId), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for constraint %u", conId); + con = (Form_pg_constraint) GETSTRUCT(tuple); + + /* + * We need to check whether the name is already in use --- note that + * there currently is not a unique index that would catch this. + */ + if (OidIsValid(con->conrelid) && + ConstraintNameIsUsed(CONSTRAINT_RELATION, + con->conrelid, + con->connamespace, + newname)) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("constraint \"%s\" for relation \"%s\" already exists", + newname, get_rel_name(con->conrelid)))); + if (OidIsValid(con->contypid) && + ConstraintNameIsUsed(CONSTRAINT_DOMAIN, + con->contypid, + con->connamespace, + newname)) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("constraint \"%s\" for domain \"%s\" already exists", + newname, format_type_be(con->contypid)))); + + /* OK, do the rename --- tuple is a copy, so OK to scribble on it */ + namestrcpy(&(con->conname), newname); + + simple_heap_update(conDesc, &tuple->t_self, tuple); + + /* update the system catalog indexes */ + CatalogUpdateIndexes(conDesc, tuple); + + heap_freetuple(tuple); + heap_close(conDesc, RowExclusiveLock); + } + + /* * AlterConstraintNamespaces * Find any constraints belonging to the specified object, * and move them to the specified new namespace. Index: src/backend/commands/tablecmds.c =================================================================== RCS file: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v retrieving revision 1.239 diff -c -r1.239 tablecmds.c *** src/backend/commands/tablecmds.c 2 Jan 2008 23:34:42 -0000 1.239 --- src/backend/commands/tablecmds.c 17 Jan 2008 01:14:33 -0000 *************** *** 1659,1671 **** * or ALTER INDEX is used to rename a sequence or view. */ relkind = targetrelation->rd_rel->relkind; ! if (reltype == OBJECT_SEQUENCE && relkind != 'S') ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("\"%s\" is not a sequence", RelationGetRelationName(targetrelation)))); ! if (reltype == OBJECT_VIEW && relkind != 'v') ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("\"%s\" is not a view", --- 1659,1671 ---- * or ALTER INDEX is used to rename a sequence or view. */ relkind = targetrelation->rd_rel->relkind; ! if (reltype == OBJECT_SEQUENCE && relkind != RELKIND_SEQUENCE) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("\"%s\" is not a sequence", RelationGetRelationName(targetrelation)))); ! if (reltype == OBJECT_VIEW && relkind != RELKIND_VIEW) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("\"%s\" is not a view", *************** *** 1712,1717 **** --- 1712,1728 ---- TypeRename(targetrelation->rd_rel->reltype, newrelname, namespaceId); /* + * Also rename the associated constraint, if any. + */ + if (relkind == RELKIND_INDEX) + { + Oid constraintId = get_index_constraint(myrelid); + + if (OidIsValid(constraintId)) + RenameConstraintById(constraintId, newrelname); + } + + /* * Close rel, but keep exclusive lock! */ relation_close(targetrelation, NoLock); Index: src/include/catalog/pg_constraint.h =================================================================== RCS file: /cvsroot/pgsql/src/include/catalog/pg_constraint.h,v retrieving revision 1.26 diff -c -r1.26 pg_constraint.h *** src/include/catalog/pg_constraint.h 1 Jan 2008 19:45:56 -0000 1.26 --- src/include/catalog/pg_constraint.h 17 Jan 2008 01:14:33 -0000 *************** *** 201,206 **** --- 201,207 ---- const char *conSrc); extern void RemoveConstraintById(Oid conId); + extern void RenameConstraintById(Oid conId, const char *newname); extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, Oid objNamespace, const char *conname);