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 1vnqaF-00CTUT-19 for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Feb 2026 04:03:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnqaE-00EuRU-1K for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Feb 2026 04:03:10 +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 1vnqaE-00EuRG-0O for pgsql-hackers@lists.postgresql.org; Thu, 05 Feb 2026 04:03:09 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vnqaB-000000018Hp-2O93 for pgsql-hackers@postgresql.org; Thu, 05 Feb 2026 04:03:09 +0000 Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-59dcdf60427so713444e87.3 for ; Wed, 04 Feb 2026 20:03:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1770264187; cv=none; d=google.com; s=arc-20240605; b=GvXQhxK8BPOnHoxvfORGPYTcdtQv8bPfJTjxWt4FYrxNt6noSEnE1+U2F/jR/c2lOW 0muRZyTRxjUMs66emDm8xWOFbStgLw7yEbFEBhrOFC44ZK7DYo40eG0stydfVn+lvaJv dpGyOjb49XmnWkVnszLiBpLBT7KhFHXy1+4iC4k3H79E90GAKYbFbztwVCcRMCkT55tt UFwteqdqL6+cqCCGGDqPk9Uf4w7fSYztj0KrUEhcpT6Kwwb5Kr7jifmsuLk0SRaEnMJs KsFcibenScOGofpeX065FAwmQXjl4MnaJQskdt16LIPAB/M95SQcKyTfgJmZ3t2OXoQg 5ZcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=kvT0vaLBZK1kDNGv8moDWqRqmXZBJZQRtU7GzMOGa8I=; fh=3XIccx/lx7FKy7v28m47YtNz6YlKhVlTKMkiNcbtjA8=; b=WrdOJZXIMAHjTajXxXeCiI9nxHPx4wj0XUPGzPtPW5aNUskKOI0ARpCmaf0Nn7dGd7 ARysBm/5xlQpQdruTXl/pryF3zXH3FbwqnGJkAwEvca5rtuga+qs33VrMTI7cbktjKDn dRX8YI9UzO2+F8FaYSw2Vu4gULhLBsIJrwjOwfoJEwfoXiWLKkDuPi+0HRRmmiAiHwjs e2bWZFUyfKR3Iq1Ot6SG1z9onm8dHA4LR175Lh6ZBlxLHKyU3U2B8E4MBaemfGOW9/Qp 1Tzu6AKn4x776iw9djfR0ecHyCSYzP2NLZn72PuQ5B2Me4tv//0xfEwIfgcTEbKZVpHF AuCA==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770264187; x=1770868987; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=kvT0vaLBZK1kDNGv8moDWqRqmXZBJZQRtU7GzMOGa8I=; b=dVbcM9tj6d9ghuQMsNh6XOmz0qsjzUEtjO1/O5+JVYpRlOIYBxib/jG8RvvxjoMNDU cqlTYcIQgs79qDipw4o0HwqMDvD6kKbSMYQzcpJX4KPT78awfshB1g/ZPU4s7M4tacfb EoCkjxXRIMmxcSNfxa+AO0NQiQ6eMr4B0qBSRNRofnE5gVL/bB2AbyfbKXieIgqzWKUg kvUoooM2FlG8Yz+oP3hr9960VB2rnML5DX1iAq7WHCWKw5u5VIp7UaPmPh7VI0GdN60H retj+tYzRsClRzBnTz/G83MTO5vutKDdxnygwWke4nEELr1Zx3pm6+ihISdMnQ1F2vHU 8nIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770264187; x=1770868987; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=kvT0vaLBZK1kDNGv8moDWqRqmXZBJZQRtU7GzMOGa8I=; b=UXeWu9GqsUATMNb9exjI8xH3BFssKfvKAOXnCxkXVKdoQ9QSwifDw0e5lCy45FvUgE a/qpheYWRbUISUklIucoHQQHG2My/5e6GEw9exc59J0RHn7xvrBG18RhU08POCYDFJfl 2dGZvnw/6IxomHg1xLbJu1oHTJjAZO/avvX+Kxdut5YGkWvIBVOnbgRFnHUJw4O+Y6jf RnPvHtu0jr/w9T4mjIOMiBkknJviqiPFD/KpJ/dq7uwOTkikOC0A+YdxyME8OVLAchIW IyReyDyfLJaSznaSiehqA6IdS7J3xaXno7NdwWTY4KYTHyh+JWw5i8Oo821rLcK858eI 3p5Q== X-Gm-Message-State: AOJu0YyESIaos/AJZ0mH5IqAdgHuttMhtOVZ2ICJc5SjSmPylQN4Wl0w F7dFQXwAMWXmV4n1AgDwTC7JicyxawwCYxJopxsWYiOB7pr9mIHdO96iBY+fp01hlsAPHYf5f63 pNA+FccWOuErCTfdky2J90MwEtrsHC2MLxRmk X-Gm-Gg: AZuq6aLeP8tOkJ5FucXmR6RJPcyl+V348X0Hryi50nmV5b1MXjy6cCGWRmK1YXkgM/w BIxxZcCV1uoCY+MvcUoY6v2YeE5fFPYG54jgXy4q66YKIaY5kaeTrwbCc35ZGebAWsrimMrv+W9 pH5dRUkDPr7Y4diY4BjHT4EYCm9zwKFYwR+p3ynuN/jeCEfF08uZva3BeKvoR6MTeiLuDrEjZF8 qhfoZ5RCA8oltfubDmvHDmfV331j5WpU2m3DpRmxRy7IscMvzXUuE0iafTXuubS0mbxBH4= X-Received: by 2002:a05:6512:1294:b0:59b:7c9f:625e with SMTP id 2adb3069b0e04-59e38c2de10mr1973394e87.44.1770264186491; Wed, 04 Feb 2026 20:03:06 -0800 (PST) MIME-Version: 1.0 References: <5DED17B4-D948-4C0B-9DE1-A915D0BFAA54@gmail.com> In-Reply-To: <5DED17B4-D948-4C0B-9DE1-A915D0BFAA54@gmail.com> From: Dilip Kumar Date: Thu, 5 Feb 2026 09:32:49 +0530 X-Gm-Features: AZwV_QjqD1USO4v1quUkY37jIIqC0UsceHqp2L0Y5eHI3CHHVN_dfIHu01jn1R0 Message-ID: Subject: Re: pg_upgrade: fix memory leak in SLRU I/O code To: Chao Li Cc: PostgreSQL mailing lists Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Feb 4, 2026 at 2:51=E2=80=AFPM Chao Li wro= te: > > Hi Hackers, > > This comes from a previous review and has been on my to-do list for a whi= le. > > 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, w= hich 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 m= emory 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 th= is to pg_strdup() and introduces a common cleanup helper to free all resour= ces associated with SlruSegState. > > See the attached patch. The patch LGTM, just one suggestion, adds a one liner comment over the new function[1] for consistency. [1] +static void +FreeSlruSegState(SlruSegState *state) +{ + if (state->fd !=3D -1) + close(state->fd); + + pg_free(state->fn); + pg_free(state->dir); + pg_free(state); +} --=20 Regards, Dilip Kumar Google