Re: Speed up COPY TO text/CSV parsing using SIMD

From: Ayoub Kazar <ayoub(dot)kazar(at)data-bene(dot)io>
To: Nathan Bossart <nathandbossart(at)gmail(dot)com>
Cc: KAZAR Ayoub <ma_kazar(at)esi(dot)dz>, Andres Freund <andres(at)anarazel(dot)de>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, Neil Conway <neil(dot)conway(at)gmail(dot)com>, Manni Wood <manni(dot)wood(at)enterprisedb(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Shinya Kato <shinya11(dot)kato(at)gmail(dot)com>, Mark Wong <markwkm(at)gmail(dot)com>, Nazir Bilal Yavuz <byavuz81(at)gmail(dot)com>
Subject: Re: Speed up COPY TO text/CSV parsing using SIMD
Date: 2026-09-08 00:27:14
Message-ID: 2ca51c76-90b2-4a3f-bed7-bb4e08d22782@data-bene.io
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello,

On 03/09/2026 20:21, Nathan Bossart wrote:
> On Thu, Sep 03, 2026 at 06:01:15PM +0200, Ayoub Kazar wrote:
>> After looking into more test cases, the opportunity here isn't as big as it
>> was for COPY FROM in general cases, yet i have seen good performance gains
>> show up in most cases, and avoiding regressions elsewhere adds some
>> complexity, there's also the possibility to include the case of TOASTed
>> attributes here ; which i think is a win.
I've tested this even more, regressions are limited to 3-4%, so the
previous results are still stable for me, which i find not very bad
again. I might need to justify this further with heavier COPY TO
commands in larger variety of cases maybe, we'll see whether the current
state holds strong or not.
> Yeah, when I looked into this one a while ago, I couldn't find a good way
> to avoid regressions. I wonder if we'll need to make some architectural
> adjustments before trying again.
>

Coming back to that ...

I looked at some simple tests to see where most of the time is spent in
the COPY TO command, in the referenced flamegraph [1], i noticed some
parts that are interesting to optimize, noticeably:

#1 Output functions: in datum_to_json_internal, i saw that we save some
cycles by avoiding palloc, memcpy from using OutputFunctions, but only
for interesting cases of TEXT and similar. I think we can do this too
for COPY TO for the same reasons, we save some allocs and copies.

#2 (Attached patch) CopyAttributeOutCSV scanned each attribute value
twice: once to decide whether quoting was needed, then again in the
escape loop from the start of the string. On the unquoted path it also
called CopySendString, which internally called strlen on a string we had
just walked. The fix hoists tptr so the unquoted path uses tptr - ptr as
the length directly, and the quoted path bulk-emits the already-scanned
clean prefix in one CopySendData call when escapec == quotec (standard
case) before entering the escape loop at tptr. A previous patch i had
also tracked whether we see an escapec in the first loop or not to emit
prefix in both standard CSV case and custom escapec, but this had 11%
regression in clean text strings benchmark.

Benchmarks show: master 798bdc
1: Clean Prefix + Quote trigger: 20-25% improvement
2: Long clean text: 3% improvement
3: Mixed column types: 12-16% improvement
4: First byte triggers (checking for regressions): 1-2% improvement
5: Tiny strings: 4-5% improvement

AFAICT, there shouldn't be any regression in any case i can think of,
one thing to note is:
-        CopySendString(cstate, ptr);
+        CopySendData(cstate, ptr, tptr - ptr);

Doesn't show big improvements alone because in my case strlen() is
vectorized (avx2) so its pretty fast, yet i don't think it should be
faster (atleast for large enough clean strings).

What do you think ?

[1]: https://gist.github.com/AyoubKaz07/43feb5ca88e7e118f1f0e4996a624bb7

Regards,
Ayoub

Attachment Content-Type Size
0001-COPY-TO-avoid-prefix-re-scan-and-strlen-in-CopyAttributeOutCSV.patch text/x-patch 3.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message shihao zhong 2026-09-08 00:33:05 Add a permission check to pg_stat_get_backend_subxact()
Previous Message Henson Choi 2026-09-08 00:11:29 Re: Row pattern recognition