Re: Fix memory leak in postmasterMain

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, Henrik TJ <henrik(at)0x48(dot)dk>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix memory leak in postmasterMain
Date: 2026-04-23 05:22:04
Message-ID: E07322B5-FDE6-4334-BBE5-01D5E5208C61@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Apr 23, 2026, at 01:55, Andres Freund <andres(at)anarazel(dot)de> wrote:
>
> Hi,
>
> On 2026-04-23 00:29:29 +0900, Fujii Masao wrote:
>> On Wed, Apr 22, 2026 at 3:46 PM Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>>>
>>> From my experience, most of the time the postmaster is started with -D. On Linux and macOS, that path can be quite long, PATH_MAX is often 4096 on many Unix-like systems, and I am not sure about Windows. So I think this leak is worth fixing.
>>
>> We can also free()/pfree() userDoption in postgres.c and bootstrap.c?
>>
>> Since userDoption typically uses only a small amount of memory, I'm not sure
>> this patch provides much practical benefit from a memory-leak perspective.
>
> I don't think this is a leak at all. We *never* can reach the end of the scope
> in which userDoption is allocated. We abort() if the end of PostmasterMain()
> is ever reached. What is the leak here supposed to actually be?
>

Yes, calling it “memory leak” is too strict. Usually, memory leak implies repeatedly losing memory over time, but in this case, userDoption is no longer used after feeding to SelectConfigFiles(). So calling it “unnecessary retained memory” might be more accurate.

In theory, it occupies at most PATH_MAX bytes, but in practice it should be much less than that. So, I agree the benefit of fixing is trivial.

>
> ISTM those strdup()s should actually be pstrdup()s? I suspect changing that
> would also silence valgrind.
>

I also had the question when I read the code. But looks like startup phase all uses malloc/strdup etc. C functions, for example SelectConfigFiles() in guc.c also uses malloc/free. I am not sure what is the standard.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-04-23 05:28:23 Re: Question about criteria for adding items to the v19 open items wiki page
Previous Message David Rowley 2026-04-23 05:19:22 Re: Cleanup shadows variable warnings, round 1