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 1x3jgO-006dd8-1B for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 00:27:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x3jgM-003K7W-2z for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 00:27:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x3jgM-003K7N-0Q for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2026 00:27:26 +0000 Received: from smtpout9.mo540.mail-out.ovh.net ([51.210.91.58]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x3jgH-00000004VXS-3eEP for pgsql-hackers@postgresql.org; Tue, 08 Sep 2026 00:27:24 +0000 Received: from director5.derp.mail-out.ovh.net (director5.derp.mail-out.ovh.net [79.137.60.225]) by mo540.mail-out.ovh.net (Postfix) with ESMTPS id 4hf4Wn0jmrz45Dr; Tue, 8 Sep 2026 00:27:16 +0000 (UTC) Received: from director5.derp.mail-out.ovh.net (director5.derp.mail-out.ovh.net. [127.0.0.1]) by director5.derp.mail-out.ovh.net (inspect_sender_mail_agent) with SMTP for ; Tue, 8 Sep 2026 00:27:16 +0000 (UTC) Received: from mta6.priv.ovhmail-u1.ea.mail.ovh.net (unknown [10.110.168.39]) by director5.derp.mail-out.ovh.net (Postfix) with ESMTPS id 4hf4Wm50BVz6MbV; Tue, 8 Sep 2026 00:27:16 +0000 (UTC) Received: from data-bene.io (unknown [10.1.6.5]) (Authenticated sender: ayoub.kazar@data-bene.io) by mta6.priv.ovhmail-u1.ea.mail.ovh.net (Postfix) with ESMTPSA id 077D88E1BC5; Tue, 8 Sep 2026 00:27:14 +0000 (UTC) Authentication-Results: garm.ovh; auth=pass (GARM-102R0044e41fe01-78a1-4340-ac07-450cdc92996c, 094218D66575358A4F6CE4DBBB1FC6AE564425ED) smtp.auth=ayoub.kazar@data-bene.io X-OVh-ClientIp: 105.105.146.106 Content-Type: multipart/mixed; boundary="------------lPYrz8EahpP2a9N0Ard9jWgt" Message-ID: <2ca51c76-90b2-4a3f-bed7-bb4e08d22782@data-bene.io> Date: Tue, 8 Sep 2026 02:27:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Speed up COPY TO text/CSV parsing using SIMD To: Nathan Bossart Cc: KAZAR Ayoub , Andres Freund , Pg Hackers , Neil Conway , Manni Wood , Andrew Dunstan , Shinya Kato , Mark Wong , Nazir Bilal Yavuz References: <777bc2f5-957e-4c8b-9f63-1d54e1f1bc3d@data-bene.io> Content-Language: en-US From: Ayoub Kazar In-Reply-To: x-ovh-tracer-id: 6225100585792143662 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: dmFkZTGkgJFHE6dGCXaEOscv3zAgSnDHw6FLCDYxPno/VoirYsps8VBb+xEoFI+z6JUPy6qGT+dOO42UGWqHzM/LOy6FxQFGj90SMwkmccYUlHRiVIikXbAEahLJGkNVPXHNuBX8lq7aEf183Bc7/yxVBVUom+KIIo/boi5U/9do7+LrQmejHKJ3gjr9P6FCPS9eeUZGEwwy/HjIF3agAx5D2v+YrwBl7vmCVm0tELmIjrL4U/JFPj6ZiMjD9RAiihR4KHOucZZZV/heTcW8g2FbVZHm+pENYJCM9To1u9Na+w5l6L26DnHV3+uJcg8aZeET2oeIZJg9cDvB4Va9vkRXXmVGn8vgKugZq+9UvyB3HEMXitwdU0S9raJbooVvHuP2fCxTE8/MBd7TSWSlE8cfD+jLlNC6TkHjz8PcJP+22/8KQMFHHTgsnYapXbuHDGkcJAaZKDY7CJLzaF1hTR7BMx9XiC9DWcNlQBA8uNwbtnm5nz1C16/JqFlxGSDRnAH0RhANkTz03pjTR1DYIdapaTgHoSZLr01ZbLmQG7URbMFRk4wUxLTFuivF8AltndjujcDw7Pw2lQDiTsKjBVcEKwaw3CHUkZqEW6FuUKu44KAdeEUjiJZ1CwZ1ZltvEIR2yQoYO9ONuKm/WvJosTa4FBMtUlTikX8VdSaMLW1YlkHABw DKIM-Signature: a=rsa-sha256; bh=vgIiSH8JwBaGDTFOK/Fp3Lt1Oq1iiAAVWyvr6jhGFYY=; c=relaxed/relaxed; d=data-bene.io; h=From; s=ovhmo-selector-1; t=1788827237; v=1; b=QbXMDvR447C/DXXjvbXfhlc5NrCi2YCgninfhj8JiRdUqF0abHCqmaU75WHS4g4jEUS5cjc5 gUYKPqGTsLiioyWR/LGBkOAg03LxyoLPOpxkNJZM7zLVgI2uezw8RZbRFAtthaUnfKrSh0bMho2 RORLyW2IwkwFiIYch0cdtta6wkRTySacRQ+l3is6c/m0zdJtUseI1P1QItwty+C2kgDUlmNpg4k 7qFWWct6xgLj/vqnGnG6Afl0OnluavcScc2i0Jc27N2HWNiWQDHeOidCEp+FW8yqKkaGxywkBra jayhNpx1nFAkB+AnxuK87vcy9mAkct7NmHxKSZMwAACxw== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------lPYrz8EahpP2a9N0Ard9jWgt Content-Type: multipart/alternative; boundary="------------pqaM4KgjlaFp02dVQsqLpmd8" --------------pqaM4KgjlaFp02dVQsqLpmd8 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 --------------pqaM4KgjlaFp02dVQsqLpmd8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

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 ? 


Regards,
Ayoub
--------------pqaM4KgjlaFp02dVQsqLpmd8-- --------------lPYrz8EahpP2a9N0Ard9jWgt Content-Type: text/x-patch; charset=UTF-8; name="0001-COPY-TO-avoid-prefix-re-scan-and-strlen-in-CopyAttributeOutCSV.patch" Content-Disposition: attachment; filename*0="0001-COPY-TO-avoid-prefix-re-scan-and-strlen-in-CopyAttribut"; filename*1="eOutCSV.patch" Content-Transfer-Encoding: base64 RnJvbSA4NGQ3MjcwZTgyMTE3NTA0MTgzOTAyMWM4MDZkOWI2NmZkNTBmM2I4IE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBBeW91YktBWiA8a2F6YXJheW91YjIwMDRAZ21haWwu Y29tPgpEYXRlOiBNb24sIDcgU2VwIDIwMjYgMDE6NDU6NDMgKzAyMDAKU3ViamVjdDogW1BB VENIXSBDT1BZIFRPOiBhdm9pZCBwcmVmaXggcmUtc2NhbiBhbmQgc3RybGVuIGluCiBDb3B5 QXR0cmlidXRlT3V0Q1NWCgpDb3B5QXR0cmlidXRlT3V0Q1NWIG1ha2VzIGEgcHJlbGltaW5h cnkgc2NhbiBvdmVyIGVhY2ggYXR0cmlidXRlIHRvCmRldGVybWluZSBpZiBxdW90aW5nIGlz IG5lZWRlZC4KCk9uIHRoZSB1bnF1b3RlZCBwYXRoLCB0aGUgcHJlbGltaW5hcnkgc2NhbiBo YXMgYWxyZWFkeSB3YWxrZWQgdGhlIGVudGlyZQpzdHJpbmcgdW50aWwgJ1wwJy4gIEJ5IGhv aXN0aW5nIHRwdHIgdG8gdGhlIG91dGVyIHNjb3BlLCB3ZSBrbm93IHRoZQpleGFjdCBzdHJp bmcgbGVuZ3RoICh0cHRyIC0gcHRyKSBhbmQgY2FuIHBhc3MgaXQgZGlyZWN0bHkgdG8KQ29w eVNlbmREYXRhLCBhdm9pZGluZyBhIHJlZHVuZGFudCBzdHJsZW4oKSBwYXNzLgoKT24gdGhl IHF1b3RlZCBwYXRoLCB0aGUgc2NhbiBzdG9wcGVkIGF0IHRoZSBmaXJzdCBxdW90aW5nIGNo YXJ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