Matt Magoffin <postgresql.org@xxxxxxx> 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