Need some help on code

Maarten Boekhold <maartenb@dutepp0.et.tudelft.nl>

From: Maarten Boekhold <maartenb@dutepp0.et.tudelft.nl>
To: pgsql-hackers@postgresql.org
Date: 1998-06-07T19:27:13Z
Lists: pgsql-hackers
Hi,

I was trying to change to cluster command to do the its writes clustered 
by a 100 tuples, thus hoping to improve performance. However, the code 
I've written crashes. This has certainly to do with some internal states 
of pgsql that aren't preserved in a HeapTuple.

Could somebody with knowledge have a brief glimpse on my code and perhaps 
tell me how to do it properly?

Maarten

_____________________________________________________________________________
| TU Delft, The Netherlands, Faculty of Information Technology and Systems  |
|                   Department of Electrical Engineering                    |
|           Computer Architecture and Digital Technique section             |
|                          M.Boekhold@et.tudelft.nl                         |
-----------------------------------------------------------------------------

static void
rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex)
{
	Relation	LocalNewHeap,
				LocalOldHeap,
				LocalOldIndex;
	IndexScanDesc ScanDesc;
	RetrieveIndexResult ScanResult;
	ItemPointer HeapTid;
	HeapTuple	LocalHeapTuple;
	Buffer		LocalBuffer[100];
	Oid			OIDNewHeapInsert;
	Dllist      *ScanResList;
	Dlelem      *ListEl;
	int         count, loop;

	/*
	 * Open the relations I need. Scan through the OldHeap on the OldIndex
	 * and insert each tuple into the NewHeap.
	 */
	LocalNewHeap = (Relation) heap_open(OIDNewHeap);
	LocalOldHeap = (Relation) heap_open(OIDOldHeap);
	LocalOldIndex = (Relation) index_open(OIDOldIndex);
	ScanResList = DLNewList();

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

	loop = 1;
	while (loop) {
		count = 0;
		while ((count < 100) &&
			   ((ScanResult =
				index_getnext(ScanDesc, 
					ForwardScanDirection)) != NULL))
		{
			
			HeapTid = &ScanResult->heap_iptr;
			pfree(ScanResult);
			LocalHeapTuple = heap_fetch(LocalOldHeap, false,
					HeapTid, &LocalBuffer[count]);
			ListEl = DLNewElem(LocalHeapTuple);
			DLAddTail(ScanResList, ListEl);
			count++;
		}

		if (count < 100) loop = 0;

		count = 0;
		while ((ListEl = DLRemHead(ScanResList)) != NULL) {
			LocalHeapTuple = (HeapTuple)ListEl->dle_val;
			DLFreeElem(ListEl);
			OIDNewHeapInsert =
				heap_insert(LocalNewHeap, LocalHeapTuple);
			ReleaseBuffer(LocalBuffer[count]);
			count++;
		}
	}

	index_endscan(ScanDesc);

	index_close(LocalOldIndex);
	heap_close(LocalOldHeap);
	heap_close(LocalNewHeap);
	DLFreeList(ScanResList);
}