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 1wLDTl-001cxO-19 for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2026 05:10:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wLDTj-0081A3-0w for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2026 05:10:23 +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 1wLDTi-00819t-36 for pgsql-hackers@lists.postgresql.org; Fri, 08 May 2026 05:10:22 +0000 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wLDTg-000000019V6-3GFa for pgsql-hackers@postgresql.org; Fri, 08 May 2026 05:10:22 +0000 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-834f1075805so1076151b3a.2 for ; Thu, 07 May 2026 22:10:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778217018; x=1778821818; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=BfEqbZFQY4t2JM5jJ89uxLXmYPsKJDLGx4ibLlSoG2Q=; b=awxStG5ZUZJ69fRPIrlC3QdSxksBksMnaSnMKYm1xhMmgHZBrsEItLHblNakessfm3 c1wzhu85pebaQfLHP8VlT5dOFlMar4ILVSH7+LKfYm5w1erjiooDyxxPJ3TyrxOlWCtK c3dM9piKymeWPFPLwwNYVM+huCjXnYNct1Qaw+7N1eyj9ukRbsEHE0sB8uOH/sRteduJ CNODZ/Tndu4Icew4M9Sq56WFimyBQNZpH0TA7opI/gNj0HgUxSlfjemvUCMMTzx4ZfHe 52iX0vB3KcWziDpUYahrIl3RA6q61oTgR9xbeutljUMteYeOhHDK2Nsx+wbx/n/2nSi+ F85w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778217018; x=1778821818; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=BfEqbZFQY4t2JM5jJ89uxLXmYPsKJDLGx4ibLlSoG2Q=; b=o5oGbETOMJnnB2VQYXYkeNs0eYUJyKEgxOVtRU7I0ypGlHI1CCCfjAkLx0NEjx9F/n oK5+X99ygRoIsEG90tLvYuuqwiviYRia5t1GdJ6fg0y9/K433+wP+fwhEjaGZpCTn7W9 fdax1FQc2Yt+zWjJOQOKQ20MTrqfBa6cD8wfIwOaFHhkGSSixMSzhen7udT0sSqYVVu1 Om0WC97HwQpNaHhNxZVzu/V7mLd3TFmRgyMg0uXp9I8IkNLcsxpf7NMflL4Le/NnGNon 91xPSRQ7kuXeA3fsTLc0e3j+sIgYku2BaLRj6JHJ54BpqRjCYvRS+YKbUA5zAMjOVfkL XaQA== X-Forwarded-Encrypted: i=1; AFNElJ9DPwZFEgWLi+5EegkZuvP3JrYTVnwQh2iPQq26e9h2aL6vxnRV1KPfgjnIGq9AGOdsgVdK4mtO/IIKvE6/@postgresql.org X-Gm-Message-State: AOJu0YwcfxytP9XxC1QAS/6k/suxEZktSwxP0PHesPzivGRpE+UP1ors 89nREpyjc25K8WFOPqJIQgnv85C3bu7o5EjNwy4Z/quCOJzuDAdRuWG8 X-Gm-Gg: AeBDiev041p5MHVSfu1uHgZ7StPeJ+CpQAQmkxq2SaZc3LSAFm0WwmMtyQsbUKTtUsF 1JKwEfTyiGErhuZSwry7iJkTk/vAG2/BFHi+IqAfdMpW2qEsQfbzNx9c48P+HFe/Y2OR0XQ1sVA EkSVFXczvOmQ93XlxZ2laOsAVXWRiWkkg7rn/YtDidhC5YHx2AobdXtNIZkWsX6bMnaAbHML8/+ bFgrLZ5Mn8L8ZY/HswwpOhyamJZk6wJwrlNEYQJbp8tcTzSW4mWqZbjXP/wGlONZTcX28LKAXQD /MGcBCgCf8duKKgoMbgjkXEskJ2gpbMn9BGmQt4+R+F9Yo75ZzH3iDrk8NDS13j7DTJVynZcpWE 5amdzkSf6wRgipVR+pv9+PjM1TsMaktRdCX1JrXuFYmogWUSzHRPGOFph+GLOc14VHWRBOIo4sw pwVMuhsQeV+3DEdnvH3GeF0KO0o2Fa57k= X-Received: by 2002:a05:6a00:84c3:b0:83d:446c:2aa4 with SMTP id d2e1a72fcca58-83d446c2c07mr292182b3a.33.1778217018103; Thu, 07 May 2026 22:10:18 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-83965a3e3ecsm11063767b3a.19.2026.05.07.22.10.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 May 2026 22:10:17 -0700 (PDT) From: Chao Li Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_4F37E405-9B49-480C-9046-122F947DDC77" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Call EndCopyFrom() after initial table sync in logical replication Date: Fri, 8 May 2026 13:09:38 +0800 In-Reply-To: Cc: cca5507 , Shinya Kato , PostgreSQL-development To: Fujii Masao References: X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_4F37E405-9B49-480C-9046-122F947DDC77 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On May 8, 2026, at 12:21, Fujii Masao wrote: >=20 > On Fri, May 8, 2026 at 11:34=E2=80=AFAM cca5507 = wrote: >>=20 >> Hi, >>=20 >> Maybe we want to add "free_parsestate(pstate);" after the = "EndCopyFrom()" as well? >=20 > What actual issue could occur if free_parsestate() is not called = there? >=20 > Since pstate->p_target_relation does not seem to be used afterward, > omitting free_parsestate() appears mostly harmless to me. Bascailly > calling free_parsestate() after make_parsestate() seems intuitive, > but from a quick grep I found several places that call = make_parsestate() > without a corresponding free_parsestate(). >=20 > Regards, >=20 > --=20 > Fujii Masao I don=E2=80=99t think this is a serious leak. In this path, pstate and = attnamelist 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. 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 = with that direction. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_4F37E405-9B49-480C-9046-122F947DDC77 Content-Disposition: attachment; filename=tablesync.c.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="tablesync.c.diff" Content-Transfer-Encoding: 7bit diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index eb718114297..1dee2480e2f 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -126,7 +126,7 @@ List *table_states_not_ready = NIL; -static StringInfo copybuf = NULL; +static StringInfoData copybuf = {0}; /* * Wait until the relation sync state is set in the catalog to the expected @@ -649,13 +649,13 @@ copy_read_data(void *outbuf, int minread, int maxread) int avail; /* If there are some leftover data from previous read, use it. */ - avail = copybuf->len - copybuf->cursor; + avail = copybuf.len - copybuf.cursor; if (avail) { if (avail > maxread) avail = maxread; - memcpy(outbuf, ©buf->data[copybuf->cursor], avail); - copybuf->cursor += avail; + memcpy(outbuf, ©buf.data[copybuf.cursor], avail); + copybuf.cursor += avail; maxread -= avail; bytesread += avail; } @@ -680,16 +680,16 @@ copy_read_data(void *outbuf, int minread, int maxread) else { /* Process the data */ - copybuf->data = buf; - copybuf->len = len; - copybuf->cursor = 0; + copybuf.data = buf; + copybuf.len = len; + copybuf.cursor = 0; - avail = copybuf->len - copybuf->cursor; + avail = copybuf.len - copybuf.cursor; if (avail > maxread) avail = maxread; - memcpy(outbuf, ©buf->data[copybuf->cursor], avail); + memcpy(outbuf, ©buf.data[copybuf.cursor], avail); outbuf = (char *) outbuf + avail; - copybuf->cursor += avail; + copybuf.cursor += avail; maxread -= avail; bytesread += avail; } @@ -1199,7 +1199,7 @@ copy_table(Relation rel) lrel.nspname, lrel.relname, res->err))); walrcv_clear_result(res); - copybuf = makeStringInfo(); + memset(©buf, 0, sizeof(copybuf)); pstate = make_parsestate(NULL); (void) addRangeTableEntryForRelation(pstate, rel, AccessShareLock, --Apple-Mail=_4F37E405-9B49-480C-9046-122F947DDC77--