diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c
index a63fde2..c883ae9 100644
*** a/src/backend/access/spgist/spgscan.c
--- b/src/backend/access/spgist/spgscan.c
*************** spgbeginscan(Relation rel, int keysz, in
*** 294,303 ****
  	/* Set up indexTupDesc and xs_hitupdesc in case it's an index-only scan */
  	so->indexTupDesc = scan->xs_hitupdesc = RelationGetDescr(rel);
  
  	if (scan->numberOfOrderBys > 0)
  	{
! 		so->zeroDistances = palloc(sizeof(double) * scan->numberOfOrderBys);
! 		so->infDistances = palloc(sizeof(double) * scan->numberOfOrderBys);
  
  		for (i = 0; i < scan->numberOfOrderBys; i++)
  		{
--- 294,311 ----
  	/* Set up indexTupDesc and xs_hitupdesc in case it's an index-only scan */
  	so->indexTupDesc = scan->xs_hitupdesc = RelationGetDescr(rel);
  
+ 	/* Allocate various arrays needed for order-by scans */
  	if (scan->numberOfOrderBys > 0)
  	{
! 		/* This will be filled in spgrescan, but allocate the space here */
! 		so->orderByTypes = (Oid *)
! 			palloc(sizeof(Oid) * scan->numberOfOrderBys);
! 
! 		/* These arrays have constant contents, so we can fill them now */
! 		so->zeroDistances = (double *)
! 			palloc(sizeof(double) * scan->numberOfOrderBys);
! 		so->infDistances = (double *)
! 			palloc(sizeof(double) * scan->numberOfOrderBys);
  
  		for (i = 0; i < scan->numberOfOrderBys; i++)
  		{
*************** spgbeginscan(Relation rel, int keysz, in
*** 305,313 ****
  			so->infDistances[i] = get_float8_infinity();
  		}
  
! 		scan->xs_orderbyvals = palloc0(sizeof(Datum) * scan->numberOfOrderBys);
! 		scan->xs_orderbynulls = palloc(sizeof(bool) * scan->numberOfOrderBys);
! 		memset(scan->xs_orderbynulls, true, sizeof(bool) * scan->numberOfOrderBys);
  	}
  
  	fmgr_info_copy(&so->innerConsistentFn,
--- 313,324 ----
  			so->infDistances[i] = get_float8_infinity();
  		}
  
! 		scan->xs_orderbyvals = (Datum *)
! 			palloc0(sizeof(Datum) * scan->numberOfOrderBys);
! 		scan->xs_orderbynulls = (bool *)
! 			palloc(sizeof(bool) * scan->numberOfOrderBys);
! 		memset(scan->xs_orderbynulls, true,
! 			   sizeof(bool) * scan->numberOfOrderBys);
  	}
  
  	fmgr_info_copy(&so->innerConsistentFn,
*************** spgrescan(IndexScanDesc scan, ScanKey sc
*** 336,341 ****
--- 347,353 ----
  		memmove(scan->keyData, scankey,
  				scan->numberOfKeys * sizeof(ScanKeyData));
  
+ 	/* initialize order-by data if needed */
  	if (orderbys && scan->numberOfOrderBys > 0)
  	{
  		int			i;
*************** spgrescan(IndexScanDesc scan, ScanKey sc
*** 343,350 ****
  		memmove(scan->orderByData, orderbys,
  				scan->numberOfOrderBys * sizeof(ScanKeyData));
  
- 		so->orderByTypes = (Oid *) palloc(sizeof(Oid) * scan->numberOfOrderBys);
- 
  		for (i = 0; i < scan->numberOfOrderBys; i++)
  		{
  			ScanKey		skey = &scan->orderByData[i];
--- 355,360 ----
*************** spgendscan(IndexScanDesc scan)
*** 380,390 ****
--- 390,411 ----
  	MemoryContextDelete(so->tempCxt);
  	MemoryContextDelete(so->traversalCxt);
  
+ 	if (so->keyData)
+ 		pfree(so->keyData);
+ 
+ 	if (so->state.deadTupleStorage)
+ 		pfree(so->state.deadTupleStorage);
+ 
  	if (scan->numberOfOrderBys > 0)
  	{
+ 		pfree(so->orderByTypes);
  		pfree(so->zeroDistances);
  		pfree(so->infDistances);
+ 		pfree(scan->xs_orderbyvals);
+ 		pfree(scan->xs_orderbynulls);
  	}
+ 
+ 	pfree(so);
  }
  
  /*
