Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions - Mailing list pgsql-general

From Tom Lane
Subject Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions
Date
Msg-id 2643578.1638634565@sss.pgh.pa.us
Whole thread Raw
In response to Handling memory contexts in aggregate function invoking other built-in aggregate functions  (Matt Magoffin <postgresql.org@msqr.us>)
Responses Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions  (Matt Magoffin <postgresql.org@msqr.us>)
Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions  (Matt Magoffin <postgresql.org@msqr.us>)
List pgsql-general
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



pgsql-general by date:

Previous
From: Matt Magoffin
Date:
Subject: Handling memory contexts in aggregate function invoking other built-in aggregate functions
Next
From: Matt Magoffin
Date:
Subject: Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions