Re: Missing free_var() at end of accum_sum_final()?

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Joel Jacobson <joel(at)compiler(dot)org>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Missing free_var() at end of accum_sum_final()?
Date: 2023-02-16 06:26:26
Message-ID: Y+3Mkq0DoI31Ek5u@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Feb 16, 2023 at 06:59:13AM +0100, Joel Jacobson wrote:
> I noticed the NumericVar's pos_var and neg_var are not free_var()'d
> at the end of accum_sum_final().
>
> The potential memory leak seems small, since the function is called
> only once per sum() per worker (and from a few more places), but
> maybe they should be free'd anyways for correctness?

Indeed, it is true that any code path of numeric.c that relies on a
NumericVar with an allocation done in its buffer is careful enough to
free it, except for generate_series's SRF where one step of the
computation is done. I don't see directly why you could not do the
following:
@@ -11973,6 +11973,9 @@ accum_sum_final(NumericSumAccum *accum, NumericVar *result)
/* And add them together */
add_var(&pos_var, &neg_var, result);

+ free_var(&pos_var);
+ free_var(&neg_var);
+
/* Remove leading/trailing zeroes */
strip_var(result);
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2023-02-16 06:32:16 Re: Support logical replication of global object commands
Previous Message Hayato Kuroda (Fujitsu) 2023-02-16 06:20:23 RE: Time delayed LR (WAS Re: logical replication restrictions)