pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Freeing transient memory in aggregate functions
3+ messages / 2 participants
[nested] [flat]

* Freeing transient memory in aggregate functions
@ 2021-12-20 20:26 Matt Magoffin <postgresql.org@msqr.us>
  2021-12-20 21:25 ` Re: Freeing transient memory in aggregate functions Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Matt Magoffin @ 2021-12-20 20:26 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I have a question about trying to keep memory from growing too much in a C aggregate function with pass-by-reference types. I am trying to keep track of a last-seen value in my aggregate state, so I have code roughly doing this:

Datum current;
MemoryContext aggContext;
AggCheckCallContext(fcinfo, &aggContext);
old = MemoryContextSwitchTo(aggContext);

if (!PG_ARGISNULL(0)) {
  current = PG_GETARG_DATUM(0);
  state->last = datumCopy(&current, typbyval, typlen);
}
MemoryContextSwitchTo(old);

I’m essentially doing a datumCopy() on every non-null input value. I was wondering if there is a way to free the previously copied datum, since I don’t really need it anymore? Something like

if (!PG_ARGISNULL(0)) {
  current = PG_GETARG_DATUM(0);
  if (state->last != NULL) {
    pfree(state->last);
  }
  state->last = datumCopy(&current, typbyval, typlen);
}

I wasn’t sure it was allowed to call pfree() like this. My actual function is dealing with array input values, and for large sets of inputs I didn’t want to grow memory use as large as the entire data set being aggregated.

Kind regards,
Matt=

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Freeing transient memory in aggregate functions
  2021-12-20 20:26 Freeing transient memory in aggregate functions Matt Magoffin <postgresql.org@msqr.us>
@ 2021-12-20 21:25 ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-12-21 19:29   ` Re: Freeing transient memory in aggregate functions Matt Magoffin <postgresql.org@msqr.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2021-12-20 21:25 UTC (permalink / raw)
  To: Matt Magoffin <postgresql.org@msqr.us>; +Cc: pgsql-general@lists.postgresql.org

Matt Magoffin <postgresql.org@msqr.us> writes:
> I’m essentially doing a datumCopy() on every non-null input value.

If you're doing that in the aggContext, you definitely need to free
the prior one(s).

> I was wondering if there is a way to free the previously copied datum, since I don’t really need it anymore? Something like

>   if (state->last != NULL) {
>     pfree(state->last);
>   }

Not quite like that.  Look into nodeAgg.c, which solves a similar problem
for the transvalues themselves with code like

            /* forget the old value, if any */
            if (!oldIsNull && !pertrans->inputtypeByVal)
                pfree(DatumGetPointer(oldVal));

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Freeing transient memory in aggregate functions
  2021-12-20 20:26 Freeing transient memory in aggregate functions Matt Magoffin <postgresql.org@msqr.us>
  2021-12-20 21:25 ` Re: Freeing transient memory in aggregate functions Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-12-21 19:29   ` Matt Magoffin <postgresql.org@msqr.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Matt Magoffin @ 2021-12-21 19:29 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org

On 21/12/2021, at 10:25 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Not quite like that.  Look into nodeAgg.c, which solves a similar problem
> for the transvalues themselves with code like
> 
>            /* forget the old value, if any */
>            if (!oldIsNull && !pertrans->inputtypeByVal)
>                pfree(DatumGetPointer(oldVal));

Oh great, thanks for the tip!

— m@=

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2021-12-21 19:29 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-12-20 20:26 Freeing transient memory in aggregate functions Matt Magoffin <postgresql.org@msqr.us>
2021-12-20 21:25 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-12-21 19:29   ` Matt Magoffin <postgresql.org@msqr.us>

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