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 1vnZ4m-008tNN-1A for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Feb 2026 09:21:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnZ4k-00A7tF-1B for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Feb 2026 09:21:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vnZ4k-00A7t7-0D for pgsql-hackers@lists.postgresql.org; Wed, 04 Feb 2026 09:21:29 +0000 Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vnZ4h-0000000107o-0hni for pgsql-hackers@postgresql.org; Wed, 04 Feb 2026 09:21:29 +0000 Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-8c9f6b78ca4so691900585a.0 for ; Wed, 04 Feb 2026 01:21:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770196885; x=1770801685; darn=postgresql.org; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=uuG+FRfXxyicyzXPKIsoZhOFX1WUG4nzJlnnvXpOfIs=; b=nEEnzo41GBF8uXM6alnBxF0+/kqIbqPENtDBFTW+SamfFD8TISsVKlBZS67iUiGI3f iRYzqOsAFdlKOdHnJk3Vhrjd1b2AiRIATjypTM8kcT/qscjx8CiLptXsyDFKGmz+LZum +MmIiqTAnBT7zHMrWIgc8Xl6vFrLtGGFOqetEsNOlMZ/f5lMpoXQWX8+pjmBD17IhFkn 3qWEVINKFY1s37ivq7fEKf5seqxkRSiun/DlfL5xa6vxFe/P//cPWfO3ZmjYMCWsw2ba kqM7LL/79/5+g2Ub3wUWF5wLExiF7ktmxCuDG3nRStRnyaWJ2UAQ80qShZ33f1saO+R/ YAyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770196885; x=1770801685; h=to:date:message-id:subject:mime-version:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uuG+FRfXxyicyzXPKIsoZhOFX1WUG4nzJlnnvXpOfIs=; b=Dkz4Wmm88+LXCn49lyQkP8hdFe6XLNQ7b2fjhXOUWEUJATD+OKDLHTuuRQmZV/XRrF fBRs2OjLxH7ZsL9kUVgRrxu8CUvn9I+k8Zk4UHmrgBv1HQ0EqmRRoosBiAilq/09kgSe d+eEa6hAlerhBxNmSJ1voEbJc/ioW7iLxYUvqVqRB2L6Y6zB/QhBxNn9dXGcFB25BTgI pSDJoUgNwcAvz1kIE+eJbsQwAL/km2zpnwBCpTlDg7qcL0khYmQKE9lZcy100YdNULUS T/ygjdf7MOjE9WEew+4is7t0sa8zsddWyrpqLS9xJ8L5kTaelK+/BllAnf7Ar7dpIq5y P1cA== X-Gm-Message-State: AOJu0Yy2Ojy8abYXGgh1k9v4y9Vfvc2Q28Zt8oW26+Q3oOhvUtcYAD8V olqKw7tWF2toG1LUZcEcUJC3ta4P69kyLnzyg9k05+IIJq34Oud09aKoAOcVK4QtU8c= X-Gm-Gg: AZuq6aIEUEONsLi5l9ocWYOrUj3pVEpcPQRUvGSXpAJ0Abr5y0Pzu8qyoE9XXH2aaOW 9y2C9h7cI8qr2JNBk05aFBDcPm7FSdyL6NLBpHk0Yx9k75DyeIphJ2or2DImLM2j7DunTpl71ys AGonxCJf8QWw6lxerS5Utq500FJWhIeWq1/8iCUqamsIHRk25ghLNnxqNONLcVfU/wkyCTy3kn/ Zr2Xyx86hs+m9RTRy02ZOpXy1Xn/13qUewQhu6PQRmqv1fthoteRsOmHCduKbX/PHuqCegLxBDH 6+Kv8SAqyQYPtTRlJrRXU0yIuK/FW/ZWMDAPpJ2q+HgcjcfzTns9qSyw8O8MLR70kDvXC3ZvPL9 wMdZB3w43Q/WHD1r0zz2xwNl7xnGux1RwpWfHAGxa0kOjro0y25sPK09gseTBDw/HlZo1oa747I C3CjUrElOO5ptAkWaw42t9 X-Received: by 2002:a05:620a:3184:b0:8c7:1156:efeb with SMTP id af79cd13be357-8ca2f8319f0mr293081585a.24.1770196884603; Wed, 04 Feb 2026 01:21:24 -0800 (PST) Received: from smtpclient.apple ([209.127.78.222]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-89521bfe715sm15698826d6.3.2026.02.04.01.21.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Feb 2026 01:21:23 -0800 (PST) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_32812FDA-1F35-450E-A02E-0862971F05AC" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.300.41.1.7\)) Subject: pg_upgrade: fix memory leak in SLRU I/O code Message-Id: <5DED17B4-D948-4C0B-9DE1-A915D0BFAA54@gmail.com> Date: Wed, 4 Feb 2026 17:20:49 +0800 To: PostgreSQL mailing lists X-Mailer: Apple Mail (2.3864.300.41.1.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_32812FDA-1F35-450E-A02E-0862971F05AC Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 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 !=3D -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=E2=80=99t 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/ --Apple-Mail=_32812FDA-1F35-450E-A02E-0862971F05AC Content-Disposition: attachment; filename=v1-0001-pg_upgrade-fix-memory-leak-in-SLRU-I-O-code.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-pg_upgrade-fix-memory-leak-in-SLRU-I-O-code.patch" Content-Transfer-Encoding: quoted-printable =46rom=20f06d718e119cbf17ac9a7aecb41eed385a4f4f41=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Wed,=204=20Feb=202026=2016:53:00=20+0800=0ASubject:=20[PATCH=20= v1]=20pg_upgrade:=20fix=20memory=20leak=20in=20SLRU=20I/O=20code=0A=0A= SlruSegState->dir=20is=20allocated=20in=20AllocSlruSegState()=20but=20= was=20never=20freed.=0AAdd=20a=20common=20cleanup=20helper=20to=20= release=20all=20resources=20associated=20with=20an=0ASLRU=20segment=20= state=20and=20use=20it=20for=20both=20readers=20and=20writers.=0A=0A= Author:=20Chao=20Li=20=0AReviewed-by:=0ADiscussion:=20= https://postgr.es/m/=0A---=0A=20src/bin/pg_upgrade/slru_io.c=20|=2022=20= +++++++++++++++-------=0A=201=20file=20changed,=2015=20insertions(+),=20= 7=20deletions(-)=0A=0Adiff=20--git=20a/src/bin/pg_upgrade/slru_io.c=20= b/src/bin/pg_upgrade/slru_io.c=0Aindex=20ae3e224d7b1..efc0fb71dda=20= 100644=0A---=20a/src/bin/pg_upgrade/slru_io.c=0A+++=20= b/src/bin/pg_upgrade/slru_io.c=0A@@=20-19,6=20+19,7=20@@=0A=20#include=20= "slru_io.h"=0A=20=0A=20static=20SlruSegState=20*AllocSlruSegState(const=20= char=20*dir);=0A+static=20void=20FreeSlruSegState(SlruSegState=20= *state);=0A=20static=20char=20*SlruFileName(SlruSegState=20*state,=20= int64=20segno);=0A=20static=20void=20SlruFlush(SlruSegState=20*state);=0A= =20=0A@@=20-28,7=20+29,7=20@@=20AllocSlruSegState(const=20char=20*dir)=0A= =20{=0A=20=09SlruSegState=20*state=20=3D=20pg_malloc(sizeof(*state));=0A=20= =0A-=09state->dir=20=3D=20pstrdup(dir);=0A+=09state->dir=20=3D=20= pg_strdup(dir);=0A=20=09state->fn=20=3D=20NULL;=0A=20=09state->fd=20=3D=20= -1;=0A=20=09state->segno=20=3D=20-1;=0A@@=20-69,6=20+70,17=20@@=20= AllocSlruRead(const=20char=20*dir,=20bool=20long_segment_names)=0A=20=09= return=20state;=0A=20}=0A=20=0A+static=20void=0A= +FreeSlruSegState(SlruSegState=20*state)=0A+{=0A+=09if=20(state->fd=20!=3D= =20-1)=0A+=09=09close(state->fd);=0A+=0A+=09pg_free(state->fn);=0A+=09= pg_free(state->dir);=0A+=09pg_free(state);=0A+}=0A+=0A=20/*=0A=20=20*=20= Read=20the=20given=20page=20into=20memory=20buffer.=0A=20=20*=0A@@=20= -154,9=20+166,7=20@@=20FreeSlruRead(SlruSegState=20*state)=0A=20{=0A=20=09= Assert(!state->writing);=09/*=20read=20only=20mode=20*/=0A=20=0A-=09if=20= (state->fd=20!=3D=20-1)=0A-=09=09close(state->fd);=0A-=09pg_free(state);=0A= +=09FreeSlruSegState(state);=0A=20}=0A=20=0A=20/*=0A@@=20-263,7=20+273,5=20= @@=20FreeSlruWrite(SlruSegState=20*state)=0A=20=0A=20=09= SlruFlush(state);=0A=20=0A-=09if=20(state->fd=20!=3D=20-1)=0A-=09=09= close(state->fd);=0A-=09pg_free(state);=0A+=09FreeSlruSegState(state);=0A= =20}=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_32812FDA-1F35-450E-A02E-0862971F05AC--