Commit d5167516 authored by Tom Lane's avatar Tom Lane

Make renamerel take an OID, not a RangeVar, to identify the relation

to rename.  Avoids some corner-case bugs in cluster.c, improves
consistency with renameatt.
parent 31141025
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.76 2002/03/31 06:26:30 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.77 2002/03/31 07:49:30 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -37,8 +37,9 @@ ...@@ -37,8 +37,9 @@
#include "utils/syscache.h" #include "utils/syscache.h"
static Oid copy_heap(Oid OIDOldHeap, char *NewName); static Oid copy_heap(Oid OIDOldHeap, const char *NewName);
static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName); static Oid copy_index(Oid OIDOldIndex, Oid OIDNewHeap,
const char *NewIndexName);
static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex); static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex);
/* /*
...@@ -58,13 +59,12 @@ cluster(RangeVar *oldrelation, char *oldindexname) ...@@ -58,13 +59,12 @@ cluster(RangeVar *oldrelation, char *oldindexname)
{ {
Oid OIDOldHeap, Oid OIDOldHeap,
OIDOldIndex, OIDOldIndex,
OIDNewHeap; OIDNewHeap,
OIDNewIndex;
Relation OldHeap, Relation OldHeap,
OldIndex; OldIndex;
char NewHeapName[NAMEDATALEN]; char NewHeapName[NAMEDATALEN];
char NewIndexName[NAMEDATALEN]; char NewIndexName[NAMEDATALEN];
RangeVar *NewHeap;
RangeVar *NewIndex;
/* /*
* We grab exclusive access to the target rel and index for the * We grab exclusive access to the target rel and index for the
...@@ -115,7 +115,7 @@ cluster(RangeVar *oldrelation, char *oldindexname) ...@@ -115,7 +115,7 @@ cluster(RangeVar *oldrelation, char *oldindexname)
/* Create new index over the tuples of the new heap. */ /* Create new index over the tuples of the new heap. */
snprintf(NewIndexName, NAMEDATALEN, "temp_%u", OIDOldIndex); snprintf(NewIndexName, NAMEDATALEN, "temp_%u", OIDOldIndex);
copy_index(OIDOldIndex, OIDNewHeap, NewIndexName); OIDNewIndex = copy_index(OIDOldIndex, OIDNewHeap, NewIndexName);
CommandCounterIncrement(); CommandCounterIncrement();
...@@ -124,23 +124,16 @@ cluster(RangeVar *oldrelation, char *oldindexname) ...@@ -124,23 +124,16 @@ cluster(RangeVar *oldrelation, char *oldindexname)
CommandCounterIncrement(); CommandCounterIncrement();
/* XXX ugly, and possibly wrong in the presence of schemas... */ renamerel(OIDNewHeap, oldrelation->relname);
/* would be better to pass OIDs to renamerel. */
NewHeap = copyObject(oldrelation);
NewHeap->relname = NewHeapName;
NewIndex = copyObject(oldrelation);
NewIndex->relname = NewIndexName;
renamerel(NewHeap, oldrelation->relname);
/* This one might be unnecessary, but let's be safe. */ /* This one might be unnecessary, but let's be safe. */
CommandCounterIncrement(); CommandCounterIncrement();
renamerel(NewIndex, oldindexname); renamerel(OIDNewIndex, oldindexname);
} }
static Oid static Oid
copy_heap(Oid OIDOldHeap, char *NewName) copy_heap(Oid OIDOldHeap, const char *NewName)
{ {
TupleDesc OldHeapDesc, TupleDesc OldHeapDesc,
tupdesc; tupdesc;
...@@ -181,9 +174,10 @@ copy_heap(Oid OIDOldHeap, char *NewName) ...@@ -181,9 +174,10 @@ copy_heap(Oid OIDOldHeap, char *NewName)
return OIDNewHeap; return OIDNewHeap;
} }
static void static Oid
copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName) copy_index(Oid OIDOldIndex, Oid OIDNewHeap, const char *NewIndexName)
{ {
Oid OIDNewIndex;
Relation OldIndex, Relation OldIndex,
NewHeap; NewHeap;
IndexInfo *indexInfo; IndexInfo *indexInfo;
...@@ -198,7 +192,7 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName) ...@@ -198,7 +192,7 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName)
*/ */
indexInfo = BuildIndexInfo(OldIndex->rd_index); indexInfo = BuildIndexInfo(OldIndex->rd_index);
index_create(OIDNewHeap, OIDNewIndex = index_create(OIDNewHeap,
NewIndexName, NewIndexName,
indexInfo, indexInfo,
OldIndex->rd_rel->relam, OldIndex->rd_rel->relam,
...@@ -211,6 +205,8 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName) ...@@ -211,6 +205,8 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName)
index_close(OldIndex); index_close(OldIndex);
heap_close(NewHeap, NoLock); heap_close(NewHeap, NoLock);
return OIDNewIndex;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.67 2002/03/31 06:26:30 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.68 2002/03/31 07:49:30 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -69,8 +69,8 @@ static void update_ri_trigger_args(Oid relid, ...@@ -69,8 +69,8 @@ static void update_ri_trigger_args(Oid relid,
*/ */
void void
renameatt(Oid relid, renameatt(Oid relid,
char *oldattname, const char *oldattname,
char *newattname, const char *newattname,
bool recurse) bool recurse)
{ {
Relation targetrelation; Relation targetrelation;
...@@ -250,51 +250,36 @@ renameatt(Oid relid, ...@@ -250,51 +250,36 @@ renameatt(Oid relid,
* renamerel - change the name of a relation * renamerel - change the name of a relation
*/ */
void void
renamerel(const RangeVar *relation, const char *newrelname) renamerel(Oid relid, const char *newrelname)
{ {
Relation targetrelation; Relation targetrelation;
Relation relrelation; /* for RELATION relation */ Relation relrelation; /* for RELATION relation */
HeapTuple reltup; HeapTuple reltup;
Oid namespaceId; Oid namespaceId;
Oid reloid;
char relkind; char relkind;
bool relhastriggers; bool relhastriggers;
Relation irelations[Num_pg_class_indices]; Relation irelations[Num_pg_class_indices];
if (!allowSystemTableMods && IsSystemRelationName(relation->relname))
elog(ERROR, "renamerel: system relation \"%s\" may not be renamed",
relation->relname);
if (!allowSystemTableMods && IsSystemRelationName(newrelname))
elog(ERROR, "renamerel: Illegal class name: \"%s\" -- pg_ is reserved for system catalogs",
newrelname);
/* /*
* Grab an exclusive lock on the target table or index, which we will * Grab an exclusive lock on the target table or index, which we will
* NOT release until end of transaction. * NOT release until end of transaction.
*/ */
targetrelation = relation_openrv(relation, AccessExclusiveLock); targetrelation = relation_open(relid, AccessExclusiveLock);
namespaceId = RelationGetNamespace(targetrelation); namespaceId = RelationGetNamespace(targetrelation);
reloid = RelationGetRelid(targetrelation);
relkind = targetrelation->rd_rel->relkind;
relhastriggers = (targetrelation->rd_rel->reltriggers > 0);
/* /* Validity checks */
* Close rel, but keep exclusive lock! if (!allowSystemTableMods &&
*/ IsSystemRelationName(RelationGetRelationName(targetrelation)))
relation_close(targetrelation, NoLock); elog(ERROR, "renamerel: system relation \"%s\" may not be renamed",
RelationGetRelationName(targetrelation));
/* if (!allowSystemTableMods && IsSystemRelationName(newrelname))
* Flush the relcache entry (easier than trying to change it at elog(ERROR, "renamerel: Illegal class name: \"%s\" -- pg_ is reserved for system catalogs",
* exactly the right instant). It'll get rebuilt on next access to newrelname);
* relation.
* relkind = targetrelation->rd_rel->relkind;
* XXX What if relation is myxactonly? relhastriggers = (targetrelation->rd_rel->reltriggers > 0);
*
* XXX this is probably not necessary anymore?
*/
RelationIdInvalidateRelationCacheByRelationId(reloid);
/* /*
* Find relation's pg_class tuple, and make sure newrelname isn't in * Find relation's pg_class tuple, and make sure newrelname isn't in
...@@ -303,11 +288,11 @@ renamerel(const RangeVar *relation, const char *newrelname) ...@@ -303,11 +288,11 @@ renamerel(const RangeVar *relation, const char *newrelname)
relrelation = heap_openr(RelationRelationName, RowExclusiveLock); relrelation = heap_openr(RelationRelationName, RowExclusiveLock);
reltup = SearchSysCacheCopy(RELOID, reltup = SearchSysCacheCopy(RELOID,
PointerGetDatum(reloid), PointerGetDatum(relid),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(reltup)) if (!HeapTupleIsValid(reltup))
elog(ERROR, "renamerel: relation \"%s\" does not exist", elog(ERROR, "renamerel: relation \"%s\" does not exist",
relation->relname); RelationGetRelationName(targetrelation));
if (get_relname_relid(newrelname, namespaceId) != InvalidOid) if (get_relname_relid(newrelname, namespaceId) != InvalidOid)
elog(ERROR, "renamerel: relation \"%s\" exists", newrelname); elog(ERROR, "renamerel: relation \"%s\" exists", newrelname);
...@@ -332,7 +317,8 @@ renamerel(const RangeVar *relation, const char *newrelname) ...@@ -332,7 +317,8 @@ renamerel(const RangeVar *relation, const char *newrelname)
* Also rename the associated type, if any. * Also rename the associated type, if any.
*/ */
if (relkind != RELKIND_INDEX) if (relkind != RELKIND_INDEX)
TypeRename(relation->relname, namespaceId, newrelname); TypeRename(RelationGetRelationName(targetrelation), namespaceId,
newrelname);
/* /*
* If it's a view, must also rename the associated ON SELECT rule. * If it's a view, must also rename the associated ON SELECT rule.
...@@ -342,7 +328,7 @@ renamerel(const RangeVar *relation, const char *newrelname) ...@@ -342,7 +328,7 @@ renamerel(const RangeVar *relation, const char *newrelname)
char *oldrulename, char *oldrulename,
*newrulename; *newrulename;
oldrulename = MakeRetrieveViewRuleName(relation->relname); oldrulename = MakeRetrieveViewRuleName(RelationGetRelationName(targetrelation));
newrulename = MakeRetrieveViewRuleName(newrelname); newrulename = MakeRetrieveViewRuleName(newrelname);
RenameRewriteRule(oldrulename, newrulename); RenameRewriteRule(oldrulename, newrulename);
} }
...@@ -353,14 +339,21 @@ renamerel(const RangeVar *relation, const char *newrelname) ...@@ -353,14 +339,21 @@ renamerel(const RangeVar *relation, const char *newrelname)
if (relhastriggers) if (relhastriggers)
{ {
/* update tgargs where relname is primary key */ /* update tgargs where relname is primary key */
update_ri_trigger_args(reloid, update_ri_trigger_args(relid,
relation->relname, newrelname, RelationGetRelationName(targetrelation),
newrelname,
false, true); false, true);
/* update tgargs where relname is foreign key */ /* update tgargs where relname is foreign key */
update_ri_trigger_args(reloid, update_ri_trigger_args(relid,
relation->relname, newrelname, RelationGetRelationName(targetrelation),
newrelname,
true, true); true, true);
} }
/*
* Close rel, but keep exclusive lock!
*/
relation_close(targetrelation, NoLock);
} }
/* /*
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.143 2002/03/31 06:26:31 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.144 2002/03/31 07:49:30 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -385,32 +385,20 @@ ProcessUtility(Node *parsetree, ...@@ -385,32 +385,20 @@ ProcessUtility(Node *parsetree,
CheckOwnership(stmt->relation, true); CheckOwnership(stmt->relation, true);
/* ----------------
* XXX using len == 3 to tell the difference
* between "rename rel to newrel" and
* "rename att in rel to newatt" will not
* work soon because "rename type/operator/rule"
* stuff is being added. - cim 10/24/90
* ----------------
* [another piece of amuzing but useless anecdote -- ay]
*/
if (stmt->column == NULL) if (stmt->column == NULL)
{ {
/* /*
* rename relation * rename relation
*
* Note: we also rename the "type" tuple corresponding to
* the relation.
*/ */
renamerel(stmt->relation, /* old relation */ renamerel(RangeVarGetRelid(stmt->relation, false),
stmt->newname); /* new name */ stmt->newname);
} }
else else
{ {
/* /*
* rename attribute * rename attribute
*/ */
renameatt(RangeVarGetRelid(stmt->relation, false), /* relation */ renameatt(RangeVarGetRelid(stmt->relation, false),
stmt->column, /* old att name */ stmt->column, /* old att name */
stmt->newname, /* new att name */ stmt->newname, /* new att name */
interpretInhOption(stmt->relation->inhOpt)); /* recursive? */ interpretInhOption(stmt->relation->inhOpt)); /* recursive? */
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: rename.h,v 1.15 2002/03/29 19:06:22 tgl Exp $ * $Id: rename.h,v 1.16 2002/03/31 07:49:30 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -15,11 +15,11 @@ ...@@ -15,11 +15,11 @@
#define RENAME_H #define RENAME_H
extern void renameatt(Oid relid, extern void renameatt(Oid relid,
char *oldattname, const char *oldattname,
char *newattname, const char *newattname,
bool recurse); bool recurse);
extern void renamerel(const RangeVar *relation, extern void renamerel(Oid relid,
const char *newrelname); const char *newrelname);
#endif /* RENAME_H */ #endif /* RENAME_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment