diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index f0b3b87..462a2d9 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -3177,6 +3177,22 @@ makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2) } /* + * As makeNumericAggState() but allocate the state in the current memory + * context. + */ +static NumericAggState * +makeNumericAggStateCurrentContext(bool calcSumX2) +{ + NumericAggState *state; + + state = (NumericAggState *) palloc0(sizeof(NumericAggState)); + state->calcSumX2 = calcSumX2; + state->agg_context = CurrentMemoryContext; + + return state; +} + +/* * Accumulate a new input value for numeric aggregate functions. */ static void @@ -3374,7 +3390,7 @@ numeric_combine(PG_FUNCTION_ARGS) { old_context = MemoryContextSwitchTo(agg_context); - state1 = makeNumericAggState(fcinfo, true); + state1 = makeNumericAggStateCurrentContext(true); state1->N = state2->N; state1->NaNcount = state2->NaNcount; state1->maxScale = state2->maxScale; @@ -3465,7 +3481,7 @@ numeric_avg_combine(PG_FUNCTION_ARGS) { old_context = MemoryContextSwitchTo(agg_context); - state1 = makeNumericAggState(fcinfo, false); + state1 = makeNumericAggStateCurrentContext(false); state1->N = state2->N; state1->NaNcount = state2->NaNcount; state1->maxScale = state2->maxScale; @@ -3584,7 +3600,7 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS) initStringInfo(&buf); appendBinaryStringInfo(&buf, VARDATA(sstate), VARSIZE(sstate) - VARHDRSZ); - result = makeNumericAggState(fcinfo, false); + result = makeNumericAggStateCurrentContext(false); /* N */ result->N = pq_getmsgint64(&buf); @@ -3696,7 +3712,7 @@ numeric_deserialize(PG_FUNCTION_ARGS) initStringInfo(&buf); appendBinaryStringInfo(&buf, VARDATA(sstate), VARSIZE(sstate) - VARHDRSZ); - result = makeNumericAggState(fcinfo, false); + result = makeNumericAggStateCurrentContext(false); /* N */ result->N = pq_getmsgint64(&buf);