Re: [HACKERS] Need some help on code

From: dg(at)illustra(dot)com (David Gould)
To: M(dot)Boekhold(at)et(dot)tudelft(dot)nl
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] Need some help on code
Date: 1998-06-08 22:12:55
Message-ID: 9806082212.AA11383@hawk.illustra.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Maarten wrote:

> 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?
...
> 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);
^^^^^^^^^^^^^^^^^^
Hmmm, at this point, HeapTid is a pointer to what?

> LocalHeapTuple = heap_fetch(LocalOldHeap, false,
> HeapTid, &LocalBuffer[count]);

Given more than one tuple on a page, then there may exist some
LocalBuffer[i] == LocalBuffer[j] where i and j are distinct values of 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]);

So here we ReleaseBuffer(LocalBuffer[count]) which if there are more than
one LocalBuffer[] that are in fact the same buffer will release the buffer
multiple times.

> count++;
> }
> }
>
> index_endscan(ScanDesc);
>
> index_close(LocalOldIndex);
> heap_close(LocalOldHeap);
> heap_close(LocalNewHeap);
> DLFreeList(ScanResList);
> }

Hope this helps.
-dg

David Gould dg(at)illustra(dot)com 510.628.3783 or 510.305.9468
Informix Software (No, really) 300 Lakeside Drive Oakland, CA 94612
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats." -- Howard Aiken

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Gould 1998-06-08 22:22:14 Re: [HACKERS] keeping track of connections
Previous Message Bruce Momjian 1998-06-08 21:55:22 Re: [HACKERS] Re: Cancel key now ready