pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Matt Magoffin <postgresql.org@msqr.us>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions
Date: Sat, 04 Dec 2021 11:16:05 -0500
Message-ID: <2643578.1638634565@sss.pgh.pa.us> (raw)
In-Reply-To: <AA3A2911-8555-49C9-AA42-8A5470D1728D@msqr.us>
References: <AA3A2911-8555-49C9-AA42-8A5470D1728D@msqr.us>

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 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





view thread (5+ messages)  latest in thread

Message-ID: <2643578.1638634565@sss.pgh.pa.us>
Permalink:  ../2643578.1638634565@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2643578.1638634565@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, postgresql.org@msqr.us
  Subject: Re: Handling memory contexts in aggregate function invoking other built-in aggregate functions
  In-Reply-To: <2643578.1638634565@sss.pgh.pa.us>
  References: <AA3A2911-8555-49C9-AA42-8A5470D1728D@msqr.us> <2643578.1638634565@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox