Re: Freeing transient memory 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: Freeing transient memory in aggregate functions
Date: 2021-12-20 21:25:05
Message-ID: 2658304.1640035505@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:
> 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Shaozhong SHI 2021-12-20 22:10:05 How best to turn select result into options like 'a|b|c''
Previous Message Peter Eisentraut 2021-12-20 21:07:17 Re: PGBouncer logs explanation required