8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.242 2008/02/07 17:09:51 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.243 2008/03/19 18:38:30 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -1612,47 +1612,32 @@ renameatt(Oid myrelid,
1612
1612
relation_close (targetrelation , NoLock ); /* close rel but keep lock */
1613
1613
}
1614
1614
1615
+
1615
1616
/*
1616
- * renamerel - change the name of a relation
1617
+ * Execute ALTER TABLE/INDEX/SEQUENCE/VIEW RENAME
1617
1618
*
1618
- * XXX - When renaming sequences, we don't bother to modify the
1619
- * sequence name that is stored within the sequence itself
1620
- * (this would cause problems with MVCC). In the future,
1621
- * the sequence name should probably be removed from the
1622
- * sequence, AFAIK there's no need for it to be there.
1619
+ * Caller has already done permissions checks.
1623
1620
*/
1624
1621
void
1625
- renamerel (Oid myrelid , const char * newrelname , ObjectType reltype )
1622
+ RenameRelation (Oid myrelid , const char * newrelname , ObjectType reltype )
1626
1623
{
1627
1624
Relation targetrelation ;
1628
- Relation relrelation ; /* for RELATION relation */
1629
- HeapTuple reltup ;
1630
- Form_pg_class relform ;
1631
1625
Oid namespaceId ;
1632
- char * oldrelname ;
1633
1626
char relkind ;
1634
- bool relhastriggers ;
1635
1627
1636
1628
/*
1637
1629
* Grab an exclusive lock on the target table, index, sequence or view,
1638
1630
* which we will NOT release until end of transaction.
1639
1631
*/
1640
1632
targetrelation = relation_open (myrelid , AccessExclusiveLock );
1641
1633
1642
- oldrelname = pstrdup (RelationGetRelationName (targetrelation ));
1643
1634
namespaceId = RelationGetNamespace (targetrelation );
1644
-
1645
- if (!allowSystemTableMods && IsSystemRelation (targetrelation ))
1646
- ereport (ERROR ,
1647
- (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
1648
- errmsg ("permission denied: \"%s\" is a system catalog" ,
1649
- RelationGetRelationName (targetrelation ))));
1635
+ relkind = targetrelation -> rd_rel -> relkind ;
1650
1636
1651
1637
/*
1652
1638
* For compatibility with prior releases, we don't complain if ALTER TABLE
1653
1639
* or ALTER INDEX is used to rename a sequence or view.
1654
1640
*/
1655
- relkind = targetrelation -> rd_rel -> relkind ;
1656
1641
if (reltype == OBJECT_SEQUENCE && relkind != RELKIND_SEQUENCE )
1657
1642
ereport (ERROR ,
1658
1643
(errcode (ERRCODE_WRONG_OBJECT_TYPE ),
@@ -1665,7 +1650,48 @@ renamerel(Oid myrelid, const char *newrelname, ObjectType reltype)
1665
1650
errmsg ("\"%s\" is not a view" ,
1666
1651
RelationGetRelationName (targetrelation ))));
1667
1652
1668
- relhastriggers = (targetrelation -> rd_rel -> reltriggers > 0 );
1653
+ /*
1654
+ * Don't allow ALTER TABLE on composite types.
1655
+ * We want people to use ALTER TYPE for that.
1656
+ */
1657
+ if (relkind == RELKIND_COMPOSITE_TYPE )
1658
+ ereport (ERROR ,
1659
+ (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
1660
+ errmsg ("\"%s\" is a composite type" ,
1661
+ RelationGetRelationName (targetrelation )),
1662
+ errhint ("Use ALTER TYPE instead." )));
1663
+
1664
+ /* Do the work */
1665
+ RenameRelationInternal (myrelid , newrelname , namespaceId );
1666
+
1667
+ /*
1668
+ * Close rel, but keep exclusive lock!
1669
+ */
1670
+ relation_close (targetrelation , NoLock );
1671
+ }
1672
+
1673
+ /*
1674
+ * RenameRelationInternal - change the name of a relation
1675
+ *
1676
+ * XXX - When renaming sequences, we don't bother to modify the
1677
+ * sequence name that is stored within the sequence itself
1678
+ * (this would cause problems with MVCC). In the future,
1679
+ * the sequence name should probably be removed from the
1680
+ * sequence, AFAIK there's no need for it to be there.
1681
+ */
1682
+ void
1683
+ RenameRelationInternal (Oid myrelid , const char * newrelname , Oid namespaceId )
1684
+ {
1685
+ Relation targetrelation ;
1686
+ Relation relrelation ; /* for RELATION relation */
1687
+ HeapTuple reltup ;
1688
+ Form_pg_class relform ;
1689
+
1690
+ /*
1691
+ * Grab an exclusive lock on the target table, index, sequence or
1692
+ * view, which we will NOT release until end of transaction.
1693
+ */
1694
+ targetrelation = relation_open (myrelid , AccessExclusiveLock );
1669
1695
1670
1696
/*
1671
1697
* Find relation's pg_class tuple, and make sure newrelname isn't in use.
@@ -1703,12 +1729,13 @@ renamerel(Oid myrelid, const char *newrelname, ObjectType reltype)
1703
1729
* Also rename the associated type, if any.
1704
1730
*/
1705
1731
if (OidIsValid (targetrelation -> rd_rel -> reltype ))
1706
- TypeRename (targetrelation -> rd_rel -> reltype , newrelname , namespaceId );
1732
+ RenameTypeInternal (targetrelation -> rd_rel -> reltype ,
1733
+ newrelname , namespaceId );
1707
1734
1708
1735
/*
1709
1736
* Also rename the associated constraint, if any.
1710
1737
*/
1711
- if (relkind == RELKIND_INDEX )
1738
+ if (targetrelation -> rd_rel -> relkind == RELKIND_INDEX )
1712
1739
{
1713
1740
Oid constraintId = get_index_constraint (myrelid );
1714
1741
0 commit comments