*** ./src/backend/utils/sort/tuplesort.c.orig	Thu Mar 17 11:48:21 2005
--- ./src/backend/utils/sort/tuplesort.c	Thu Mar 17 12:01:59 2005
***************
*** 96,101 ****
--- 96,102 ----
  #include "utils/lsyscache.h"
  #include "utils/syscache.h"
  #include "utils/tuplesort.h"
+ #include "executor/tuptable.h"
  
  
  /*
***************
*** 258,264 ****
  	 * These variables are specific to the HeapTuple case; they are set by
  	 * tuplesort_begin_heap and used only by the HeapTuple routines.
  	 */
! 	TupleDesc	tupDesc;
  	int			nKeys;
  	ScanKey		scanKeys;
  	SortFunctionKind *sortFnKinds;
--- 259,266 ----
  	 * These variables are specific to the HeapTuple case; they are set by
  	 * tuplesort_begin_heap and used only by the HeapTuple routines.
  	 */
! 	TupleTableSlot	*leftSlot;
! 	TupleTableSlot	*rightSlot;
  	int			nKeys;
  	ScanKey		scanKeys;
  	SortFunctionKind *sortFnKinds;
***************
*** 460,466 ****
  	state->writetup = writetup_heap;
  	state->readtup = readtup_heap;
  
! 	state->tupDesc = tupDesc;
  	state->nKeys = nkeys;
  	state->scanKeys = (ScanKey) palloc0(nkeys * sizeof(ScanKeyData));
  	state->sortFnKinds = (SortFunctionKind *)
--- 462,469 ----
  	state->writetup = writetup_heap;
  	state->readtup = readtup_heap;
  
! 	state->leftSlot = MakeSingleTupleTableSlot(tupDesc);
! 	state->rightSlot = MakeSingleTupleTableSlot(tupDesc);
  	state->nKeys = nkeys;
  	state->scanKeys = (ScanKey) palloc0(nkeys * sizeof(ScanKeyData));
  	state->sortFnKinds = (SortFunctionKind *)
***************
*** 572,577 ****
--- 575,585 ----
  	if (state->sortFnKinds)
  		pfree(state->sortFnKinds);
  
+ 	if (state->leftSlot)
+ 		ExecDropSingleTupleTableSlot(state->leftSlot);
+ 	if (state->rightSlot)
+ 		ExecDropSingleTupleTableSlot(state->rightSlot);
+ 
  	pfree(state);
  }
  
***************
*** 1919,1928 ****
  static int
  comparetup_heap(Tuplesortstate *state, const void *a, const void *b)
  {
! 	HeapTuple	ltup = (HeapTuple) a;
! 	HeapTuple	rtup = (HeapTuple) b;
! 	TupleDesc	tupDesc = state->tupDesc;
! 	int			nkey;
  
  	for (nkey = 0; nkey < state->nKeys; nkey++)
  	{
--- 1927,1940 ----
  static int
  comparetup_heap(Tuplesortstate *state, const void *a, const void *b)
  {
! 	HeapTuple		ltup = (HeapTuple) a;
! 	HeapTuple		rtup = (HeapTuple) b;
! 	TupleTableSlot *lslot = state->leftSlot;
! 	TupleTableSlot *rslot = state->rightSlot;
! 	int				nkey;
! 
! 	ExecStoreTuple(ltup, lslot, InvalidBuffer, false);
! 	ExecStoreTuple(rtup, rslot, InvalidBuffer, false);
  
  	for (nkey = 0; nkey < state->nKeys; nkey++)
  	{
***************
*** 1934,1941 ****
  					isnull2;
  		int32		compare;
  
! 		datum1 = heap_getattr(ltup, attno, tupDesc, &isnull1);
! 		datum2 = heap_getattr(rtup, attno, tupDesc, &isnull2);
  
  		compare = inlineApplySortFunction(&scanKey->sk_func,
  										  state->sortFnKinds[nkey],
--- 1946,1953 ----
  					isnull2;
  		int32		compare;
  
! 		datum1 = slot_getattr(lslot, attno, &isnull1);
! 		datum2 = slot_getattr(rslot, attno, &isnull2);
  
  		compare = inlineApplySortFunction(&scanKey->sk_func,
  										  state->sortFnKinds[nkey],
