Not quite like that. Look into nodeAgg.c, which solves a similar problemfor the transvalues themselves with code like /* forget the old value, if any */ if (!oldIsNull && !pertrans->inputtypeByVal) pfree(DatumGetPointer(oldVal));