aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/rename.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-05-11 03:54:18 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-05-11 03:54:18 +0000
commit37c652f89bfc6ce428dad082a8623751604dd9b9 (patch)
tree1b076117f692f347217c60c21784b9611233fda9 /src/backend/commands/rename.c
parente10c5597b72435ac4ddb7d7b10858b85d6af0908 (diff)
downloadpostgresql-37c652f89bfc6ce428dad082a8623751604dd9b9.tar.gz
postgresql-37c652f89bfc6ce428dad082a8623751604dd9b9.zip
Fix CLUSTER ... or at least undo the bit-rot it's suffered since 6.5.
It's still pretty fundamentally bogus :-(. Freebie side benefit: ALTER TABLE RENAME works on indexes now.
Diffstat (limited to 'src/backend/commands/rename.c')
-rw-r--r--src/backend/commands/rename.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/backend/commands/rename.c b/src/backend/commands/rename.c
index 6a9c92b1e63..48454b128d3 100644
--- a/src/backend/commands/rename.c
+++ b/src/backend/commands/rename.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.42 2000/04/12 17:14:59 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.43 2000/05/11 03:54:18 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -18,6 +18,7 @@
#include "access/heapam.h"
#include "catalog/catname.h"
+#include "catalog/pg_type.h"
#include "utils/syscache.h"
#include "catalog/heap.h"
#include "catalog/indexing.h"
@@ -27,6 +28,8 @@
#include "storage/smgr.h"
#include "optimizer/prep.h"
#include "utils/acl.h"
+#include "utils/relcache.h"
+
/*
* renameatt - changes the name of a attribute in a relation
@@ -180,6 +183,7 @@ renamerel(const char *oldrelname, const char *newrelname)
Relation targetrelation;
Relation relrelation; /* for RELATION relation */
HeapTuple oldreltup;
+ char relkind;
char oldpath[MAXPGPATH],
newpath[MAXPGPATH],
toldpath[MAXPGPATH + 10],
@@ -195,10 +199,19 @@ renamerel(const char *oldrelname, const char *newrelname)
newrelname);
/*
+ * Instead of using heap_openr(), do it the hard way, so that we
+ * can rename indexes as well as regular relations.
+ */
+ targetrelation = RelationNameGetRelation(oldrelname);
+
+ if (!RelationIsValid(targetrelation))
+ elog(ERROR, "Relation '%s' does not exist", oldrelname);
+
+ /*
* Grab an exclusive lock on the target table, which we will NOT
* release until end of transaction.
*/
- targetrelation = heap_openr(oldrelname, AccessExclusiveLock);
+ LockRelation(targetrelation, AccessExclusiveLock);
/* ----------------
* RENAME TABLE within a transaction block is dangerous, because
@@ -215,6 +228,8 @@ renamerel(const char *oldrelname, const char *newrelname)
if (IsTransactionBlock() && !targetrelation->rd_myxactonly)
elog(NOTICE, "Caution: RENAME TABLE cannot be rolled back, so don't abort now");
+ relkind = targetrelation->rd_rel->relkind;
+
/*
* Flush all blocks of the relation out of the buffer pool. We need
* this because the blocks are marked with the relation's name as well
@@ -304,4 +319,10 @@ renamerel(const char *oldrelname, const char *newrelname)
CatalogCloseIndices(Num_pg_class_indices, irelations);
heap_close(relrelation, RowExclusiveLock);
+
+ /*
+ * Also rename the associated type, if any.
+ */
+ if (relkind != RELKIND_INDEX)
+ TypeRename(oldrelname, newrelname);
}