Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Matt Magoffin <postgresql(dot)org(at)msqr(dot)us>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions
Date: 2021-12-04 16:16:05
Message-ID: 2643578.1638634565@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Matt Magoffin <postgresql(dot)org(at)msqr(dot)us> writes:
> So far, I have been working on average support via the vec_to_mean() aggregate, and my aggregate's [2] transition function sets 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 switch the 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Matt Magoffin 2021-12-04 17:56:07 Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions
Previous Message Matt Magoffin 2021-12-04 05:14:46 Handling memory contexts in aggregate function invoking other built-in aggregate functions