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 1wLPZu-001lNd-0L for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2026 18:05:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wLPZs-00Almn-2U for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2026 18:05:32 +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 1wLPZs-00Alme-0x for pgsql-hackers@lists.postgresql.org; Fri, 08 May 2026 18:05:32 +0000 Received: from mail-qk1-x733.google.com ([2607:f8b0:4864:20::733]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wLPZp-00000000qfj-3fnG for pgsql-hackers@postgresql.org; Fri, 08 May 2026 18:05:31 +0000 Received: by mail-qk1-x733.google.com with SMTP id af79cd13be357-8ec37d52c0dso316846085a.0 for ; Fri, 08 May 2026 11:05:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778263529; cv=none; d=google.com; s=arc-20240605; b=OtWhGkg4IW3M0Acq2x94lqDMV83DoHCecT1zXUohQastOOovni+7xVcvuBgZ7iIJt7 OsMJ6/RmpTk/nawPBxJZhh7n7lu7NVaoPPUUvGYHecRSo0CnyqsUA1RA3dVtrBgQ8TlP tDR8EhMkghfTfMSFU0fSGbAwsy9CcpeZN7Fwv5jnEPpxiN0riSLr3AAsQYYEHrcinfnr /tZdw1xwX83ZskngGuZokKpMGX8o9WKxVRefMhLLIMKVUcwkoRvaMkA8cxXI2WRphaDA vIUEn93Z7Is5lOy2C6rtbgpgdpikm8nECZB2lW1IJalK+Ex36be1R2FHL2THFtmpv8YZ YCgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=RtxpbYdjWgsJq6JYZnXBKab6BD1bh6i3ZUuRpVRDlkM=; fh=U8cNGG26YB8TDbPsp6iqTaOkLGHKiedG618uzQ3YsVE=; b=Ln7JYFkXCcQqbuSPgOF5sz/5zKK+fUXTudABeHQZR1E7W94Nzm0S6pTqu27zX4QlOp c821roKPMiArdVhUvDAjUSGPv03CF1JOgrZIXTTXh1T6BE9kA1uflcGSo6f4RhdJYaEm WUWPy24LWQTVdXxQEPArjADhc5qkfgNP6z5viXpnhvf65O7UZq3+w0ivDGmZifEKhk6O oUYLH1cDfazkkynYk9NgpMj3uJNhq5o7Bit5l18qW/uQe+/7LR3S6Kek/9EWF7rdKikA Z5pzipSl5OA6kXHay0SlcReYxZ4/DWfizZV4ULeZsI0ho/O/bOARtTN9El4AdCLahnDb 153A==; 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=20251104; t=1778263529; x=1778868329; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=RtxpbYdjWgsJq6JYZnXBKab6BD1bh6i3ZUuRpVRDlkM=; b=MPtgftXqljkUb+hX2qb9qIwqXgnYe9k291+4bumB4eY8KM1DVbFW/UXExNfWVLt5EX 53XP10r8mSiS3PoJ+3lNVKQhwd3gmLvd2ej3bqCWOieDejwfQCPu78kGYscH/77AS/2g SYlNswDqaY9qTWWe0puuMTN4IByIkEzJOEuNrK+xG5FdHeaIwzwhBISok8kQRkuJYH3i Cj5oXo3z+uYuEDDx/1nh6DnahB8Z/DARC0ut7ySKoPuFdKk+vD7eVYERbKpPJDx1ASRB D58etcVPTANGbJF6CReHGD4OEsH0EwGxDX7yl833Dmb5qDDU/A9UP4b9RpvABQWasFXt 07Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778263529; x=1778868329; h=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=RtxpbYdjWgsJq6JYZnXBKab6BD1bh6i3ZUuRpVRDlkM=; b=d+iXksDa2m+nYNKBqHOJSMpjQvHh1b7/aR4OW++7twbJQWEv+wiKrctZ+R/FiO0kTG XlWbizjx6ytmEOdqgpzI+2bHqe+A1j/5M25kNVgl+heIUaHZq3iMxbH2dI/Y+UbDsphH GNTt8xHUS5tGI8vDIvkdcl3CFKg0X3JumIoDtd2iifXSFxDe10LfM+mXNnI0juiGjMYW 7bk4ttdlES6/d7y9v5bBsNbmnu0w2NWlYYUktv8I/sljwaVCjHN1W5dGM4WwXUfsH3gL NTrl/NEDnsyQA23WjtPz+AFHsASeok8jz/s1wZJeonRHbZ9MUKme7u8dDnnfqSJorxKF HN0Q== X-Forwarded-Encrypted: i=1; AFNElJ/R8smw3NUSsVREALhFBdkHNIiu+4Vkauft060gmO2+ZDBauuepuytLT2DgaC8/vFM6U3dTsVqY8K0IfOmp@postgresql.org X-Gm-Message-State: AOJu0Yw9QZgbzb4Xnsm5CjD6PpaW+4yE3xPLJ01VEa0MqfE0bYqdjndE 3LRjCu8Ux40nwSN5uFP+zJh14RLrUjoxkoPgEdfTJS4yZu+VXTqYIkrWwqKXairfPVWPWcPPOrn 6CVG4LKb0xeabFGBujXor4FGSf4K4CQ== X-Gm-Gg: AeBDiesciCAr1yph4MbdQuIRY8SZPbU/IVQY44vxAwrXUuCGf09Gk/uyQJzyw+q1Lv4 OzIoL0WI5JxSetZy4iYcqRA8eizhDjrqtoqFL6bZDYlwtBu06shcLIfEKStJzBqvpwT5NgrBbzc EWGaju8rMaK7FjHmPEh12BZ+dbJGNU/sQXqBJDnpDiwrK0fUy+SEamP+arwCTIBA0Q4UbijyaxZ ke4msqGr7V0fL6GG5FQp7A8IgShwDzQypiXdJJ29wX/5pvxrto3zJkpRDFpsxufuMrHFPpHzG4f XhCJBtM= X-Received: by 2002:a05:620a:450f:b0:8cd:b315:6af6 with SMTP id af79cd13be357-904d4392116mr1926198885a.8.1778263529493; Fri, 08 May 2026 11:05:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Shinya Kato Date: Sat, 9 May 2026 03:05:20 +0900 X-Gm-Features: AVHnY4JV590yU3SZJhYFRPH96Jmlxwqduxg8jPhcqdijI5vpEzdtSuk4JJcNWmw Message-ID: Subject: Re: Call EndCopyFrom() after initial table sync in logical replication To: Chao Li Cc: Fujii Masao , cca5507 , PostgreSQL-development Content-Type: multipart/alternative; boundary="000000000000fe19a20651523da7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000fe19a20651523da7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, May 8, 2026, 14:10 Chao Li wrote: > > I don=E2=80=99t think this is a serious leak. In this path, pstate and at= tnamelist > are allocated in CurTransactionContext, and the transaction is committed > immediately after copy_table() finishes, so that memory is reclaimed at > transaction end. Explicitly freeing them would be mostly for code > readability, not to fix a memory leak. So, I am okay to not free them. > I agree that no additional memory cleanup is needed here. > While tracing the code, I noticed another issue that is probably more > worth addressing. copy_table() currently does: > ``` > copybuf =3D makeStringInfo(); > ``` > > But copybuf is only used by copy_read_data(), and there it's really just > acting as a small state holder for data, len, and cursor, rather than as = a > normal growable StringInfo. That means we do not need to allocate a > StringInfo object or its backing buffer at all. > > It would be cleaner to use a plain StringInfoData and simply reinitialize > or zero it in copy_table(). See the attached diff for the proposed change= . > > David Rowley has made several cleanup changes in this area to prefer > stack-allocated StringInfoData, for example > a63bbc811d41b3567eb37fe2636e660a852dbbf2. This change seems consistent wi= th > that direction. > Thanks for the suggestion. The copybuf change looks worthwhile, but perhaps it=E2=80=99s better discus= sed in a separate thread. -- Shinya Kato > --000000000000fe19a20651523da7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, May 8, 2026, 14:10 Chao = Li <li.evan.chao@gmail.com= > wrote:

I don=E2=80=99t think this is a serious leak. In this path, pstate and attn= amelist are allocated in CurTransactionContext, and the transaction is comm= itted immediately after copy_table() finishes, so that memory is reclaimed = at transaction end. Explicitly freeing them would be mostly for code readab= ility, not to fix a memory leak. So, I am okay to not free them.

I agree tha= t no additional memory cleanup is=E3=80=80needed here.


While tracing the code, I noticed another issue that is probably more worth= addressing. copy_table() currently does:
```
=C2=A0 =C2=A0 copybuf =3D makeStringInfo();
```

But copybuf is only used by copy_read_data(), and there it's really jus= t acting as a small state holder for data, len, and cursor, rather than as = a normal growable StringInfo. That means we do not need to allocate a Strin= gInfo object or its backing buffer at all.

It would be cleaner to use a plain StringInfoData and simply reinitialize o= r zero it in copy_table(). See the attached diff for the proposed change.
David Rowley has made several cleanup changes in this area to prefer stack-= allocated StringInfoData, for example a63bbc811d41b3567eb37fe2636e660a852db= bf2. This change seems consistent with that direction.

Thanks for the sugges= tion.

The copybuf change= looks worthwhile, but perhaps it=E2=80=99s better discussed in a separate = thread.

--
Shinya Kato
--000000000000fe19a20651523da7--