1111 *
1212 *
1313 * IDENTIFICATION
14- * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.95 2002/11/18 17:12:07 momjian Exp $
14+ * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.96 2002/11/23 04:05:51 momjian Exp $
1515 *
1616 *-------------------------------------------------------------------------
1717 */
2020#include "access/genam.h"
2121#include "access/heapam.h"
2222#include "catalog/catalog.h"
23+ #include "catalog/catname.h"
2324#include "catalog/dependency.h"
2425#include "catalog/heap.h"
2526#include "catalog/index.h"
2627#include "catalog/indexing.h"
27- #include "catalog/catname.h"
2828#include "catalog/namespace.h"
29+ #include "catalog/pg_constraint.h"
2930#include "commands/cluster.h"
3031#include "commands/tablecmds.h"
3132#include "miscadmin.h"
@@ -63,7 +64,6 @@ typedef struct
6364
6465static Oid make_new_heap (Oid OIDOldHeap , const char * NewName );
6566static void copy_heap_data (Oid OIDNewHeap , Oid OIDOldHeap , Oid OIDOldIndex );
66- static List * get_indexattr_list (Relation OldHeap , Oid OldIndex );
6767static void recreate_indexattr (Oid OIDOldHeap , List * indexes );
6868static void swap_relfilenodes (Oid r1 , Oid r2 );
6969static void cluster_rel (relToCluster * rv );
@@ -92,11 +92,8 @@ static MemoryContext cluster_context = NULL;
9292void
9393cluster_rel (relToCluster * rvtc )
9494{
95- Oid OIDNewHeap ;
9695 Relation OldHeap ,
9796 OldIndex ;
98- char NewHeapName [NAMEDATALEN ];
99- ObjectAddress object ;
10097 List * indexes ;
10198
10299 /* Check for user-requested abort. */
@@ -172,6 +169,22 @@ cluster_rel(relToCluster *rvtc)
172169 index_close (OldIndex );
173170 heap_close (OldHeap , NoLock );
174171
172+ /* rebuild_rel does all the dirty work */
173+ rebuild_rel (rvtc -> tableOid , rvtc -> indexOid , indexes , true);
174+ }
175+
176+ void
177+ rebuild_rel (Oid tableOid , Oid indexOid , List * indexes , bool dataCopy )
178+ {
179+ Oid OIDNewHeap ;
180+ char NewHeapName [NAMEDATALEN ];
181+ ObjectAddress object ;
182+
183+ /*
184+ * If dataCopy is true, we assume that we will be basing the
185+ * copy off an index for cluster operations.
186+ */
187+ Assert (!dataCopy || indexOid != NULL );
175188 /*
176189 * Create the new heap, using a temporary name in the same namespace
177190 * as the existing table. NOTE: there is some risk of collision with
@@ -180,10 +193,9 @@ cluster_rel(relToCluster *rvtc)
180193 * namespace from the old, or we will have problems with the TEMP
181194 * status of temp tables.
182195 */
183- snprintf (NewHeapName , NAMEDATALEN , "pg_temp_%u" , rvtc -> tableOid );
184-
185- OIDNewHeap = make_new_heap (rvtc -> tableOid , NewHeapName );
196+ snprintf (NewHeapName , NAMEDATALEN , "pg_temp_%u" , tableOid );
186197
198+ OIDNewHeap = make_new_heap (tableOid , NewHeapName );
187199 /*
188200 * We don't need CommandCounterIncrement() because make_new_heap did
189201 * it.
@@ -192,13 +204,14 @@ cluster_rel(relToCluster *rvtc)
192204 /*
193205 * Copy the heap data into the new table in the desired order.
194206 */
195- copy_heap_data (OIDNewHeap , rvtc -> tableOid , rvtc -> indexOid );
207+ if (dataCopy )
208+ copy_heap_data (OIDNewHeap , tableOid , indexOid );
196209
197210 /* To make the new heap's data visible (probably not needed?). */
198211 CommandCounterIncrement ();
199212
200213 /* Swap the relfilenodes of the old and new heaps. */
201- swap_relfilenodes (rvtc -> tableOid , OIDNewHeap );
214+ swap_relfilenodes (tableOid , OIDNewHeap );
202215
203216 CommandCounterIncrement ();
204217
@@ -219,7 +232,7 @@ cluster_rel(relToCluster *rvtc)
219232 * Recreate each index on the relation. We do not need
220233 * CommandCounterIncrement() because recreate_indexattr does it.
221234 */
222- recreate_indexattr (rvtc -> tableOid , indexes );
235+ recreate_indexattr (tableOid , indexes );
223236}
224237
225238/*
@@ -322,7 +335,7 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex)
322335 * Get the necessary info about the indexes of the relation and
323336 * return a list of IndexAttrs structures.
324337 */
325- static List *
338+ List *
326339get_indexattr_list (Relation OldHeap , Oid OldIndex )
327340{
328341 List * indexes = NIL ;
0 commit comments