cluster.c 10.1 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * cluster.c
4
 *	  Paul Brown's implementation of cluster index.
5
 *
6 7 8 9 10 11
 *	  I am going to use the rename function as a model for this in the
 *	  parser and executor, and the vacuum code as an example in this
 *	  file. As I go - in contrast to the rest of postgres - there will
 *	  be BUCKETS of comments. This is to allow reviewers to understand
 *	  my (probably bogus) assumptions about the way this works.
 *														[pbrown '94]
12
 *
B
Add:  
Bruce Momjian 已提交
13 14
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994-5, Regents of the University of California
15 16 17
 *
 *
 * IDENTIFICATION
18
 *	  $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.56 2000/06/17 23:41:36 tgl Exp $
19 20 21
 *
 *-------------------------------------------------------------------------
 */
22

23
#include "postgres.h"
24

25
#include "access/genam.h"
B
Bruce Momjian 已提交
26 27
#include "access/heapam.h"
#include "catalog/heap.h"
28
#include "catalog/index.h"
B
Bruce Momjian 已提交
29
#include "catalog/pg_index.h"
B
Bruce Momjian 已提交
30
#include "catalog/pg_proc.h"
31 32
#include "commands/cluster.h"
#include "commands/rename.h"
B
Bruce Momjian 已提交
33 34
#include "utils/builtins.h"
#include "utils/syscache.h"
35

36
static Relation copy_heap(Oid OIDOldHeap);
37 38
static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap);
static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex);
39

40 41 42
/*
 * cluster
 *
43 44 45
 *	 Check that the relation is a relation in the appropriate user
 *	 ACL. I will use the same security that limits users on the
 *	 renamerel() function.
46
 *
47 48
 *	 Check that the index specified is appropriate for the task
 *	 ( ie it's an index over this relation ). This is trickier.
49
 *
50 51 52 53 54 55 56
 *	 Create a list of all the other indicies on this relation. Because
 *	 the cluster will wreck all the tids, I'll need to destroy bogus
 *	 indicies. The user will have to re-create them. Not nice, but
 *	 I'm not a nice guy. The alternative is to try some kind of post
 *	 destroy re-build. This may be possible. I'll check out what the
 *	 index create functiond want in the way of paramaters. On the other
 *	 hand, re-creating n indicies may blow out the space.
57
 *
58 59 60 61 62 63
 *	 Create new (temporary) relations for the base heap and the new
 *	 index.
 *
 *	 Exclusively lock the relations.
 *
 *	 Create new clustered index and base heap relation.
64 65 66
 *
 */
void
67
cluster(char *oldrelname, char *oldindexname)
68
{
69 70 71
	Oid			OIDOldHeap,
				OIDOldIndex,
				OIDNewHeap;
72

73 74 75
	Relation	OldHeap,
				OldIndex;
	Relation	NewHeap;
76

77 78 79 80
	char		NewIndexName[NAMEDATALEN];
	char		NewHeapName[NAMEDATALEN];
	char		saveoldrelname[NAMEDATALEN];
	char		saveoldindexname[NAMEDATALEN];
81 82

	/*
83 84
	 * Copy the arguments into local storage, because they are probably
	 * in palloc'd storage that will go away when we commit a transaction.
85 86 87 88 89 90
	 */
	strcpy(saveoldrelname, oldrelname);
	strcpy(saveoldindexname, oldindexname);

	/*
	 * Like vacuum, cluster spans transactions, so I'm going to handle it
91 92
	 * in the same way: commit and restart transactions where needed.
	 *
93 94
	 * We grab exclusive access to the target rel and index for the duration
	 * of the initial transaction.
95 96
	 */

97
	OldHeap = heap_openr(saveoldrelname, AccessExclusiveLock);
98
	OIDOldHeap = RelationGetRelid(OldHeap);
99

100
	OldIndex = index_openr(saveoldindexname); /* Open old index relation	*/
101 102
	LockRelation(OldIndex, AccessExclusiveLock);
	OIDOldIndex = RelationGetRelid(OldIndex);
103

104 105 106 107
	/*
	 * XXX Should check that index is in fact an index on this relation?
	 */

108
	heap_close(OldHeap, NoLock);/* do NOT give up the locks */
109 110 111 112 113 114 115 116
	index_close(OldIndex);

	/*
	 * I need to build the copies of the heap and the index. The Commit()
	 * between here is *very* bogus. If someone is appending stuff, they
	 * will get the lock after being blocked and add rows which won't be
	 * present in the new table. Bleagh! I'd be best to try and ensure
	 * that no-one's in the tables for the entire duration of this process
117
	 * with a pg_vlock.  XXX Isn't the above comment now invalid?
118 119
	 */
	NewHeap = copy_heap(OIDOldHeap);
120
	OIDNewHeap = RelationGetRelid(NewHeap);
121
	strcpy(NewHeapName, RelationGetRelationName(NewHeap));
122 123 124 125 126 127 128 129 130 131 132

	/* To make the new heap visible (which is until now empty). */
	CommandCounterIncrement();

	rebuildheap(OIDNewHeap, OIDOldHeap, OIDOldIndex);

	/* To flush the filled new heap (and the statistics about it). */
	CommandCounterIncrement();

	/* Create new index over the tuples of the new heap. */
	copy_index(OIDOldIndex, OIDNewHeap);
M
 
Marc G. Fournier 已提交
133
	snprintf(NewIndexName, NAMEDATALEN, "temp_%x", OIDOldIndex);
134 135 136 137 138 139 140 141 142

	/*
	 * make this really happen. Flush all the buffers. (Believe me, it is
	 * necessary ... ended up in a mess without it.)
	 */
	CommitTransactionCommand();
	StartTransactionCommand();

	/* Destroy old heap (along with its index) and rename new. */
143
	heap_drop_with_catalog(saveoldrelname);
144

B
Bruce Momjian 已提交
145 146 147
	CommitTransactionCommand();
	StartTransactionCommand();

148 149
	renamerel(NewHeapName, saveoldrelname);
	renamerel(NewIndexName, saveoldindexname);
150 151
}

152
static Relation
153 154
copy_heap(Oid OIDOldHeap)
{
155 156 157 158 159 160
	char		NewName[NAMEDATALEN];
	TupleDesc	OldHeapDesc,
				tupdesc;
	Oid			OIDNewHeap;
	Relation	NewHeap,
				OldHeap;
161 162 163 164 165

	/*
	 * Create a new heap relation with a temporary name, which has the
	 * same tuple description as the old one.
	 */
M
 
Marc G. Fournier 已提交
166
	snprintf(NewName, NAMEDATALEN, "temp_%x", OIDOldHeap);
167

168
	OldHeap = heap_open(OIDOldHeap, AccessExclusiveLock);
169
	OldHeapDesc = RelationGetDescr(OldHeap);
170 171

	/*
172 173
	 * Need to make a copy of the tuple descriptor,
	 * heap_create_with_catalog modifies it.
174 175 176 177
	 */

	tupdesc = CreateTupleDescCopy(OldHeapDesc);

178 179
	OIDNewHeap = heap_create_with_catalog(NewName, tupdesc,
										  RELKIND_RELATION, false);
180 181

	if (!OidIsValid(OIDNewHeap))
182
		elog(ERROR, "clusterheap: cannot create temporary heap relation\n");
183

184 185
	/* XXX why are we bothering to do this: */
	NewHeap = heap_open(OIDNewHeap, AccessExclusiveLock);
186

187 188
	heap_close(NewHeap, AccessExclusiveLock);
	heap_close(OldHeap, AccessExclusiveLock);
189 190

	return NewHeap;
191 192
}

193
static void
194 195
copy_index(Oid OIDOldIndex, Oid OIDNewHeap)
{
B
Bruce Momjian 已提交
196 197 198 199 200
	Relation	OldIndex,
				NewHeap;
	HeapTuple	Old_pg_index_Tuple,
				Old_pg_index_relation_Tuple,
				pg_proc_Tuple;
201
	Form_pg_index Old_pg_index_Form;
202
	Form_pg_class Old_pg_index_relation_Form;
B
Bruce Momjian 已提交
203 204 205 206
	Form_pg_proc pg_proc_Form;
	char	   *NewIndexName;
	AttrNumber *attnumP;
	int			natts;
207
	FuncIndexInfo *finfo;
208

209
	NewHeap = heap_open(OIDNewHeap, AccessExclusiveLock);
210 211 212 213 214 215 216
	OldIndex = index_open(OIDOldIndex);

	/*
	 * OK. Create a new (temporary) index for the one that's already here.
	 * To do this I get the info from pg_index, re-build the FunctInfo if
	 * I have to, and add a new index with a temporary name.
	 */
217
	Old_pg_index_Tuple = SearchSysCacheTuple(INDEXRELID,
218
							ObjectIdGetDatum(RelationGetRelid(OldIndex)),
B
Bruce Momjian 已提交
219
											 0, 0, 0);
220 221

	Assert(Old_pg_index_Tuple);
222
	Old_pg_index_Form = (Form_pg_index) GETSTRUCT(Old_pg_index_Tuple);
223

224
	Old_pg_index_relation_Tuple = SearchSysCacheTuple(RELOID,
225
							ObjectIdGetDatum(RelationGetRelid(OldIndex)),
B
Bruce Momjian 已提交
226
													  0, 0, 0);
227 228

	Assert(Old_pg_index_relation_Tuple);
229
	Old_pg_index_relation_Form = (Form_pg_class) GETSTRUCT(Old_pg_index_relation_Tuple);
230

M
 
Marc G. Fournier 已提交
231
	/* Set the name. */
232
	NewIndexName = palloc(NAMEDATALEN); /* XXX */
M
 
Marc G. Fournier 已提交
233
	snprintf(NewIndexName, NAMEDATALEN, "temp_%x", OIDOldIndex);
234 235 236 237 238 239 240

	/*
	 * Ugly as it is, the only way I have of working out the number of
	 * attribues is to count them. Mostly there'll be just one but I've
	 * got to be sure.
	 */
	for (attnumP = &(Old_pg_index_Form->indkey[0]), natts = 0;
B
Bruce Momjian 已提交
241
		 natts < INDEX_MAX_KEYS && *attnumP != InvalidAttrNumber;
242 243 244 245 246 247 248 249 250 251 252 253
		 attnumP++, natts++);

	/*
	 * If this is a functional index, I need to rebuild the functional
	 * component to pass it to the defining procedure.
	 */
	if (Old_pg_index_Form->indproc != InvalidOid)
	{
		finfo = (FuncIndexInfo *) palloc(sizeof(FuncIndexInfo));
		FIgetnArgs(finfo) = natts;
		FIgetProcOid(finfo) = Old_pg_index_Form->indproc;

254
		pg_proc_Tuple = SearchSysCacheTuple(PROCOID,
255
							ObjectIdGetDatum(Old_pg_index_Form->indproc),
B
Bruce Momjian 已提交
256
											0, 0, 0);
257 258 259 260

		Assert(pg_proc_Tuple);
		pg_proc_Form = (Form_pg_proc) GETSTRUCT(pg_proc_Tuple);
		namecpy(&(finfo->funcName), &(pg_proc_Form->proname));
261
		natts = 1;				/* function result is a single column */
262 263 264 265 266 267
	}
	else
	{
		finfo = (FuncIndexInfo *) NULL;
	}

268
	index_create(RelationGetRelationName(NewHeap),
269 270 271 272 273 274 275
				 NewIndexName,
				 finfo,
				 NULL,			/* type info is in the old index */
				 Old_pg_index_relation_Form->relam,
				 natts,
				 Old_pg_index_Form->indkey,
				 Old_pg_index_Form->indclass,
276
				 (Node *) NULL,	/* XXX where's the predicate? */
277
				 Old_pg_index_Form->indislossy,
278
				 Old_pg_index_Form->indisunique,
B
Bruce Momjian 已提交
279
				 Old_pg_index_Form->indisprimary);
280

281 282
	setRelhasindexInplace(OIDNewHeap, true, false);

283 284
	index_close(OldIndex);
	heap_close(NewHeap, AccessExclusiveLock);
285 286 287
}


288
static void
289 290
rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex)
{
B
Bruce Momjian 已提交
291 292 293 294 295
	Relation	LocalNewHeap,
				LocalOldHeap,
				LocalOldIndex;
	IndexScanDesc ScanDesc;
	RetrieveIndexResult ScanResult;
296 297 298 299 300

	/*
	 * Open the relations I need. Scan through the OldHeap on the OldIndex
	 * and insert each tuple into the NewHeap.
	 */
301 302
	LocalNewHeap = heap_open(OIDNewHeap, AccessExclusiveLock);
	LocalOldHeap = heap_open(OIDOldHeap, AccessExclusiveLock);
303
	LocalOldIndex = index_open(OIDOldIndex);
304 305 306

	ScanDesc = index_beginscan(LocalOldIndex, false, 0, (ScanKey) NULL);

B
Bruce Momjian 已提交
307
	while ((ScanResult = index_getnext(ScanDesc, ForwardScanDirection)) != NULL)
308
	{
309 310
		HeapTupleData LocalHeapTuple;
		Buffer		LocalBuffer;
311

312
		LocalHeapTuple.t_self = ScanResult->heap_iptr;
313 314
		LocalHeapTuple.t_datamcxt = NULL;
		LocalHeapTuple.t_data = NULL;
315
		heap_fetch(LocalOldHeap, SnapshotNow, &LocalHeapTuple, &LocalBuffer);
316 317 318 319 320 321 322 323 324 325 326 327 328 329
		if (LocalHeapTuple.t_data != NULL) {
			/*
			 * We must copy the tuple because heap_insert() will overwrite
			 * the commit-status fields of the tuple it's handed, and the
			 * retrieved tuple will actually be in a disk buffer!  Thus,
			 * the source relation would get trashed, which is bad news
			 * if we abort later on.  (This was a bug in releases thru 7.0)
			 */
			HeapTuple	copiedTuple = heap_copytuple(&LocalHeapTuple);

			ReleaseBuffer(LocalBuffer);
			heap_insert(LocalNewHeap, copiedTuple);
			heap_freetuple(copiedTuple);
		}
330
		pfree(ScanResult);
331
	}
332

333 334 335
	index_endscan(ScanDesc);

	index_close(LocalOldIndex);
336 337
	heap_close(LocalOldHeap, AccessExclusiveLock);
	heap_close(LocalNewHeap, AccessExclusiveLock);
338
}