Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mzQ9d-0003Ko-TV for pgsql-general@arkaria.postgresql.org; Mon, 20 Dec 2021 21:25:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mzQ9c-0007ee-SE for pgsql-general@arkaria.postgresql.org; Mon, 20 Dec 2021 21:25:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mzQ9c-0007bM-Hf for pgsql-general@lists.postgresql.org; Mon, 20 Dec 2021 21:25:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mzQ9a-0006TT-MH for pgsql-general@lists.postgresql.org; Mon, 20 Dec 2021 21:25:07 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1BKLP5qK2658305; Mon, 20 Dec 2021 16:25:05 -0500 From: Tom Lane To: Matt Magoffin cc: pgsql-general@lists.postgresql.org Subject: Re: Freeing transient memory in aggregate functions In-reply-to: References: Comments: In-reply-to Matt Magoffin message dated "Tue, 21 Dec 2021 09:26:47 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2658303.1640035505.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 20 Dec 2021 16:25:05 -0500 Message-ID: <2658304.1640035505@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Matt Magoffin writes: > I=E2=80=99m essentially doing a datumCopy() on every non-null input valu= e. 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, s= ince I don=E2=80=99t really need it anymore? Something like > if (state->last !=3D 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