11
11
*
12
12
*
13
13
* 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 $
15
15
*
16
16
*-------------------------------------------------------------------------
17
17
*/
20
20
#include "access/genam.h"
21
21
#include "access/heapam.h"
22
22
#include "catalog/catalog.h"
23
+ #include "catalog/catname.h"
23
24
#include "catalog/dependency.h"
24
25
#include "catalog/heap.h"
25
26
#include "catalog/index.h"
26
27
#include "catalog/indexing.h"
27
- #include "catalog/catname.h"
28
28
#include "catalog/namespace.h"
29
+ #include "catalog/pg_constraint.h"
29
30
#include "commands/cluster.h"
30
31
#include "commands/tablecmds.h"
31
32
#include "miscadmin.h"
@@ -63,7 +64,6 @@ typedef struct
63
64
64
65
static Oid make_new_heap (Oid OIDOldHeap , const char * NewName );
65
66
static void copy_heap_data (Oid OIDNewHeap , Oid OIDOldHeap , Oid OIDOldIndex );
66
- static List * get_indexattr_list (Relation OldHeap , Oid OldIndex );
67
67
static void recreate_indexattr (Oid OIDOldHeap , List * indexes );
68
68
static void swap_relfilenodes (Oid r1 , Oid r2 );
69
69
static void cluster_rel (relToCluster * rv );
@@ -92,11 +92,8 @@ static MemoryContext cluster_context = NULL;
92
92
void
93
93
cluster_rel (relToCluster * rvtc )
94
94
{
95
- Oid OIDNewHeap ;
96
95
Relation OldHeap ,
97
96
OldIndex ;
98
- char NewHeapName [NAMEDATALEN ];
99
- ObjectAddress object ;
100
97
List * indexes ;
101
98
102
99
/* Check for user-requested abort. */
@@ -172,6 +169,22 @@ cluster_rel(relToCluster *rvtc)
172
169
index_close (OldIndex );
173
170
heap_close (OldHeap , NoLock );
174
171
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 );
175
188
/*
176
189
* Create the new heap, using a temporary name in the same namespace
177
190
* as the existing table. NOTE: there is some risk of collision with
@@ -180,10 +193,9 @@ cluster_rel(relToCluster *rvtc)
180
193
* namespace from the old, or we will have problems with the TEMP
181
194
* status of temp tables.
182
195
*/
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 );
186
197
198
+ OIDNewHeap = make_new_heap (tableOid , NewHeapName );
187
199
/*
188
200
* We don't need CommandCounterIncrement() because make_new_heap did
189
201
* it.
@@ -192,13 +204,14 @@ cluster_rel(relToCluster *rvtc)
192
204
/*
193
205
* Copy the heap data into the new table in the desired order.
194
206
*/
195
- copy_heap_data (OIDNewHeap , rvtc -> tableOid , rvtc -> indexOid );
207
+ if (dataCopy )
208
+ copy_heap_data (OIDNewHeap , tableOid , indexOid );
196
209
197
210
/* To make the new heap's data visible (probably not needed?). */
198
211
CommandCounterIncrement ();
199
212
200
213
/* Swap the relfilenodes of the old and new heaps. */
201
- swap_relfilenodes (rvtc -> tableOid , OIDNewHeap );
214
+ swap_relfilenodes (tableOid , OIDNewHeap );
202
215
203
216
CommandCounterIncrement ();
204
217
@@ -219,7 +232,7 @@ cluster_rel(relToCluster *rvtc)
219
232
* Recreate each index on the relation. We do not need
220
233
* CommandCounterIncrement() because recreate_indexattr does it.
221
234
*/
222
- recreate_indexattr (rvtc -> tableOid , indexes );
235
+ recreate_indexattr (tableOid , indexes );
223
236
}
224
237
225
238
/*
@@ -322,7 +335,7 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex)
322
335
* Get the necessary info about the indexes of the relation and
323
336
* return a list of IndexAttrs structures.
324
337
*/
325
- static List *
338
+ List *
326
339
get_indexattr_list (Relation OldHeap , Oid OldIndex )
327
340
{
328
341
List * indexes = NIL ;
0 commit comments