public inbox for [email protected]
help / color / mirror / Atom feedFrom: Chao Li <[email protected]>
To: PostgreSQL mailing lists <[email protected]>
Subject: pg_upgrade: fix memory leak in SLRU I/O code
Date: Wed, 4 Feb 2026 17:20:49 +0800
Message-ID: <[email protected]> (raw)
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.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
[application/octet-stream] v1-0001-pg_upgrade-fix-memory-leak-in-SLRU-I-O-code.patch (2.0K, ../[email protected]/2-v1-0001-pg_upgrade-fix-memory-leak-in-SLRU-I-O-code.patch)
download | inline diff:
From f06d718e119cbf17ac9a7aecb41eed385a4f4f41 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Wed, 4 Feb 2026 16:53:00 +0800
Subject: [PATCH v1] pg_upgrade: fix memory leak in SLRU I/O code
SlruSegState->dir is allocated in AllocSlruSegState() but was never freed.
Add a common cleanup helper to release all resources associated with an
SLRU segment state and use it for both readers and writers.
Author: Chao Li <[email protected]>
Reviewed-by:
Discussion: https://postgr.es/m/
---
src/bin/pg_upgrade/slru_io.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/src/bin/pg_upgrade/slru_io.c b/src/bin/pg_upgrade/slru_io.c
index ae3e224d7b1..efc0fb71dda 100644
--- a/src/bin/pg_upgrade/slru_io.c
+++ b/src/bin/pg_upgrade/slru_io.c
@@ -19,6 +19,7 @@
#include "slru_io.h"
static SlruSegState *AllocSlruSegState(const char *dir);
+static void FreeSlruSegState(SlruSegState *state);
static char *SlruFileName(SlruSegState *state, int64 segno);
static void SlruFlush(SlruSegState *state);
@@ -28,7 +29,7 @@ AllocSlruSegState(const char *dir)
{
SlruSegState *state = pg_malloc(sizeof(*state));
- state->dir = pstrdup(dir);
+ state->dir = pg_strdup(dir);
state->fn = NULL;
state->fd = -1;
state->segno = -1;
@@ -69,6 +70,17 @@ AllocSlruRead(const char *dir, bool long_segment_names)
return state;
}
+static void
+FreeSlruSegState(SlruSegState *state)
+{
+ if (state->fd != -1)
+ close(state->fd);
+
+ pg_free(state->fn);
+ pg_free(state->dir);
+ pg_free(state);
+}
+
/*
* Read the given page into memory buffer.
*
@@ -154,9 +166,7 @@ FreeSlruRead(SlruSegState *state)
{
Assert(!state->writing); /* read only mode */
- if (state->fd != -1)
- close(state->fd);
- pg_free(state);
+ FreeSlruSegState(state);
}
/*
@@ -263,7 +273,5 @@ FreeSlruWrite(SlruSegState *state)
SlruFlush(state);
- if (state->fd != -1)
- close(state->fd);
- pg_free(state);
+ FreeSlruSegState(state);
}
--
2.50.1 (Apple Git-155)
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]
Subject: Re: pg_upgrade: fix memory leak in SLRU I/O code
In-Reply-To: <[email protected]>
* 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