Re: Avoid unnecessary StringInfo allocation in tablesync COPY buffer

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Álvaro Herrera <alvherre(at)kurilemu(dot)de>
Cc: Aleksander Alekseev <aleksander(at)tigerdata(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, ZizhuanLiu X-MAN <44973863(at)qq(dot)com>, Denis Smirnov <darthunix(at)gmail(dot)com>, David Rowley <dgrowleyml(at)gmail(dot)com>, Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Subject: Re: Avoid unnecessary StringInfo allocation in tablesync COPY buffer
Date: 2026-11-25 00:47:25
Message-ID: D3AFE25A-B970-4333-AF17-9EB4FA664262@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Nov 25, 2026, at 04:39, Álvaro Herrera <alvherre(at)kurilemu(dot)de> wrote:
>
> On 2026-Nov-23, Álvaro Herrera wrote:
>
>> I agree that there are more important things to fix. I thought using a
>> read-only StringInfo and getting rid of the global wasn't a completely
>> bad idea. I was reading this patch thinking it could be a quick push,
>> but I realized that it's not trivial to see from eyeballing alone that
>> we don't call walrcv_receive() one more time before consuming the whole
>> buffer (which libpqrcv_receive warns against). Admittedly, the same can
>> be said of the original code, so this is not a new concern. Maybe a few
>> extra comments would make this business clearer.
>
> I pushed this after all, thanks.
>
> --
> Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
> "Los minutos y los segundos son mercadería de la ciudad, donde un infeliz se
> afana por no perder ni siquiera un segundo y no advierte que obrando de ese
> modo pierde una vida." ("La vuelta de Don Camilo", G. Guareschi)

Thanks a lot for pushing this patch, and sorry for missing the previous discussion.

I wasn’t aware of read-only StringInfo, but after reading f0efa5aec193, I agree it’s a perfect fit for copybuf.

While reading the pushed commit, I just found a pre-existing bug in copy_read_data():
```
/* If there are some leftover data from previous read, use it. */
avail = copybuf.len - copybuf.cursor;
if (avail)
{
if (avail > maxread)
avail = maxread;
memcpy(outbuf, &copybuf.data[copybuf.cursor], avail);
copybuf.cursor += avail;
maxread -= avail;
bytesread += avail;
}
```

After copying the leftover bytes, copy_read_data() doesn’t advance outbuf. If those bytes are less then minread, the next memcpy() would overwrite them. The current caller only passes 1 for minread, so this bug cannot be triggered today. I’ll propose a patch in a new thread.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Lukas Fittl 2026-11-25 01:13:35 Re: Parallel GIN, BRIN builds fail to maintain ii_BrokenHotChain
Previous Message Manu 2026-11-25 00:35:10 Re: Allow progress tracking of sub-commands