diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index 9a7ed93..7643ebb 100644
*** a/src/backend/executor/nodeAgg.c
--- b/src/backend/executor/nodeAgg.c
*************** typedef struct AggStatePerGroupData
*** 267,272 ****
--- 267,274 ----
  	bool		transValueIsNull;
  
  	bool		noTransValue;	/* true if transValue not set yet */
+ 	
+ 	char		fcflags;		/* flags to pass to transition function */
  
  	/*
  	 * Note: noTransValue initially has the same value as transValueIsNull,
*************** initialize_aggregates(AggState *aggstate
*** 399,404 ****
--- 401,408 ----
  		 * signals that we still need to do this.
  		 */
  		pergroupstate->noTransValue = peraggstate->initValueIsNull;
+ 		
+ 		pergroupstate->fcflags = FCFlagAggTransition1st;
  	}
  }
  
*************** advance_transition_function(AggState *ag
*** 479,488 ****
--- 483,495 ----
  	fcinfo->arg[0] = pergroupstate->transValue;
  	fcinfo->argnull[0] = pergroupstate->transValueIsNull;
  	fcinfo->isnull = false;		/* just in case transfn doesn't set it */
+ 	fcinfo->flags = pergroupstate->fcflags;
  
  	newVal = FunctionCallInvoke(fcinfo);
  
+ 	pergroupstate->fcflags = FCFlagAggTransitionNth;
  	aggstate->curperagg = NULL;
+ 	
  
  	/*
  	 * If pass-by-ref datatype, must copy the new value into aggcontext and
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 2a29124..bfe81f0 100644
*** a/src/backend/utils/adt/numeric.c
--- b/src/backend/utils/adt/numeric.c
*************** int4_avg_accum(PG_FUNCTION_ARGS)
*** 3437,3458 ****
  	ArrayType  *transarray;
  	int32		newval = PG_GETARG_INT32(1);
  	Int8TransTypeData *transdata;
! 
! 	/*
! 	 * If we're invoked as an aggregate, we can cheat and modify our first
! 	 * parameter in-place to reduce palloc overhead. Otherwise we need to make
! 	 * a copy of it before scribbling on it.
! 	 */
! 	if (AggCheckCallContext(fcinfo, NULL))
! 		transarray = PG_GETARG_ARRAYTYPE_P(0);
! 	else
  		transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
  
! 	if (ARR_HASNULL(transarray) ||
! 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
! 		elog(ERROR, "expected 2-element int8 array");
! 
! 	transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
  	transdata->count++;
  	transdata->sum += newval;
  
--- 3437,3455 ----
  	ArrayType  *transarray;
  	int32		newval = PG_GETARG_INT32(1);
  	Int8TransTypeData *transdata;
! 	
! 	if (fcinfo->flags & FCFlagAggTransitionNth)
! 		transarray = (ArrayType *) PG_GETARG_DATUM(0);
! 	else {
  		transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
+ 		
+ 		if (ARR_HASNULL(transarray) ||
+ 			ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
+ 			elog(ERROR, "expected 2-element int8 array");
+ 	}
  
! 	transdata = (Int8TransTypeData *) (((char *) transarray) +
! 									   ARR_OVERHEAD_NONULLS(1));
  	transdata->count++;
  	transdata->sum += newval;
  
diff --git a/src/include/fmgr.h b/src/include/fmgr.h
index c4d4864..72f63d8 100644
*** a/src/include/fmgr.h
--- b/src/include/fmgr.h
*************** typedef struct FunctionCallInfoData
*** 74,79 ****
--- 74,80 ----
  	fmNodePtr	resultinfo;		/* pass or return extra info about result */
  	Oid			fncollation;	/* collation for function to use */
  	bool		isnull;			/* function must set true if result is NULL */
+ 	char		flags;			/* various flags about the type of call */
  	short		nargs;			/* # arguments actually passed */
  	Datum		arg[FUNC_MAX_ARGS];		/* Arguments passed to function */
  	bool		argnull[FUNC_MAX_ARGS]; /* T if arg[i] is actually NULL */
*************** extern void fmgr_info_cxt(Oid functionId
*** 103,108 ****
--- 104,112 ----
  extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
  			   MemoryContext destcxt);
  
+ #define FCFlagAggTransition1st 1
+ #define FCFlagAggTransitionNth 2
+ 
  /*
   * This macro initializes all the fields of a FunctionCallInfoData except
   * for the arg[] and argnull[] arrays.	Performance testing has shown that
*************** extern void fmgr_info_copy(FmgrInfo *dst
*** 117,122 ****
--- 121,127 ----
  		(Fcinfo).resultinfo = (Resultinfo); \
  		(Fcinfo).fncollation = (Collation); \
  		(Fcinfo).isnull = false; \
+ 		(Fcinfo).flags = 0; \
  		(Fcinfo).nargs = (Nargs); \
  	} while (0)
  
