Matt Magoffin <postgresql.org@msqr.us> writes:
> So far, I have been working on average support via the vec_to_mean() aggregate, and my aggregate's [2] transition
functionsets up a FunctionCallInfo for the numeric_avg_accum() [3] function and then loops over the input array
elements,calling numeric_avg_accum() and saving its result state object in my aggregate’s state. Before looping, I
switchthe memory context to the aggregate’s context, i.e. there is stuff like
> MemoryContext aggContext;
> AggCheckCallContext(fcinfo, &aggContext);
> old = MemoryContextSwitchTo(aggContext);
> for (i = 0; i < arrayLength; i++) {
> // invoke numeric_avg_accum() for each array element, store result in my state
> }
> MemoryContextSwitchTo(old);
Calling numeric_avg_accum in the agg_context is unnecessary, and possibly
counterproductive (it might leak memory in that context, since like all
other aggregates it assumes it's called in a short-lived context).
That doesn't seem to explain your crash though.
Are you testing in an --enable-cassert build? If not, do that;
it might make the cause of the crashes more apparent, thanks to
CLOBBER_FREED_MEMORY and other debug support.
regards, tom lane