public inbox for [email protected]  
help / color / mirror / Atom feed
From: Dilip Kumar <[email protected]>
To: Chao Li <[email protected]>
Cc: PostgreSQL mailing lists <[email protected]>
Subject: Re: pg_upgrade: fix memory leak in SLRU I/O code
Date: Thu, 5 Feb 2026 09:32:49 +0530
Message-ID: <CAFiTN-v2y0AV5YNeG5aTaupi_5+HKGw-gXtFuV3ixYpqSW_iwQ@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

On Wed, Feb 4, 2026 at 2:51 PM Chao Li <[email protected]> wrote:
>
> Hi Hackers,
>
> This comes from a previous review and has been on my to-do list for a while.
>
> Since src/bin/pg_upgrade/slru_io.c includes postgres_fe.h, it is frontend code, so backend memory contexts are not used here.
>
> In the current code:
> ```
> void
> FreeSlruWrite(SlruSegState *state)
> {
>         Assert(state->writing);
>
>         SlruFlush(state);
>
>         if (state->fd != -1)
>                 close(state->fd);
>         pg_free(state);
> }
> ```
>
> the SlruSegState itself is freed, but state->dir and state->fn are not, which results in a memory leak during pg_upgrade runs. More generally, I don’t see a reason to free an object itself without also freeing the memory owned by its members.
>
> While looking at this, I also noticed that state->dir is allocated using pstrdup(). To better align with frontend conventions, the patch switches this to pg_strdup() and introduces a common cleanup helper to free all resources associated with SlruSegState.
>
> See the attached patch.

The patch LGTM, just one suggestion, adds a one liner comment over the
new function[1] for consistency.

[1]
+static void
+FreeSlruSegState(SlruSegState *state)
+{
+ if (state->fd != -1)
+ close(state->fd);
+
+ pg_free(state->fn);
+ pg_free(state->dir);
+ pg_free(state);
+}

-- 
Regards,
Dilip Kumar
Google






view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: pg_upgrade: fix memory leak in SLRU I/O code
  In-Reply-To: <CAFiTN-v2y0AV5YNeG5aTaupi_5+HKGw-gXtFuV3ixYpqSW_iwQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox