*** ./src/backend/executor/nodeAgg.c.orig	Wed Aug 31 10:11:05 2005
--- ./src/backend/executor/nodeAgg.c	Wed Aug 31 10:13:06 2005
***************
*** 392,398 ****
  	if (!peraggstate->transtypeByVal &&
  	DatumGetPointer(newVal) != DatumGetPointer(pergroupstate->transValue))
  	{
! 		if (!fcinfo.isnull)
  		{
  			MemoryContextSwitchTo(aggstate->aggcontext);
  			newVal = datumCopy(newVal,
--- 392,399 ----
  	if (!peraggstate->transtypeByVal &&
  	DatumGetPointer(newVal) != DatumGetPointer(pergroupstate->transValue))
  	{
! 		if (!fcinfo.isnull &&
! 			!MemoryContextContains(aggstate->aggcontext, (void *)newVal))
  		{
  			MemoryContextSwitchTo(aggstate->aggcontext);
  			newVal = datumCopy(newVal,
*** ./src/backend/utils/adt/numeric.c.orig	Wed Aug 31 09:47:36 2005
--- ./src/backend/utils/adt/numeric.c	Wed Aug 31 10:22:08 2005
***************
*** 33,38 ****
--- 33,39 ----
  #include "utils/builtins.h"
  #include "utils/int8.h"
  #include "utils/numeric.h"
+ #include "nodes/execnodes.h"
  
  /* ----------
   * Uncomment the following to enable compilation of dump_numeric()
***************
*** 2372,2378 ****
  			PG_RETURN_NULL();	/* still no non-null */
  		/* This is the first non-null input. */
  		newval = (int64) PG_GETARG_INT16(1);
! 		PG_RETURN_INT64(newval);
  	}
  
  	/*
--- 2373,2398 ----
  			PG_RETURN_NULL();	/* still no non-null */
  		/* This is the first non-null input. */
  		newval = (int64) PG_GETARG_INT16(1);
! 
! 		/*
! 		 * If we're invoked by nodeAgg, we use AggState->aggcontext instead
! 		 * of per-input-tuple memory context to reduce cycle at AllocSetReset.
! 		 */
! 		if (fcinfo->context && IsA(fcinfo->context, AggState))
! 		{
! 			AggState *aggstate = (AggState *)fcinfo->context;
! 			MemoryContext oldContext =
! 				MemoryContextSwitchTo(aggstate->aggcontext);
! 
! 			newval = Int64GetDatum(newval);
! 			MemoryContextSwitchTo(oldContext);
! 
! 			PG_RETURN_POINTER(newval);
! 		}
! 		else
! 		{
! 			PG_RETURN_INT64(newval);
! 		}
  	}
  
  	/*
***************
*** 2417,2423 ****
  			PG_RETURN_NULL();	/* still no non-null */
  		/* This is the first non-null input. */
  		newval = (int64) PG_GETARG_INT32(1);
! 		PG_RETURN_INT64(newval);
  	}
  
  	/*
--- 2437,2462 ----
  			PG_RETURN_NULL();	/* still no non-null */
  		/* This is the first non-null input. */
  		newval = (int64) PG_GETARG_INT32(1);
! 
! 		/*
! 		 * If we're invoked by nodeAgg, we use AggState->aggcontext instead
! 		 * of per-input-tuple memory context to reduce cycle at AllocSetReset.
! 		 */
! 		if (fcinfo->context && IsA(fcinfo->context, AggState))
! 		{
! 			AggState *aggstate = (AggState *)fcinfo->context;
! 			MemoryContext oldContext =
! 				MemoryContextSwitchTo(aggstate->aggcontext);
! 
! 			newval = Int64GetDatum(newval);
! 			MemoryContextSwitchTo(oldContext);
! 
! 			PG_RETURN_POINTER(newval);
! 		}
! 		else
! 		{
! 			PG_RETURN_INT64(newval);
! 		}
  	}
  
  	/*
