| From: | Shinya Kato <shinya11(dot)kato(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Cc: | Andres Freund <andres(at)anarazel(dot)de> |
| Subject: | Speed up binary COPY TO by not allocating 1 kB per datum |
| Date: | 2026-11-24 02:35:32 |
| Message-ID: | CAOzEurReWaX8i72CBbBzYZzNSP=XRQdp1=cB+rYhir1CB9DUUg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi hackers,
The design in the attached patches is Andres's, proposed in [1] and
last posted as a ten patch series in [2]. I picked the two patches
that size the send buffer, his v3-0004 and v3-0006, and applied the
review comments they received [3].
Most send functions start with pq_begintypsend(), which allocates the
1 kB default StringInfo buffer. int4send() writes 4 bytes into it.
printtup and COPY TO call send functions in a per-row memory context
that is reset after each row, and its keeper block is 8 kB, so with a
1 kB chunk per attribute the eighth binary column of a row starts a
new block. Every row then costs a malloc() and a free() as well as the
oversized chunks.
```
=# CREATE TABLE wide AS
SELECT i AS id, i::bigint AS b1, (i*2)::bigint AS b2, i::float8 AS f1,
(i*1.5)::float8 AS f2, i AS i1, i AS i2, i AS i3, i AS i4, i AS i5
FROM generate_series(1, 1000000) i;
=# COPY (SELECT * FROM wide) TO '/dev/null' (FORMAT binary);
```
Profiling that COPY before the patches:
```
11.25% [.] appendBinaryStringInfo
11.17% [.] enlargeStringInfo
9.49% [.] CopyToBinaryOneRow
9.23% [.] pq_begintypsend
7.76% [.] AllocSetAlloc
6.49% [.] __memmove_evex_unaligned_erms
5.71% [.] tts_buffer_heap_getsomeattrs
4.20% [.] SendFunctionCall
3.61% [.] int4send
2.87% [.] AllocSetAllocFromNewBlock
```
The 0001 patch adds pq_begintypsend_with_size(), which takes the
number of bytes the caller expects to append. With it, int4send() asks
for 9 bytes instead of 1024. The size is a hint rather than a limit,
so a string that encoding conversion in pq_sendtext() expands past it
just grows the buffer as before. pq_begintypsend() keeps allocating 1
kB, so unconverted callers are unaffected. The length word is now
zeroed with a single store, where before it took four
appendStringInfoCharMacro() calls that each read and wrote the
StringInfo length.
The 0002 patch uses the new function in 43 send functions.
Each workload is a binary COPY of the named table to /dev/null, median
of 10 runs, -O2 without assertions on an otherwise idle 8-core
i5-1135G7.
```
workload | time (ms) | instructions (G)
-------------------------+---------------------+-----------------------
wide, 1M rows | 300 -> 231 (-23.1%) | 4.68 -> 3.85 (-17.8%)
100 int cols, 200k rows | 472 -> 373 (-21.0%) | 7.59 -> 6.40 (-15.7%)
int[1000] col, 20k rows | 470 -> 413 (-12.1%) | 6.83 -> 6.17 ( -9.7%)
5x 100B text, 300k rows | 92 -> 91 ( -1.6%) | 0.94 -> 0.90 ( -4.2%)
```
AllocSetAllocFromNewBlock is gone from the profile after the patches,
together with _int_malloc and _int_free further down.
Leaving the length word uninitialized saves another 0.4% of
instructions on the first workload:
```
variant | instructions (G)
-------------------------+------------------
master | 4.68
zeroed, as attached | 3.85 (-17.8%)
length word not written | 3.83 (-18.2%)
```
I kept the zeroing. A send function that returns its buffer without
calling pq_endtypsend() otherwise gets a length word made of whatever
the allocator left there. Zeroed, the caller rejects that buffer with
an error. Uninitialized, I saw it crash the backend or send heap
memory to the client.
Thoughts?
[1] https://postgr.es/m/CAMkU%3D1whbRDUwa4eayD9%2B59K-coxO9senDkPRbTn3cg0pUz4AQ@mail.gmail.com
[2] https://postgr.es/m/20240218015955.rmw5mcmobt5hbene@awork3.anarazel.de
[3] https://postgr.es/m/CA+TgmobjqMZaSb7-Zb54fMEOoav8z8x7JPeetH6jJGgZLQhdiw@mail.gmail.com
--
Shinya Kato
NTT OSS Center
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Add-pq_begintypsend_with_size-for-send-functions-.patch | application/octet-stream | 4.4 KB |
| v1-0002-Use-pq_begintypsend_with_size-in-send-functions-w.patch | application/octet-stream | 19.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Chao Li | 2026-11-24 02:37:58 | Re: Make pg_strtok() a bit faster |
| Previous Message | shihao zhong | 2026-11-24 02:13:59 | Re: Fix two race conditions in processing interrupts |