Commit 412cb388 authored by Tom Lane's avatar Tom Lane

Do The Right Thing (tm) if asked to cluster a temp table. Previous

code would cluster, but table would magically lose its tempness.
parent 353f71a3
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.61 2001/01/01 21:35:00 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.62 2001/01/10 01:12:28 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -34,8 +34,10 @@ ...@@ -34,8 +34,10 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/syscache.h" #include "utils/syscache.h"
#include "utils/temprel.h"
static Oid copy_heap(Oid OIDOldHeap, char *NewName);
static Oid copy_heap(Oid OIDOldHeap, char *NewName, bool istemp);
static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName); static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName);
static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex); static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex);
...@@ -60,6 +62,7 @@ cluster(char *oldrelname, char *oldindexname) ...@@ -60,6 +62,7 @@ cluster(char *oldrelname, char *oldindexname)
Relation OldHeap, Relation OldHeap,
OldIndex; OldIndex;
HeapTuple tuple; HeapTuple tuple;
bool istemp;
char NewHeapName[NAMEDATALEN]; char NewHeapName[NAMEDATALEN];
char NewIndexName[NAMEDATALEN]; char NewIndexName[NAMEDATALEN];
char saveoldrelname[NAMEDATALEN]; char saveoldrelname[NAMEDATALEN];
...@@ -82,6 +85,8 @@ cluster(char *oldrelname, char *oldindexname) ...@@ -82,6 +85,8 @@ cluster(char *oldrelname, char *oldindexname)
LockRelation(OldIndex, AccessExclusiveLock); LockRelation(OldIndex, AccessExclusiveLock);
OIDOldIndex = RelationGetRelid(OldIndex); OIDOldIndex = RelationGetRelid(OldIndex);
istemp = is_temp_rel_name(saveoldrelname);
/* /*
* Check that index is in fact an index on the given relation * Check that index is in fact an index on the given relation
*/ */
...@@ -105,7 +110,7 @@ cluster(char *oldrelname, char *oldindexname) ...@@ -105,7 +110,7 @@ cluster(char *oldrelname, char *oldindexname)
*/ */
snprintf(NewHeapName, NAMEDATALEN, "temp_%u", OIDOldHeap); snprintf(NewHeapName, NAMEDATALEN, "temp_%u", OIDOldHeap);
OIDNewHeap = copy_heap(OIDOldHeap, NewHeapName); OIDNewHeap = copy_heap(OIDOldHeap, NewHeapName, istemp);
/* We do not need CommandCounterIncrement() because copy_heap did it. */ /* We do not need CommandCounterIncrement() because copy_heap did it. */
...@@ -138,7 +143,7 @@ cluster(char *oldrelname, char *oldindexname) ...@@ -138,7 +143,7 @@ cluster(char *oldrelname, char *oldindexname)
} }
static Oid static Oid
copy_heap(Oid OIDOldHeap, char *NewName) copy_heap(Oid OIDOldHeap, char *NewName, bool istemp)
{ {
TupleDesc OldHeapDesc, TupleDesc OldHeapDesc,
tupdesc; tupdesc;
...@@ -155,7 +160,7 @@ copy_heap(Oid OIDOldHeap, char *NewName) ...@@ -155,7 +160,7 @@ copy_heap(Oid OIDOldHeap, char *NewName)
tupdesc = CreateTupleDescCopy(OldHeapDesc); tupdesc = CreateTupleDescCopy(OldHeapDesc);
OIDNewHeap = heap_create_with_catalog(NewName, tupdesc, OIDNewHeap = heap_create_with_catalog(NewName, tupdesc,
RELKIND_RELATION, false, RELKIND_RELATION, istemp,
allowSystemTableMods); allowSystemTableMods);
/* /*
...@@ -192,9 +197,13 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName) ...@@ -192,9 +197,13 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName)
OldIndex = index_open(OIDOldIndex); OldIndex = index_open(OIDOldIndex);
/* /*
* Create a new (temporary) index like the one that's already here. * Create a new index like the old one. To do this I get the info
* To do this I get the info from pg_index, and add a new index with * from pg_index, and add a new index with a temporary name (that
* a temporary name. * will be changed later).
*
* NOTE: index_create will cause the new index to be a temp relation
* if its parent table is, so we don't need to do anything special
* for the temp-table case here.
*/ */
Old_pg_index_Tuple = SearchSysCache(INDEXRELID, Old_pg_index_Tuple = SearchSysCache(INDEXRELID,
ObjectIdGetDatum(OIDOldIndex), ObjectIdGetDatum(OIDOldIndex),
......
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