Re: Speed up binary COPY TO by not allocating 1 kB per datum

From: Shinya Kato <shinya11(dot)kato(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Speed up binary COPY TO by not allocating 1 kB per datum
Date: 2026-12-04 02:40:29
Message-ID: CAOzEurRwhCHRO5z4UnRxbgArzjpeS3dtdoB9EEZ+dX9e+T7=cQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Tue, Dec 1, 2026 at 3:31 AM Andres Freund <andres(at)anarazel(dot)de> wrote:

> To make it viable I think you'd need to have fcinfo->context point to
> something that indicates it's the output buffer (via a node tag) and have a
> helper function for output functions that will give you the output buffer
> referenced by fcinfo->context if available, and otherwise allocates a new
> stringinfo.

Thanks. v2 does that for send functions, and drops the size hints.

0001 is the length word zeroing that Horiguchi-san agreed with. 0002
is a trivial fix I noticed while writing the patches. range_send() and
multirange_send() called initStringInfo() before pq_begintypsend(),
which initializes the buffer again.

0003 adds SendFunctionCallInto(). It passes the caller's buffer to the
send function through a new SendBufferContext node in fcinfo->context,
like ErrorSaveContext. A send function written as

```
StringInfo buf;

buf = pq_begintypsend_fcinfo(fcinfo);
pq_sendint32(buf, arg1);
PG_RETURN_BYTEA_P(pq_endtypsend_fcinfo(fcinfo, buf));
```

appends its value and fills in the length word in place when the node
is present. Otherwise it allocates a StringInfo and builds a bytea as
before, which happens only when it is called from SQL or by a caller
still using SendFunctionCall(). A send function that still uses
pq_begintypsend() keeps working, and its result is copied. 0003 also
converts the callers, and 0004 the built-in send functions.

COPY TO '/dev/null' (FORMAT binary) and SELECT * with binary results,
median of 10 runs, on the same machine as before:

```
| COPY TO (ms) | printtup (ms)
-------------------------+----------------------+----------------------
wide, 1M rows | 314 -> 147 (-53%) | 313 -> 201 (-36%)
100 int cols, 200k rows | 488 -> 216 (-56%) | 508 -> 401 (-21%)
int[1000] col, 20k rows | 468 -> 228 (-51%) | 514 -> 264 (-49%)
5x 100B text, 300k rows | 80 -> 63 (-22%) | 110 -> 108 ( -2%)
10 kB text, 20k rows | 117 -> 115 ( -2%) | 167 -> 160 ( -4%)
```

For comparison, v1 gave -24% and -22% on the first two COPY rows.

I have not touched the text output functions yet. Their output is in
the server encoding and the caller converts it to the client encoding
afterwards, so it can be written directly into the caller's buffer
only when no conversion is needed. I think the caller could pass the
buffer only in that case and fall back to the current path otherwise.
Does this shape look right before I do that?

--
Shinya Kato
NTT OSS Center

Attachment Content-Type Size
v2-0001-Zero-the-length-word-in-pq_begintypsend-with-a-si.patch application/octet-stream 1.7 KB
v2-0002-Remove-useless-initStringInfo-calls-in-range-and-.patch application/octet-stream 1.5 KB
v2-0003-Add-SendFunctionCallInto-to-append-a-send-functio.patch application/octet-stream 16.6 KB
v2-0004-Convert-built-in-send-functions-to-write-into-the.patch application/octet-stream 38.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-12-04 02:52:11 Re: Standby writes pages past minRecoveryPoint after crash recovery
Previous Message Baji Shaik 2026-12-04 01:56:31 Re: pg_dump writes a subscription's default slot name, which can't be restored