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 1wLaua-001t3e-16 for pgsql-hackers@arkaria.postgresql.org; Sat, 09 May 2026 06:11:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wLauZ-00CQY1-0B for pgsql-hackers@arkaria.postgresql.org; Sat, 09 May 2026 06:11:39 +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 1wLauY-00CQXt-2N for pgsql-hackers@lists.postgresql.org; Sat, 09 May 2026 06:11:38 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wLauU-00000001M5v-22o1 for pgsql-hackers@postgresql.org; Sat, 09 May 2026 06:11:38 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2ba21d32776so18991715ad.2 for ; Fri, 08 May 2026 23:11:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778307090; x=1778911890; darn=postgresql.org; h=to:cc:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=/boWkLCow2ERTLJ4EeFtYwx1q6oPeMMoKhiBWqIPWpo=; b=YHmr9sVeJB99iXfsYVI80VS0N2YzM1GGfxJB4VmIEYye276umXYdzoanT54JkmQFDo SZ/2uWLdtvf3fE/OsVnIhp4WOMag6K0YMPkj8SU7WdvqWVQqvyY4+h9FCWZOLfmxSkh8 SOZS5HW8EOcYq8fzj2yy9pPTR0/FV8smPgcwWk0jZIOW9usO81uPN+ilJOVD4B0PEkOO SmTAcKipZYUWZUYLLqVbQScSK0Psn9kBsH+5tz0vS1BD+SuTAiftt50wxrpX//zXUJXA 5jrm4WOsV4dkuFB0suYJAgBL7fq/KwOP5cXa4kJMmItPRDxefalmlXJj5eHnT3QLh5Cv GGQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778307090; x=1778911890; h=to:cc:date:message-id:subject:mime-version:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/boWkLCow2ERTLJ4EeFtYwx1q6oPeMMoKhiBWqIPWpo=; b=XIsxYRyQh+gUlzH1KOsb0ebg46sAoA9aiYqB8JI7rz32DpuCJ76mcCT6k947U+mlLL pTtJCibyK60IqZPhnadtO+cIobip3gCmSmAHJ5rssmrBtZH1W7SoUHJ3kFSFwKJYGfp9 VJNM83p9OiYYeIXDPil+M+uptcjdaBkqCn45lBK8yC2rSJBkW/NdRuNWvrmx7jZX/eku aIcAnjZEpvtedIRT9Ukr5N2w61ni5ppbdOqwF8jkfv/8WB9Ygxd43fkhAuV2UiR0qRej rd9JeqS5XG8a5B/if1HIE5HWZAwOJGMRJapI8k4se6eAM809o/k/M3HVEcAZawrKP/pB ltFQ== X-Gm-Message-State: AOJu0Yxdg4mYOXo68C5gZZud7v1v2vVN2l3loUouKsCEyuaereCOpWBT ysEQ/puXxZw+hJnr239bNMalGqRj2X5x/oTEjzDgubuCABm2Z5GtBDznhhmVP/SH X-Gm-Gg: Acq92OELrnhIM4AAsSnxItYKwPSpfbpJN12zBGMLxcDtxJbkRcH4EK/Mcbs6F4awJsg 6MtPAGaHbrS+XDZA9zhMZjMRq6N4O6ogSPJKDGVDcBREBQ5q7EXpe7wPBgqhLqsQ2sDuv2ktEIi tYadfNrb2dvF7i7M37GEmpCdHgYltJUPgYvlsofXbLsJghdk5fg0SIg2ZG3iUnruRqPSjoSg9un uaEJAe5zER+pi50pWlJxeqsgLS+cSpGFIBDlVEaC0YU16QbvV2hqDSAP5cj3QoiR6xtfFd1EoTo KIc4Sj2HBe8+icIuxURf7qndDSx1lpRjf+Mx2ewZfadKlIsiHf49CTtbtSHLyV4yl5XtwCy1ee8 Gw2VP+rtArHige8s5icuvtCQ5sx1UlpG3GEGrqgzIl7MnkqmBTE/Hk+6ngQL2ut+plrKc09gIWH FbijjFlY16vtewCFPOzXkR61hNEnTRR9w= X-Received: by 2002:a17:902:e747:b0:2b9:ef36:434b with SMTP id d9443c01a7336-2ba78c40a85mr165947545ad.12.1778307090566; Fri, 08 May 2026 23:11:30 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2baf1e3571dsm40879505ad.42.2026.05.08.23.11.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 May 2026 23:11:29 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_13E750B7-273F-41A6-B7D9-0A0C7079F7EA" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Avoid unnecessary StringInfo allocation in tablesync COPY buffer Message-Id: <5B2C9B4C-EAE6-4F21-AF99-613A561D26DC@gmail.com> Date: Sat, 9 May 2026 14:10:50 +0800 Cc: David Rowley , Fujii Masao To: PostgreSQL-development 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=_13E750B7-273F-41A6-B7D9-0A0C7079F7EA Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi, I found this issue while reviewing the patch [1] and was suggested use a = separate thread for the issue. In tablesync.c, 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 patch 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. [1] = https://postgr.es/m/CAOzEurQKuy3RiPkd=3D25PEwEzaqHuGvEOf=3DX7vaVzhgNjaukYz= A@mail.gmail.com Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_13E750B7-273F-41A6-B7D9-0A0C7079F7EA Content-Disposition: attachment; filename=v1-0001-Avoid-unnecessary-StringInfo-allocation-in-tables.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Avoid-unnecessary-StringInfo-allocation-in-tables.patch" Content-Transfer-Encoding: quoted-printable =46rom=20cffb27e65afaf3b7b0202f4e14518a424cf52ae3=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Sat,=209=20May=202026=2014:00:02=20+0800=0ASubject:=20[PATCH=20= v1]=20Avoid=20unnecessary=20StringInfo=20allocation=20in=20tablesync=20= COPY=0A=20buffer=0A=0Acopybuf=20is=20only=20used=20to=20track=20the=20= data=20pointer,=20length,=20and=20cursor=20for=0Aleftover=20COPY=20data.=20= The=20buffer=20allocated=20by=20makeStringInfo()=20is=20not=20used,=0A= because=20copybuf->data=20is=20later=20replaced=20by=20the=20buffer=20= returned=20from=0Awalrcv_receive().=0A=0AUse=20a=20static=20= StringInfoData=20instead=20and=20reset=20it=20before=20starting=20COPY.=0A= =0AAuthor:=20Chao=20Li=20=0A---=0A=20= src/backend/replication/logical/tablesync.c=20|=2022=20= ++++++++++-----------=0A=201=20file=20changed,=2011=20insertions(+),=20= 11=20deletions(-)=0A=0Adiff=20--git=20= a/src/backend/replication/logical/tablesync.c=20= b/src/backend/replication/logical/tablesync.c=0Aindex=20= eb718114297..1dee2480e2f=20100644=0A---=20= a/src/backend/replication/logical/tablesync.c=0A+++=20= b/src/backend/replication/logical/tablesync.c=0A@@=20-126,7=20+126,7=20= @@=0A=20=0A=20List=09=20=20=20*table_states_not_ready=20=3D=20NIL;=0A=20=0A= -static=20StringInfo=20copybuf=20=3D=20NULL;=0A+static=20StringInfoData=20= copybuf=20=3D=20{0};=0A=20=0A=20/*=0A=20=20*=20Wait=20until=20the=20= relation=20sync=20state=20is=20set=20in=20the=20catalog=20to=20the=20= expected=0A@@=20-649,13=20+649,13=20@@=20copy_read_data(void=20*outbuf,=20= int=20minread,=20int=20maxread)=0A=20=09int=09=09=09avail;=0A=20=0A=20=09= /*=20If=20there=20are=20some=20leftover=20data=20from=20previous=20read,=20= use=20it.=20*/=0A-=09avail=20=3D=20copybuf->len=20-=20copybuf->cursor;=0A= +=09avail=20=3D=20copybuf.len=20-=20copybuf.cursor;=0A=20=09if=20(avail)=0A= =20=09{=0A=20=09=09if=20(avail=20>=20maxread)=0A=20=09=09=09avail=20=3D=20= maxread;=0A-=09=09memcpy(outbuf,=20©buf->data[copybuf->cursor],=20= avail);=0A-=09=09copybuf->cursor=20+=3D=20avail;=0A+=09=09memcpy(outbuf,=20= ©buf.data[copybuf.cursor],=20avail);=0A+=09=09copybuf.cursor=20+=3D=20= avail;=0A=20=09=09maxread=20-=3D=20avail;=0A=20=09=09bytesread=20+=3D=20= avail;=0A=20=09}=0A@@=20-680,16=20+680,16=20@@=20copy_read_data(void=20= *outbuf,=20int=20minread,=20int=20maxread)=0A=20=09=09=09else=0A=20=09=09= =09{=0A=20=09=09=09=09/*=20Process=20the=20data=20*/=0A-=09=09=09=09= copybuf->data=20=3D=20buf;=0A-=09=09=09=09copybuf->len=20=3D=20len;=0A-=09= =09=09=09copybuf->cursor=20=3D=200;=0A+=09=09=09=09copybuf.data=20=3D=20= buf;=0A+=09=09=09=09copybuf.len=20=3D=20len;=0A+=09=09=09=09= copybuf.cursor=20=3D=200;=0A=20=0A-=09=09=09=09avail=20=3D=20= copybuf->len=20-=20copybuf->cursor;=0A+=09=09=09=09avail=20=3D=20= copybuf.len=20-=20copybuf.cursor;=0A=20=09=09=09=09if=20(avail=20>=20= maxread)=0A=20=09=09=09=09=09avail=20=3D=20maxread;=0A-=09=09=09=09= memcpy(outbuf,=20©buf->data[copybuf->cursor],=20avail);=0A+=09=09=09=09= memcpy(outbuf,=20©buf.data[copybuf.cursor],=20avail);=0A=20=09=09=09=09= outbuf=20=3D=20(char=20*)=20outbuf=20+=20avail;=0A-=09=09=09=09= copybuf->cursor=20+=3D=20avail;=0A+=09=09=09=09copybuf.cursor=20+=3D=20= avail;=0A=20=09=09=09=09maxread=20-=3D=20avail;=0A=20=09=09=09=09= bytesread=20+=3D=20avail;=0A=20=09=09=09}=0A@@=20-1199,7=20+1199,7=20@@=20= copy_table(Relation=20rel)=0A=20=09=09=09=09=09=09lrel.nspname,=20= lrel.relname,=20res->err)));=0A=20=09walrcv_clear_result(res);=0A=20=0A-=09= copybuf=20=3D=20makeStringInfo();=0A+=09memset(©buf,=200,=20= sizeof(copybuf));=0A=20=0A=20=09pstate=20=3D=20make_parsestate(NULL);=0A=20= =09(void)=20addRangeTableEntryForRelation(pstate,=20rel,=20= AccessShareLock,=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_13E750B7-273F-41A6-B7D9-0A0C7079F7EA--