pg_upgrade: fix memory leak in SLRU I/O code

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: pg_upgrade: fix memory leak in SLRU I/O code
Date: 2026-02-04 09:20:49
Message-ID: 5DED17B4-D948-4C0B-9DE1-A915D0BFAA54@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/

Attachment Content-Type Size
v1-0001-pg_upgrade-fix-memory-leak-in-SLRU-I-O-code.patch application/octet-stream 2.0 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2026-02-04 09:24:27 Re: AIX support
Previous Message Bertrand Drouvot 2026-02-04 09:00:18 Re: Re[2]: [PATCH] Add last_executed timestamp to pg_stat_statements