Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wFmWa-005XU6-2P for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Apr 2026 05:22:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wFmWY-00H8AM-2j for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Apr 2026 05:22:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wFmWY-00H8AE-1N for pgsql-hackers@lists.postgresql.org; Thu, 23 Apr 2026 05:22:50 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wFmWT-00000002MbU-1KmF for pgsql-hackers@postgresql.org; Thu, 23 Apr 2026 05:22:49 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2aaed195901so27225235ad.0 for ; Wed, 22 Apr 2026 22:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776921765; x=1777526565; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=X1jw75OPx37IH9zxzFKlwZoFPPBEAMqZFBbLA/Gmw1M=; b=UDia45HJITyrYTXZ0sSAABNClaVralmdcAu2plgoPhBkCDwNt3LQPhsT6/+HmZxnOx rBMEGto+LAGgePtaNbWiKgy83+VwmJgJ2eKcH5DgM/6Zg50plKicD2KTA2azs4ZGV+yn Tkf5e/9wnQkKbt5wa3wwO9Z/6ZlC+dvYvR9WaTbCIxhdvbF/OkEN3/7VOtPDMhuxn6kf Si5DDjR+sEf6snDcuFrOucJ/zFZueEUV28Xk75dttOgL2fWh6Zuz3yAWSa+zPgMbYBOc i09vTkIW7/bnypxOF+TsrY0aBVW5L23NO82AFLuMV4Jz353m08D6J4SH1ggrqZEMYfr/ PqKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776921765; x=1777526565; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=X1jw75OPx37IH9zxzFKlwZoFPPBEAMqZFBbLA/Gmw1M=; b=hQ0e2L97Tg8hdtujQVjD/wPiJYLIyky2qkA9j6C+wEHUlOoTmsDJyjqfa2h7TSvxEC ouFiMqvrjU1rRsENecQiEP55T27RWzqx/yKBTzzNkbw5UlPcBfIkoSY+XJe9GRQpYWEP fKoG3ClHbAyMUtK2L4D4FlGfvqmPB0vXymT9GVJ7cEpJ5LhlRu4fIU1zyoGZ6aa00j7r izlUhBJToMSDBEQXzxfOHT45bWhQs+rFCRA6roXZ1Jsc7E+bZqd+I9xYV3kkbQSQsZS+ XPnzefUiqOBT2p1tDFgg2pvraibD6+gl5sEd1QsapAd+2RXgciF5oOVVsX/awcFtsiOL H5fg== X-Forwarded-Encrypted: i=1; AFNElJ9SZKuBrQRnlXXU8iKJvgWZrcXCyK+s1Rz6k9XKsDE+b43SyxrC8s062KrlF3wiSMMsnYaEhnpheGQ2xuyL@postgresql.org X-Gm-Message-State: AOJu0Ywdo3DYdkcfX7o3eEvdr74b3viZpy5tHsfHQl/d4mQuWe7wEtVO yZpic5etPwsB819o0arCtToqpWfVRJU4zaYSAdOwDnMQleuFAp7shmqI X-Gm-Gg: AeBDievYv2RAc7uyKahKXlN7aAxfEfNIO+HS2JZl9CN9lv00VEPxbX7ORki1j2dbADQ IIVdZpruLWQUCwIkfgsaZL0/KKRVOtNAzM7mY1SsG9zz7hIAuyzzuwT+rYM2YA1icFsTn03T1LU TJeZm3TitU0toFSbHaWT6rYL5ALI186ojgd4yCyWN4ZbJNbEmItimkUmbnwYkbWJ6o7AaNuFkMX AjCLZPLQ1FAnVmiNDQL8IHn7Qy0Gd4Hd+srKbvEFhvutmFA7fnGLVoPNNMO21XO5u4ihjfFxnzJ vYIyHiazCj42zNroOyngzfeSZmmWUpG2kl7aZEer9b5eE8gsrOZYHlcbH5U4WSgmid+sWkYWIE5 fSc8qdGzmflii6YSLB3eP6bCshfqyC/pGyCibyPMk6xSKYBJeYNyfm8tXFc4nbDHV0iOLvpEl6P /725ich9gxXgmFjiLb9IVmQFDCWe1kuRijJQ9uJgKd9Q== X-Received: by 2002:a17:903:1b03:b0:2ae:ac0c:5a2a with SMTP id d9443c01a7336-2b5f9e7a9a0mr267763805ad.6.1776921764687; Wed, 22 Apr 2026 22:22:44 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab40786sm195451315ad.79.2026.04.22.22.22.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Apr 2026 22:22:44 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Fix memory leak in postmasterMain From: Chao Li In-Reply-To: Date: Thu, 23 Apr 2026 13:22:04 +0800 Cc: Fujii Masao , Henrik TJ , PostgreSQL Hackers Content-Transfer-Encoding: quoted-printable Message-Id: References: <3e890874-8204-e1c2-8def-3a02cbad2232@0x48.dk> <1658D50F-A8B3-406A-94ED-FA8C7F9DA90B@gmail.com> To: Andres Freund X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Apr 23, 2026, at 01:55, Andres Freund wrote: >=20 > Hi, >=20 > On 2026-04-23 00:29:29 +0900, Fujii Masao wrote: >> On Wed, Apr 22, 2026 at 3:46=E2=80=AFPM Chao Li = wrote: >>>=20 >>> =46rom 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. >>=20 >> We can also free()/pfree() userDoption in postgres.c and bootstrap.c? >>=20 >> 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. >=20 > 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? >=20 Yes, calling it =E2=80=9Cmemory leak=E2=80=9D 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 =E2=80=9Cunnecessary retained = memory=E2=80=9D 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. >=20 > ISTM those strdup()s should actually be pstrdup()s? I suspect = changing that > would also silence valgrind. >=20 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/