Speed up COPY FROM text/CSV parsing using SIMD
Speed up COPY FROM text/CSV parsing using SIMD
Hi hackers,
I have implemented SIMD optimization for the COPY FROM (FORMAT {csv,
text}) command and observed approximately a 5% performance
improvement. Please see the detailed test results below.
Idea
====
The current text/CSV parser processes input byte-by-byte, checking
whether each byte is a special character (\n, \r, quote, escape) or a
regular character, and transitions states in a state machine. This
sequential processing is inefficient and likely causes frequent branch
mispredictions due to the many if statements.
I thought this problem could be addressed by leveraging SIMD and
vectorized operations for faster processing.
Implementation Overview
=======================
1. Create a vector of special characters (e.g., Vector8 nl =
vector8_broadcast('\n');).
2. Load the input buffer into a Vector8 variable called chunk.
3. Perform vectorized operations between chunk and the special
character vectors to check if the buffer contains any special
characters.
4-1. If no special characters are found, advance the input_buf_ptr by
sizeof(Vector8).
4-2. If special characters are found, advance the input_buf_ptr as far
as possible, then fall back to the original text/CSV parser for
byte-by-byte processing.
Test
====
I tested the performance by measuring the time it takes to load a CSV
file created using the attached SQL script with the following COPY
command:
=# COPY t FROM '/tmp/t.csv' (FORMAT csv);
Environment
-----------
OS: Rocky Linux 9.6
CPU: Intel Core i7-10710U (6 Cores / 12 Threads, 1.1 GHz Base / 4.7
GHz Boost, AVX2 & FMA supported)
Time
----
master: 02.44.943
patch applied: 02:36.878 (about 5% faster)
Perf
----
Each call graphs are attached and the rates of CopyReadLineText are:
master: 12.15%
patch applied: 8.04%
Thought?
I would appreciate feedback on the implementation and any suggestions
for further improvement.
--
Best regards,
Shinya Kato
NTT OSS Center
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
Thank you for working on this!
On Thu, 7 Aug 2025 at 04:49, Shinya Kato wrote:
>
> Hi hackers,
>
> I have implemented SIMD optimization for the COPY FROM (FORMAT {csv,
> text}) command and observed approximately a 5% performance
> improvement. Please see the detailed test results below.
I have been working on the same idea. I was not moving input_buf_ptr
as far as possible, so I think your approach is better.
Also, I did a benchmark on text format. I created a benchmark for line
length in a table being from 1 byte to 1 megabyte.The peak improvement
is line length being 4096 and the improvement is more than 20% [1], I
saw no regression on your patch.
> Idea
> ====
> The current text/CSV parser processes input byte-by-byte, checking
> whether each byte is a special character (\n, \r, quote, escape) or a
> regular character, and transitions states in a state machine. This
> sequential processing is inefficient and likely causes frequent branch
> mispredictions due to the many if statements.
>
> I thought this problem could be addressed by leveraging SIMD and
> vectorized operations for faster processing.
>
> Implementation Overview
> =======================
> 1. Create a vector of special characters (e.g., Vector8 nl =
> vector8_broadcast('\n');).
> 2. Load the input buffer into a Vector8 variable called chunk.
> 3. Perform vectorized operations between chunk and the special
> character vectors to check if the buffer contains any special
> characters.
> 4-1. If no special characters are found, advance the input_buf_ptr by
> sizeof(Vector8).
> 4-2. If special characters are found, advance the input_buf_ptr as far
> as possible, then fall back to the original text/CSV parser for
> byte-by-byte processing.
>
...
> Thought?
> I would appreciate feedback on the implementation and any suggestions
> for further improvement.
I have a couple of ideas that I was working on:
---
+ * However, SIMD optimization cannot be applied in the following cases:
+ * - Inside quoted fields, where escape sequences and closing quotes
+ * require sequential processing to handle correctly.
I think you can continue SIMD inside quoted fields. Only important
thing is you need to set last_was_esc to false when SIMD skipped the
chunk.
---
+ * - When the remaining buffer size is smaller than the size of a SIMD
+ * vector register, as SIMD operations require processing data in
+ * fixed-size chunks.
You run SIMD when 'copy_buf_len - input_buf_ptr >= sizeof(Vector8)'
but you only call CopyLoadInputBuf() when 'input_buf_ptr >=
copy_buf_len || need_data' so basically you need to wait at least the
sizeof(Vector8) character to pass for the next SIMD. And in the worst
case; if CopyLoadInputBuf() puts one character less than
sizeof(Vector8), then you can't ever run SIMD. I think we need to make
sure that CopyLoadInputBuf() loads at least the sizeof(Vector8)
character to the input_buf so we do not encounter that problem.
---
What do you think about adding SIMD to CopyReadAttributesText() and
CopyReadAttributesCSV() functions? When I add your SIMD approach to
CopyReadAttributesText() function, the improvement on the 4096 byte
line length input [1] goes from 20% to 30%.
---
I shared my ideas as a Feedback.txt file (.txt to stay off CFBot's
radar for this thread). I hope these help, please let me know if you
have any questions.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Thu, 7 Aug 2025 at 14:15, Nazir Bilal Yavuz wrote:
>
> On Thu, 7 Aug 2025 at 04:49, Shinya Kato wrote:
> >
> > I have implemented SIMD optimization for the COPY FROM (FORMAT {csv,
> > text}) command and observed approximately a 5% performance
> > improvement. Please see the detailed test results below.
>
> Also, I did a benchmark on text format. I created a benchmark for line
> length in a table being from 1 byte to 1 megabyte.The peak improvement
> is line length being 4096 and the improvement is more than 20% [1], I
> saw no regression on your patch.
I did the same benchmark for the CSV format. The peak improvement is
line length being 4096 and the improvement is more than 25% [1]. I saw
a 5% regression on the 1 byte benchmark, there are no other
regressions.
> What do you think about adding SIMD to CopyReadAttributesText() and
> CopyReadAttributesCSV() functions? When I add your SIMD approach to
> CopyReadAttributesText() function, the improvement on the 4096 byte
> line length input [1] goes from 20% to 30%.
I wanted to try using SIMD in CopyReadAttributesCSV() as well. The
improvement on the 4096 byte line length input [1] goes from 25% to
35%, the regression on the 1 byte input is the same.
CopyReadAttributesCSV() changes are attached as feedback v2.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, Aug 7, 2025 at 8:15 PM Nazir Bilal Yavuz wrote:
>
> Hi,
>
> Thank you for working on this!
>
> On Thu, 7 Aug 2025 at 04:49, Shinya Kato wrote:
> >
> > Hi hackers,
> >
> > I have implemented SIMD optimization for the COPY FROM (FORMAT {csv,
> > text}) command and observed approximately a 5% performance
> > improvement. Please see the detailed test results below.
>
> I have been working on the same idea. I was not moving input_buf_ptr
> as far as possible, so I think your approach is better.
Great. I'm looking forward to working with you on this feature implementation.
> Also, I did a benchmark on text format. I created a benchmark for line
> length in a table being from 1 byte to 1 megabyte.The peak improvement
> is line length being 4096 and the improvement is more than 20% [1], I
> saw no regression on your patch.
Thank you for the additional benchmarks.
> I have a couple of ideas that I was working on:
> ---
>
> + * However, SIMD optimization cannot be applied in the following cases:
> + * - Inside quoted fields, where escape sequences and closing quotes
> + * require sequential processing to handle correctly.
>
> I think you can continue SIMD inside quoted fields. Only important
> thing is you need to set last_was_esc to false when SIMD skipped the
> chunk.
That's a clever point that last_was_esc should be reset to false when
a SIMD chunk is skipped. You're right about that specific case.
However, the core challenge is not what happens when we skip a chunk,
but what happens when a chunk contains special characters like quotes
or escapes. The main reason we avoid SIMD inside quoted fields is that
the parsing logic becomes fundamentally sequential and
context-dependent.
To correctly parse a "" as a single literal quote, we must perform a
lookahead to check the next character. This is an inherently
sequential operation that doesn't map well to SIMD's parallel nature.
Trying to handle this stateful logic with SIMD would lead to
significant implementation complexity, especially with edge cases like
an escape character falling on the last byte of a chunk.
> + * - When the remaining buffer size is smaller than the size of a SIMD
> + * vector register, as SIMD operations require processing data in
> + * fixed-size chunks.
>
> You run SIMD when 'copy_buf_len - input_buf_ptr >= sizeof(Vector8)'
> but you only call CopyLoadInputBuf() when 'input_buf_ptr >=
> copy_buf_len || need_data' so basically you need to wait at least the
> sizeof(Vector8) character to pass for the next SIMD. And in the worst
> case; if CopyLoadInputBuf() puts one character less than
> sizeof(Vector8), then you can't ever run SIMD. I think we need to make
> sure that CopyLoadInputBuf() loads at least the sizeof(Vector8)
> character to the input_buf so we do not encounter that problem.
I think you're probably right, but we only need to account for
sizeof(Vector8) when USE_NO_SIMD is not defined.
> What do you think about adding SIMD to CopyReadAttributesText() and
> CopyReadAttributesCSV() functions? When I add your SIMD approach to
> CopyReadAttributesText() function, the improvement on the 4096 byte
> line length input [1] goes from 20% to 30%.
Agreed, I will.
> I shared my ideas as a Feedback.txt file (.txt to stay off CFBot's
> radar for this thread). I hope these help, please let me know if you
> have any questions.
Thanks a lot!
On Mon, Aug 11, 2025 at 5:52 PM Nazir Bilal Yavuz wrote:
>
> Hi,
>
> On Thu, 7 Aug 2025 at 14:15, Nazir Bilal Yavuz wrote:
> >
> > On Thu, 7 Aug 2025 at 04:49, Shinya Kato wrote:
> > >
> > > I have implemented SIMD optimization for the COPY FROM (FORMAT {csv,
> > > text}) command and observed approximately a 5% performance
> > > improvement. Please see the detailed test results below.
> >
> > Also, I did a benchmark on text format. I created a benchmark for line
> > length in a table being from 1 byte to 1 megabyte.The peak improvement
> > is line length being 4096 and the improvement is more than 20% [1], I
> > saw no regression on your patch.
>
> I did the same benchmark for the CSV format. The peak improvement is
> line length being 4096 and the improvement is more than 25% [1]. I saw
> a 5% regression on the 1 byte benchmark, there are no other
> regressions.
Thank you. I'm not too concerned about a regression when there's only
one byte per line.
> > What do you think about adding SIMD to CopyReadAttributesText() and
> > CopyReadAttributesCSV() functions? When I add your SIMD approach to
> > CopyReadAttributesText() function, the improvement on the 4096 byte
> > line length input [1] goes from 20% to 30%.
>
> I wanted to try using SIMD in CopyReadAttributesCSV() as well. The
> improvement on the 4096 byte line length input [1] goes from 25% to
> 35%, the regression on the 1 byte input is the same.
Yes, I'm on it. I'm currently adding the SIMD logic to
CopyReadAttributesCSV() as you suggested. I'll share the new version
of the patch soon.
--
Best regards,
Shinya Kato
NTT OSS Center
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Aug 12, 2025 at 4:25 PM Shinya Kato wrote: > > + * However, SIMD optimization cannot be applied in the following cases: > > + * - Inside quoted fields, where escape sequences and closing quotes > > + * require sequential processing to handle correctly. > > > > I think you can continue SIMD inside quoted fields. Only important > > thing is you need to set last_was_esc to false when SIMD skipped the > > chunk. > > That's a clever point that last_was_esc should be reset to false when > a SIMD chunk is skipped. You're right about that specific case. > > However, the core challenge is not what happens when we skip a chunk, > but what happens when a chunk contains special characters like quotes > or escapes. The main reason we avoid SIMD inside quoted fields is that > the parsing logic becomes fundamentally sequential and > context-dependent. > > To correctly parse a "" as a single literal quote, we must perform a > lookahead to check the next character. This is an inherently > sequential operation that doesn't map well to SIMD's parallel nature. > > Trying to handle this stateful logic with SIMD would lead to > significant implementation complexity, especially with edge cases like > an escape character falling on the last byte of a chunk. Ah, you're right. My apologies, I misunderstood the implementation. It appears that SIMD can be used even within quoted strings. I think it would be better not to use the SIMD path when last_was_esc is true. The next character is likely to be a special character, and handling this case outside the SIMD loop would also improve readability by consolidating the last_was_esc toggle logic in one place. Furthermore, when inside a quote (in_quote) in CSV mode, the detection of \n and \r can be disabled. + last_was_esc = false; Regarding the implementation, I believe we must set last_was_esc to false when advancing input_buf_ptr, as shown in the code below. For this reason, I think it’s best to keep the current logic for toggling last_was_esc. + int advance = pg_rightmost_one_pos32(mask); + input_buf_ptr += advance; I've attached a new patch that includes these changes. Further modifications are still in progress. -- Best regards, Shinya Kato NTT OSS Center
Re: Speed up COPY FROM text/CSV parsing using SIMD
Results for a 1/3 of line length of special characters:
~43.9% slower copy in TEXT format
~16.7% slower copy in CSV format
On Tue, Aug 12, 2025 at 4:25 PM Shinya Kato <shinya11.kato@gmail.com> wrote:
> > + * However, SIMD optimization cannot be applied in the following cases:
> > + * - Inside quoted fields, where escape sequences and closing quotes
> > + * require sequential processing to handle correctly.
> >
> > I think you can continue SIMD inside quoted fields. Only important
> > thing is you need to set last_was_esc to false when SIMD skipped the
> > chunk.
>
> That's a clever point that last_was_esc should be reset to false when
> a SIMD chunk is skipped. You're right about that specific case.
>
> However, the core challenge is not what happens when we skip a chunk,
> but what happens when a chunk contains special characters like quotes
> or escapes. The main reason we avoid SIMD inside quoted fields is that
> the parsing logic becomes fundamentally sequential and
> context-dependent.
>
> To correctly parse a "" as a single literal quote, we must perform a
> lookahead to check the next character. This is an inherently
> sequential operation that doesn't map well to SIMD's parallel nature.
>
> Trying to handle this stateful logic with SIMD would lead to
> significant implementation complexity, especially with edge cases like
> an escape character falling on the last byte of a chunk.
Ah, you're right. My apologies, I misunderstood the implementation. It
appears that SIMD can be used even within quoted strings.
I think it would be better not to use the SIMD path when last_was_esc
is true. The next character is likely to be a special character, and
handling this case outside the SIMD loop would also improve
readability by consolidating the last_was_esc toggle logic in one
place.
Furthermore, when inside a quote (in_quote) in CSV mode, the detection
of \n and \r can be disabled.
+ last_was_esc = false;
Regarding the implementation, I believe we must set last_was_esc to
false when advancing input_buf_ptr, as shown in the code below. For
this reason, I think it’s best to keep the current logic for toggling
last_was_esc.
+ int advance = pg_rightmost_one_pos32(mask);
+ input_buf_ptr += advance;
I've attached a new patch that includes these changes. Further
modifications are still in progress.
--
Best regards,
Shinya Kato
NTT OSS Center
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 14 Aug 2025 at 05:25, KAZAR Ayoub wrote: > > Following Nazir's findings about 4096 bytes being the performant line length, I did more benchmarks from my side on both TEXT and CSV formats with two different cases of normal data (no special characters) and data with many special characters. > > Results are con good as expected and similar to previous benchmarks > ~30.9% faster copy in TEXT format > ~32.4% faster copy in CSV format > 20%-30% reduces cycles per instructions > > In the case of doing a lot of special characters in the lines (e.g., tables with large numbers of columns maybe), we obviously expect regressions here because of the overhead of many fallbacks to scalar processing. > Results for a 1/3 of line length of special characters: > ~43.9% slower copy in TEXT format > ~16.7% slower copy in CSV format > So for even less occurrences of special characters or wider distance between there might still be some regressions in this case, a non-significant case maybe, but can be treated in other patches if we consider to not use SIMD path sometimes. > > I hope this helps more and confirms the patch. Thanks for running that benchmark! Would you mind sharing a reproducer for the regression you observed? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Thu, 14 Aug 2025 at 05:25, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> Following Nazir's findings about 4096 bytes being the performant line length, I did more benchmarks from my side on both TEXT and CSV formats with two different cases of normal data (no special characters) and data with many special characters.
>
> Results are con good as expected and similar to previous benchmarks
> ~30.9% faster copy in TEXT format
> ~32.4% faster copy in CSV format
> 20%-30% reduces cycles per instructions
>
> In the case of doing a lot of special characters in the lines (e.g., tables with large numbers of columns maybe), we obviously expect regressions here because of the overhead of many fallbacks to scalar processing.
> Results for a 1/3 of line length of special characters:
> ~43.9% slower copy in TEXT format
> ~16.7% slower copy in CSV format
> So for even less occurrences of special characters or wider distance between there might still be some regressions in this case, a non-significant case maybe, but can be treated in other patches if we consider to not use SIMD path sometimes.
>
> I hope this helps more and confirms the patch.
Thanks for running that benchmark! Would you mind sharing a reproducer
for the regression you observed?
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, 7 Aug 2025 at 14:15, Nazir Bilal Yavuz wrote: > I have a couple of ideas that I was working on: > --- > > + * However, SIMD optimization cannot be applied in the following cases: > + * - Inside quoted fields, where escape sequences and closing quotes > + * require sequential processing to handle correctly. > > I think you can continue SIMD inside quoted fields. Only important > thing is you need to set last_was_esc to false when SIMD skipped the > chunk. There is a trick with doing carryless multiplication with -1 that can be used to SIMD process transitions between quoted/not-quoted. [1] This is able to convert a bitmask of unescaped quote character positions to a quote mask in a single operation. I last looked at it 5 years ago, but I remember coming to the conclusion that it would work for implementing PostgreSQL's interpretation of CSV. [1] https://github.com/geofflangdale/simdcsv/blob/master/src/main.cpp#L76 -- Ants
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 14 Aug 2025 at 18:00, KAZAR Ayoub wrote: >> Thanks for running that benchmark! Would you mind sharing a reproducer >> for the regression you observed? > > Of course, I attached the sql to generate the text and csv test files. > If having a 1/3 of line length of special characters can be an exaggeration, something lower might still reproduce some regressions of course for the same idea. Thank you so much! I am able to reproduce the regression you mentioned but both regressions are %20 on my end. I found that (by experimenting) SIMD causes a regression if it advances less than 5 characters. So, I implemented a small heuristic. It works like that: - If advance < 5 -> insert a sleep penalty (n cycles). - Each time advance < 5, n is doubled. - Each time advance ≥ 5, n is halved. I am sharing a POC patch to show heuristic, it can be applied on top of v1-0001. Heuristic version has the same performance improvements with the v1-0001 but the regression is %5 instead of %20 compared to the master. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 19 Aug 2025 at 15:33, Nazir Bilal Yavuz wrote: > > I am able to reproduce the regression you mentioned but both > regressions are %20 on my end. I found that (by experimenting) SIMD > causes a regression if it advances less than 5 characters. > > So, I implemented a small heuristic. It works like that: > > - If advance < 5 -> insert a sleep penalty (n cycles). 'sleep' might be a poor word choice here. I meant skipping SIMD for n number of times. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On 2025-08-19 Tu 10:14 AM, Nazir Bilal Yavuz wrote: > Hi, > > On Tue, 19 Aug 2025 at 15:33, Nazir Bilal Yavuz wrote: >> I am able to reproduce the regression you mentioned but both >> regressions are %20 on my end. I found that (by experimenting) SIMD >> causes a regression if it advances less than 5 characters. >> >> So, I implemented a small heuristic. It works like that: >> >> - If advance < 5 -> insert a sleep penalty (n cycles). > 'sleep' might be a poor word choice here. I meant skipping SIMD for n > number of times. > I was thinking a bit about that this morning. I wonder if it might be better instead of having a constantly applied heuristic like this, it might be better to do a little extra accounting in the first, say, 1000 lines of an input file, and if less than some portion of the input is found to be special characters then switch to the SIMD code. What that portion should be would need to be determined by some experimentation with a variety of typical workloads, but given your findings 20% seems like a good starting point. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, 14 Aug 2025 at 18:00, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>> Thanks for running that benchmark! Would you mind sharing a reproducer
>> for the regression you observed?
>
> Of course, I attached the sql to generate the text and csv test files.
> If having a 1/3 of line length of special characters can be an exaggeration, something lower might still reproduce some regressions of course for the same idea.
Thank you so much!
I am able to reproduce the regression you mentioned but both
regressions are %20 on my end. I found that (by experimenting) SIMD
causes a regression if it advances less than 5 characters.
So, I implemented a small heuristic. It works like that:
- If advance < 5 -> insert a sleep penalty (n cycles).
- Each time advance < 5, n is doubled.
- Each time advance ≥ 5, n is halved.
I am sharing a POC patch to show heuristic, it can be applied on top
of v1-0001. Heuristic version has the same performance improvements
with the v1-0001 but the regression is %5 instead of %20 compared to
the master.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 21 Aug 2025 at 18:47, Andrew Dunstan wrote: > > > On 2025-08-19 Tu 10:14 AM, Nazir Bilal Yavuz wrote: > > Hi, > > > > On Tue, 19 Aug 2025 at 15:33, Nazir Bilal Yavuz wrote: > >> I am able to reproduce the regression you mentioned but both > >> regressions are %20 on my end. I found that (by experimenting) SIMD > >> causes a regression if it advances less than 5 characters. > >> > >> So, I implemented a small heuristic. It works like that: > >> > >> - If advance < 5 -> insert a sleep penalty (n cycles). > > 'sleep' might be a poor word choice here. I meant skipping SIMD for n > > number of times. > > > > I was thinking a bit about that this morning. I wonder if it might be > better instead of having a constantly applied heuristic like this, it > might be better to do a little extra accounting in the first, say, 1000 > lines of an input file, and if less than some portion of the input is > found to be special characters then switch to the SIMD code. What that > portion should be would need to be determined by some experimentation > with a variety of typical workloads, but given your findings 20% seems > like a good starting point. I implemented a heuristic something similar to this. It is a mix of previous heuristic and your idea, it works like that: Overall logic is that we will not run SIMD for the entire line and we decide if it is worth it to run SIMD for the next lines. 1 - We will try SIMD and decide if it is worth it to run SIMD. 1.1 - If it is worth it, we will continue to run SIMD and we will halve the simd_last_sleep_cycle variable. 1.2 - If it is not worth it, we will double the simd_last_sleep_cycle and we will not run SIMD for these many lines. 1.3 - After skipping simd_last_sleep_cycle lines, we will go back to the #1. Note: simd_last_sleep_cycle can not pass 1024, so we will run SIMD for each 1024 lines at max. With this heuristic the regression is limited by %2 in the worst case. Patches are attached, the first patch is v2-0001 from Shinya with the '-Werror=maybe-uninitialized' fixes and the pgindent changes. 0002 is the actual heuristic patch. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
I’ve rebenchmarked the new heuristic patch, We still have the previous improvements ranging from 15% to 30%. For regressions i see at maximum 3% or 4% in the worst case, so this is solid.
I'm also trying the idea of doing SIMD inside quotes with prefix XOR using carry less multiplication avoiding the slow path in all cases even with weird looking input, but it needs to take into consideration the availability of PCLMULQDQ instruction set with <wmmintrin.h> and here we go, it quickly starts to become dirty OR we can wait for the decision to start requiring x86-64-v2 or v3 which has SSE4.2 and AVX2.
Regards,
Ayoub Kazar
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 16 Oct 2025 at 17:29, Nazir Bilal Yavuz wrote: > > Overall logic is that we will not run SIMD for the entire line and we > decide if it is worth it to run SIMD for the next lines. I had a typo there, correct sentence is that: "Overall logic is that we *will* run SIMD for the entire line and we decide if it is worth it to run SIMD for the next lines." -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Sat, 18 Oct 2025 at 21:46, KAZAR Ayoub wrote: > > Hello, > > I’ve rebenchmarked the new heuristic patch, We still have the previous improvements ranging from 15% to 30%. For regressions i see at maximum 3% or 4% in the worst case, so this is solid. Thank you so much for doing this! The results look nice, do you think there are any other benchmarks that might be interesting to try? > I'm also trying the idea of doing SIMD inside quotes with prefix XOR using carry less multiplication avoiding the slow path in all cases even with weird looking input, but it needs to take into consideration the availability of PCLMULQDQ instruction set with and here we go, it quickly starts to become dirty OR we can wait for the decision to start requiring x86-64-v2 or v3 which has SSE4.2 and AVX2. I can not quite picture this, would you mind sharing a few examples or patches? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 21 Aug 2025 at 18:47, Andrew Dunstan <andrew@dunslane.net> wrote:On 2025-08-19 Tu 10:14 AM, Nazir Bilal Yavuz wrote:Hi, On Tue, 19 Aug 2025 at 15:33, Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:I am able to reproduce the regression you mentioned but both regressions are %20 on my end. I found that (by experimenting) SIMD causes a regression if it advances less than 5 characters. So, I implemented a small heuristic. It works like that: - If advance < 5 -> insert a sleep penalty (n cycles).'sleep' might be a poor word choice here. I meant skipping SIMD for n number of times.I was thinking a bit about that this morning. I wonder if it might be better instead of having a constantly applied heuristic like this, it might be better to do a little extra accounting in the first, say, 1000 lines of an input file, and if less than some portion of the input is found to be special characters then switch to the SIMD code. What that portion should be would need to be determined by some experimentation with a variety of typical workloads, but given your findings 20% seems like a good starting point.I implemented a heuristic something similar to this. It is a mix of previous heuristic and your idea, it works like that: Overall logic is that we will not run SIMD for the entire line and we decide if it is worth it to run SIMD for the next lines. 1 - We will try SIMD and decide if it is worth it to run SIMD. 1.1 - If it is worth it, we will continue to run SIMD and we will halve the simd_last_sleep_cycle variable. 1.2 - If it is not worth it, we will double the simd_last_sleep_cycle and we will not run SIMD for these many lines. 1.3 - After skipping simd_last_sleep_cycle lines, we will go back to the #1. Note: simd_last_sleep_cycle can not pass 1024, so we will run SIMD for each 1024 lines at max. With this heuristic the regression is limited by %2 in the worst case.
My worry is that the worst case is actually quite common. Sparse data sets dominated by a lot of null values (and hence lots of special characters) are very common. Are people prepared to accept a 2% regression on load times for such data sets?
cheers
andrew
-- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Mon, Oct 20, 2025 at 10:02:23AM -0400, Andrew Dunstan wrote: > On 2025-10-16 Th 10:29 AM, Nazir Bilal Yavuz wrote: >> With this heuristic the regression is limited by %2 in the worst case. > > My worry is that the worst case is actually quite common. Sparse data sets > dominated by a lot of null values (and hence lots of special characters) are > very common. Are people prepared to accept a 2% regression on load times for > such data sets? Without knowing how common it is, I think it's difficult to judge whether 2% is a reasonable trade-off. If <5% of workloads might see a small regression while the other >95% see double-digit percentage improvements, then I might argue that it's fine. But I'm not sure we have any way to know those sorts of details at the moment. I'm also at least a little skeptical about the 2% number. IME that's generally within the noise range and can vary greatly between machines and test runs. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Mon, Oct 20, 2025 at 10:02:23AM -0400, Andrew Dunstan wrote:On 2025-10-16 Th 10:29 AM, Nazir Bilal Yavuz wrote:With this heuristic the regression is limited by %2 in the worst case.My worry is that the worst case is actually quite common. Sparse data sets dominated by a lot of null values (and hence lots of special characters) are very common. Are people prepared to accept a 2% regression on load times for such data sets?Without knowing how common it is, I think it's difficult to judge whether 2% is a reasonable trade-off. If <5% of workloads might see a small regression while the other >95% see double-digit percentage improvements, then I might argue that it's fine. But I'm not sure we have any way to know those sorts of details at the moment.
I guess what I don't understand is why we actually need to do the test continuously, even using an adaptive algorithm. Data files in my experience usually have lines with fairly similar shapes. It's highly unlikely that you will get the the first 1000 (say) lines of a file that are rich in special characters and then some later significant section that isn't, or vice versa. Therefore, doing the test once should yield the correct answer that can be applied to the rest of the file. That should reduce the worst case regression to ~0% without sacrificing any of the performance gains. I appreciate the elegance of what Bilal has done here, but it does seem like overkill.
I'm also at least a little skeptical about the 2% number. IME that's generally within the noise range and can vary greatly between machines and test runs.
Fair point.
cheers
andrew
-- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Mon, 20 Oct 2025 at 23:32, Andrew Dunstan wrote: > > > On 2025-10-20 Mo 1:04 PM, Nathan Bossart wrote: > > On Mon, Oct 20, 2025 at 10:02:23AM -0400, Andrew Dunstan wrote: > > On 2025-10-16 Th 10:29 AM, Nazir Bilal Yavuz wrote: > > With this heuristic the regression is limited by %2 in the worst case. > > My worry is that the worst case is actually quite common. Sparse data sets > dominated by a lot of null values (and hence lots of special characters) are > very common. Are people prepared to accept a 2% regression on load times for > such data sets? > > Without knowing how common it is, I think it's difficult to judge whether > 2% is a reasonable trade-off. If <5% of workloads might see a small > regression while the other >95% see double-digit percentage improvements, > then I might argue that it's fine. But I'm not sure we have any way to > know those sorts of details at the moment. > > > I guess what I don't understand is why we actually need to do the test continuously, even using an adaptive algorithm. Data files in my experience usually have lines with fairly similar shapes. It's highly unlikely that you will get the the first 1000 (say) lines of a file that are rich in special characters and then some later significant section that isn't, or vice versa. Therefore, doing the test once should yield the correct answer that can be applied to the rest of the file. That should reduce the worst case regression to ~0% without sacrificing any of the performance gains. I appreciate the elegance of what Bilal has done here, but it does seem like overkill. I think the problem is deciding how many lines to process before deciding for the rest. 1000 lines could work for the small sized data but it might not work for the big sized data. Also, it might cause a worse regressions for the small sized data. Because of this reason, I tried to implement a heuristic that will work regardless of the size of the data. The last heuristic I suggested will run SIMD for approximately (#number_of_lines / 1024 [1024 is the max number of lines to sleep before running SIMD again]) lines if all characters in the data are special characters. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Thank you so much for doing this! The results look nice, do you think
there are any other benchmarks that might be interesting to try?
> I'm also trying the idea of doing SIMD inside quotes with prefix XOR using carry less multiplication avoiding the slow path in all cases even with weird looking input, but it needs to take into consideration the availability of PCLMULQDQ instruction set with <wmmintrin.h> and here we go, it quickly starts to become dirty OR we can wait for the decision to start requiring x86-64-v2 or v3 which has SSE4.2 and AVX2.
I can not quite picture this, would you mind sharing a few examples or patches?
[1] https://branchfree.org/2019/03/06/code-fragment-finding-quote-pairs-with-carry-less-multiply-pclmulqdq/
[2] https://github.com/AyoubKaz07/postgres/commit/73c6ecfedae4cce5c3f375fd6074b1ca9dfe1daf
Re: Speed up COPY FROM text/CSV parsing using SIMD
Currently we are at 200-400Mbps which isn't that terrible compared to production and non production grade parsers (of course we don't only parse in our case), also we are using SSE2 only so theoretically if we add support for avx later on we'll have even better numbers.Maybe more micro optimizations to the current heuristic can squeeze it more.
[1] https://branchfree.org/2019/03/06/code-fragment-finding-quote-pairs-with-carry-less-multiply-pclmulqdq/
[2] https://github.com/AyoubKaz07/postgres/commit/73c6ecfedae4cce5c3f375fd6074b1ca9dfe1dafRegards,Ayoub Kazar.
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Oct 21, 2025 at 12:09:27AM +0300, Nazir Bilal Yavuz wrote: > I think the problem is deciding how many lines to process before > deciding for the rest. 1000 lines could work for the small sized data > but it might not work for the big sized data. Also, it might cause a > worse regressions for the small sized data. IMHO we have some leeway with smaller amounts of data. If COPY FROM for 1000 rows takes 19 milliseconds as opposed to 11 milliseconds, it seems unlikely users would be inconvenienced all that much. (Those numbers are completely made up in order to illustrate my point.) > Because of this reason, I > tried to implement a heuristic that will work regardless of the size > of the data. The last heuristic I suggested will run SIMD for > approximately (#number_of_lines / 1024 [1024 is the max number of > lines to sleep before running SIMD again]) lines if all characters in > the data are special characters. I wonder if we could mitigate the regression further by spacing out the checks a bit more. It could be worth comparing a variety of values to identify what works best with the test data. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Oct 21, 2025 at 08:17:01AM +0200, KAZAR Ayoub wrote: >>> I'm also trying the idea of doing SIMD inside quotes with prefix XOR >>> using carry less multiplication avoiding the slow path in all cases even >>> with weird looking input, but it needs to take into consideration the >>> availability of PCLMULQDQ instruction set with and here we >>> go, it quickly starts to become dirty OR we can wait for the decision to >>> start requiring x86-64-v2 or v3 which has SSE4.2 and AVX2. > > [...] > > Currently we are at 200-400Mbps which isn't that terrible compared to > production and non production grade parsers (of course we don't only parse > in our case), also we are using SSE2 only so theoretically if we add > support for avx later on we'll have even better numbers. > Maybe more micro optimizations to the current heuristic can squeeze it more. I'd greatly prefer that we stick with SSE2/Neon (i.e., simd.h) unless the gains are extraordinary. Beyond the inherent complexity of using architecture-specific intrinsics, you also have to deal with configure-time checks, runtime checks, and function pointer overhead juggling. That tends to be a lot of work for the amount of gain. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 21 Oct 2025 at 21:40, Nathan Bossart wrote: > > On Tue, Oct 21, 2025 at 12:09:27AM +0300, Nazir Bilal Yavuz wrote: > > I think the problem is deciding how many lines to process before > > deciding for the rest. 1000 lines could work for the small sized data > > but it might not work for the big sized data. Also, it might cause a > > worse regressions for the small sized data. > > IMHO we have some leeway with smaller amounts of data. If COPY FROM for > 1000 rows takes 19 milliseconds as opposed to 11 milliseconds, it seems > unlikely users would be inconvenienced all that much. (Those numbers are > completely made up in order to illustrate my point.) > > > Because of this reason, I > > tried to implement a heuristic that will work regardless of the size > > of the data. The last heuristic I suggested will run SIMD for > > approximately (#number_of_lines / 1024 [1024 is the max number of > > lines to sleep before running SIMD again]) lines if all characters in > > the data are special characters. > > I wonder if we could mitigate the regression further by spacing out the > checks a bit more. It could be worth comparing a variety of values to > identify what works best with the test data. Do you mean that instead of doubling the SIMD sleep, we should multiply it by 3 (or another factor)? Or are you referring to increasing the maximum sleep from 1024? Or possibly both? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Oct 22, 2025 at 03:33:37PM +0300, Nazir Bilal Yavuz wrote: > On Tue, 21 Oct 2025 at 21:40, Nathan Bossart wrote: >> I wonder if we could mitigate the regression further by spacing out the >> checks a bit more. It could be worth comparing a variety of values to >> identify what works best with the test data. > > Do you mean that instead of doubling the SIMD sleep, we should > multiply it by 3 (or another factor)? Or are you referring to > increasing the maximum sleep from 1024? Or possibly both? I'm not sure of the precise details, but the main thrust of my suggestion is to assume that whatever sampling you do to determine whether to use SIMD is good for a larger chunk of data. That is, if you are sampling 1K lines and then using the result to choose whether to use SIMD for the next 100K lines, we could instead bump the latter number to 1M lines (or something). That way we minimize the regression for relatively uniform data sets while retaining some ability to adapt in case things change halfway through a large table. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On 2025-10-22 We 3:24 PM, Nathan Bossart wrote: > On Wed, Oct 22, 2025 at 03:33:37PM +0300, Nazir Bilal Yavuz wrote: >> On Tue, 21 Oct 2025 at 21:40, Nathan Bossart wrote: >>> I wonder if we could mitigate the regression further by spacing out the >>> checks a bit more. It could be worth comparing a variety of values to >>> identify what works best with the test data. >> Do you mean that instead of doubling the SIMD sleep, we should >> multiply it by 3 (or another factor)? Or are you referring to >> increasing the maximum sleep from 1024? Or possibly both? > I'm not sure of the precise details, but the main thrust of my suggestion > is to assume that whatever sampling you do to determine whether to use SIMD > is good for a larger chunk of data. That is, if you are sampling 1K lines > and then using the result to choose whether to use SIMD for the next 100K > lines, we could instead bump the latter number to 1M lines (or something). > That way we minimize the regression for relatively uniform data sets while > retaining some ability to adapt in case things change halfway through a > large table. > I'd be ok with numbers like this, although I suspect the numbers of cases where we see shape shifts like this in the middle of a data set would be vanishingly small. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
On 2025-10-22 We 3:24 PM, Nathan Bossart wrote:
> On Wed, Oct 22, 2025 at 03:33:37PM +0300, Nazir Bilal Yavuz wrote:
>> On Tue, 21 Oct 2025 at 21:40, Nathan Bossart <nathandbossart@gmail.com> wrote:
>>> I wonder if we could mitigate the regression further by spacing out the
>>> checks a bit more. It could be worth comparing a variety of values to
>>> identify what works best with the test data.
>> Do you mean that instead of doubling the SIMD sleep, we should
>> multiply it by 3 (or another factor)? Or are you referring to
>> increasing the maximum sleep from 1024? Or possibly both?
> I'm not sure of the precise details, but the main thrust of my suggestion
> is to assume that whatever sampling you do to determine whether to use SIMD
> is good for a larger chunk of data. That is, if you are sampling 1K lines
> and then using the result to choose whether to use SIMD for the next 100K
> lines, we could instead bump the latter number to 1M lines (or something).
> That way we minimize the regression for relatively uniform data sets while
> retaining some ability to adapt in case things change halfway through a
> large table.
>
I'd be ok with numbers like this, although I suspect the numbers of
cases where we see shape shifts like this in the middle of a data set
would be vanishingly small.
cheers
andrew
--
Andrew Dunstan
EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello!I wanted reproduce the results using files attached by Shinya Kato and Ayoub Kazar. I installed a postgres compiled from master, and then I installed a postgres built from master plus Nazir Bilal Yavuz's v3 patches applied.The master+v3patches postgres naturally performed better on copying into the database: anywhere from 11% better for the t.csv file produced by Shinyo's test.sql, to 35% better copying in the t_4096_none.csv file created by Ayoub Kazar's simd-copy-from-bench.sql.But here's where it gets weird. The two files created by Ayoub Kazar's simd-copy-from-bench.sql that are supposed to be slower, t_4096_escape.txt, and t_4096_quote.csv, actually ran faster on my machine, by 11% and 5% respectively.This seems impossible.A few things I should note:I timed the commands using the Unix time command, like so:time psql -X -U mwood -h localhost -d postgres -c '\copy t from /tmp/t_4096_escape.txt'For each file, I timed the copy 6 times and took the average.This was done on my work Linux machine while also running Chrome and an Open Office spreadsheet; not a dedicated machine only running postgres.
All of the copy results took between 4.5 seconds (Shinyo's t.csv copied into postgres compiled from master) to 2 seconds (Ayoub Kazar's t_4096_none.csv copied into postgres compiled from master plus Nazir's v3 patches).Perhaps I need to fiddle with the provided SQL to produce larger files to get longer run times? Maybe sub-second differences won't tell as interesting a story as minutes-long copy commands?
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Nov 11, 2025 at 11:23 PM Manni Wood <manni.wood@enterprisedb.com> wrote:Hello!I wanted reproduce the results using files attached by Shinya Kato and Ayoub Kazar. I installed a postgres compiled from master, and then I installed a postgres built from master plus Nazir Bilal Yavuz's v3 patches applied.The master+v3patches postgres naturally performed better on copying into the database: anywhere from 11% better for the t.csv file produced by Shinyo's test.sql, to 35% better copying in the t_4096_none.csv file created by Ayoub Kazar's simd-copy-from-bench.sql.But here's where it gets weird. The two files created by Ayoub Kazar's simd-copy-from-bench.sql that are supposed to be slower, t_4096_escape.txt, and t_4096_quote.csv, actually ran faster on my machine, by 11% and 5% respectively.This seems impossible.A few things I should note:I timed the commands using the Unix time command, like so:time psql -X -U mwood -h localhost -d postgres -c '\copy t from /tmp/t_4096_escape.txt'For each file, I timed the copy 6 times and took the average.This was done on my work Linux machine while also running Chrome and an Open Office spreadsheet; not a dedicated machine only running postgres.Hello,I think if you do a perf benchmark (if it still reproduces) it would probably be possible to explain why it's performing like that looking at the CPI and other metrics and compare it to my findings.What i also suggest is to make the data close even closer to the worst case i.e: more special characters where it hurts the switching between SIMD and scalar processing (in simd-copy-from-bench.sql file), if still does a good job then there's something to look at.All of the copy results took between 4.5 seconds (Shinyo's t.csv copied into postgres compiled from master) to 2 seconds (Ayoub Kazar's t_4096_none.csv copied into postgres compiled from master plus Nazir's v3 patches).Perhaps I need to fiddle with the provided SQL to produce larger files to get longer run times? Maybe sub-second differences won't tell as interesting a story as minutes-long copy commands?I did try it on some GBs (around 2-5GB only), the differences were not that much, but if you can run this on more GBs (at least 10GB) it would be good to look at, although i don't suspect anything interesting since the shape of data is the same for the totality of the COPY.Thanks for the info.Regards,Ayoub Kazar.
Re: Speed up COPY FROM text/CSV parsing using SIMD
I'd like to mark myself as the committer this one, but I noticed that the commitfest entry [0] has been marked as Withdrawn. Could someone either reopen it or create a new one as appropriate (assuming there is a desire to continue with it)? I'm hoping to start spending more time on it soon. [0] https://commitfest.postgresql.org/patch/5952/ -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
I'd like to mark myself as the committer this one, but I noticed that the
commitfest entry [0] has been marked as Withdrawn. Could someone either
reopen it or create a new one as appropriate (assuming there is a desire to
continue with it)? I'm hoping to start spending more time on it soon.
[0] https://commitfest.postgresql.org/patch/5952/
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 18 Nov 2025 at 01:53, Shinya Kato wrote: > > > On Tue, Nov 18, 2025, 07:16 Nathan Bossart wrote: >> >> I'd like to mark myself as the committer this one, but I noticed that the >> commitfest entry [0] has been marked as Withdrawn. Could someone either >> reopen it or create a new one as appropriate (assuming there is a desire to >> continue with it)? I'm hoping to start spending more time on it soon. >> >> [0] https://commitfest.postgresql.org/patch/5952/ > > > I closed this entry because I currently don't have enough time to continue developing this patch. It is fine if someone else reopens it; I will do my best to see the patch whenever I can. Thank you for all your work on this patch. I would like to continue working on this but I am not sure what are the correct steps to reopen this commitfest entry. Do I just need to change commitfest entry's status to 'Needs review'? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On 2025-11-18 Tu 3:04 AM, Nazir Bilal Yavuz wrote: > Hi, > > On Tue, 18 Nov 2025 at 01:53, Shinya Kato wrote: >> >> On Tue, Nov 18, 2025, 07:16 Nathan Bossart wrote: >>> I'd like to mark myself as the committer this one, but I noticed that the >>> commitfest entry [0] has been marked as Withdrawn. Could someone either >>> reopen it or create a new one as appropriate (assuming there is a desire to >>> continue with it)? I'm hoping to start spending more time on it soon. >>> >>> [0] https://commitfest.postgresql.org/patch/5952/ >> >> I closed this entry because I currently don't have enough time to continue developing this patch. It is fine if someone else reopens it; I will do my best to see the patch whenever I can. > Thank you for all your work on this patch. > > I would like to continue working on this but I am not sure what are > the correct steps to reopen this commitfest entry. Do I just need to > change commitfest entry's status to 'Needs review'? That should do it, I believe. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 18 Nov 2025 at 17:01, Andrew Dunstan wrote: > > > On 2025-11-18 Tu 3:04 AM, Nazir Bilal Yavuz wrote: > > Hi, > > > > On Tue, 18 Nov 2025 at 01:53, Shinya Kato wrote: > >> > >> On Tue, Nov 18, 2025, 07:16 Nathan Bossart wrote: > >>> I'd like to mark myself as the committer this one, but I noticed that the > >>> commitfest entry [0] has been marked as Withdrawn. Could someone either > >>> reopen it or create a new one as appropriate (assuming there is a desire to > >>> continue with it)? I'm hoping to start spending more time on it soon. > >>> > >>> [0] https://commitfest.postgresql.org/patch/5952/ > >> > >> I closed this entry because I currently don't have enough time to continue developing this patch. It is fine if someone else reopens it; I will do my best to see the patch whenever I can. > > Thank you for all your work on this patch. > > > > I would like to continue working on this but I am not sure what are > > the correct steps to reopen this commitfest entry. Do I just need to > > change commitfest entry's status to 'Needs review'? > > That should do it, I believe. Thanks, done. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
(assuming there is a desire to
continue with it)?
I'm hoping to start spending more time on it soon.
Somethings worth noting for future reference (so someone else wouldn't waste time thinking about it), previously I tried extra several micro optimizations inside and around CopyReadLineText:
SIMD alignment: Forcing 16-byte aligned buffers so we could use aligned memory instructions (_mm_load_si128 vs _mm_loadu_si128) provided no measurable benefit on modern CPUs (there's definitely a thread somewhere talking about it that i didn't encounter yet). This likely explains why simd.h exclusively uses unaligned load intrinsics the performance difference has become negligible since Nehalem processors.
Memory prefetching: Explicit prefetch instructions for the COPY buffer pipeline (copy_raw_buf, input buffers, etc.) either showed no improvement or slight regression. Multiple chunks are already within a cache line, other buffers are too far to prefetch and the next part of the buffer is easily prefetched, nothing special, so it turns out to be not worth having more uops.
Instruction-level parallelism: Spreading too many independent vector operations to increase ILP eventually degrades performance, likely due to backend saturation observed through perf (execution port and execution units contention most likely ?)
.....
This simply suggests that further optimization work should focus on the pipeline as a whole for large benefits (parallel copy[0], maybe ?).
[0] https://www.postgresql.org/message-id/CAA4eK1+kpddvvLxWm4BuG_AhVvYz8mKAEa7osxp_X0d4ZEiV=g@mail.gmail.com
--
Regards,
Ayoub Kazar
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Nov 18, 2025 at 05:20:05PM +0300, Nazir Bilal Yavuz wrote:
> Thanks, done.
I took a look at the v3 patches. Here are my high-level thoughts:
+ /*
+ * Parse data and transfer into line_buf. To get benefit from inlining,
+ * call CopyReadLineText() with the constant boolean variables.
+ */
+ if (cstate->simd_continue)
+ result = CopyReadLineText(cstate, is_csv, true);
+ else
+ result = CopyReadLineText(cstate, is_csv, false);
I'm curious whether this actually generates different code, and if it does,
if it's actually faster. We're already branching on cstate->simd_continue
here.
+ /* Load a chunk of data into a vector register */
+ vector8_load(&chunk, (const uint8 *) ©_input_buf[input_buf_ptr]);
In other places, processing 2 or 4 vectors of data at a time has proven
faster. Have you tried that here?
+ /* \n and \r are not special inside quotes */
+ if (!in_quote)
+ match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
+
+ if (is_csv)
+ {
+ match = vector8_or(match, vector8_eq(chunk, quote));
+ if (escapec != '\0')
+ match = vector8_or(match, vector8_eq(chunk, escape));
+ }
+ else
+ match = vector8_or(match, vector8_eq(chunk, bs));
The amount of branching here catches my eye. Some branching might be
unavoidable, but in general we want to keep these SIMD paths as branch-free
as possible.
+ /*
+ * Found a special character. Advance up to that point and let
+ * the scalar code handle it.
+ */
+ int advance = pg_rightmost_one_pos32(mask);
+
+ input_buf_ptr += advance;
+ simd_total_advance += advance;
Do we actually need to advance here? Or could we just fall through to the
scalar path? My suspicion is that this extra code doesn't gain us much.
+ if (simd_last_sleep_cycle == 0)
+ simd_last_sleep_cycle = 1;
+ else if (simd_last_sleep_cycle >= SIMD_SLEEP_MAX / 2)
+ simd_last_sleep_cycle = SIMD_SLEEP_MAX;
+ else
+ simd_last_sleep_cycle <<= 1;
+ cstate->simd_current_sleep_cycle = simd_last_sleep_cycle;
+ cstate->simd_last_sleep_cycle = simd_last_sleep_cycle;
IMHO we should be looking for ways to simplify this should-we-use-SIMD
code. For example, perhaps we could just disable the SIMD path for 10K or
100K lines any time a special character is found. I'm dubious that a lot
of complexity is warranted.
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
Thank you for looking into this!
On Thu, 20 Nov 2025 at 00:01, Nathan Bossart wrote:
>
> On Tue, Nov 18, 2025 at 05:20:05PM +0300, Nazir Bilal Yavuz wrote:
> > Thanks, done.
>
> I took a look at the v3 patches. Here are my high-level thoughts:
>
> + /*
> + * Parse data and transfer into line_buf. To get benefit from inlining,
> + * call CopyReadLineText() with the constant boolean variables.
> + */
> + if (cstate->simd_continue)
> + result = CopyReadLineText(cstate, is_csv, true);
> + else
> + result = CopyReadLineText(cstate, is_csv, false);
>
> I'm curious whether this actually generates different code, and if it does,
> if it's actually faster. We're already branching on cstate->simd_continue
> here.
I had the same doubts before but my benchmark shows nice speedup. I
used a test which is full of delimiters. The current code gives 2700
ms but when I changed these lines with the 'result =
CopyReadLineText(cstate, is_csv, cstate->simd_continue);', the result
was 2920 ms. I compiled code with both -O3 and -O2 and the results
were similar.
>
> + /* Load a chunk of data into a vector register */
> + vector8_load(&chunk, (const uint8 *) ©_input_buf[input_buf_ptr]);
>
> In other places, processing 2 or 4 vectors of data at a time has proven
> faster. Have you tried that here?
Sorry, I could not find the related code piece. I only saw the
vector8_load() inside of hex_decode_safe() function and its comment
says:
/*
* We must process 2 vectors at a time since the output will be half the
* length of the input.
*/
But this does not mention any speedup from using 2 vectors at a time.
Could you please show the related code?
>
> + /* \n and \r are not special inside quotes */
> + if (!in_quote)
> + match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
> +
> + if (is_csv)
> + {
> + match = vector8_or(match, vector8_eq(chunk, quote));
> + if (escapec != '\0')
> + match = vector8_or(match, vector8_eq(chunk, escape));
> + }
> + else
> + match = vector8_or(match, vector8_eq(chunk, bs));
>
> The amount of branching here catches my eye. Some branching might be
> unavoidable, but in general we want to keep these SIMD paths as branch-free
> as possible.
You are right, I will check these branches and will try to remove as
many branches as possible.
>
> + /*
> + * Found a special character. Advance up to that point and let
> + * the scalar code handle it.
> + */
> + int advance = pg_rightmost_one_pos32(mask);
> +
> + input_buf_ptr += advance;
> + simd_total_advance += advance;
>
> Do we actually need to advance here? Or could we just fall through to the
> scalar path? My suspicion is that this extra code doesn't gain us much.
My testing shows that if we advance more than ~5 characters then SIMD
is worth it, but if we advance less than ~5; then code causes a
regression. I used this information while writing a heuristic.
>
> + if (simd_last_sleep_cycle == 0)
> + simd_last_sleep_cycle = 1;
> + else if (simd_last_sleep_cycle >= SIMD_SLEEP_MAX / 2)
> + simd_last_sleep_cycle = SIMD_SLEEP_MAX;
> + else
> + simd_last_sleep_cycle <<= 1;
> + cstate->simd_current_sleep_cycle = simd_last_sleep_cycle;
> + cstate->simd_last_sleep_cycle = simd_last_sleep_cycle;
>
> IMHO we should be looking for ways to simplify this should-we-use-SIMD
> code. For example, perhaps we could just disable the SIMD path for 10K or
> 100K lines any time a special character is found. I'm dubious that a lot
> of complexity is warranted.
I think this is a bit too harsh since SIMD is still worth it if SIMD
can advance more than ~5 character average. I am trying to use SIMD as
much as possible when it is worth it but what you said can remove the
regression completely, perhaps that is the correct way.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On 2025-11-20 Th 7:55 AM, Nazir Bilal Yavuz wrote: > Hi, > > Thank you for looking into this! > > On Thu, 20 Nov 2025 at 00:01, Nathan Bossart wrote: > >> IMHO we should be looking for ways to simplify this should-we-use-SIMD >> code. For example, perhaps we could just disable the SIMD path for 10K or >> 100K lines any time a special character is found. I'm dubious that a lot >> of complexity is warranted. > I think this is a bit too harsh since SIMD is still worth it if SIMD > can advance more than ~5 character average. I am trying to use SIMD as > much as possible when it is worth it but what you said can remove the > regression completely, perhaps that is the correct way. > Perhaps a very small regression (say under 1%) in the worst case would be OK. But the closer you can get that to zero the more acceptable this will be. Very large loads of sparse data, which will often have lots of special characters AIUI, are very common, so we should not dismiss the worst case as an outlier. I still like the idea of testing, say, a thousand lines every million, or something like that. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, Nov 20, 2025 at 03:55:43PM +0300, Nazir Bilal Yavuz wrote: > On Thu, 20 Nov 2025 at 00:01, Nathan Bossart wrote: >> + /* Load a chunk of data into a vector register */ >> + vector8_load(&chunk, (const uint8 *) ©_input_buf[input_buf_ptr]); >> >> In other places, processing 2 or 4 vectors of data at a time has proven >> faster. Have you tried that here? > > Sorry, I could not find the related code piece. I only saw the > vector8_load() inside of hex_decode_safe() function and its comment > says: > > /* > * We must process 2 vectors at a time since the output will be half the > * length of the input. > */ > > But this does not mention any speedup from using 2 vectors at a time. > Could you please show the related code? See pg_lfind32(). -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, Nov 20, 2025 at 03:55:43PM +0300, Nazir Bilal Yavuz wrote:
> On Thu, 20 Nov 2025 at 00:01, Nathan Bossart <nathandbossart@gmail.com> wrote:
>> + /* Load a chunk of data into a vector register */
>> + vector8_load(&chunk, (const uint8 *) ©_input_buf[input_buf_ptr]);
>>
>> In other places, processing 2 or 4 vectors of data at a time has proven
>> faster. Have you tried that here?
>
> Sorry, I could not find the related code piece. I only saw the
> vector8_load() inside of hex_decode_safe() function and its comment
> says:
>
> /*
> * We must process 2 vectors at a time since the output will be half the
> * length of the input.
> */
>
> But this does not mention any speedup from using 2 vectors at a time.
> Could you please show the related code?
See pg_lfind32().
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Nov 18, 2025 at 05:20:05PM +0300, Nazir Bilal Yavuz wrote:I've compiled both versions with -O2 and confirmed they generate different code. When simd_continue is passed as a constant to CopyReadLineText, the compiler optimizes out the condition checks from the SIMD path.
> Thanks, done.
I took a look at the v3 patches. Here are my high-level thoughts:
+ /*
+ * Parse data and transfer into line_buf. To get benefit from inlining,
+ * call CopyReadLineText() with the constant boolean variables.
+ */
+ if (cstate->simd_continue)
+ result = CopyReadLineText(cstate, is_csv, true);
+ else
+ result = CopyReadLineText(cstate, is_csv, false);
I'm curious whether this actually generates different code, and if it does,
if it's actually faster. We're already branching on cstate->simd_continue
here.
A small benchmark on a 1GB+ file shows the expected benefit which is around 6% performance improvement.
I've attached the assembly outputs in case someone wants to check something else.
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Nov 18, 2025 at 05:20:05PM +0300, Nazir Bilal Yavuz wrote:I've compiled both versions with -O2 and confirmed they generate different code. When simd_continue is passed as a constant to CopyReadLineText, the compiler optimizes out the condition checks from the SIMD path.
> Thanks, done.
I took a look at the v3 patches. Here are my high-level thoughts:
+ /*
+ * Parse data and transfer into line_buf. To get benefit from inlining,
+ * call CopyReadLineText() with the constant boolean variables.
+ */
+ if (cstate->simd_continue)
+ result = CopyReadLineText(cstate, is_csv, true);
+ else
+ result = CopyReadLineText(cstate, is_csv, false);
I'm curious whether this actually generates different code, and if it does,
if it's actually faster. We're already branching on cstate->simd_continue
here.
A small benchmark on a 1GB+ file shows the expected benefit which is around 6% performance improvement.
I've attached the assembly outputs in case someone wants to check something else.Regards,Ayoub Kazar
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Nov 26, 2025 at 5:51 AM KAZAR Ayoub <ma_kazar@esi.dz> wrote:On Tue, Nov 18, 2025 at 05:20:05PM +0300, Nazir Bilal Yavuz wrote:I've compiled both versions with -O2 and confirmed they generate different code. When simd_continue is passed as a constant to CopyReadLineText, the compiler optimizes out the condition checks from the SIMD path.
> Thanks, done.
I took a look at the v3 patches. Here are my high-level thoughts:
+ /*
+ * Parse data and transfer into line_buf. To get benefit from inlining,
+ * call CopyReadLineText() with the constant boolean variables.
+ */
+ if (cstate->simd_continue)
+ result = CopyReadLineText(cstate, is_csv, true);
+ else
+ result = CopyReadLineText(cstate, is_csv, false);
I'm curious whether this actually generates different code, and if it does,
if it's actually faster. We're already branching on cstate->simd_continue
here.
A small benchmark on a 1GB+ file shows the expected benefit which is around 6% performance improvement.
I've attached the assembly outputs in case someone wants to check something else.Regards,Ayoub KazarCorrection to my last post:I also tried files that alternated lines with no special characters and lines with 1/3rd special characters, thinking I could force the algorithm to continually check whether or not it should use simd and therefore force more overhead in the try-simd/don't-try-simd housekeeping code. The text file was still 20% faster (not 50% faster as I originally stated --- that was a typo). The CSV file was still 13% faster.Also, apologies for posting at the top in my last e-mail.---- Manni Wood EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 6 Dec 2025 at 04:40, Manni Wood wrote:
> Hello, all.
>
> Andrew, I tried your suggestion of just reading the first chunk of the copy file to determine if SIMD is worth using. Attached are v4 versions of the patches showing a first attempt at doing that.
Thank you for doing this!
> I attached test.sh.txt to show how I've been testing, with 5 million lines of the various copy file variations introduced by Ayub Kazar.
>
> The text copy with no special chars is 30% faster. The CSV copy with no special chars is 48% faster. The text with 1/3rd escapes is 3% slower. The CSV with 1/3rd quotes is 0.27% slower.
>
> This set of patches follows the simplest suggestion of just testing the first N lines (actually first N bytes) of the file and then deciding whether or not to enable SIMD. This set of patches does not follow Andrew's later suggestion of maybe checking again every million lines or so.
My input-generation script is not ready to share yet, but the inputs
follow this format: text_${n}.input, where n represents the number of
normal characters before the delimiter. For example:
n = 0 -> "\n\n\n\n\n..." (no normal characters)
n = 1 -> "a\n..." (1 normal character before the delimiter)
...
n = 5 -> "aaaaa\n..."
… continuing up to n = 32.
Each line has 4096 chars and there are a total of 100000 lines in each
input file.
I only benchmarked the text format. I compared the latest heuristic I
shared [1] with the current method. The benchmarks show roughly a ~16%
regression at the worst case (n = 2), with regressions up to n = 5.
For the remaining values, performance was similar.
Actual comparison of timings (in ms):
current method / heuristic
n = 0 -> 3252.7253 / 2856.2753 (%12)
n = 1 -> 2910.321 / 2520.7717 (%13)
n = 2 -> 2865.008 / 2403.2017 (%16)
n = 3 -> 2608.649 / 2353.1477 (%9)
n = 4 -> 2460.74 / 2300.1783 (%6)
n = 5 -> 2451.696 / 2362.1573 (%3)
No difference for the rest.
Side note: Sorry for the delay in responding, I will continue working
on this next week.
[1] https://postgr.es/m/CAN55FZ1KF7XNpm2XyG%3DM-sFUODai%3D6Z8a11xE3s4YRBeBKY3tA%40mail.gmail.com
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 6 Dec 2025 at 10:55, Bilal Yavuz wrote:
>
> Hi,
>
> On Sat, 6 Dec 2025 at 04:40, Manni Wood wrote:
> > Hello, all.
> >
> > Andrew, I tried your suggestion of just reading the first chunk of the copy file to determine if SIMD is worth using. Attached are v4 versions of the patches showing a first attempt at doing that.
>
> Thank you for doing this!
>
> > I attached test.sh.txt to show how I've been testing, with 5 million lines of the various copy file variations introduced by Ayub Kazar.
> >
> > The text copy with no special chars is 30% faster. The CSV copy with no special chars is 48% faster. The text with 1/3rd escapes is 3% slower. The CSV with 1/3rd quotes is 0.27% slower.
> >
> > This set of patches follows the simplest suggestion of just testing the first N lines (actually first N bytes) of the file and then deciding whether or not to enable SIMD. This set of patches does not follow Andrew's later suggestion of maybe checking again every million lines or so.
>
> My input-generation script is not ready to share yet, but the inputs
> follow this format: text_${n}.input, where n represents the number of
> normal characters before the delimiter. For example:
>
> n = 0 -> "\n\n\n\n\n..." (no normal characters)
> n = 1 -> "a\n..." (1 normal character before the delimiter)
> ...
> n = 5 -> "aaaaa\n..."
> … continuing up to n = 32.
>
> Each line has 4096 chars and there are a total of 100000 lines in each
> input file.
>
> I only benchmarked the text format. I compared the latest heuristic I
> shared [1] with the current method. The benchmarks show roughly a ~16%
> regression at the worst case (n = 2), with regressions up to n = 5.
> For the remaining values, performance was similar.
I tried to improve the v4 patchset. My changes are:
1 - I changed CopyReadLineText() to an inline function and sent the
use_simd variable as an argument to get help from inlining.
2 - A main for loop in the CopyReadLineText() function is called many
times, so I moved the use_simd check to the CopyReadLine() function.
3 - Instead of 'bytes_processed', I used 'chars_processed' because
cstate->bytes_processed is increased before we process them and this
can cause wrong results.
4 - Because of #2 and #3, instead of having
'SPECIAL_CHAR_SIMD_THRESHOLD', I used the ratio of 'chars_processed /
special_chars_encountered' to determine whether we want to use SIMD.
5 - cstate->special_chars_encountered is incremented wrongly for the
CSV case. It is not incremented for the quote and escape delimiters. I
moved all increments of cstate->special_chars_encountered to the
central place and tried to optimize it but it still causes a
regression as it creates one more branching.
With these changes, I am able to decrease the regression to %10 from
%16. Regression decreases to %7 if I modify #5 for the only text input
but I did not do that.
My changes are in the 0003.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 6 Dec 2025 at 10:55, Bilal Yavuz <byavuz81@gmail.com> wrote:
>
> Hi,
>
> On Sat, 6 Dec 2025 at 04:40, Manni Wood <manni.wood@enterprisedb.com> wrote:
> > Hello, all.
> >
> > Andrew, I tried your suggestion of just reading the first chunk of the copy file to determine if SIMD is worth using. Attached are v4 versions of the patches showing a first attempt at doing that.
>
> Thank you for doing this!
>
> > I attached test.sh.txt to show how I've been testing, with 5 million lines of the various copy file variations introduced by Ayub Kazar.
> >
> > The text copy with no special chars is 30% faster. The CSV copy with no special chars is 48% faster. The text with 1/3rd escapes is 3% slower. The CSV with 1/3rd quotes is 0.27% slower.
> >
> > This set of patches follows the simplest suggestion of just testing the first N lines (actually first N bytes) of the file and then deciding whether or not to enable SIMD. This set of patches does not follow Andrew's later suggestion of maybe checking again every million lines or so.
>
> My input-generation script is not ready to share yet, but the inputs
> follow this format: text_${n}.input, where n represents the number of
> normal characters before the delimiter. For example:
>
> n = 0 -> "\n\n\n\n\n..." (no normal characters)
> n = 1 -> "a\n..." (1 normal character before the delimiter)
> ...
> n = 5 -> "aaaaa\n..."
> … continuing up to n = 32.
>
> Each line has 4096 chars and there are a total of 100000 lines in each
> input file.
>
> I only benchmarked the text format. I compared the latest heuristic I
> shared [1] with the current method. The benchmarks show roughly a ~16%
> regression at the worst case (n = 2), with regressions up to n = 5.
> For the remaining values, performance was similar.
I tried to improve the v4 patchset. My changes are:
1 - I changed CopyReadLineText() to an inline function and sent the
use_simd variable as an argument to get help from inlining.
2 - A main for loop in the CopyReadLineText() function is called many
times, so I moved the use_simd check to the CopyReadLine() function.
3 - Instead of 'bytes_processed', I used 'chars_processed' because
cstate->bytes_processed is increased before we process them and this
can cause wrong results.
4 - Because of #2 and #3, instead of having
'SPECIAL_CHAR_SIMD_THRESHOLD', I used the ratio of 'chars_processed /
special_chars_encountered' to determine whether we want to use SIMD.
5 - cstate->special_chars_encountered is incremented wrongly for the
CSV case. It is not incremented for the quote and escape delimiters. I
moved all increments of cstate->special_chars_encountered to the
central place and tried to optimize it but it still causes a
regression as it creates one more branching.
With these changes, I am able to decrease the regression to %10 from
%16. Regression decreases to %7 if I modify #5 for the only text input
but I did not do that.
My changes are in the 0003.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 10 Dec 2025 at 01:13, Manni Wood wrote: > > Bilal Yavuz (Nazir Bilal Yavuz?), It is Nazir Bilal Yavuz, I changed some settings on my phone and it seems that it affected my mail account, hopefully it should be fixed now. > I did not get a chance to do any work on this today, but wanted to thank you for finding my logic errors in counting special chars for CSV, and hacking on my naive solution to make it faster. By attempting Andrew Dunstan's suggestion, I got a better feel for the reality that the "housekeeping" code produces a significant amount of overhead. You are welcome! v4.1 has some problems with in_quote case in SIMD handling code and counting cstate->chars_processed variable. I fixed them in v4.2. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi everyone,
On Tue, Dec 09, 2025 at 04:40:19PM +0300, Bilal Yavuz wrote:
> Hi,
>
> On Sat, 6 Dec 2025 at 10:55, Bilal Yavuz wrote:
> >
> > Hi,
> >
> > On Sat, 6 Dec 2025 at 04:40, Manni Wood wrote:
> > > Hello, all.
> > >
> > > Andrew, I tried your suggestion of just reading the first chunk of the copy file to determine if SIMD is worth using. Attached are v4 versions of the patches showing a first attempt at doing that.
> >
> > Thank you for doing this!
> >
> > > I attached test.sh.txt to show how I've been testing, with 5 million lines of the various copy file variations introduced by Ayub Kazar.
> > >
> > > The text copy with no special chars is 30% faster. The CSV copy with no special chars is 48% faster. The text with 1/3rd escapes is 3% slower. The CSV with 1/3rd quotes is 0.27% slower.
> > >
> > > This set of patches follows the simplest suggestion of just testing the first N lines (actually first N bytes) of the file and then deciding whether or not to enable SIMD. This set of patches does not follow Andrew's later suggestion of maybe checking again every million lines or so.
> >
> > My input-generation script is not ready to share yet, but the inputs
> > follow this format: text_${n}.input, where n represents the number of
> > normal characters before the delimiter. For example:
> >
> > n = 0 -> "\n\n\n\n\n..." (no normal characters)
> > n = 1 -> "a\n..." (1 normal character before the delimiter)
> > ...
> > n = 5 -> "aaaaa\n..."
> > … continuing up to n = 32.
> >
> > Each line has 4096 chars and there are a total of 100000 lines in each
> > input file.
> >
> > I only benchmarked the text format. I compared the latest heuristic I
> > shared [1] with the current method. The benchmarks show roughly a ~16%
> > regression at the worst case (n = 2), with regressions up to n = 5.
> > For the remaining values, performance was similar.
>
> I tried to improve the v4 patchset. My changes are:
>
> 1 - I changed CopyReadLineText() to an inline function and sent the
> use_simd variable as an argument to get help from inlining.
>
> 2 - A main for loop in the CopyReadLineText() function is called many
> times, so I moved the use_simd check to the CopyReadLine() function.
>
> 3 - Instead of 'bytes_processed', I used 'chars_processed' because
> cstate->bytes_processed is increased before we process them and this
> can cause wrong results.
>
> 4 - Because of #2 and #3, instead of having
> 'SPECIAL_CHAR_SIMD_THRESHOLD', I used the ratio of 'chars_processed /
> special_chars_encountered' to determine whether we want to use SIMD.
>
> 5 - cstate->special_chars_encountered is incremented wrongly for the
> CSV case. It is not incremented for the quote and escape delimiters. I
> moved all increments of cstate->special_chars_encountered to the
> central place and tried to optimize it but it still causes a
> regression as it creates one more branching.
>
> With these changes, I am able to decrease the regression to %10 from
> %16. Regression decreases to %7 if I modify #5 for the only text input
> but I did not do that.
>
> My changes are in the 0003.
I was helping collect some data, but I'm a little behind sharing what I
ran against the v4.1 patches (on commit 07961ef8) with the v4.2 version
out there...
I hope it's still helpfule that I share what I collected even though
they are not quite as nice, but maybe it's more about how/where I ran
them.
My laptop has a Intel(R) Core(TM) Ultra 7 165H, where most of these
tests were using up 95%+ of one of the cores (I have hyperthreading
disabled), and using about 10% the ssd's capacity.
Summarizing my results from the same script Manni ran, I didn't see as
much as an improvement in the positive tests, and then saw more negative
results in the other tests.
text copy with no special chars: 18% improvement of 15s from 80s before
the patch
CSV copy with no special chars: 23% improvement of 23s from 96s before
the patch
text with 1/3rd escapes: 6% slower, an additional 5s to 85 seconds
before the patch
CSV with 1/3rd quotes: 7% slower, an additional 10 seconds to 129
seconds before the patch
I'm wondering if my laptop/processor isn't the best test bed for this...
Regards,
Mark
--
Mark Wong
EDB https://enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi everyone,
On Tue, Dec 09, 2025 at 04:40:19PM +0300, Bilal Yavuz wrote:
> Hi,
>
> On Sat, 6 Dec 2025 at 10:55, Bilal Yavuz <byavuz81@gmail.com> wrote:
> >
> > Hi,
> >
> > On Sat, 6 Dec 2025 at 04:40, Manni Wood <manni.wood@enterprisedb.com> wrote:
> > > Hello, all.
> > >
> > > Andrew, I tried your suggestion of just reading the first chunk of the copy file to determine if SIMD is worth using. Attached are v4 versions of the patches showing a first attempt at doing that.
> >
> > Thank you for doing this!
> >
> > > I attached test.sh.txt to show how I've been testing, with 5 million lines of the various copy file variations introduced by Ayub Kazar.
> > >
> > > The text copy with no special chars is 30% faster. The CSV copy with no special chars is 48% faster. The text with 1/3rd escapes is 3% slower. The CSV with 1/3rd quotes is 0.27% slower.
> > >
> > > This set of patches follows the simplest suggestion of just testing the first N lines (actually first N bytes) of the file and then deciding whether or not to enable SIMD. This set of patches does not follow Andrew's later suggestion of maybe checking again every million lines or so.
> >
> > My input-generation script is not ready to share yet, but the inputs
> > follow this format: text_${n}.input, where n represents the number of
> > normal characters before the delimiter. For example:
> >
> > n = 0 -> "\n\n\n\n\n..." (no normal characters)
> > n = 1 -> "a\n..." (1 normal character before the delimiter)
> > ...
> > n = 5 -> "aaaaa\n..."
> > … continuing up to n = 32.
> >
> > Each line has 4096 chars and there are a total of 100000 lines in each
> > input file.
> >
> > I only benchmarked the text format. I compared the latest heuristic I
> > shared [1] with the current method. The benchmarks show roughly a ~16%
> > regression at the worst case (n = 2), with regressions up to n = 5.
> > For the remaining values, performance was similar.
>
> I tried to improve the v4 patchset. My changes are:
>
> 1 - I changed CopyReadLineText() to an inline function and sent the
> use_simd variable as an argument to get help from inlining.
>
> 2 - A main for loop in the CopyReadLineText() function is called many
> times, so I moved the use_simd check to the CopyReadLine() function.
>
> 3 - Instead of 'bytes_processed', I used 'chars_processed' because
> cstate->bytes_processed is increased before we process them and this
> can cause wrong results.
>
> 4 - Because of #2 and #3, instead of having
> 'SPECIAL_CHAR_SIMD_THRESHOLD', I used the ratio of 'chars_processed /
> special_chars_encountered' to determine whether we want to use SIMD.
>
> 5 - cstate->special_chars_encountered is incremented wrongly for the
> CSV case. It is not incremented for the quote and escape delimiters. I
> moved all increments of cstate->special_chars_encountered to the
> central place and tried to optimize it but it still causes a
> regression as it creates one more branching.
>
> With these changes, I am able to decrease the regression to %10 from
> %16. Regression decreases to %7 if I modify #5 for the only text input
> but I did not do that.
>
> My changes are in the 0003.
I was helping collect some data, but I'm a little behind sharing what I
ran against the v4.1 patches (on commit 07961ef8) with the v4.2 version
out there...
I hope it's still helpfule that I share what I collected even though
they are not quite as nice, but maybe it's more about how/where I ran
them.
My laptop has a Intel(R) Core(TM) Ultra 7 165H, where most of these
tests were using up 95%+ of one of the cores (I have hyperthreading
disabled), and using about 10% the ssd's capacity.
Summarizing my results from the same script Manni ran, I didn't see as
much as an improvement in the positive tests, and then saw more negative
results in the other tests.
text copy with no special chars: 18% improvement of 15s from 80s before
the patch
CSV copy with no special chars: 23% improvement of 23s from 96s before
the patch
text with 1/3rd escapes: 6% slower, an additional 5s to 85 seconds
before the patch
CSV with 1/3rd quotes: 7% slower, an additional 10 seconds to 129
seconds before the patch
I'm wondering if my laptop/processor isn't the best test bed for this...
Regards,
Mark
--
Mark Wong <markwkm@gmail.com>
EDB https://enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 13 Dec 2025 at 02:09, Manni Wood wrote:
>
> Hello, Everyone!
>
> I have attached two files. 1) the shell script that Mark and I have been using to get our test results, and 2) a screenshot of a spreadsheet of my latest test results. (Please let me know if there's a different format than a screenshot that I could share my spreadsheet in.)
>
> I took greater care this time to compile all three variants of Postgres (master at bfb335df, master at bfb335df with v4.2 patches installed, master at bfb335df with v3 patches installed) with the same gcc optimization flags that would be used to build Postgres packages. To the best of my knowledge, the two gcc flags of greatest interest would be -g and -O2. I built all three variants of Postgres using meson like so:
>
> BRANCH=$(git branch --show-current)
> meson setup build --prefix=/home/mwood/compiled-pg-instances/${BRANCH} --buildtype=debugoptimized
>
> It occurred to me that in addition to end users only caring about 1) wall clock time (is the speedup noticeable in "real time" or just technically faster / uses less CPU?) and 2) Postgres binaries compiled with the same optimization level one would get when installing Postgres from packages like .deb or .rpm; in other words, will the user see speedups without having do manually compile postgres.
>
> My interesting finding, on my laptop (ThinkPad P14s Gen 1 running Ubuntu 24.04.3), is different from Mark Wong's. On my laptop, using three Postgres installations all compiled with the -O2 optimization flag, I see speedups with the v4.2 patch except for a 2% slowdown with CSV with 1/3rd quotes (a 2% slowdown). But with Nazir's proposed v3 patch, I see improvements across the board. So even for a text file with 1/3rd escape characters, and even with a CSV file with 1/3rd quotes, I see speedups of 11% and 26% respectively.
>
> The format of these test files originally comes from Ayoub Kazar's test scripts; all Mark and I have done in playing with them is make them much larger: 5,000,000 rows, based on the assumption that longer tests are better tests.
>
> I find my results interesting enough that I'd be curious to know if anybody else can reproduce them. It is very interesting that Mark's results are noticeably different from mine.
Thank you for sharing the benchmark script! I ran the benchmarks using
your script with --buildtype=debugoptimized. My results are below:
master: 85ddcc2f4c
text, no special: 102294
text, 1/3 special: 108946
csv, no special: 121831
csv, 1/3 special: 140063
v3
text, no special: 88890 (13.1% speedup)
text, 1/3 special: 110463 (1.4% regression)
csv, no special: 89781 (26.3% speedup)
csv, 1/3 special: 147094 (5.0% regression)
v4.2
text, no special: 87785 (14.2% speedup)
text, 1/3 special: 127008 (16.6% regression)
csv, no special: 88093 (27.7% speedup)
csv, 1/3 special: 164487 (17.4% regression)
One thing I noticed is that your benchmark timings appear to have some
variance. In my runs, I did not observe differences greater than one
second between runs. It is possible that this variance is affecting
your results.
Before running the benchmarks, I use the these commands [1] to improve
result stability; they might be helpful if you are not already using
something similar:
I did this benchmark on my local and my specs are Intel i5 13600k,
32GB Memory and SATA SSD.
[1]
sudo cpupower frequency-set --governor=performance
sudo cpupower idle-set -D 0 # disable idle
echo "1" | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo (intel only)
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Regards,
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello,Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this.I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again...There's two ways to do this:1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again.- This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars).2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time.- This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars).Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM.So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial.Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one.
Regards,Ayoub
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 24 Dec 2025 at 18:08, KAZAR Ayoub wrote: > > Hello, > Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this. > > I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again... > There's two ways to do this: > 1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again. > - This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars). > > 2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time. > - This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars). > > Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM. > > So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial. > Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one. Patches look correct to me. I think we could move these SIMD code portions into a shared function to remove duplication, although that might have a performance impact. I have not benchmarked these patches yet. Another consideration is that these patches might need their own thread, though I am not completely sure about this yet. One question: what do you think about having a 0004-style approach for COPY FROM? What I have in mind is running SIMD for each line & column, stopping SIMD once it can no longer skip an entire chunk, and then continuing with the next line & column. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Wed, 24 Dec 2025 at 18:08, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> Hello,
> Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this.
>
> I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again...
> There's two ways to do this:
> 1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again.
> - This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars).
>
> 2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time.
> - This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars).
>
> Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM.
>
> So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial.
> Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one.
Patches look correct to me. I think we could move these SIMD code
portions into a shared function to remove duplication, although that
might have a performance impact. I have not benchmarked these patches
yet.
Another consideration is that these patches might need their own
thread, though I am not completely sure about this yet.
One question: what do you think about having a 0004-style approach for
COPY FROM? What I have in mind is running SIMD for each line & column,
stopping SIMD once it can no longer skip an entire chunk, and then
continuing with the next line & column.
--
Regards,
Nazir Bilal Yavuz
Microsoft
This time, I ran the tests on my Tower PC instead of on my laptop.
I also followed Mark Wong's advice and used the taskset command to pin my postgres postmaster (and all of its children) to a single cpu core.
So when I start postgres, I do this to pin it to core 27:
${PGHOME}/bin/pg_ctl -D ${PGHOME}/data -l ${PGHOME}/logfile.txt start
PGPID=$(head -1 ${PGHOME}/data/postmaster.pid)
taskset --cpu-list -p 27 ${PGPID}
My results seem similar to yours:
master: Nazir 85ddcc2f4c | Manni 877ae5db
text, no special: 102294 | 302651
text, 1/3 special: 108946 | 326208
csv, no special: 121831 | 348930
csv, 1/3 special: 140063 | 439786
v3
text, no special: 88890 (13.1% speedup) | 227874 (24.7% speedup)
text, 1/3 special: 110463 (1.4% regression) | 322637 (1.1% speedup)
csv, no special: 89781 (26.3% speedup) | 226525 (35.1% speedup)
csv, 1/3 special: 147094 (5.0% regression) | 461501 (4.9% regression)
v4.2
text, no special: 87785 (14.2% speedup) | 225702 (25.4% speedup)
text, 1/3 special: 127008 (16.6% regression) | 343480 (5.3% regression)
csv, no special: 88093 (27.7% speedup) | 226633 (35.0% speedup)
csv, 1/3 special: 164487 (17.4% regression) | 510954 (16.2% regression)
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Dec 31, 2025 at 7:04 AM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
On Wed, 24 Dec 2025 at 18:08, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> Hello,
> Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this.
>
> I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again...
> There's two ways to do this:
> 1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again.
> - This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars).
>
> 2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time.
> - This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars).
>
> Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM.
>
> So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial.
> Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one.
Patches look correct to me. I think we could move these SIMD code
portions into a shared function to remove duplication, although that
might have a performance impact. I have not benchmarked these patches
yet.
Another consideration is that these patches might need their own
thread, though I am not completely sure about this yet.
One question: what do you think about having a 0004-style approach for
COPY FROM? What I have in mind is running SIMD for each line & column,
stopping SIMD once it can no longer skip an entire chunk, and then
continuing with the next line & column.
--
Regards,
Nazir Bilal Yavuz
MicrosoftHello, Nazir, I tried your suggested cpupower commands as well as disabling turbo, and my results are indeed more uniform. (see attached screenshot of my spreadsheet).
This time, I ran the tests on my Tower PC instead of on my laptop.
I also followed Mark Wong's advice and used the taskset command to pin my postgres postmaster (and all of its children) to a single cpu core.
So when I start postgres, I do this to pin it to core 27:
${PGHOME}/bin/pg_ctl -D ${PGHOME}/data -l ${PGHOME}/logfile.txt start
PGPID=$(head -1 ${PGHOME}/data/postmaster.pid)
taskset --cpu-list -p 27 ${PGPID}
My results seem similar to yours:
master: Nazir 85ddcc2f4c | Manni 877ae5db
text, no special: 102294 | 302651
text, 1/3 special: 108946 | 326208
csv, no special: 121831 | 348930
csv, 1/3 special: 140063 | 439786
v3
text, no special: 88890 (13.1% speedup) | 227874 (24.7% speedup)
text, 1/3 special: 110463 (1.4% regression) | 322637 (1.1% speedup)
csv, no special: 89781 (26.3% speedup) | 226525 (35.1% speedup)
csv, 1/3 special: 147094 (5.0% regression) | 461501 (4.9% regression)
v4.2
text, no special: 87785 (14.2% speedup) | 225702 (25.4% speedup)
text, 1/3 special: 127008 (16.6% regression) | 343480 (5.3% regression)
csv, no special: 88093 (27.7% speedup) | 226633 (35.0% speedup)
csv, 1/3 special: 164487 (17.4% regression) | 510954 (16.2% regression)It would seem that both your results and mine show a more serious worst-case regression for the v4.2 patches than for the v3 patches. It seems also that the speedups for v4.2 and v3 are similar.I'm currently working with Mark Wong to see if his results continue to be dissimilar (as they currently are now) and, if so, why.---- Manni Wood EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Jan 6, 2026 at 2:05 PM Manni Wood <manni.wood@enterprisedb.com> wrote:On Wed, Dec 31, 2025 at 7:04 AM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
On Wed, 24 Dec 2025 at 18:08, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> Hello,
> Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this.
>
> I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again...
> There's two ways to do this:
> 1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again.
> - This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars).
>
> 2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time.
> - This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars).
>
> Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM.
>
> So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial.
> Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one.
Patches look correct to me. I think we could move these SIMD code
portions into a shared function to remove duplication, although that
might have a performance impact. I have not benchmarked these patches
yet.
Another consideration is that these patches might need their own
thread, though I am not completely sure about this yet.
One question: what do you think about having a 0004-style approach for
COPY FROM? What I have in mind is running SIMD for each line & column,
stopping SIMD once it can no longer skip an entire chunk, and then
continuing with the next line & column.
--
Regards,
Nazir Bilal Yavuz
MicrosoftHello, Nazir, I tried your suggested cpupower commands as well as disabling turbo, and my results are indeed more uniform. (see attached screenshot of my spreadsheet).
This time, I ran the tests on my Tower PC instead of on my laptop.
I also followed Mark Wong's advice and used the taskset command to pin my postgres postmaster (and all of its children) to a single cpu core.
So when I start postgres, I do this to pin it to core 27:
${PGHOME}/bin/pg_ctl -D ${PGHOME}/data -l ${PGHOME}/logfile.txt start
PGPID=$(head -1 ${PGHOME}/data/postmaster.pid)
taskset --cpu-list -p 27 ${PGPID}
My results seem similar to yours:
master: Nazir 85ddcc2f4c | Manni 877ae5db
text, no special: 102294 | 302651
text, 1/3 special: 108946 | 326208
csv, no special: 121831 | 348930
csv, 1/3 special: 140063 | 439786
v3
text, no special: 88890 (13.1% speedup) | 227874 (24.7% speedup)
text, 1/3 special: 110463 (1.4% regression) | 322637 (1.1% speedup)
csv, no special: 89781 (26.3% speedup) | 226525 (35.1% speedup)
csv, 1/3 special: 147094 (5.0% regression) | 461501 (4.9% regression)
v4.2
text, no special: 87785 (14.2% speedup) | 225702 (25.4% speedup)
text, 1/3 special: 127008 (16.6% regression) | 343480 (5.3% regression)
csv, no special: 88093 (27.7% speedup) | 226633 (35.0% speedup)
csv, 1/3 special: 164487 (17.4% regression) | 510954 (16.2% regression)It would seem that both your results and mine show a more serious worst-case regression for the v4.2 patches than for the v3 patches. It seems also that the speedups for v4.2 and v3 are similar.I'm currently working with Mark Wong to see if his results continue to be dissimilar (as they currently are now) and, if so, why.---- Manni Wood EDB: https://www.enterprisedb.comHello, all.Now that I am following Nazir's on how to configure my CPU for performance test run, and now that I am following Mark's advice on pinning the postmaster to a particular CPU core, I figured I would share the scripts I have been using to build, run, and test Postges with various patches applied: https://github.com/manniwood/copysimdperfWith Nazir and Mark's tips, I have seen more consistent numbers on my tower PC, as shared in a previous e-mail. But Mark and I saw rather variable results on a different Linux system he has access to. So this has inspired me to spin up an AWS EC2 instance and test that when I find the time. And maybe re-test on my Linux laptop.If anybody else is inspired to test on different setups, that would be great.---- Manni Wood EDB: https://www.enterprisedb.com
text, no special: 30372
text, 1/3 special: 32665
csv, no special: 34925
csv, 1/3 special: 44044
v3
text, no special: 22840 (24.7% speedup)
text, 1/3 special: 32448 (0.6% speedup)
csv, no special: 22642 (35.1% speedup)
csv, 1/3 special: 46280 (5.1% regression)
v4.2
text, no special: 22677 (25.3% speedup)
text, 1/3 special: 34512 (6.5% regression)
csv, no special: 22686 (35.0% speedup)
csv, 1/3 special: 51411 (16.7% regression)
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Jan 7, 2026 at 1:13 PM Manni Wood <manni.wood@enterprisedb.com> wrote:On Tue, Jan 6, 2026 at 2:05 PM Manni Wood <manni.wood@enterprisedb.com> wrote:On Wed, Dec 31, 2025 at 7:04 AM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
On Wed, 24 Dec 2025 at 18:08, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> Hello,
> Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this.
>
> I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again...
> There's two ways to do this:
> 1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again.
> - This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars).
>
> 2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time.
> - This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars).
>
> Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM.
>
> So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial.
> Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one.
Patches look correct to me. I think we could move these SIMD code
portions into a shared function to remove duplication, although that
might have a performance impact. I have not benchmarked these patches
yet.
Another consideration is that these patches might need their own
thread, though I am not completely sure about this yet.
One question: what do you think about having a 0004-style approach for
COPY FROM? What I have in mind is running SIMD for each line & column,
stopping SIMD once it can no longer skip an entire chunk, and then
continuing with the next line & column.
--
Regards,
Nazir Bilal Yavuz
MicrosoftHello, Nazir, I tried your suggested cpupower commands as well as disabling turbo, and my results are indeed more uniform. (see attached screenshot of my spreadsheet).
This time, I ran the tests on my Tower PC instead of on my laptop.
I also followed Mark Wong's advice and used the taskset command to pin my postgres postmaster (and all of its children) to a single cpu core.
So when I start postgres, I do this to pin it to core 27:
${PGHOME}/bin/pg_ctl -D ${PGHOME}/data -l ${PGHOME}/logfile.txt start
PGPID=$(head -1 ${PGHOME}/data/postmaster.pid)
taskset --cpu-list -p 27 ${PGPID}
My results seem similar to yours:
master: Nazir 85ddcc2f4c | Manni 877ae5db
text, no special: 102294 | 302651
text, 1/3 special: 108946 | 326208
csv, no special: 121831 | 348930
csv, 1/3 special: 140063 | 439786
v3
text, no special: 88890 (13.1% speedup) | 227874 (24.7% speedup)
text, 1/3 special: 110463 (1.4% regression) | 322637 (1.1% speedup)
csv, no special: 89781 (26.3% speedup) | 226525 (35.1% speedup)
csv, 1/3 special: 147094 (5.0% regression) | 461501 (4.9% regression)
v4.2
text, no special: 87785 (14.2% speedup) | 225702 (25.4% speedup)
text, 1/3 special: 127008 (16.6% regression) | 343480 (5.3% regression)
csv, no special: 88093 (27.7% speedup) | 226633 (35.0% speedup)
csv, 1/3 special: 164487 (17.4% regression) | 510954 (16.2% regression)It would seem that both your results and mine show a more serious worst-case regression for the v4.2 patches than for the v3 patches. It seems also that the speedups for v4.2 and v3 are similar.I'm currently working with Mark Wong to see if his results continue to be dissimilar (as they currently are now) and, if so, why.---- Manni Wood EDB: https://www.enterprisedb.comHello, all.Now that I am following Nazir's on how to configure my CPU for performance test run, and now that I am following Mark's advice on pinning the postmaster to a particular CPU core, I figured I would share the scripts I have been using to build, run, and test Postges with various patches applied: https://github.com/manniwood/copysimdperfWith Nazir and Mark's tips, I have seen more consistent numbers on my tower PC, as shared in a previous e-mail. But Mark and I saw rather variable results on a different Linux system he has access to. So this has inspired me to spin up an AWS EC2 instance and test that when I find the time. And maybe re-test on my Linux laptop.If anybody else is inspired to test on different setups, that would be great.---- Manni Wood EDB: https://www.enterprisedb.comI tested master (bfb335d) and v3 and v4.2 patches on an amazon ec2 instance (t2.small) and, with Mark's help, proved that on such a small system with default storage configured, IO will be the bottleneck and the v3 and v4.2 patches show no significant differences over master because the CPU is always waiting on IO. This is presumably an experience Postgres users will have when running on systems with IO so slow that the CPU is always waiting for data.I went in the other direction and tested an all-RAM setup on my tower PC. I put the entire data dir in RAM for each postgres instance (master, v3 patch, v4.2 patch), and wrote and copied the test copyfiles from RAM. On Linux, /ram/user/<myuserid> is tmpfs (ramdisk), so I just put everything there. I had to shrink the data sizes compared to previous runs (to not run out of ramdisk space) but Nazir's cpupower tips are making all of my test runs much more uniform, so I no longer feel that I need huge data sizes to get good results.Here are the results when all of the files are on RAM disks:master: bfb335df
text, no special: 30372
text, 1/3 special: 32665
csv, no special: 34925
csv, 1/3 special: 44044
v3
text, no special: 22840 (24.7% speedup)
text, 1/3 special: 32448 (0.6% speedup)
csv, no special: 22642 (35.1% speedup)
csv, 1/3 special: 46280 (5.1% regression)
v4.2
text, no special: 22677 (25.3% speedup)
text, 1/3 special: 34512 (6.5% regression)
csv, no special: 22686 (35.0% speedup)
csv, 1/3 special: 51411 (16.7% regression)Assuming all-storage-is-RAM setups get us closer to the theoretical limit of each patch, it looks like v3 holds up quite well to v4.2 in the best case scenarios, while v3 has better performance than v4.2 in the worst-case scenarios.Let me know what you think!---- Manni Wood EDB: https://www.enterprisedb.com
text, no special: 8165
text, 1/3 special: 22662
csv, no special: 9619
csv, 1/3 special: 23213
v4 (copy to)
text, no special: 4577 (43.9% speedup)
text, 1/3 special: 22847 (0.8% regression)
csv, no special: 4720 (50.9% speedup)
csv, 1/3 special: 23195 (0.07% regression)
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
Firstly, thank you for all of the benchmarks!
On Tue, 6 Jan 2026 at 23:05, Manni Wood wrote:
>
> Hello, Nazir, I tried your suggested cpupower commands as well as disabling turbo, and my results are indeed more uniform. (see attached screenshot of my spreadsheet).
I am glad that it helped!
> This time, I ran the tests on my Tower PC instead of on my laptop.
>
> I also followed Mark Wong's advice and used the taskset command to pin my postgres postmaster (and all of its children) to a single cpu core.
That is nice advice, I should apply this. Thank you for sharing it.
> So when I start postgres, I do this to pin it to core 27:
>
> ${PGHOME}/bin/pg_ctl -D ${PGHOME}/data -l ${PGHOME}/logfile.txt start
> PGPID=$(head -1 ${PGHOME}/data/postmaster.pid)
> taskset --cpu-list -p 27 ${PGPID}
>
>
> My results seem similar to yours:
It is nice to see that we get similar results.
> master: Nazir 85ddcc2f4c | Manni 877ae5db
>
> text, no special: 102294 | 302651
> text, 1/3 special: 108946 | 326208
> csv, no special: 121831 | 348930
> csv, 1/3 special: 140063 | 439786
>
> v3
>
> text, no special: 88890 (13.1% speedup) | 227874 (24.7% speedup)
> text, 1/3 special: 110463 (1.4% regression) | 322637 (1.1% speedup)
> csv, no special: 89781 (26.3% speedup) | 226525 (35.1% speedup)
> csv, 1/3 special: 147094 (5.0% regression) | 461501 (4.9% regression)
>
> v4.2
>
> text, no special: 87785 (14.2% speedup) | 225702 (25.4% speedup)
> text, 1/3 special: 127008 (16.6% regression) | 343480 (5.3% regression)
> csv, no special: 88093 (27.7% speedup) | 226633 (35.0% speedup)
> csv, 1/3 special: 164487 (17.4% regression) | 510954 (16.2% regression)
>
> It would seem that both your results and mine show a more serious worst-case regression for the v4.2 patches than for the v3 patches. It seems also that the speedups for v4.2 and v3 are similar.
Yes, you are right. Also, the regression on the CSV is worse than
TEXT, do you have any idea why?
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 7 Jan 2026 at 22:13, Manni Wood wrote: > > Now that I am following Nazir's on how to configure my CPU for performance test run, and now that I am following Mark's advice on pinning the postmaster to a particular CPU core, I figured I would share the scripts I have been using to build, run, and test Postges with various patches applied: https://github.com/manniwood/copysimdperf Thank you for sharing this! I will try to use it soon. > With Nazir and Mark's tips, I have seen more consistent numbers on my tower PC, as shared in a previous e-mail. But Mark and I saw rather variable results on a different Linux system he has access to. So this has inspired me to spin up an AWS EC2 instance and test that when I find the time. And maybe re-test on my Linux laptop. Were you able to understand why Mark's benchmark results are different from ours? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 8 Jan 2026 at 23:50, Manni Wood wrote: > > I tested master (bfb335d) and v3 and v4.2 patches on an amazon ec2 instance (t2.small) and, with Mark's help, proved that on such a small system with default storage configured, IO will be the bottleneck and the v3 and v4.2 patches show no significant differences over master because the CPU is always waiting on IO. This is presumably an experience Postgres users will have when running on systems with IO so slow that the CPU is always waiting for data. I think this is expected and acceptable since there is no regression. > I went in the other direction and tested an all-RAM setup on my tower PC. I put the entire data dir in RAM for each postgres instance (master, v3 patch, v4.2 patch), and wrote and copied the test copyfiles from RAM. On Linux, /ram/user/ is tmpfs (ramdisk), so I just put everything there. I had to shrink the data sizes compared to previous runs (to not run out of ramdisk space) but Nazir's cpupower tips are making all of my test runs much more uniform, so I no longer feel that I need huge data sizes to get good results. This is an informative scenario to test, thank you for doing this. > Here are the results when all of the files are on RAM disks: > > master: bfb335df > > text, no special: 30372 > text, 1/3 special: 32665 > csv, no special: 34925 > csv, 1/3 special: 44044 > > v3 > > text, no special: 22840 (24.7% speedup) > text, 1/3 special: 32448 (0.6% speedup) > csv, no special: 22642 (35.1% speedup) > csv, 1/3 special: 46280 (5.1% regression) > > v4.2 > > text, no special: 22677 (25.3% speedup) > text, 1/3 special: 34512 (6.5% regression) > csv, no special: 22686 (35.0% speedup) > csv, 1/3 special: 51411 (16.7% regression) > > Assuming all-storage-is-RAM setups get us closer to the theoretical limit of each patch, it looks like v3 holds up quite well to v4.2 in the best case scenarios, while v3 has better performance than v4.2 in the worst-case scenarios.! I agree with you, I think all-storage-is-RAM is the best scenario to benchmark regressions. Your all-storage-is-RAM benchmark results are similar to your normal benchmark here [1]. I expect the regressions in the all-storage-is-RAM results to be worse than [1] since the effect of SIMD will be more visible as there is no IO wait. What do you think about that? [1] https://postgr.es/m/CAKWEB6qQmPejLwndzMcVqhO%2Bu9vgUf44kYAKg4QtyG-%3DKH%3DYGg%40mail.gmail.com -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Jan 09, 2026 at 05:21:45PM +0300, Nazir Bilal Yavuz wrote: > Were you able to understand why Mark's benchmark results are different > from ours? Not yet... I had some guesses, which is why I suggested the processor pinning and using a ramdisk. But we haven't tried applying all of those to my laptop, which has 3 core types, or the POWER system, which may be interesting to use a ram disk on. I'm curious though, and admittedly haven't tried looking myself yet, about how the SIMD calls might look across different processor architectures. We'll try to get that on the POWER system soon... Regards, Mark -- Mark Wong EDB https://enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Jan 09, 2026 at 05:21:45PM +0300, Nazir Bilal Yavuz wrote:
> Were you able to understand why Mark's benchmark results are different
> from ours?
Not yet... I had some guesses, which is why I suggested the processor pinning
and using a ramdisk. But we haven't tried applying all of those to my laptop,
which has 3 core types, or the POWER system, which may be interesting to use a
ram disk on.
I'm curious though, and admittedly haven't tried looking myself yet, about how
the SIMD calls might look across different processor architectures. We'll try
to get that on the POWER system soon...
Regards,
Mark
--
Mark Wong <markwkm@gmail.com>
EDB https://enterprisedb.com
text, no special: 14996
text, 1/3 special: 17270
csv, no special: 18274
csv, 1/3 special: 23852
v3
text, no special: 11282 (24.7% speedup)
text, 1/3 special: 15748 (8.8% speedup) <-- I don't believe this but it's what I got
csv, no special: 11571 (36.6% speedup)
csv, 1/3 special: 19934 (16.4% speedup) <-- I don't believe this but it's what I got
v4.2
text, no special: 11139 (25.7% speedup)
text, 1/3 special: 18900 (9.4% regression)
csv, no special: 11490 (37.1% speedup)
csv, 1/3 special: 26134 (9.5% regression)
text, no special: 20677
text, 1/3 special: 22660
csv, no special: 24534
csv, 1/3 special: 30999
v3
text, no special: 17534 (15.2% speedup)
text, 1/3 special: 22816 (0.6% regression)
csv, no special: 17664 (28.0% speedup)
csv, 1/3 special: 29338 (5.3% speedup) <-- I don't believe this but it's what I got
v4.2
text, no special: 17459 (15.5% speedup)
text, 1/3 special: 25051 (10.5% regression)
csv, no special: 17574 (28.3% speedup)
csv, 1/3 special: 32092 (3.5% regression)
text, no special: 22081
text, 1/3 special: 25100
csv, no special: 27296
csv, 1/3 special: 32344
v3
text, no special: 17724 (19.7% speedup)
text, 1/3 special: 27606 (9.9% regression) <-- yikes! We would want to test this more
csv, no special: 17597 (35.5% speedup)
csv, 1/3 special: 32597 (0.8% regression)
v4.2
text, no special: 17674 (20% speedup)
text, 1/3 special: 25773 (2.6% regression) <-- this regression is less than for the v3 patch? Atypical...
csv, no special: 17651 (35.3% speedup)
csv, 1/3 special: 34055 (5.3% regression)
text, no special: 2948
text, 1/3 special: 11258
csv, no special: 6245
csv, 1/3 special: 11258
v4 (copy to)
text, no special: 2126 (27.9% speedup)
text, 1/3 special: 12080 (7.3% regression) <-- did not see such a big regression before
csv, no special: 2432 (61.0% speedup)
csv, 1/3 special: 12344 (4.0% regression) <-- did not see such a big regression before
text, no special: 4647
text, 1/3 special: 13865
csv, no special: 5421
csv, 1/3 special: 15284
v4 (copy to)
text, no special: 2460 (47.0% speedup)
text, 1/3 special: 14023 (1.1% regression)
csv, no special: 2667 (50.7% speedup)
csv, 1/3 special: 15251 (0.2% speedup)
text, no special: 6951
text, 1/3 special: 17857
csv, no special: 7951
csv, 1/3 special: 18504
v4 (copy to)
text, no special: 3372 (51.4% speedup)
text, 1/3 special: 15713 (12.0% speedup)
csv, no special: 3233 (59.3% speedup)
csv, 1/3 special: 1622 (12.3% speedup)
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Wed, 24 Dec 2025 at 18:08, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> Hello,
> Following the same path of optimizing COPY FROM using SIMD, i found that COPY TO can also benefit from this.
>
> I attached a small patch that uses SIMD to skip data and advance as far as the first special character is found, then fallback to scalar processing for that character and re-enter the SIMD path again...
> There's two ways to do this:
> 1) Essentially we do SIMD until we find a special character, then continue scalar path without re-entering SIMD again.
> - This gives from 10% to 30% speedups depending on the weight of special characters in the attribute, we don't lose anything here since it advances with SIMD until it can't (using the previous scripts: 1/3, 2/3 specials chars).
>
> 2) Do SIMD path, then use scalar path when we hit a special character, keep re-entering the SIMD path each time.
> - This is equivalent to the COPY FROM story, we'll need to find the same heuristic to use for both COPY FROM/TO to reduce the regressions (same regressions: around from 20% to 30% with 1/3, 2/3 specials chars).
>
> Something else to note is that the scalar path for COPY TO isn't as heavy as the state machine in COPY FROM.
>
> So if we find the sweet spot for the heuristic, doing the same for COPY TO will be trivial and always beneficial.
> Attached is 0004 which is option 1 (SIMD without re-entering), 0005 is the second one.
Patches look correct to me. I think we could move these SIMD code
portions into a shared function to remove duplication, although that
might have a performance impact. I have not benchmarked these patches
yet.
Another consideration is that these patches might need their own
thread, though I am not completely sure about this yet.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello!Nazir, I'm glad you are finding the benchmarks useful. I have more! :-)All of these benchmarks are all-in-RAM, because I do think that is the best way of getting closest to the theoretical best and worst case scenarios.My laptop:master: (852558b9)
text, no special: 14996
text, 1/3 special: 17270
csv, no special: 18274
csv, 1/3 special: 23852
v3
text, no special: 11282 (24.7% speedup)
text, 1/3 special: 15748 (8.8% speedup) <-- I don't believe this but it's what I got
csv, no special: 11571 (36.6% speedup)
csv, 1/3 special: 19934 (16.4% speedup) <-- I don't believe this but it's what I got
v4.2
text, no special: 11139 (25.7% speedup)
text, 1/3 special: 18900 (9.4% regression)
csv, no special: 11490 (37.1% speedup)
csv, 1/3 special: 26134 (9.5% regression)An AWS EC2 t2.2xlarge instancemaster: (852558b9)
text, no special: 20677
text, 1/3 special: 22660
csv, no special: 24534
csv, 1/3 special: 30999
v3
text, no special: 17534 (15.2% speedup)
text, 1/3 special: 22816 (0.6% regression)
csv, no special: 17664 (28.0% speedup)
csv, 1/3 special: 29338 (5.3% speedup) <-- I don't believe this but it's what I got
v4.2
text, no special: 17459 (15.5% speedup)
text, 1/3 special: 25051 (10.5% regression)
csv, no special: 17574 (28.3% speedup)
csv, 1/3 special: 32092 (3.5% regression)An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM processor!)master: (852558b9)
text, no special: 22081
text, 1/3 special: 25100
csv, no special: 27296
csv, 1/3 special: 32344
v3
text, no special: 17724 (19.7% speedup)
text, 1/3 special: 27606 (9.9% regression) <-- yikes! We would want to test this more
csv, no special: 17597 (35.5% speedup)
csv, 1/3 special: 32597 (0.8% regression)
v4.2
text, no special: 17674 (20% speedup)
text, 1/3 special: 25773 (2.6% regression) <-- this regression is less than for the v3 patch? Atypical...
csv, no special: 17651 (35.3% speedup)
csv, 1/3 special: 34055 (5.3% regression)Yes, I think I agree with you that the everything-in-RAM benchmarks will make the regressions more pronounced, just like the everything-in-RAM benchmarks make the improvements more pronounced.I am not sure why the CSV regression, compared to the TXT regression (even for the v3 patch which has smaller regressions than the v4.2 patch) is usually worse. I probably should look over some flame graphs and see if I can find the place where the CSV-parsing code is so much slower. The CSV regression is actually a bit frustrating (at around 5%) because the TXT regression, at less than 1% (for the v3 patch) is so much easier to bare.
Here are some copy-to benchmarks for the v4 patch that applies SIMD to the copy-to code.These were all-in-RAM tests.My laptopmaster: (852558b9)
text, no special: 2948
text, 1/3 special: 11258
csv, no special: 6245
csv, 1/3 special: 11258
v4 (copy to)
text, no special: 2126 (27.9% speedup)
text, 1/3 special: 12080 (7.3% regression) <-- did not see such a big regression before
csv, no special: 2432 (61.0% speedup)
csv, 1/3 special: 12344 (4.0% regression) <-- did not see such a big regression beforeAn AWS EC2 t2.2xlarge instancemaster: (852558b9)
text, no special: 4647
text, 1/3 special: 13865
csv, no special: 5421
csv, 1/3 special: 15284
v4 (copy to)
text, no special: 2460 (47.0% speedup)
text, 1/3 special: 14023 (1.1% regression)
csv, no special: 2667 (50.7% speedup)
csv, 1/3 special: 15251 (0.2% speedup)An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM processor!)master: (852558b9)
text, no special: 6951
text, 1/3 special: 17857
csv, no special: 7951
csv, 1/3 special: 18504
v4 (copy to)
text, no special: 3372 (51.4% speedup)
text, 1/3 special: 15713 (12.0% speedup)
csv, no special: 3233 (59.3% speedup)
csv, 1/3 special: 1622 (12.3% speedup)Once again, the v4 patch for copy-to seems like a clearer win, though, to be fair, there were regressions when running on my laptop. (I'm starting to think servers or desktops are better than laptops for testing these things, though maybe that's my bias: it just seems like the server results are always less surprising.)
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello,Thank you for these benchmarks, this is helpful !On Wed, Jan 14, 2026 at 1:20 AM Manni Wood <manni.wood@enterprisedb.com> wrote:Hello!Nazir, I'm glad you are finding the benchmarks useful. I have more! :-)All of these benchmarks are all-in-RAM, because I do think that is the best way of getting closest to the theoretical best and worst case scenarios.My laptop:master: (852558b9)
text, no special: 14996
text, 1/3 special: 17270
csv, no special: 18274
csv, 1/3 special: 23852
v3
text, no special: 11282 (24.7% speedup)
text, 1/3 special: 15748 (8.8% speedup) <-- I don't believe this but it's what I got
csv, no special: 11571 (36.6% speedup)
csv, 1/3 special: 19934 (16.4% speedup) <-- I don't believe this but it's what I got
v4.2
text, no special: 11139 (25.7% speedup)
text, 1/3 special: 18900 (9.4% regression)
csv, no special: 11490 (37.1% speedup)
csv, 1/3 special: 26134 (9.5% regression)An AWS EC2 t2.2xlarge instancemaster: (852558b9)
text, no special: 20677
text, 1/3 special: 22660
csv, no special: 24534
csv, 1/3 special: 30999
v3
text, no special: 17534 (15.2% speedup)
text, 1/3 special: 22816 (0.6% regression)
csv, no special: 17664 (28.0% speedup)
csv, 1/3 special: 29338 (5.3% speedup) <-- I don't believe this but it's what I got
v4.2
text, no special: 17459 (15.5% speedup)
text, 1/3 special: 25051 (10.5% regression)
csv, no special: 17574 (28.3% speedup)
csv, 1/3 special: 32092 (3.5% regression)An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM processor!)master: (852558b9)
text, no special: 22081
text, 1/3 special: 25100
csv, no special: 27296
csv, 1/3 special: 32344
v3
text, no special: 17724 (19.7% speedup)
text, 1/3 special: 27606 (9.9% regression) <-- yikes! We would want to test this more
csv, no special: 17597 (35.5% speedup)
csv, 1/3 special: 32597 (0.8% regression)
v4.2
text, no special: 17674 (20% speedup)
text, 1/3 special: 25773 (2.6% regression) <-- this regression is less than for the v3 patch? Atypical...
csv, no special: 17651 (35.3% speedup)
csv, 1/3 special: 34055 (5.3% regression)Yes, I think I agree with you that the everything-in-RAM benchmarks will make the regressions more pronounced, just like the everything-in-RAM benchmarks make the improvements more pronounced.I am not sure why the CSV regression, compared to the TXT regression (even for the v3 patch which has smaller regressions than the v4.2 patch) is usually worse. I probably should look over some flame graphs and see if I can find the place where the CSV-parsing code is so much slower. The CSV regression is actually a bit frustrating (at around 5%) because the TXT regression, at less than 1% (for the v3 patch) is so much easier to bare.The only reasons that i can think of for this problem is the CSV state machine is more complex than TEXT, which might imply that for everything related to branch prediction, stalls ..etc becomes more demanding in CSV mode, i see this by previous tight micro benchmarks on CopyReadLineText, it has tiny less IPC, more branch misses, stalls and i assume instruction cache misses shouldn't be a problem since the generated code duplicates the scalar path, also the code for it isn't that large for one core instruction cache anyways (mine has 5.8KB per core).We can use perf_event_open[1] around CopyReadLine to see what's going on exactly with the counters if someone wants to confirm.Here are some copy-to benchmarks for the v4 patch that applies SIMD to the copy-to code.These were all-in-RAM tests.My laptopmaster: (852558b9)
text, no special: 2948
text, 1/3 special: 11258
csv, no special: 6245
csv, 1/3 special: 11258
v4 (copy to)
text, no special: 2126 (27.9% speedup)
text, 1/3 special: 12080 (7.3% regression) <-- did not see such a big regression before
csv, no special: 2432 (61.0% speedup)
csv, 1/3 special: 12344 (4.0% regression) <-- did not see such a big regression beforeAn AWS EC2 t2.2xlarge instancemaster: (852558b9)
text, no special: 4647
text, 1/3 special: 13865
csv, no special: 5421
csv, 1/3 special: 15284
v4 (copy to)
text, no special: 2460 (47.0% speedup)
text, 1/3 special: 14023 (1.1% regression)
csv, no special: 2667 (50.7% speedup)
csv, 1/3 special: 15251 (0.2% speedup)An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM processor!)master: (852558b9)
text, no special: 6951
text, 1/3 special: 17857
csv, no special: 7951
csv, 1/3 special: 18504
v4 (copy to)
text, no special: 3372 (51.4% speedup)
text, 1/3 special: 15713 (12.0% speedup)
csv, no special: 3233 (59.3% speedup)
csv, 1/3 special: 1622 (12.3% speedup)Once again, the v4 patch for copy-to seems like a clearer win, though, to be fair, there were regressions when running on my laptop. (I'm starting to think servers or desktops are better than laptops for testing these things, though maybe that's my bias: it just seems like the server results are always less surprising.)Regards,Ayoub Kazar
master: (852558b9)
text, no special: 9111
text, 1/3 special: 10302
csv, no special: 11147
csv, 1/3 special: 13375
v3
text, no special: 7351 (19.3% speedup)
text, 1/3 special: 10397 (0.9% regression)
csv, no special: 7272 (34.7% speedup)
csv, 1/3 special: 13472 (0.7% regression)
v4.2
text, no special: 7300 (19.6% speedup)
text, 1/3 special: 10537 (2.3% regression)
csv, no special: 7260 (34.8% speedup)
csv, 1/3 special: 13881 (3.8% regression)
COPY TO
master: (852558b9)
text, no special: 2446
text, 1/3 special: 6988
csv, no special: 2822
csv, 1/3 special: 6967
v4 (copy to)
text, no special: 1533 (37.3% speedup)
text, 1/3 special: 5949 (14.8% speedup)
csv, no special: 1560 (44.7% speedup)
csv, 1/3 special: 6006 (13.8% speedup)
Re: Speed up COPY FROM text/CSV parsing using SIMD
* I'm curious if we'll see better performance on large inputs if we flush to `line_buf` periodically (e.g., at least every few thousand bytes or so). Otherwise we might see poor data cache behavior if large inputs with no control characters get evicted before we've copied them over. See the approach taken in escape_json_with_len() in utils/adt/json.c
Hi,
On Wed, 10 Dec 2025 at 01:13, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> Bilal Yavuz (Nazir Bilal Yavuz?),
It is Nazir Bilal Yavuz, I changed some settings on my phone and it
seems that it affected my mail account, hopefully it should be fixed
now.
> I did not get a chance to do any work on this today, but wanted to thank you for finding my logic errors in counting special chars for CSV, and hacking on my naive solution to make it faster. By attempting Andrew Dunstan's suggestion, I got a better feel for the reality that the "housekeeping" code produces a significant amount of overhead.
You are welcome! v4.1 has some problems with in_quote case in SIMD
handling code and counting cstate->chars_processed variable. I fixed
them in v4.2.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello, all I have more benchmarks.These benchmarks are from a Raspberry Pi 5 that I bought. It has an Arm Cortex A76 processor.(I was so impressed with the stability of the results I got on my standalone Intel tower PC that I figured I needed a standalone Arm-based machine that was not a laptop and not a VM at a cloud service provider. The run-to-run results were indeed more stable, just like with my standalone tower PC.)COPY FROM
master: (852558b9)
text, no special: 9111
text, 1/3 special: 10302
csv, no special: 11147
csv, 1/3 special: 13375
v3
text, no special: 7351 (19.3% speedup)
text, 1/3 special: 10397 (0.9% regression)
csv, no special: 7272 (34.7% speedup)
csv, 1/3 special: 13472 (0.7% regression)
v4.2
text, no special: 7300 (19.6% speedup)
text, 1/3 special: 10537 (2.3% regression)
csv, no special: 7260 (34.8% speedup)
csv, 1/3 special: 13881 (3.8% regression)
COPY TO
master: (852558b9)
text, no special: 2446
text, 1/3 special: 6988
csv, no special: 2822
csv, 1/3 special: 6967
v4 (copy to)
text, no special: 1533 (37.3% speedup)
text, 1/3 special: 5949 (14.8% speedup)
csv, no special: 1560 (44.7% speedup)
csv, 1/3 special: 6006 (13.8% speedup)I find these results particularly exciting because with the COPY FROM v3 patch, the worst-case scenarios are just under 1% regression. The v4 COPY TO patch is a win across the board.Note that I ran these benchmarks with everything in RAM disk and using the cpupower instructions that Nazir suggested.So on Arm, the v3 COPY FROM patch is almost all upside, and the v4 COPY TO patch is all upside. The same is almost true for Intel, but the CSV COPY FROM regression, even from the V3 COPY FROM patch, is about 5%. The v4.2 COPY FROM patch always performs worse than the v3 COPY FROM patch in worst-case scenarios.Does it seem reasonable to stop performance testing the v4.2 COPY FROM patch? Have we collected enough benchmark data to be confident that the v3 COPY FROM patch is the one we should be moving forward with?
Re: Speed up COPY FROM text/CSV parsing using SIMD
A few suggestions:
* I'm curious if we'll see better performance on large inputs if we flush to `line_buf` periodically (e.g., at least every few thousand bytes or so). Otherwise we might see poor data cache behavior if large inputs with no control characters get evicted before we've copied them over. See the approach taken in escape_json_with_len() in utils/adt/json.c
* Did you compare the approach taken in the patch with a simpler approach that just doesif (!(vector8_has(chunk, '\\') ||vector8_has(chunk, '\r') ||vector8_has(chunk, '\n') /* and so on, accounting for CSV / escapec / quotec stuff */)){/* skip chunk */}That's roughly what we do elsewhere (e.g., escape_json_with_len). It has the advantage of being more readable, along with potentially having fewer data dependencies.
text, 1/3 special: -21.3%
csv, no special: +29.5%
csv, 1/3 special: -33.5%
text, 1/3 special: -20.1% (weird) ; but overall the idea is clear maybe.
csv, no special: +29.4%
csv, 1/3 special: -4.4%
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Jan 13, 2026 at 06:20:27PM -0600, Manni Wood wrote: > On Tue, Jan 13, 2026 at 1:12 PM Mark Wong wrote: > > > On Fri, Jan 09, 2026 at 05:21:45PM +0300, Nazir Bilal Yavuz wrote: > > > Were you able to understand why Mark's benchmark results are different > > > from ours? > > > > Not yet... I had some guesses, which is why I suggested the processor > > pinning > > and using a ramdisk. But we haven't tried applying all of those to my > > laptop, > > which has 3 core types, or the POWER system, which may be interesting to > > use a > > ram disk on. > > > > I'm curious though, and admittedly haven't tried looking myself yet, about > > how > > the SIMD calls might look across different processor architectures. We'll > > try > > to get that on the POWER system soon... > > > > Regards, > > Mark > > Hello! > > Nazir, I'm glad you are finding the benchmarks useful. I have more! :-) > > All of these benchmarks are all-in-RAM, because I do think that is the best > way of getting closest to the theoretical best and worst case scenarios. > > My laptop: > > master: (852558b9) > > text, no special: 14996 > text, 1/3 special: 17270 > csv, no special: 18274 > csv, 1/3 special: 23852 > > v3 > > text, no special: 11282 (24.7% speedup) > text, 1/3 special: 15748 (8.8% speedup) <-- I don't believe this but it's > what I got > csv, no special: 11571 (36.6% speedup) > csv, 1/3 special: 19934 (16.4% speedup) <-- I don't believe this but it's > what I got > > v4.2 > > text, no special: 11139 (25.7% speedup) > text, 1/3 special: 18900 (9.4% regression) > csv, no special: 11490 (37.1% speedup) > csv, 1/3 special: 26134 (9.5% regression) > > An AWS EC2 t2.2xlarge instance > > master: (852558b9) > > text, no special: 20677 > text, 1/3 special: 22660 > csv, no special: 24534 > csv, 1/3 special: 30999 > > v3 > > text, no special: 17534 (15.2% speedup) > text, 1/3 special: 22816 (0.6% regression) > csv, no special: 17664 (28.0% speedup) > csv, 1/3 special: 29338 (5.3% speedup) <-- I don't believe this but it's > what I got > > v4.2 > > text, no special: 17459 (15.5% speedup) > text, 1/3 special: 25051 (10.5% regression) > csv, no special: 17574 (28.3% speedup) > csv, 1/3 special: 32092 (3.5% regression) > > An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM > processor!) > > master: (852558b9) > > text, no special: 22081 > text, 1/3 special: 25100 > csv, no special: 27296 > csv, 1/3 special: 32344 > > v3 > > text, no special: 17724 (19.7% speedup) > text, 1/3 special: 27606 (9.9% regression) <-- yikes! We would want to test > this more > csv, no special: 17597 (35.5% speedup) > csv, 1/3 special: 32597 (0.8% regression) > > v4.2 > > text, no special: 17674 (20% speedup) > text, 1/3 special: 25773 (2.6% regression) <-- this regression is less than > for the v3 patch? Atypical... > csv, no special: 17651 (35.3% speedup) > csv, 1/3 special: 34055 (5.3% regression) I'm still lagging behind a little I ran the v4.2 patches again applied to 71c11369 on the POWER system that I have access to, using Manni's copysimdperf scripts to use a ramdisk and processor pinning. text, no special: -2508 (30% speedup) text, 1/3 special: -1753 (48% speedup) csv, no special: 9264 (3% regression) csv, 1/3 special: -4077 (0.3% speedup) Using Manni's script makes me feel more confident about executing the tests in the same way, so I don't know how concerning the difference in results are compared to the other architectures. > Yes, I think I agree with you that the everything-in-RAM benchmarks will > make the regressions more pronounced, just like the everything-in-RAM > benchmarks make the improvements more pronounced. > > I am not sure why the CSV regression, compared to the TXT regression (even > for the v3 patch which has smaller regressions than the v4.2 patch) is > usually worse. I probably should look over some flame graphs and see if I > can find the place where the CSV-parsing code is so much slower. The CSV > regression is actually a bit frustrating (at around 5%) because the TXT > regression, at less than 1% (for the v3 patch) is so much easier to bare. > > Here are some copy-to benchmarks for the v4 patch that applies SIMD to the > copy-to code. > > These were all-in-RAM tests. > > My laptop > > master: (852558b9) > > text, no special: 2948 > text, 1/3 special: 11258 > csv, no special: 6245 > csv, 1/3 special: 11258 > > v4 (copy to) > > text, no special: 2126 (27.9% speedup) > text, 1/3 special: 12080 (7.3% regression) <-- did not see such a big > regression before > csv, no special: 2432 (61.0% speedup) > csv, 1/3 special: 12344 (4.0% regression) <-- did not see such a big > regression before > > An AWS EC2 t2.2xlarge instance > > master: (852558b9) > > text, no special: 4647 > text, 1/3 special: 13865 > csv, no special: 5421 > csv, 1/3 special: 15284 > > v4 (copy to) > > text, no special: 2460 (47.0% speedup) > text, 1/3 special: 14023 (1.1% regression) > csv, no special: 2667 (50.7% speedup) > csv, 1/3 special: 15251 (0.2% speedup) > > An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM > processor!) > > master: (852558b9) > > text, no special: 6951 > text, 1/3 special: 17857 > csv, no special: 7951 > csv, 1/3 special: 18504 > > v4 (copy to) > > text, no special: 3372 (51.4% speedup) > text, 1/3 special: 15713 (12.0% speedup) > csv, no special: 3233 (59.3% speedup) > csv, 1/3 special: 1622 (12.3% speedup) > > Once again, the v4 patch for copy-to seems like a clearer win, though, to > be fair, there were regressions when running on my laptop. (I'm starting to > think servers or desktops are better than laptops for testing these things, > though maybe that's my bias: it just seems like the server results are > always less surprising.) > > Hope you all continue to find these useful... Regards, Mark -- Mark Wong EDB https://enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
A few suggestions:
* I'm curious if we'll see better performance on large inputs if we flush to `line_buf` periodically (e.g., at least every few thousand bytes or so). Otherwise we might see poor data cache behavior if large inputs with no control characters get evicted before we've copied them over. See the approach taken in escape_json_with_len() in utils/adt/json.c
| Workload | v3 | v3.1 (2k) | v3.1 (4k) | v3.1 (8k) | v3.1 (16k) | v3.1 (20k) | v3.1 (28k) |
|---|---|---|---|---|---|---|---|
| text/none | -16.5% | -17.4% | -14.3% | -12.6% | -13.6% | -10.5% | -16.3% |
| text/esc | +5.6% | +11.1% | +3.1% | +7.6% | +3.0% | +4.9% | +4.2% |
| csv/none | -31.0% | -29.9% | -26.7% | -30.1% | -27.9% | -30.2% | -29.6% |
| csv/quote | +0.2% | -0.6% | -0.4% | -1.0% | +0.1% | +2.5% | -1.0% |
| Workload | Master | v3 | v3.1 (2k) | v3.1 (4k) | v3.1 (8k) | v3.1 (16k) | v3.1 (20k) | v3.1 (28k) |
|---|---|---|---|---|---|---|---|---|
| text/none | 0.20% | 0.23% | 0.21% | 0.22% | 0.21% | 0.21% | 0.21% | 0.22% |
| text/esc | 0.21% | 0.22% | 0.22% | 0.22% | 0.22% | 0.21% | 0.22% | 0.22% |
| csv/none | 0.17% | 0.22% | 0.21% | 0.22% | 0.21% | 0.21% | 0.22% | 0.22% |
| csv/quote | 0.18% | 0.22% | 0.19% | 0.20% | 0.20% | 0.19% | 0.20% | 0.20% |
If Manni can rerun the benchmarks on these too, it would be nice to confirm this.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello,On Wed, Jan 21, 2026 at 9:50 PM Neil Conway <neil.conway@gmail.com> wrote:A few suggestions:
* I'm curious if we'll see better performance on large inputs if we flush to `line_buf` periodically (e.g., at least every few thousand bytes or so). Otherwise we might see poor data cache behavior if large inputs with no control characters get evicted before we've copied them over. See the approach taken in escape_json_with_len() in utils/adt/json.cSo i gave this a try, attached is the small patch that has v3 + the suggestion added, here are the results with different threshold for line_buf refill:Execution time compared to master:
Workload v3 v3.1 (2k) v3.1 (4k) v3.1 (8k) v3.1 (16k) v3.1 (20k) v3.1 (28k) text/none -16.5% -17.4% -14.3% -12.6% -13.6% -10.5% -16.3% text/esc +5.6% +11.1% +3.1% +7.6% +3.0% +4.9% +4.2% csv/none -31.0% -29.9% -26.7% -30.1% -27.9% -30.2% -29.6% csv/quote +0.2% -0.6% -0.4% -1.0% +0.1% +2.5% -1.0% L1d cache miss rates:
Workload Master v3 v3.1 (2k) v3.1 (4k) v3.1 (8k) v3.1 (16k) v3.1 (20k) v3.1 (28k) text/none 0.20% 0.23% 0.21% 0.22% 0.21% 0.21% 0.21% 0.22% text/esc 0.21% 0.22% 0.22% 0.22% 0.22% 0.21% 0.22% 0.22% csv/none 0.17% 0.22% 0.21% 0.22% 0.21% 0.21% 0.22% 0.22% csv/quote 0.18% 0.22% 0.19% 0.20% 0.20% 0.19% 0.20% 0.20% On my laptop I have 32KB L1 cache per core.Results are super close, it is hard to see in the cache misses numbers but execution times are saying other things, doing the periodic filling of line_buf seems good to do.
If Manni can rerun the benchmarks on these too, it would be nice to confirm this.Regards,Ayoub
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Jan 13, 2026 at 06:20:27PM -0600, Manni Wood wrote:
> On Tue, Jan 13, 2026 at 1:12 PM Mark Wong <markwkm@gmail.com> wrote:
>
> > On Fri, Jan 09, 2026 at 05:21:45PM +0300, Nazir Bilal Yavuz wrote:
> > > Were you able to understand why Mark's benchmark results are different
> > > from ours?
> >
> > Not yet... I had some guesses, which is why I suggested the processor
> > pinning
> > and using a ramdisk. But we haven't tried applying all of those to my
> > laptop,
> > which has 3 core types, or the POWER system, which may be interesting to
> > use a
> > ram disk on.
> >
> > I'm curious though, and admittedly haven't tried looking myself yet, about
> > how
> > the SIMD calls might look across different processor architectures. We'll
> > try
> > to get that on the POWER system soon...
> >
> > Regards,
> > Mark
>
> Hello!
>
> Nazir, I'm glad you are finding the benchmarks useful. I have more! :-)
>
> All of these benchmarks are all-in-RAM, because I do think that is the best
> way of getting closest to the theoretical best and worst case scenarios.
>
> My laptop:
>
> master: (852558b9)
>
> text, no special: 14996
> text, 1/3 special: 17270
> csv, no special: 18274
> csv, 1/3 special: 23852
>
> v3
>
> text, no special: 11282 (24.7% speedup)
> text, 1/3 special: 15748 (8.8% speedup) <-- I don't believe this but it's
> what I got
> csv, no special: 11571 (36.6% speedup)
> csv, 1/3 special: 19934 (16.4% speedup) <-- I don't believe this but it's
> what I got
>
> v4.2
>
> text, no special: 11139 (25.7% speedup)
> text, 1/3 special: 18900 (9.4% regression)
> csv, no special: 11490 (37.1% speedup)
> csv, 1/3 special: 26134 (9.5% regression)
>
> An AWS EC2 t2.2xlarge instance
>
> master: (852558b9)
>
> text, no special: 20677
> text, 1/3 special: 22660
> csv, no special: 24534
> csv, 1/3 special: 30999
>
> v3
>
> text, no special: 17534 (15.2% speedup)
> text, 1/3 special: 22816 (0.6% regression)
> csv, no special: 17664 (28.0% speedup)
> csv, 1/3 special: 29338 (5.3% speedup) <-- I don't believe this but it's
> what I got
>
> v4.2
>
> text, no special: 17459 (15.5% speedup)
> text, 1/3 special: 25051 (10.5% regression)
> csv, no special: 17574 (28.3% speedup)
> csv, 1/3 special: 32092 (3.5% regression)
>
> An AWS EC2 t4g.2xlarge instance (using ARM processor; first test of ARM
> processor!)
>
> master: (852558b9)
>
> text, no special: 22081
> text, 1/3 special: 25100
> csv, no special: 27296
> csv, 1/3 special: 32344
>
> v3
>
> text, no special: 17724 (19.7% speedup)
> text, 1/3 special: 27606 (9.9% regression) <-- yikes! We would want to test
> this more
> csv, no special: 17597 (35.5% speedup)
> csv, 1/3 special: 32597 (0.8% regression)
>
> v4.2
>
> text, no special: 17674 (20% speedup)
> text, 1/3 special: 25773 (2.6% regression) <-- this regression is less than
> for the v3 patch? Atypical...
> csv, no special: 17651 (35.3% speedup)
> csv, 1/3 special: 34055 (5.3% regression)
I'm still lagging behind a little I ran the v4.2 patches again applied to
71c11369 on the POWER system that I have access to, using Manni's copysimdperf
scripts to use a ramdisk and processor pinning.
text, no special: -2508 (30% speedup)
text, 1/3 special: -1753 (48% speedup)
csv, no special: 9264 (3% regression)
csv, 1/3 special: -4077 (0.3% speedup)
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Mon, 2 Feb 2026 at 23:45, KAZAR Ayoub wrote: > >> > Thanks for the benchmark, I'm a bit suspicious about this because I find it illogical or at least highly unexpected for a 1/3 specials workload to perform better than no specials ! > or csv no special regressing ! because it's expected to take the simd path for the whole processing, so it's supposed to perform better than master (at least ...). I agree with you. I think it is highly unexpected that 1/3 specials cases perform better than no special cases. > I wonder what the results look like for COPY TO case on POWER. If you can try, that case is at least even more theoretically predictable. Yes, that would be helpful. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, Thank you for all your help! This thread has a couple of discussions and it can be hard to follow; I think it would be better if we can separate these discussions. This would make it easier to follow the topics. What I have in my mind is: * There are four patches in this thread, three of them COPY FROM related: v3, v4 and v5. One of them is COPY TO related. So, I guess it would be better to move COPY TO discussion to another thread. * Manni suggested to stop benchmarking v4.2 COPY FROM patch, I agree with him. Then it would be easier to address reviews and do benchmarking. * Kazar applied Neil's review on top of v3 and benchmarked, v3 seems to perform better but I think flushing line_buf needs more benchmarking. * I am planning to create a new version (v6) of the patch, which will be basically v3 with one less branching (moving !in_quote check under is_csv and else). So that it would be easy to review and follow the thread for new reviewers. What do you think about these? Do you think these make sense and / or do you have any other suggestions? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
* There are four patches in this thread, three of them COPY FROM
related: v3, v4 and v5. One of them is COPY TO related. So, I guess it
would be better to move COPY TO discussion to another thread.
* I am planning to create a new version (v6) of the patch, which will
be basically v3 with one less branching (moving !in_quote check under
is_csv and else). So that it would be easy to review and follow the
thread for new reviewers.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello,On Tue, Feb 3, 2026, 12:02 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
* There are four patches in this thread, three of them COPY FROM
related: v3, v4 and v5. One of them is COPY TO related. So, I guess it
would be better to move COPY TO discussion to another thread.If there's possible discussions concerning COPY TO, then yes because i don't find many concerns about COPY TO.* I am planning to create a new version (v6) of the patch, which will
be basically v3 with one less branching (moving !in_quote check under
is_csv and else). So that it would be easy to review and follow the
thread for new reviewers.Oh cool, looking forward to this.Regards,Ayoub
commit 78bf28e3bf504db0eea5e3bcb3c43e9908108480 (HEAD -> master, origin/master, origin/HEAD)
commit 78bf28e3bf504db0eea5e3bcb3c43e9908108480 (HEAD -> master, origin/master, origin/HEAD)
plus
0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch
CSV, no special characters 23475.94ms 33.59202224% improvement
Text, with 1/3 escapes 33530.8395ms -2.470329558% regression
CSV, with 1/3 quotes 45268.28275ms -2.777637533 regression
commit 78bf28e3bf504db0eea5e3bcb3c43e9908108480 (HEAD -> master, origin/master, origin/HEAD)
plus
v5.1-0001-Simple-heuristic-for-SIMD-COPY-FROM.patch.patch
CSV, no special characters 22777.7805ms 35.56695319% improvement
Text, with 1/3 escapes 34542.75625ms -5.562749683% regression
CSV, with 1/3 quotes 45793.0095 -3.96898327 regression
commit 78bf28e3bf504db0eea5e3bcb3c43e9908108480 (HEAD -> master, postgres/master, postgres/HEAD)
Text, with 1/3 escapes 10765.8125ms
CSV, with 1/3 quotes 14055.28925ms
commit 78bf28e3bf504db0eea5e3bcb3c43e9908108480 (HEAD -> master, postgres/master, postgres/HEAD)
with
0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch
CSV, no special characters 7349.53475ms 33.98210649% improvement
Text, with 1/3 escapes 10350.6385ms 3.856411209% improvement
CSV, with 1/3 quotes 12407.22725ms 11.72556445% improvement
commit 78bf28e3bf504db0eea5e3bcb3c43e9908108480 (HEAD -> master, postgres/master, postgres/HEAD)
with
v5.1-0001-Simple-heuristic-for-SIMD-COPY-FROM.patch.patch
CSV, no special characters 7411.73225ms 33.42341154% improvement
Text, with 1/3 escapes 11288.465ms -4.854742733% regression
Re: Speed up COPY FROM text/CSV parsing using SIMD
The 0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch seems nice! On My x86 PC, it had the usual performance improvment of earlier patches, but the regression seemed more similar for both text and csv inputs. Unfortunately, the regression is about 2.5%, but maybe that is an acceptable worst-case for an improvement of 22% for text inputs and 33% for CSV inputs?The 0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch looks even better on my Raspberry Pi's arm processor: not only do we see a 22% improvement for text and an almost 34% improvement for CSV, even the worst-case scenarios show an almost 4% improvement for text and an 11.7% improvement for CSV.By comparison, the v5.1-0001-Simple-heuristic-for-SIMD-COPY-FROM.patch.patch's worst-case performance is poorer on both architectures.I'd be curious to know if anyone else can reproduces these numbers. 0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch seems like a real winner.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello,On Wed, Feb 4, 2026, 6:38 AM Manni Wood <manni.wood@enterprisedb.com> wrote:The 0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch seems nice! On My x86 PC, it had the usual performance improvment of earlier patches, but the regression seemed more similar for both text and csv inputs. Unfortunately, the regression is about 2.5%, but maybe that is an acceptable worst-case for an improvement of 22% for text inputs and 33% for CSV inputs?The 0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch looks even better on my Raspberry Pi's arm processor: not only do we see a 22% improvement for text and an almost 34% improvement for CSV, even the worst-case scenarios show an almost 4% improvement for text and an 11.7% improvement for CSV.By comparison, the v5.1-0001-Simple-heuristic-for-SIMD-COPY-FROM.patch.patch's worst-case performance is poorer on both architectures.I'd be curious to know if anyone else can reproduces these numbers. 0001-COPY-from-SIMD-v3-with-line_buf-periodic-refill.patch seems like a real winner.Thanks for the benchmark Manni, i suppose this is the same threshold as patch has (4096 bytes), have you tried any bigger values for the threshold ?Because i'm still expecting less l1d cache misses and execution times the more we increase the threshold (relatively to l1d cache size per core).As per my previous not-so-stable numbers 28KB wasn't too bad.Regards,Ayoub
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Sat, 31 Jan 2026 at 19:21, KAZAR Ayoub wrote: > > On Wed, Jan 21, 2026 at 9:50 PM Neil Conway wrote: >> >> * I'm curious if we'll see better performance on large inputs if we flush to `line_buf` periodically (e.g., at least every few thousand bytes or so). Otherwise we might see poor data cache behavior if large inputs with no control characters get evicted before we've copied them over. See the approach taken in escape_json_with_len() in utils/adt/json.c >> > So i gave this a try, attached is the small patch that has v3 + the suggestion added, here are the results with different threshold for line_buf refill: > > Execution time compared to master: > Workloadv3v3.1 (2k)v3.1 (4k)v3.1 (8k)v3.1 (16k)v3.1 (20k)v3.1 (28k) > text/none-16.5%-17.4%-14.3%-12.6%-13.6%-10.5%-16.3% > text/esc+5.6%+11.1%+3.1%+7.6%+3.0%+4.9%+4.2% > csv/none-31.0%-29.9%-26.7%-30.1%-27.9%-30.2%-29.6% > csv/quote+0.2%-0.6%-0.4%-1.0%+0.1%+2.5%-1.0% > > L1d cache miss rates: > WorkloadMasterv3v3.1 (2k)v3.1 (4k)v3.1 (8k)v3.1 (16k)v3.1 (20k)v3.1 (28k) > text/none0.20%0.23%0.21%0.22%0.21%0.21%0.21%0.22% > text/esc0.21%0.22%0.22%0.22%0.22%0.21%0.22%0.22% > csv/none0.17%0.22%0.21%0.22%0.21%0.21%0.22%0.22% > csv/quote0.18%0.22%0.19%0.20%0.20%0.19%0.20%0.20% > > On my laptop I have 32KB L1 cache per core. > Results are super close, it is hard to see in the cache misses numbers but execution times are saying other things, doing the periodic filling of line_buf seems good to do. > If Manni can rerun the benchmarks on these too, it would be nice to confirm this. I looked at this change and had a couple of points. We already have REFILL_LINEBUF at the start of the for loop in the CopyReadLineText() function (let’s call this refill #1). This refills when the input_buf_ptr >= copy_buf_len check is true. On my end, copy_buf_len stays at 8191 until the end of the input, and then it becomes the remaining amount. So when I set LINE_BUF_FLUSH_AFTER to 8192, the REFILL_LINEBUF you added shouldn’t be called; instead, refill #1 should be triggered. I verified this manually by adding some logging, and the results seem to confirm this behavior. Based on that, there shouldn’t be a performance difference when LINE_BUF_FLUSH_AFTER >= 8k. Could you please take a look and confirm whether you see the same behavior? Also, I noticed that json.c uses ESCAPE_JSON_FLUSH_AFTER set to 512, so it might be worth trying smaller values here as well. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 31 Jan 2026 at 19:21, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> On Wed, Jan 21, 2026 at 9:50 PM Neil Conway <neil.conway@gmail.com> wrote:
>>
>> * I'm curious if we'll see better performance on large inputs if we flush to `line_buf` periodically (e.g., at least every few thousand bytes or so). Otherwise we might see poor data cache behavior if large inputs with no control characters get evicted before we've copied them over. See the approach taken in escape_json_with_len() in utils/adt/json.c
>>
> So i gave this a try, attached is the small patch that has v3 + the suggestion added, here are the results with different threshold for line_buf refill:
>
> Execution time compared to master:
> Workloadv3v3.1 (2k)v3.1 (4k)v3.1 (8k)v3.1 (16k)v3.1 (20k)v3.1 (28k)
> text/none-16.5%-17.4%-14.3%-12.6%-13.6%-10.5%-16.3%
> text/esc+5.6%+11.1%+3.1%+7.6%+3.0%+4.9%+4.2%
> csv/none-31.0%-29.9%-26.7%-30.1%-27.9%-30.2%-29.6%
> csv/quote+0.2%-0.6%-0.4%-1.0%+0.1%+2.5%-1.0%
>
> L1d cache miss rates:
> WorkloadMasterv3v3.1 (2k)v3.1 (4k)v3.1 (8k)v3.1 (16k)v3.1 (20k)v3.1 (28k)
> text/none0.20%0.23%0.21%0.22%0.21%0.21%0.21%0.22%
> text/esc0.21%0.22%0.22%0.22%0.22%0.21%0.22%0.22%
> csv/none0.17%0.22%0.21%0.22%0.21%0.21%0.22%0.22%
> csv/quote0.18%0.22%0.19%0.20%0.20%0.19%0.20%0.20%
>
> On my laptop I have 32KB L1 cache per core.
> Results are super close, it is hard to see in the cache misses numbers but execution times are saying other things, doing the periodic filling of line_buf seems good to do.
> If Manni can rerun the benchmarks on these too, it would be nice to confirm this.
I looked at this change and had a couple of points.
We already have REFILL_LINEBUF at the start of the for loop in the
CopyReadLineText() function (let’s call this refill #1). This refills
when the input_buf_ptr >= copy_buf_len check is true. On my end,
copy_buf_len stays at 8191 until the end of the input, and then it
becomes the remaining amount. So when I set LINE_BUF_FLUSH_AFTER to
8192, the REFILL_LINEBUF you added shouldn’t be called; instead,
refill #1 should be triggered.
I verified this manually by adding some logging, and the results seem
to confirm this behavior. Based on that, there shouldn’t be a
performance difference when LINE_BUF_FLUSH_AFTER >= 8k.
Could you please take a look and confirm whether you see the same behavior?
CopyReadLine(CopyFromState cstate, bool is_csv)
{
bool result;
resetStringInfo(&cstate->line_buf);
....
}
Also, I noticed that json.c uses ESCAPE_JSON_FLUSH_AFTER set to 512,
so it might be worth trying smaller values here as well.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Sorry for disappearing from this thread for a while. It looks like a lot of energy has been put into benchmarking and refining the heuristic for deciding when to use the SIMD path so that we avoid large regressions when there are special characters. I think this is all valuable work, but I'm a bit concerned that we are putting the cart before the horse. IMHO it would be better to first get the SIMD code committed with the absolute simplest heuristic we can think of (e.g., as soon as we see a special character, switch to the scalar path for the remainder of COPY FROM). My hope is that would be far easier to reason about from a performance angle. If we immediately fall back to the existing code path, we don't need to worry about how many special characters there are and whether they are sparse or clustered or whatever. We just need to measure the overhead of the new branches and ensure they don't produce meaningful regressions. Assuming that all looks good, we can then focus on the SIMD code itself and make sure that is correct and optimal. And once we get that portion committed, we could then consider more sophisticated heuristics. FWIW I'm hoping to get something in this area committed for v19, and IMO now is a good time to start thinking about how to get things over the finish line. Thanks for working on it. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, Thank you for sharing your thoughts! On Sat, 7 Feb 2026 at 00:29, Nathan Bossart wrote: > > It looks like a lot of energy has been put into benchmarking and refining > the heuristic for deciding when to use the SIMD path so that we avoid large > regressions when there are special characters. I think this is all > valuable work, but I'm a bit concerned that we are putting the cart before > the horse. IMHO it would be better to first get the SIMD code committed > with the absolute simplest heuristic we can think of (e.g., as soon as we > see a special character, switch to the scalar path for the remainder of > COPY FROM). My hope is that would be far easier to reason about from a > performance angle. If we immediately fall back to the existing code path, > we don't need to worry about how many special characters there are and > whether they are sparse or clustered or whatever. We just need to measure > the overhead of the new branches and ensure they don't produce meaningful > regressions. Assuming that all looks good, we can then focus on the SIMD > code itself and make sure that is correct and optimal. And once we get > that portion committed, we could then consider more sophisticated > heuristics. I have three possible approaches in my mind, they are actually similar to each other. 1- After encountering a special character, disable SIMD for the rest of the current line and also for the rest of the data. 2- It is a mixed version of the current heuristic and #1. After encountering a special character, skip SIMD for the current line (let' say line 1) and for the next line (line 2). Then try running SIMD for the next line (line 3), if there is no special character continue to run SIMD but if there is a special character then skip running SIMD for two lines this time. And it goes like that, everytime special character is encountered in the SIMD run, skipped SIMD lines are doubled. 3- This version is a bit different from #2. Instead of calculating the number of lines to skip dynamically, skip the constant N number of lines and then try to run SIMD again after these lines. N could be something like 100, 1000, or 10000 etc.. Actually, you and Andrew suggested this approach before [1]. I think what you suggested is closer to #1 or #3. I just wanted to hear your opinions, and whether you think any of these approaches are good to implement / work on. [1] https://postgr.es/m/aR4wDwNdLc5TmcQq%40nathan -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
Thank you for sharing your thoughts!
On Sat, 7 Feb 2026 at 00:29, Nathan Bossart <nathandbossart@gmail.com> wrote:
>
> It looks like a lot of energy has been put into benchmarking and refining
> the heuristic for deciding when to use the SIMD path so that we avoid large
> regressions when there are special characters. I think this is all
> valuable work, but I'm a bit concerned that we are putting the cart before
> the horse. IMHO it would be better to first get the SIMD code committed
> with the absolute simplest heuristic we can think of (e.g., as soon as we
> see a special character, switch to the scalar path for the remainder of
> COPY FROM). My hope is that would be far easier to reason about from a
> performance angle. If we immediately fall back to the existing code path,
> we don't need to worry about how many special characters there are and
> whether they are sparse or clustered or whatever. We just need to measure
> the overhead of the new branches and ensure they don't produce meaningful
> regressions. Assuming that all looks good, we can then focus on the SIMD
> code itself and make sure that is correct and optimal. And once we get
> that portion committed, we could then consider more sophisticated
> heuristics.
I have three possible approaches in my mind, they are actually similar
to each other.
1- After encountering a special character, disable SIMD for the rest
of the current line and also for the rest of the data.
2- It is a mixed version of the current heuristic and #1. After
encountering a special character, skip SIMD for the current line (let'
say line 1) and for the next line (line 2). Then try running SIMD for
the next line (line 3), if there is no special character continue to
run SIMD but if there is a special character then skip running SIMD
for two lines this time. And it goes like that, everytime special
character is encountered in the SIMD run, skipped SIMD lines are
doubled.
3- This version is a bit different from #2. Instead of calculating the
number of lines to skip dynamically, skip the constant N number of
lines and then try to run SIMD again after these lines. N could be
something like 100, 1000, or 10000 etc.. Actually, you and Andrew
suggested this approach before [1].
I think what you suggested is closer to #1 or #3. I just wanted to
hear your opinions, and whether you think any of these approaches are
good to implement / work on.
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Sat, Feb 07, 2026 at 01:19:16AM +0300, Nazir Bilal Yavuz wrote: > I have three possible approaches in my mind, they are actually similar > to each other. > > 1- After encountering a special character, disable SIMD for the rest > of the current line and also for the rest of the data. > > 2- It is a mixed version of the current heuristic and #1. After > encountering a special character, skip SIMD for the current line (let' > say line 1) and for the next line (line 2). Then try running SIMD for > the next line (line 3), if there is no special character continue to > run SIMD but if there is a special character then skip running SIMD > for two lines this time. And it goes like that, everytime special > character is encountered in the SIMD run, skipped SIMD lines are > doubled. > > 3- This version is a bit different from #2. Instead of calculating the > number of lines to skip dynamically, skip the constant N number of > lines and then try to run SIMD again after these lines. N could be > something like 100, 1000, or 10000 etc.. Actually, you and Andrew > suggested this approach before [1]. > > I think what you suggested is closer to #1 or #3. I just wanted to > hear your opinions, and whether you think any of these approaches are > good to implement / work on. Yeah, I think either (1) or (3) would be a good starting point. (1) is basically just (3) with N set to infinity, anyway. I imagine there's some value less than infinity that is acceptable, but if I had to pick an approach right now, I'd probably go with (1) to essentially remove the heuristic from the discussion until we're ready to focus on it. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Sat, 7 Feb 2026 at 01:47, Nathan Bossart wrote: > > On Sat, Feb 07, 2026 at 01:19:16AM +0300, Nazir Bilal Yavuz wrote: > > I have three possible approaches in my mind, they are actually similar > > to each other. > > > > 1- After encountering a special character, disable SIMD for the rest > > of the current line and also for the rest of the data. > > > > 2- It is a mixed version of the current heuristic and #1. After > > encountering a special character, skip SIMD for the current line (let' > > say line 1) and for the next line (line 2). Then try running SIMD for > > the next line (line 3), if there is no special character continue to > > run SIMD but if there is a special character then skip running SIMD > > for two lines this time. And it goes like that, everytime special > > character is encountered in the SIMD run, skipped SIMD lines are > > doubled. > > > > 3- This version is a bit different from #2. Instead of calculating the > > number of lines to skip dynamically, skip the constant N number of > > lines and then try to run SIMD again after these lines. N could be > > something like 100, 1000, or 10000 etc.. Actually, you and Andrew > > suggested this approach before [1]. > > > > I think what you suggested is closer to #1 or #3. I just wanted to > > hear your opinions, and whether you think any of these approaches are > > good to implement / work on. > > Yeah, I think either (1) or (3) would be a good starting point. (1) is > basically just (3) with N set to infinity, anyway. I imagine there's some > value less than infinity that is acceptable, but if I had to pick an > approach right now, I'd probably go with (1) to essentially remove the > heuristic from the discussion until we're ready to focus on it. I am sharing a v6 which implements (1). My benchmark results show almost no difference for the special-character cases and a nice improvement for the no-special-character cases. Timing results after running Manni's v1.2.1 benchmark: +---------+---------------+----------------+--------------+----------------+ | | text | no sp. | text | 1/3 sp. | csv | no sp. | csv | 1/3 sp. | +---------+---------------+----------------+--------------+----------------+ | master | 104437 | 118711 | 121173 | 151589 | +---------+---------------+----------------+--------------+----------------+ | patched | 90062 -%13.7 | 119070 +%0.003 | 88964 -%26.5 | 153710 +%0.013 | +---------+---------------+----------------+--------------+----------------+ In case the table does not render well in your email client, here is a short summary: - Text, no special characters: 13.7% faster - Text, 1/3 special characters: %0.003 slower, no meaningful change - CSV, no special characters: 26.5% faster - CSV, 1/3 special characters: %0.013 slower, no meaningful change -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Feb 11, 2026 at 04:27:50PM +0300, Nazir Bilal Yavuz wrote:
> I am sharing a v6 which implements (1). My benchmark results show
> almost no difference for the special-character cases and a nice
> improvement for the no-special-character cases.
Thanks!
> + /* Initialize SIMD variables */
> + cstate->simd_enabled = false;
> + cstate->simd_initialized = false;
> + /* Initialize SIMD on the first read */
> + if (unlikely(!cstate->simd_initialized))
> + {
> + cstate->simd_initialized = true;
> + cstate->simd_enabled = true;
> + }
Why do we do this initialization in CopyReadLine() as opposed to setting
simd_enabled to true when initializing cstate in BeginCopyFrom()? If we
can initialize it in BeginCopyFrom, we could probably remove
simd_initialized.
> + if (cstate->simd_enabled)
> + result = CopyReadLineText(cstate, is_csv, true);
> + else
> + result = CopyReadLineText(cstate, is_csv, false);
I know we discussed this upthread, but I'd like to take a closer look at
this to see whether/why it makes such a big difference. It's a bit awkward
that CopyReadLineText() needs to manage both its local simd_enabled and
cstate->simd_enabled.
+ /* Load a chunk of data into a vector register */
+ vector8_load(&chunk, (const uint8 *) ©_input_buf[input_buf_ptr]);
As mentioned upthread [0], I think it's worth testing whether processing
multiple vectors worth of data in each loop iteration is worthwhile.
[0] https://postgr.es/m/aSTVOe6BIe5f1l3i%40nathan
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
Thanks for the review!
On Thu, 12 Feb 2026 at 01:39, Nathan Bossart wrote:
>
> On Wed, Feb 11, 2026 at 04:27:50PM +0300, Nazir Bilal Yavuz wrote:
> > I am sharing a v6 which implements (1). My benchmark results show
> > almost no difference for the special-character cases and a nice
> > improvement for the no-special-character cases.
>
> Thanks!
>
> > + /* Initialize SIMD variables */
> > + cstate->simd_enabled = false;
> > + cstate->simd_initialized = false;
>
> > + /* Initialize SIMD on the first read */
> > + if (unlikely(!cstate->simd_initialized))
> > + {
> > + cstate->simd_initialized = true;
> > + cstate->simd_enabled = true;
> > + }
>
> Why do we do this initialization in CopyReadLine() as opposed to setting
> simd_enabled to true when initializing cstate in BeginCopyFrom()? If we
> can initialize it in BeginCopyFrom, we could probably remove
> simd_initialized.
Correct, I guess this is left over from the earlier versions.
> > + if (cstate->simd_enabled)
> > + result = CopyReadLineText(cstate, is_csv, true);
> > + else
> > + result = CopyReadLineText(cstate, is_csv, false);
>
> I know we discussed this upthread, but I'd like to take a closer look at
> this to see whether/why it makes such a big difference. It's a bit awkward
> that CopyReadLineText() needs to manage both its local simd_enabled and
> cstate->simd_enabled.
I extensively benchmarked this with the new v6 version. If I change
this to either of:
CopyReadLineText(cstate, is_csv);
or
CopyReadLineText(cstate, is_csv, cstate->simd_enabled);
then there is %5-%10 regression for the scalar path. I ran my
benchmarks with both "meson --buildtype=debugoptimized" and "meson
--buildtype=release" but the result is the same.
Also, if I change this code to:
if (cstate->simd_enabled)
{
if (is_csv)
result = CopyReadLineText(cstate, true, true);
else
result = CopyReadLineText(cstate, false, true);
}
else
{
if (is_csv)
result = CopyReadLineText(cstate, true, false);
else
result = CopyReadLineText(cstate, false, false);
}
then I see ~%5 performance improvement in scalar path compared to master.
> + /* Load a chunk of data into a vector register */
> + vector8_load(&chunk, (const uint8 *) ©_input_buf[input_buf_ptr]);
>
> As mentioned upthread [0], I think it's worth testing whether processing
> multiple vectors worth of data in each loop iteration is worthwhile.
>
> [0] https://postgr.es/m/aSTVOe6BIe5f1l3i%40nathan
There are multiple keys in CopyReadLineText() compared to
pg_lfind32(). I am not sure if I correctly used multiple vectors but I
attached what I did as 0002, could you please look at it? I didn't see
any performance benefit in my benchmarks, though.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Feb 13, 2026 at 02:45:30PM +0300, Nazir Bilal Yavuz wrote:
> Also, if I change this code to:
>
> if (cstate->simd_enabled)
> {
> if (is_csv)
> result = CopyReadLineText(cstate, true, true);
> else
> result = CopyReadLineText(cstate, false, true);
> }
> else
> {
> if (is_csv)
> result = CopyReadLineText(cstate, true, false);
> else
> result = CopyReadLineText(cstate, false, false);
> }
>
> then I see ~%5 performance improvement in scalar path compared to master.
Hm. What difference do you see if you just do
if (is_csv)
result = CopyReadLineText(cstate, true);
else
result = CopyReadLineText(cstate, false);
both with and without the SIMD stuff? IIUC this is allowing the compiler
to remove several branches in CopyReadLineText(), which might be a nice
improvement on its own. That being said, I'm less convinced that adding a
simd_enabled parameter to CopyReadLineText() helps, because 1) it's
involved in fewer branches and 2) we change it within the function, so the
compiler can't remove the branches, anyway. But perhaps I'm missing
something.
Some other random thoughts:
+ match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
+ match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
Since \n and \r are well below "normal" ASCII values, I wonder if we could
simplify these to something like
match = vector8_gt(... vector with all lanes set to \r + 1 ..., chunk);
+ /* Check if we found any special characters */
+ mask = vector8_highbit_mask(match);
+ if (mask != 0)
vector8_highbit_mask() is somewhat expensive on AArch64, so I wonder if
waiting until we enter the "if" block to calculate it has any benefit.
+ simd_hit_eol = (c1 == '\r' || c1 == '\n') && (!is_csv || !in_quote);
If (is_csv && in_quote), we shouldn't have picked up \r or \n in the first
place, right?
+ simd_hit_eof = c1 == '\\' && c2 == '.' && !is_csv;
+
+ /*
+ * Do not disable SIMD when we hit EOL or EOF characters. In
+ * practice, it does not matter for EOF because parsing ends
+ * there, but we keep the behavior consistent.
+ */
+ if (!(simd_hit_eof || simd_hit_eol))
I'd think that doing less unnecessary work would outweigh the benefits of
consistency for the EOF case.
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
I ran some COPY FROM tests using master and then Nazir's v7-0001 and v7-0002 patches applied to master.
x86 master
TXT : 29222.524250 ms
CSV : 36162.588500 ms
TXT with 1/3 escapes: 32922.649750 ms
CSV with 1/3 quotes: 47631.423750 ms
x86 v7-0001
TXT : 23247.834250 ms 20.445496% improvement
CSV : 23162.711750 ms 35.948413% improvement
TXT with 1/3 escapes: 31786.386000 ms 3.451313% improvement
CSV with 1/3 quotes: 43330.475500 ms 9.029645% improvement
x86 v7-0002
TXT : 22394.812500 ms 23.364552% improvement
CSV : 22374.645750 ms 38.127643% improvement
TXT with 1/3 escapes: 32378.929750 ms 1.651507% improvement
CSV with 1/3 quotes: 47139.171750 ms 1.033461% improvement
arm master
TXT : 9448.900500 ms
CSV : 11135.871500 ms
TXT with 1/3 escapes: 10786.418750 ms
CSV with 1/3 quotes: 14115.335500 ms
arm v7-0001
TXT : 7271.170500 ms 23.047443% improvement
CSV : 7259.866750 ms 34.806479% improvement
TXT with 1/3 escapes: 10894.445500 ms -1.001507% regression
CSV with 1/3 quotes: 13398.444000 ms 5.078813% improvement
arm v7-0002
TXT : 7165.707250 ms 24.163587% improvement
CSV : 7140.497250 ms 35.878416% improvement
TXT with 1/3 escapes: 10308.782250 ms 4.428129% improvement
CSV with 1/3 quotes: 12576.179500 ms 10.904140% improvement
v7-0001 + v7-0002 applied to master certainly seems promising: nice to see speed improvements across the board on both x86 and arm!
On Fri, Feb 13, 2026 at 02:45:30PM +0300, Nazir Bilal Yavuz wrote:
> Also, if I change this code to:
>
> if (cstate->simd_enabled)
> {
> if (is_csv)
> result = CopyReadLineText(cstate, true, true);
> else
> result = CopyReadLineText(cstate, false, true);
> }
> else
> {
> if (is_csv)
> result = CopyReadLineText(cstate, true, false);
> else
> result = CopyReadLineText(cstate, false, false);
> }
>
> then I see ~%5 performance improvement in scalar path compared to master.
Hm. What difference do you see if you just do
if (is_csv)
result = CopyReadLineText(cstate, true);
else
result = CopyReadLineText(cstate, false);
both with and without the SIMD stuff? IIUC this is allowing the compiler
to remove several branches in CopyReadLineText(), which might be a nice
improvement on its own. That being said, I'm less convinced that adding a
simd_enabled parameter to CopyReadLineText() helps, because 1) it's
involved in fewer branches and 2) we change it within the function, so the
compiler can't remove the branches, anyway. But perhaps I'm missing
something.
Some other random thoughts:
+ match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
+ match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
Since \n and \r are well below "normal" ASCII values, I wonder if we could
simplify these to something like
match = vector8_gt(... vector with all lanes set to \r + 1 ..., chunk);
+ /* Check if we found any special characters */
+ mask = vector8_highbit_mask(match);
+ if (mask != 0)
vector8_highbit_mask() is somewhat expensive on AArch64, so I wonder if
waiting until we enter the "if" block to calculate it has any benefit.
+ simd_hit_eol = (c1 == '\r' || c1 == '\n') && (!is_csv || !in_quote);
If (is_csv && in_quote), we shouldn't have picked up \r or \n in the first
place, right?
+ simd_hit_eof = c1 == '\\' && c2 == '.' && !is_csv;
+
+ /*
+ * Do not disable SIMD when we hit EOL or EOF characters. In
+ * practice, it does not matter for EOF because parsing ends
+ * there, but we keep the behavior consistent.
+ */
+ if (!(simd_hit_eof || simd_hit_eol))
I'd think that doing less unnecessary work would outweigh the benefits of
consistency for the EOF case.
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Feb 13, 2026 at 09:34:13PM -0600, Manni Wood wrote: > v7-0001 + v7-0002 applied to master certainly seems promising: nice to see > speed improvements across the board on both x86 and arm! Thanks for testing. Based on these results, I think we can abandon 0002, at least for now. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Mon, Feb 16, 2026 at 11:04:58AM -0600, Nathan Bossart wrote: > On Fri, Feb 13, 2026 at 09:34:13PM -0600, Manni Wood wrote: >> v7-0001 + v7-0002 applied to master certainly seems promising: nice to see >> speed improvements across the board on both x86 and arm! > > Thanks for testing. Based on these results, I think we can abandon 0002, > at least for now. Have you tested small rows, i.e., less than 16 bytes per row? I'm wondering if that regresses at all. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Mon, Feb 16, 2026 at 11:04:58AM -0600, Nathan Bossart wrote:
> On Fri, Feb 13, 2026 at 09:34:13PM -0600, Manni Wood wrote:
>> v7-0001 + v7-0002 applied to master certainly seems promising: nice to see
>> speed improvements across the board on both x86 and arm!
>
> Thanks for testing. Based on these results, I think we can abandon 0002,
> at least for now.
Have you tested small rows, i.e., less than 16 bytes per row? I'm
wondering if that regresses at all.
--
nathan
$ head t_none.txt
BB AA
BB AA
BB AA
$ head t_none.csv
BB,AA
BB,AA
BB,AA
$ head t_escape.txt
B\\B A\\A
B\\B A\\A
B\\B A\\A
$ head t_quote.csv
"B""B","A""A"
"B""B","A""A"
"B""B","A""A"
Here are the results on my x86 tower and my arm raspberry pi 5:
x86 NARROW master copy from
TXT : 2477.022500 ms
CSV : 2825.095500 ms
TXT with 1/3 escapes: 2620.575000 ms
CSV with 1/3 quotes: 3249.058750 ms
x86 NARROW v70001 copy from
TXT : 2475.659000 ms 0.055046% improvement
CSV : 2421.976750 ms 14.269208% improvement
TXT with 1/3 escapes: 2660.953750 ms -1.540836% regression
CSV with 1/3 quotes: 3255.546750 ms -0.199689% regression
x86 NARROW v70002 copy from
TXT : 2481.372250 ms -0.175604% regression
CSV : 2437.541250 ms 13.718271% improvement
TXT with 1/3 escapes: 2646.300000 ms -0.981655% regression
CSV with 1/3 quotes: 3202.014500 ms 1.447935% improvement
arm NARROW master copy from
TXT : 2294.270500 ms
CSV : 2085.839000 ms
TXT with 1/3 escapes: 2467.966000 ms
CSV with 1/3 quotes: 2485.533000 ms
arm NARROW v70001 copy from
TXT : 1982.497500 ms 13.589200% improvement
CSV : 2005.829500 ms 3.835843% improvement
TXT with 1/3 escapes: 2111.778250 ms 14.432442% improvement
CSV with 1/3 quotes: 2441.370000 ms 1.776802% improvement
arm NARROW v70002 copy from
TXT : 1975.982250 ms 13.873179% improvement
CSV : 2022.744000 ms 3.024922% improvement
TXT with 1/3 escapes: 2080.273000 ms 15.709009% improvement
CSV with 1/3 quotes: 2476.819000 ms 0.350589% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Mon, Feb 16, 2026 at 11:01:21PM -0600, Manni Wood wrote: > Here are the results on my x86 tower and my arm raspberry pi 5: > > x86 NARROW master copy from > TXT : 2477.022500 ms > CSV : 2825.095500 ms > TXT with 1/3 escapes: 2620.575000 ms > CSV with 1/3 quotes: 3249.058750 ms > > x86 NARROW v70001 copy from > TXT : 2475.659000 ms 0.055046% improvement > CSV : 2421.976750 ms 14.269208% improvement > TXT with 1/3 escapes: 2660.953750 ms -1.540836% regression > CSV with 1/3 quotes: 3255.546750 ms -0.199689% regression > > x86 NARROW v70002 copy from > TXT : 2481.372250 ms -0.175604% regression > CSV : 2437.541250 ms 13.718271% improvement > TXT with 1/3 escapes: 2646.300000 ms -0.981655% regression > CSV with 1/3 quotes: 3202.014500 ms 1.447935% improvement > > > arm NARROW master copy from > TXT : 2294.270500 ms > CSV : 2085.839000 ms > TXT with 1/3 escapes: 2467.966000 ms > CSV with 1/3 quotes: 2485.533000 ms > > arm NARROW v70001 copy from > TXT : 1982.497500 ms 13.589200% improvement > CSV : 2005.829500 ms 3.835843% improvement > TXT with 1/3 escapes: 2111.778250 ms 14.432442% improvement > CSV with 1/3 quotes: 2441.370000 ms 1.776802% improvement > > arm NARROW v70002 copy from > TXT : 1975.982250 ms 13.873179% improvement > CSV : 2022.744000 ms 3.024922% improvement > TXT with 1/3 escapes: 2080.273000 ms 15.709009% improvement > CSV with 1/3 quotes: 2476.819000 ms 0.350589% improvement Thanks. I don't see anything too terribly concerning here, but we should still plan to redo these tests once the patch is ready for commit. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 14 Feb 2026 at 02:09, Nathan Bossart wrote:
>
> On Fri, Feb 13, 2026 at 02:45:30PM +0300, Nazir Bilal Yavuz wrote:
> > Also, if I change this code to:
> >
> > if (cstate->simd_enabled)
> > {
> > if (is_csv)
> > result = CopyReadLineText(cstate, true, true);
> > else
> > result = CopyReadLineText(cstate, false, true);
> > }
> > else
> > {
> > if (is_csv)
> > result = CopyReadLineText(cstate, true, false);
> > else
> > result = CopyReadLineText(cstate, false, false);
> > }
> >
> > then I see ~%5 performance improvement in scalar path compared to master.
>
> Hm. What difference do you see if you just do
>
> if (is_csv)
> result = CopyReadLineText(cstate, true);
> else
> result = CopyReadLineText(cstate, false);
>
> both with and without the SIMD stuff? IIUC this is allowing the compiler
> to remove several branches in CopyReadLineText(), which might be a nice
> improvement on its own. That being said, I'm less convinced that adding a
> simd_enabled parameter to CopyReadLineText() helps, because 1) it's
> involved in fewer branches and 2) we change it within the function, so the
> compiler can't remove the branches, anyway. But perhaps I'm missing
> something.
I did couple of benchmarks, some info about them:
- Benchmarks show percentage comparisons of timings against the master
branch. Positive values mean a regression, while negative values mean
an improvement.
- There are a total 200000 lines in each input and each line is 8192 bytes.
- For the columns, none means there is no special character. The other
numbers represent the ratio of normal characters to special
characters. For example, 0 means all the data is special characters, 4
means %25 of the data is special characters, 16 means 1/16 of the data
is special characters and such.
--------------------
This is the benchmark without the SIMD stuff:
- only_inline: Only change is CopyReadLineText() being inlined.
- is_csv_verbose-wo-inline: is_csv is sent as a constant boolean like
you suggested but CopyReadLineText() isn't inlined.
- is_csv_verbose-w-inline: is_csv is sent as a constant boolean and
CopyReadLineText() is inlined.
+-------------------------------+-------+------+------+-------+-------+------+
| TEXT | none | 0 | 4 | 8 | 16 | 32 |
+-------------------------------+-------+------+------+-------+-------+------+
| only_inline | 0 | +1.6 | 0 | 0 | -1 | 0 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-wo-inline | 0 | 0 | 0 | 0 | 0 | 0 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-w-inline | -3.5 | 0 | -7.7 | -2.3 | -4.1 | -3.4 |
+-------------------------------+-------+------+------+-------+-------+------+
| | | | | | | |
+-------------------------------+-------+------+------+-------+-------+------+
| | | | | | | |
+-------------------------------+-------+------+------+-------+-------+------+
| CSV | none | 0 | 4 | 8 | 16 | 32 |
+-------------------------------+-------+------+------+-------+-------+------+
| only_inline | -1.1 | 0 | 0 | 0 | 0 | -1 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-wo-inline | 0 | 0 | 0 | 0 | -0.3 | 0 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-w-inline | -4 | -2.3 | -1.2 | -1.8 | -2.8 | -2.7 |
+-------------------------------+-------+------+------+-------+-------+------+
By looking the benchmark results,
if (is_csv)
result = CopyReadLineText(cstate, true);
else
result = CopyReadLineText(cstate, false);
with inline CopyReadLineText() function helps the performance even without SIMD.
----------------------------------------
This is the same benchmark with SIMD stuff:
only_inline: Only change is CopyReadLineText() being inlined, there is
no simd_enabled argument in the CopyReadLineText().
is_csv_verbose-w-inline: is_csv is sent as a constant boolean and
CopyReadLineText() is inlined. There is no simd_enabled argument in
the CopyReadLineText().
simd_enabled_verbose-w-inline: simd_enabled is sent as a constant
boolean and CopyReadLineText() is inlined.
both_verbose_w_inline: both is_csv and simd_enabled are sent as a
constant boolean and CopyReadLineText() is inlined.
+-------------------------------+-------+------+------+-------+-------+------+
| TEXT | none | 0 | 4 | 8 | 16 | 32 |
+-------------------------------+-------+------+------+-------+-------+------+
| only_inline | -11 | +9.7 | +9.1 | +11.4 | +14.8 | +8 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-wo-inline | -11.9 | +4.5 | +2.4 | +3.5 | +2.2 | +1.8 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-w-inline | -12.6 | 0 | -2.4 | +2.8 | +1.6 | 0 |
+-------------------------------+-------+------+------+-------+-------+------+
| both_verbose_w_inline | -12.1 | 0 | -5 | 0 | +2.5 | -1.8 |
+-------------------------------+-------+------+------+-------+-------+------+
| | | | | | | |
+-------------------------------+-------+------+------+-------+-------+------+
| CSV | none | 0 | 4 | 8 | 16 | 32 |
+-------------------------------+-------+------+------+-------+-------+------+
| only_inline | -22.6 | +4.2 | +2.1 | +2.6 | 0 | +2.2 |
+-------------------------------+-------+------+------+-------+-------+------+
| is_csv_verbose-w-inline | -22.5 | -2.1 | -3.4 | -3.9 | -6.4 | -3.4 |
+-------------------------------+-------+------+------+-------+-------+------+
| simd_enabled_verbose-w-inline | -23 | 0 | -1.9 | -2.2 | -4.8 | -1.6 |
+-------------------------------+-------+------+------+-------+-------+------+
| both_verbose_w_inline | -23.3 | -2.9 | -5 | -4.5 | -7.1 | -4.3 |
+-------------------------------+-------+------+------+-------+-------+------+
By looking at these results having both is_csv and simd_enabled as an
argument and sending them as constant boolean arguments help most.
>
> Some other random thoughts:
I am sending the benchmark results for now. I haven't tested other
suggestions yet. I will follow up with another email once I have
tested them.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Feb 18, 2026 at 04:38:07PM +0300, Nazir Bilal Yavuz wrote: > By looking at these results having both is_csv and simd_enabled as an > argument and sending them as constant boolean arguments help most. Thanks for doing these tests. ISTM we might as well get this initial inlining stuff committed. Thoughts? -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 19 Feb 2026 at 00:26, Nathan Bossart wrote: > > On Wed, Feb 18, 2026 at 04:38:07PM +0300, Nazir Bilal Yavuz wrote: > > By looking at these results having both is_csv and simd_enabled as an > > argument and sending them as constant boolean arguments help most. > > Thanks for doing these tests. ISTM we might as well get this initial > inlining stuff committed. Thoughts? nitpick: -static bool CopyReadLineText(CopyFromState cstate, bool is_csv); +static pg_attribute_always_inline bool CopyReadLineText(CopyFromState cstate, + bool is_csv); Do we want to move the new CopyReadLineText() declaration below to group it with the other functions marked pg_attribute_always_inline? Other than that, LGTM. I think it makes sense to separately commit this. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Thu, 19 Feb 2026 at 00:26, Nathan Bossart <nathandbossart@gmail.com> wrote:
>
> On Wed, Feb 18, 2026 at 04:38:07PM +0300, Nazir Bilal Yavuz wrote:
> > By looking at these results having both is_csv and simd_enabled as an
> > argument and sending them as constant boolean arguments help most.
>
> Thanks for doing these tests. ISTM we might as well get this initial
> inlining stuff committed. Thoughts?
nitpick:
-static bool CopyReadLineText(CopyFromState cstate, bool is_csv);
+static pg_attribute_always_inline bool CopyReadLineText(CopyFromState cstate,
+ bool is_csv);
Do we want to move the new CopyReadLineText() declaration below to
group it with the other functions marked pg_attribute_always_inline?
Other than that, LGTM. I think it makes sense to separately commit this.
--
Regards,
Nazir Bilal Yavuz
Microsoft
TXT : 2587.642000 ms
CSV : 2621.759000 ms
TXT with 1/3 escapes: 2707.933500 ms
CSV with 1/3 quotes: 3254.896500 ms
x86 v8 NARROW
TXT : 2488.655250 ms 3.825365% improvement
CSV : 2628.818000 ms -0.269247% regression
TXT with 1/3 escapes: 2615.522000 ms 3.412621% improvement
CSV with 1/3 quotes: 3446.368000 ms -5.882568% regression
x86 master WIDE
TXT : 30583.229500 ms
CSV : 35054.533500 ms
TXT with 1/3 escapes: 32767.421500 ms
CSV with 1/3 quotes: 44214.163500 ms
x86 v8 WIDE
TXT : 26527.494250 ms 13.261305% improvement
CSV : 33364.443750 ms 4.821316% improvement
TXT with 1/3 escapes: 29320.648000 ms 10.518904% improvement
CSV with 1/3 quotes: 42334.074750 ms 4.252232% improvement
arm master NARROW
TXT : 1999.401000 ms
CSV : 2081.610750 ms
TXT with 1/3 escapes: 2053.230250 ms
CSV with 1/3 quotes: 2431.608750 ms
arm v8 NARROW
TXT : 1981.663750 ms 0.887128% improvement
CSV : 2023.892500 ms 2.772769% improvement
TXT with 1/3 escapes: 2004.215250 ms 2.387214% improvement
CSV with 1/3 quotes: 2616.872750 ms -7.618989% regression
arm master WIDE
TXT : 9120.731750 ms
CSV : 11114.478250 ms
TXT with 1/3 escapes: 10338.124500 ms
CSV with 1/3 quotes: 13404.430250 ms
arm v8 WIDE
TXT : 8430.090750 ms 7.572210% improvement
CSV : 10115.135500 ms 8.991360% improvement
TXT with 1/3 escapes: 9624.383500 ms 6.903970% improvement
CSV with 1/3 quotes: 12331.714000 ms 8.002699% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Thu, 19 Feb 2026 at 07:02, Manni Wood wrote: > > I took some time tonight to apply v8 to the latest master (759b03b2) on my x86 tower and arm raspberry pi 5. > > Here are the results, using both narrow columns and the wider columns we've been using througout: > > x86 master NARROW > TXT : 2587.642000 ms > CSV : 2621.759000 ms > TXT with 1/3 escapes: 2707.933500 ms > CSV with 1/3 quotes: 3254.896500 ms > > x86 v8 NARROW > TXT : 2488.655250 ms 3.825365% improvement > CSV : 2628.818000 ms -0.269247% regression > TXT with 1/3 escapes: 2615.522000 ms 3.412621% improvement > CSV with 1/3 quotes: 3446.368000 ms -5.882568% regression > > x86 master WIDE > TXT : 30583.229500 ms > CSV : 35054.533500 ms > TXT with 1/3 escapes: 32767.421500 ms > CSV with 1/3 quotes: 44214.163500 ms > > x86 v8 WIDE > TXT : 26527.494250 ms 13.261305% improvement > CSV : 33364.443750 ms 4.821316% improvement > TXT with 1/3 escapes: 29320.648000 ms 10.518904% improvement > CSV with 1/3 quotes: 42334.074750 ms 4.252232% improvement > > > > arm master NARROW > TXT : 1999.401000 ms > CSV : 2081.610750 ms > TXT with 1/3 escapes: 2053.230250 ms > CSV with 1/3 quotes: 2431.608750 ms > > arm v8 NARROW > TXT : 1981.663750 ms 0.887128% improvement > CSV : 2023.892500 ms 2.772769% improvement > TXT with 1/3 escapes: 2004.215250 ms 2.387214% improvement > CSV with 1/3 quotes: 2616.872750 ms -7.618989% regression > > arm master WIDE > TXT : 9120.731750 ms > CSV : 11114.478250 ms > TXT with 1/3 escapes: 10338.124500 ms > CSV with 1/3 quotes: 13404.430250 ms > > arm v8 WIDE > TXT : 8430.090750 ms 7.572210% improvement > CSV : 10115.135500 ms 8.991360% improvement > TXT with 1/3 escapes: 9624.383500 ms 6.903970% improvement > CSV with 1/3 quotes: 12331.714000 ms 8.002699% improvement Thank you for the results, they are interesting. I didn't expect to see any regression for this benchmark. Also, I would expect the non-special character cases and the 1/3 special character cases to perform similarly, since we are not using SIMD for this benchmark. I noticed that the timings in your narrow benchmark (both x86 and ARM) are quite short. Would it be possible to extend the test so that the total runtime is closer to ~10,000 ms? That might give us more stable results. Here is my benchmark with using your script: WIDE: Total 500000 lines and each line is 4096 bytes. NARROW: Total 1500000 lines and each line is 2-4 bytes (`"A""A"` and `A\\A`). +---------+---------------+---------------+---------------+----------------+ | WIDE | TXT None | TXT 1/3 | CSV None | CSV 1/3 | +---------+---------------+---------------+---------------+----------------+ | master | 10512 | 11133 | 12241 | 14321 | +---------+---------------+---------------+---------------+----------------+ | patched | 10000 (-%4.8) | 10804 (-%2.9) | 11571 (-%5.4) | 14008 (-%2.18) | +---------+---------------+---------------+---------------+----------------+ | | | | | | +---------+---------------+---------------+---------------+----------------+ | NARROW | | | | | +---------+---------------+---------------+---------------+----------------+ | master | 9702 | 9745 | 9784 | 10149 | +---------+---------------+---------------+---------------+----------------+ | patched | 9344 (-%3.6) | 9477 (-%2.7) | 9439 (-%3.5) | 9751 (-%3.9) | +---------+---------------+---------------+---------------+----------------+ The results look promising to me. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
sudo cpupower idle-set -D 0
echo "1" | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo
WIDE (500k rows)
TXT | none
Master avg: 22,183 ms
New avg: 20,435 ms
Improvement: -7.88%
CSV | none
Master avg: 26,737 ms
New avg: 24,625 ms
Improvement: -7.90%
TXT | escape
Master avg: 26,720 ms
New avg: 23,658 ms
Improvement: -11.46%
CSV | quote
Master avg: 35,961 ms
New avg: 33,317 ms
Improvement: -7.35%
--------------------------------------
NARROW (1.5M rows)
TXT | none
Master avg: 2,220 ms
New avg: 2,125 ms
Improvement: -4.28%
CSV | none
Master avg: 2,330 ms
New avg: 2,145 ms
Improvement: -7.92%
TXT | escape
Master avg: 2,425 ms
New avg: 2,187 ms
Improvement: -9.79%
CSV | quote
Master avg: 2,272 ms
New avg: 2,253 ms
Improvement: -0.85%
No regressions as expected, overall this looks good.
Regards,
Ayoub
Hi,
On Thu, 19 Feb 2026 at 07:02, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> I took some time tonight to apply v8 to the latest master (759b03b2) on my x86 tower and arm raspberry pi 5.
>
> Here are the results, using both narrow columns and the wider columns we've been using througout:
>
> x86 master NARROW
> TXT : 2587.642000 ms
> CSV : 2621.759000 ms
> TXT with 1/3 escapes: 2707.933500 ms
> CSV with 1/3 quotes: 3254.896500 ms
>
> x86 v8 NARROW
> TXT : 2488.655250 ms 3.825365% improvement
> CSV : 2628.818000 ms -0.269247% regression
> TXT with 1/3 escapes: 2615.522000 ms 3.412621% improvement
> CSV with 1/3 quotes: 3446.368000 ms -5.882568% regression
>
> x86 master WIDE
> TXT : 30583.229500 ms
> CSV : 35054.533500 ms
> TXT with 1/3 escapes: 32767.421500 ms
> CSV with 1/3 quotes: 44214.163500 ms
>
> x86 v8 WIDE
> TXT : 26527.494250 ms 13.261305% improvement
> CSV : 33364.443750 ms 4.821316% improvement
> TXT with 1/3 escapes: 29320.648000 ms 10.518904% improvement
> CSV with 1/3 quotes: 42334.074750 ms 4.252232% improvement
>
>
>
> arm master NARROW
> TXT : 1999.401000 ms
> CSV : 2081.610750 ms
> TXT with 1/3 escapes: 2053.230250 ms
> CSV with 1/3 quotes: 2431.608750 ms
>
> arm v8 NARROW
> TXT : 1981.663750 ms 0.887128% improvement
> CSV : 2023.892500 ms 2.772769% improvement
> TXT with 1/3 escapes: 2004.215250 ms 2.387214% improvement
> CSV with 1/3 quotes: 2616.872750 ms -7.618989% regression
>
> arm master WIDE
> TXT : 9120.731750 ms
> CSV : 11114.478250 ms
> TXT with 1/3 escapes: 10338.124500 ms
> CSV with 1/3 quotes: 13404.430250 ms
>
> arm v8 WIDE
> TXT : 8430.090750 ms 7.572210% improvement
> CSV : 10115.135500 ms 8.991360% improvement
> TXT with 1/3 escapes: 9624.383500 ms 6.903970% improvement
> CSV with 1/3 quotes: 12331.714000 ms 8.002699% improvement
Thank you for the results, they are interesting. I didn't expect to
see any regression for this benchmark. Also, I would expect the
non-special character cases and the 1/3 special character cases to
perform similarly, since we are not using SIMD for this benchmark.
I noticed that the timings in your narrow benchmark (both x86 and ARM)
are quite short. Would it be possible to extend the test so that the
total runtime is closer to ~10,000 ms? That might give us more stable
results.
Here is my benchmark with using your script:
WIDE: Total 500000 lines and each line is 4096 bytes.
NARROW: Total 1500000 lines and each line is 2-4 bytes (`"A""A"` and `A\\A`).
+---------+---------------+---------------+---------------+----------------+
| WIDE | TXT None | TXT 1/3 | CSV None | CSV 1/3 |
+---------+---------------+---------------+---------------+----------------+
| master | 10512 | 11133 | 12241 | 14321 |
+---------+---------------+---------------+---------------+----------------+
| patched | 10000 (-%4.8) | 10804 (-%2.9) | 11571 (-%5.4) | 14008 (-%2.18) |
+---------+---------------+---------------+---------------+----------------+
| | | | | |
+---------+---------------+---------------+---------------+----------------+
| NARROW | | | | |
+---------+---------------+---------------+---------------+----------------+
| master | 9702 | 9745 | 9784 | 10149 |
+---------+---------------+---------------+---------------+----------------+
| patched | 9344 (-%3.6) | 9477 (-%2.7) | 9439 (-%3.5) | 9751 (-%3.9) |
+---------+---------------+---------------+---------------+----------------+
The results look promising to me.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
sudo cpupower frequency-set --governor=performanceThis is on an Intel I7-1255U CPU with:Hello,I ran some long benchmarks on this, and I got stable results across multiple runs (few milliseconds difference)
sudo cpupower idle-set -D 0
echo "1" | sudo tee /sys/devices/system/cpu/intel_pstate/no_turboWIDE (500k rows)
TXT | none
Master avg: 22,183 ms
New avg: 20,435 ms
Improvement: -7.88%CSV | none
Master avg: 26,737 ms
New avg: 24,625 ms
Improvement: -7.90%TXT | escape
Master avg: 26,720 ms
New avg: 23,658 ms
Improvement: -11.46%CSV | quote
Master avg: 35,961 ms
New avg: 33,317 ms
Improvement: -7.35%--------------------------------------
NARROW (1.5M rows)TXT | none
Master avg: 2,220 ms
New avg: 2,125 ms
Improvement: -4.28%CSV | none
Master avg: 2,330 ms
New avg: 2,145 ms
Improvement: -7.92%TXT | escape
Master avg: 2,425 ms
New avg: 2,187 ms
Improvement: -9.79%CSV | quote
Master avg: 2,272 ms
New avg: 2,253 ms
Improvement: -0.85%No regressions as expected, overall this looks good.
Regards,
Ayoub
On Thu, Feb 19, 2026 at 10:01 AM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
On Thu, 19 Feb 2026 at 07:02, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> I took some time tonight to apply v8 to the latest master (759b03b2) on my x86 tower and arm raspberry pi 5.
>
> Here are the results, using both narrow columns and the wider columns we've been using througout:
>
> x86 master NARROW
> TXT : 2587.642000 ms
> CSV : 2621.759000 ms
> TXT with 1/3 escapes: 2707.933500 ms
> CSV with 1/3 quotes: 3254.896500 ms
>
> x86 v8 NARROW
> TXT : 2488.655250 ms 3.825365% improvement
> CSV : 2628.818000 ms -0.269247% regression
> TXT with 1/3 escapes: 2615.522000 ms 3.412621% improvement
> CSV with 1/3 quotes: 3446.368000 ms -5.882568% regression
>
> x86 master WIDE
> TXT : 30583.229500 ms
> CSV : 35054.533500 ms
> TXT with 1/3 escapes: 32767.421500 ms
> CSV with 1/3 quotes: 44214.163500 ms
>
> x86 v8 WIDE
> TXT : 26527.494250 ms 13.261305% improvement
> CSV : 33364.443750 ms 4.821316% improvement
> TXT with 1/3 escapes: 29320.648000 ms 10.518904% improvement
> CSV with 1/3 quotes: 42334.074750 ms 4.252232% improvement
>
>
>
> arm master NARROW
> TXT : 1999.401000 ms
> CSV : 2081.610750 ms
> TXT with 1/3 escapes: 2053.230250 ms
> CSV with 1/3 quotes: 2431.608750 ms
>
> arm v8 NARROW
> TXT : 1981.663750 ms 0.887128% improvement
> CSV : 2023.892500 ms 2.772769% improvement
> TXT with 1/3 escapes: 2004.215250 ms 2.387214% improvement
> CSV with 1/3 quotes: 2616.872750 ms -7.618989% regression
>
> arm master WIDE
> TXT : 9120.731750 ms
> CSV : 11114.478250 ms
> TXT with 1/3 escapes: 10338.124500 ms
> CSV with 1/3 quotes: 13404.430250 ms
>
> arm v8 WIDE
> TXT : 8430.090750 ms 7.572210% improvement
> CSV : 10115.135500 ms 8.991360% improvement
> TXT with 1/3 escapes: 9624.383500 ms 6.903970% improvement
> CSV with 1/3 quotes: 12331.714000 ms 8.002699% improvement
Thank you for the results, they are interesting. I didn't expect to
see any regression for this benchmark. Also, I would expect the
non-special character cases and the 1/3 special character cases to
perform similarly, since we are not using SIMD for this benchmark.
I noticed that the timings in your narrow benchmark (both x86 and ARM)
are quite short. Would it be possible to extend the test so that the
total runtime is closer to ~10,000 ms? That might give us more stable
results.
Here is my benchmark with using your script:
WIDE: Total 500000 lines and each line is 4096 bytes.
NARROW: Total 1500000 lines and each line is 2-4 bytes (`"A""A"` and `A\\A`).
+---------+---------------+---------------+---------------+----------------+
| WIDE | TXT None | TXT 1/3 | CSV None | CSV 1/3 |
+---------+---------------+---------------+---------------+----------------+
| master | 10512 | 11133 | 12241 | 14321 |
+---------+---------------+---------------+---------------+----------------+
| patched | 10000 (-%4.8) | 10804 (-%2.9) | 11571 (-%5.4) | 14008 (-%2.18) |
+---------+---------------+---------------+---------------+----------------+
| | | | | |
+---------+---------------+---------------+---------------+----------------+
| NARROW | | | | |
+---------+---------------+---------------+---------------+----------------+
| master | 9702 | 9745 | 9784 | 10149 |
+---------+---------------+---------------+---------------+----------------+
| patched | 9344 (-%3.6) | 9477 (-%2.7) | 9439 (-%3.5) | 9751 (-%3.9) |
+---------+---------------+---------------+---------------+----------------+
The results look promising to me.
--
Regards,
Nazir Bilal Yavuz
Microsoft
TXT : 30602.244000 ms
CSV : 35062.451250 ms
TXT with 1/3 escapes: 32704.250250 ms
CSV with 1/3 quotes: 44128.072500 ms
x86 WIDE v8 500,000 rows
TXT : 26611.953250 ms 13.039210% improvement
CSV : 33366.184000 ms 4.837846% improvement
TXT with 1/3 escapes: 29251.310000 ms 10.558078% improvement
CSV with 1/3 quotes: 42368.421000 ms 3.987601% improvement
x86 NARROW master 50mil rows
TXT : 25898.004000 ms
CSV : 27212.684500 ms
TXT with 1/3 escapes: 29189.518250 ms
CSV with 1/3 quotes: 33222.510250 ms
x86 NARROW v8 50mil rows
TXT : 26368.765000 ms -1.817750% regression
CSV : 26711.122250 ms 1.843119% improvement
TXT with 1/3 escapes: 28081.150750 ms 3.797142% improvement
CSV with 1/3 quotes: 32851.963500 ms 1.115348% improvement
arm WIDE master 250,000 rows
TXT : 11392.462750 ms
CSV : 13887.576500 ms
TXT with 1/3 escapes: 12908.560750 ms
CSV with 1/3 quotes: 16699.337000 ms
arm WIDE v8 250,000 rows
TXT : 10524.567750 ms 7.618151% improvement
CSV : 12621.211250 ms 9.118691% improvement
TXT with 1/3 escapes: 12017.030250 ms 6.906506% improvement
CSV with 1/3 quotes: 15428.020500 ms 7.612976% improvement
arm NARROW master 25mil rows
TXT : 10030.274000 ms
CSV : 10245.238750 ms
TXT with 1/3 escapes: 10345.224500 ms
CSV with 1/3 quotes: 12186.313250 ms
arm NARROW v8 25mil rows
TXT : 10197.386500 ms -1.666081% regression
CSV : 10257.918750 ms -0.123765% regression
TXT with 1/3 escapes: 10084.978500 ms 2.515615% improvement
CSV with 1/3 quotes: 12064.215000 ms 1.001929% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Fri, 20 Feb 2026 at 03:09, Manni Wood wrote: > > Thanks for running benchmarks, Ayoub. > > Nazir, I ran my benchmarks with more rows this time --- as many rows as would fit on my test computers without exhausting their RAM disks. That seems to have brought things more into line with what Ayoub saw. I did get some small regressions, but I suspect those are not a big deal. (For instance, on both machines I also noticed the occasional "truncate table" would take longer than the others, despite my scripts' best efforts to steady a CPU core and pin postmaster and children to that core.) Thank you both for the benchmarks. Results look good to me! -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Sat, 14 Feb 2026 at 02:09, Nathan Bossart wrote: > > Some other random thoughts: > > + match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr)); > > + match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr)); > > Since \n and \r are well below "normal" ASCII values, I wonder if we could > simplify these to something like > > match = vector8_gt(... vector with all lanes set to \r + 1 ..., chunk); This didn't work because we have horizontal tab characters whose value (9) is lower than '\r' (13). > + /* Check if we found any special characters */ > + mask = vector8_highbit_mask(match); > + if (mask != 0) > > vector8_highbit_mask() is somewhat expensive on AArch64, so I wonder if > waiting until we enter the "if" block to calculate it has any benefit. I think this makes sense, done. > + simd_hit_eol = (c1 == '\r' || c1 == '\n') && (!is_csv || !in_quote); > > If (is_csv && in_quote), we shouldn't have picked up \r or \n in the first > place, right? You are right, I put an assertion for this. > + simd_hit_eof = c1 == '\\' && c2 == '.' && !is_csv; > + > + /* > + * Do not disable SIMD when we hit EOL or EOF characters. In > + * practice, it does not matter for EOF because parsing ends > + * there, but we keep the behavior consistent. > + */ > + if (!(simd_hit_eof || simd_hit_eol)) > > I'd think that doing less unnecessary work would outweigh the benefits of > consistency for the EOF case. This will work once for the data since SIMD will be disabled afterwards. So, I think this shouldn't affect the performance but I am okay to change if you prefer. I have bencharmed v9 and didn't see any regression. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Feb 20, 2026 at 12:50:35PM +0300, Nazir Bilal Yavuz wrote: > On Fri, 20 Feb 2026 at 03:09, Manni Wood wrote: >> Nazir, I ran my benchmarks with more rows this time --- as many rows as >> would fit on my test computers without exhausting their RAM disks. That >> seems to have brought things more into line with what Ayoub saw. I did >> get some small regressions, but I suspect those are not a big deal. (For >> instance, on both machines I also noticed the occasional "truncate >> table" would take longer than the others, despite my scripts' best >> efforts to steady a CPU core and pin postmaster and children to that >> core.) Yeah, the couple of small regressions seem close to (or below) the noise level, and IIUC yours were the only benchmarks that showed them, anyway. Plus, I think we'll need this change regardless as a prerequisite for the SIMD work. > Thank you both for the benchmarks. Results look good to me! Committed that part. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Fri, 20 Feb 2026 at 21:15, Nathan Bossart wrote: > > Yeah, the couple of small regressions seem close to (or below) the noise > level, and IIUC yours were the only benchmarks that showed them, anyway. > Plus, I think we'll need this change regardless as a prerequisite for the > SIMD work. > > > Thank you both for the benchmarks. Results look good to me! > > Committed that part. Thank you! Attaching the SIMD patch only. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Fri, 20 Feb 2026 at 21:15, Nathan Bossart <nathandbossart@gmail.com> wrote:
>
> Yeah, the couple of small regressions seem close to (or below) the noise
> level, and IIUC yours were the only benchmarks that showed them, anyway.
> Plus, I think we'll need this change regardless as a prerequisite for the
> SIMD work.
>
> > Thank you both for the benchmarks. Results look good to me!
>
> Committed that part.
Thank you! Attaching the SIMD patch only.
--
Regards,
Nazir Bilal Yavuz
Microsoft
TXT : 26359.319000 ms
CSV : 25661.199750 ms
TXT with 1/3 escapes: 28170.085250 ms
CSV with 1/3 quotes: 32638.147500 ms
x86 NARROW v10 50,000,000 rows
TXT : 26416.331500 ms -0.216290% regression
CSV : 25318.727500 ms 1.334592% improvement
TXT with 1/3 escapes: 28608.007500 ms -1.554565% regression
CSV with 1/3 quotes: 32805.627750 ms -0.513143% regression
x86 WIDE master 500,000 rows
TXT : 26475.164250 ms
CSV : 31963.478500 ms
TXT with 1/3 escapes: 29671.120750 ms
CSV with 1/3 quotes: 40391.616250 ms
x86 WIDE v10 500,000 rows
TXT : 23067.046750 ms 12.872885% improvement
CSV : 23259.092250 ms 27.232287% improvement
TXT with 1/3 escapes: 31796.098250 ms -7.161770% regression
CSV with 1/3 quotes: 42925.792250 ms -6.274015% regression
arm NARROW master 25,000,000 rows
TXT : 10077.096250 ms
CSV : 10310.671250 ms
TXT with 1/3 escapes: 9893.155000 ms
CSV with 1/3 quotes: 12133.064750 ms
arm NARROW v10 25,000,000 rows
TXT : 10467.816750 ms -3.877312% regression
CSV : 9986.288000 ms 3.146092% improvement
TXT with 1/3 escapes: 10323.173750 ms -4.346629% regression
CSV with 1/3 quotes: 11843.611750 ms 2.385654% improvement
arm WIDE master 250,000 rows
TXT : 10568.344750 ms
CSV : 13046.610500 ms
TXT with 1/3 escapes: 12193.088500 ms
CSV with 1/3 quotes: 16629.319000 ms
arm WIDE v10 250,000 rows
TXT : 9064.959000 ms 14.225366% improvement
CSV : 9019.553250 ms 30.866693% improvement
TXT with 1/3 escapes: 12344.497250 ms -1.241759% regression
CSV with 1/3 quotes: 15495.863750 ms 6.816005% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 24 Feb 2026 at 07:44, Manni Wood wrote: > > Hello! > > I ran some speed tests on Nazir's v10 SIMD-only patch. I'm a bit surprised at the regression for x86 with wide rows for the 1/3rd special characters scenarios. I'm hoping it's something I did wrong. If anyone else has numbers to share, that would be excellent. Thank you for doing this! I see similar regression on the wide & CSV 1/3 case by using your benchmark script. I didn't see this regression when I used my benchmark while sharing v9 [1]. +-------------+---------------------------+---------------------------+ | | Text | CSV | +-------------+-------------+-------------+-------------+-------------+ | WIDE TEST | None | 1/3 | None | 1/3 | +-------------+-------------+-------------+-------------+-------------+ | Master | 9996 | 10769 | 11548 | 13960 | +-------------+-------------+-------------+-------------+-------------+ | v10 | 8912 %-10.8 | 10902 %+1.2 | 8952 %-22.4 | 15123 %+8.3 | +-------------+-------------+-------------+-------------+-------------+ | | | | | | +-------------+-------------+-------------+-------------+-------------+ | | Text | | CSV | +-------------+-------------+-------------+-------------+-------------+ | NARROW TEST | None | 1/3 | None | 1/3 | +-------------+-------------+-------------+-------------+-------------+ | Master | 9441 | 9561 | 9734 | 9830 | +-------------+-------------+-------------+-------------+-------------+ | v10 | 9291 %-1.5 | 9504 -%0.5 | 9644 %-0.9 | 10078 %-2.4 | +-------------+-------------+-------------+-------------+-------------+ I will investigate this. However, please note that the current master includes the inlining commit (dc592a4155), which makes the COPY FROM faster. In my case, 1: current master without dc592a4155: 14400ms 2: current master: 13960ms (%3 improvement against #1) 3: current master + SIMD: 15123ms (%5 regression against #1 and %8 regression against #2) Is it possible for you to do a similar test? I mean dropping dc592a4155 from the current master and re-running the benchmark, that would be helpful. [1] https://postgr.es/m/CAN55FZ0MiFCgK26gRgE05a%3D_ggenkxDM8H%3DA2uTHpywczqt%3D-Q%40mail.gmail.com -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Feb 24, 2026 at 2:57 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:
Hi,
On Tue, 24 Feb 2026 at 07:44, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> Hello!
>
> I ran some speed tests on Nazir's v10 SIMD-only patch. I'm a bit surprised at the regression for x86 with wide rows for the 1/3rd special characters scenarios. I'm hoping it's something I did wrong. If anyone else has numbers to share, that would be excellent.
Thank you for doing this!
I see similar regression on the wide & CSV 1/3 case by using your
benchmark script. I didn't see this regression when I used my
benchmark while sharing v9 [1].
+-------------+---------------------------+---------------------------+
| | Text | CSV |
+-------------+-------------+-------------+-------------+-------------+
| WIDE TEST | None | 1/3 | None | 1/3 |
+-------------+-------------+-------------+-------------+-------------+
| Master | 9996 | 10769 | 11548 | 13960 |
+-------------+-------------+-------------+-------------+-------------+
| v10 | 8912 %-10.8 | 10902 %+1.2 | 8952 %-22.4 | 15123 %+8.3 |
+-------------+-------------+-------------+-------------+-------------+
| | | | | |
+-------------+-------------+-------------+-------------+-------------+
| | Text | | CSV |
+-------------+-------------+-------------+-------------+-------------+
| NARROW TEST | None | 1/3 | None | 1/3 |
+-------------+-------------+-------------+-------------+-------------+
| Master | 9441 | 9561 | 9734 | 9830 |
+-------------+-------------+-------------+-------------+-------------+
| v10 | 9291 %-1.5 | 9504 -%0.5 | 9644 %-0.9 | 10078 %-2.4 |
+-------------+-------------+-------------+-------------+-------------+
I will investigate this. However, please note that the current master
includes the inlining commit (dc592a4155), which makes the COPY FROM
faster. In my case,
1: current master without dc592a4155: 14400ms
2: current master: 13960ms (%3 improvement against #1)
3: current master + SIMD: 15123ms (%5 regression against #1 and %8
regression against #2)
Is it possible for you to do a similar test? I mean dropping
dc592a4155 from the current master and re-running the benchmark, that
would be helpful.
[1] https://postgr.es/m/CAN55FZ0MiFCgK26gRgE05a%3D_ggenkxDM8H%3DA2uTHpywczqt%3D-Q%40mail.gmail.com
This is on an Intel I7-1255U CPU
WIDE (500k rows)
TXT | none
Master avg: 20,721 ms
New avg: 17,980 ms
Improvement: -13.23%
CSV | none
Master avg: 26,608 ms
New avg: 18,433 ms
Improvement: -30.73%
TXT | escape
Master avg: 25,069 ms
New avg: 22,910 ms
Improvement: -8.61%
CSV | quote
Master avg: 31,931 ms
New avg: 31,493 ms
Improvement: -1.37%
--------------------------------------
NARROW (15M rows)
TXT | none
Master avg: 20,687 ms
New avg: 20,824 ms
Regression: +0.67%
CSV | none
Master avg: 21,187 ms
New avg: 21,153 ms
Improvement: -0.16%
TXT | escape
Master avg: 20,870 ms
New avg: 21,341 ms
Regression: +2.25%
CSV | quote
Master avg: 22,074 ms
New avg: 22,267 ms
Regression: +0.87%
For narrow that would be mostly noise and extra branch effects.
Regards,
Ayoub
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Feb 24, 2026 at 04:57:21PM +0300, Nazir Bilal Yavuz wrote: > I will investigate this. However, please note that the current master > includes the inlining commit (dc592a4155), which makes the COPY FROM > faster. In my case, > > 1: current master without dc592a4155: 14400ms > 2: current master: 13960ms (%3 improvement against #1) > 3: current master + SIMD: 15123ms (%5 regression against #1 and %8 > regression against #2) > > Is it possible for you to do a similar test? I mean dropping > dc592a4155 from the current master and re-running the benchmark, that > would be helpful. IMHO as long as the difference from v18 looks reasonable, commit-by-commit regressions and improvements that even out in the end are okay. That's perhaps a bit of mental gymnastics (e.g., what if we had committed the inlining patch for v18?), but I believe that's how we've dealt with similar problems in the past. But maybe there are ways to avoid even these in-development regressions, too... -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Feb 24, 2026 at 04:57:21PM +0300, Nazir Bilal Yavuz wrote:
> I will investigate this. However, please note that the current master
> includes the inlining commit (dc592a4155), which makes the COPY FROM
> faster. In my case,
>
> 1: current master without dc592a4155: 14400ms
> 2: current master: 13960ms (%3 improvement against #1)
> 3: current master + SIMD: 15123ms (%5 regression against #1 and %8
> regression against #2)
>
> Is it possible for you to do a similar test? I mean dropping
> dc592a4155 from the current master and re-running the benchmark, that
> would be helpful.
IMHO as long as the difference from v18 looks reasonable, commit-by-commit
regressions and improvements that even out in the end are okay. That's
perhaps a bit of mental gymnastics (e.g., what if we had committed the
inlining patch for v18?), but I believe that's how we've dealt with similar
problems in the past. But maybe there are ways to avoid even these
in-development regressions, too...
--
nathan
TXT : 10997.568250 ms
CSV : 10797.549000 ms
TXT with 1/3 escapes: 10299.047000 ms
CSV with 1/3 quotes: 12559.385750 ms
arm NARROW master (v9 applied)
TXT : 10077.096250 ms 8.369778% improvement
CSV : 10310.671250 ms 4.509151% improvement
TXT with 1/3 escapes: 9893.155000 ms 3.941064% improvement
CSV with 1/3 quotes: 12133.064750 ms 3.394441% improvement
arm NARROW v10
TXT : 10467.816750 ms 4.816988% improvement
CSV : 9986.288000 ms 7.513381% improvement
TXT with 1/3 escapes: 10323.173750 ms -0.234262% regression
CSV with 1/3 quotes: 11843.611750 ms 5.699116% improvement
arm WIDE old master 18bcdb75
TXT : 11825.771250 ms
CSV : 13907.074000 ms
TXT with 1/3 escapes: 13430.691250 ms
CSV with 1/3 quotes: 17557.954500 ms
arm WIDE master (v9 applied)
TXT : 10568.344750 ms 10.632934% improvement
CSV : 13046.610500 ms 6.187236% improvement
TXT with 1/3 escapes: 12193.088500 ms 9.214736% improvement
CSV with 1/3 quotes: 16629.319000 ms 5.288973% improvement
arm WIDE v10
TXT : 9064.959000 ms 23.345727% improvement
CSV : 9019.553250 ms 35.144134% improvement
TXT with 1/3 escapes: 12344.497250 ms 8.087402% improvement
CSV with 1/3 quotes: 15495.863750 ms 11.744482% improvement
x86 NARROW old master 18bcdb75
TXT : 25909.060500 ms
CSV : 28137.591250 ms
TXT with 1/3 escapes: 27794.177000 ms
CSV with 1/3 quotes: 34541.704750 ms
x86 NARROW master
TXT : 26359.319000 ms -1.737842% regression
CSV : 25661.199750 ms 8.801007% improvement
TXT with 1/3 escapes: 28170.085250 ms -1.352471% regression
CSV with 1/3 quotes: 32638.147500 ms 5.510895% improvement
x86 NARROW v10
TXT : 26416.331500 ms -1.957890% regression
CSV : 25318.727500 ms 10.018142% improvement
TXT with 1/3 escapes: 28608.007500 ms -2.928061% regression
CSV with 1/3 quotes: 32805.627750 ms 5.026032% improvement
x86 WIDE old master 18bcdb75
TXT : 28778.426500 ms
CSV : 35671.908000 ms
TXT with 1/3 escapes: 32441.549750 ms
CSV with 1/3 quotes: 47024.416000 ms
x86 WIDE master
TXT : 26475.164250 ms 8.003434% improvement
CSV : 31963.478500 ms 10.395938% improvement
TXT with 1/3 escapes: 29671.120750 ms 8.539755% improvement
CSV with 1/3 quotes: 40391.616250 ms 14.105012% improvement
x86 WIDE v10
TXT : 23067.046750 ms 19.846046% improvement
CSV : 23259.092250 ms 34.797174% improvement
TXT with 1/3 escapes: 31796.098250 ms 1.989583% improvement
CSV with 1/3 quotes: 42925.792250 ms 8.715948% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 24 Feb 2026 at 20:48, Nathan Bossart wrote: > > On Tue, Feb 24, 2026 at 04:57:21PM +0300, Nazir Bilal Yavuz wrote: > > I will investigate this. However, please note that the current master > > includes the inlining commit (dc592a4155), which makes the COPY FROM > > faster. In my case, > > > > 1: current master without dc592a4155: 14400ms > > 2: current master: 13960ms (%3 improvement against #1) > > 3: current master + SIMD: 15123ms (%5 regression against #1 and %8 > > regression against #2) > > > > Is it possible for you to do a similar test? I mean dropping > > dc592a4155 from the current master and re-running the benchmark, that > > would be helpful. > > IMHO as long as the difference from v18 looks reasonable, commit-by-commit > regressions and improvements that even out in the end are okay. That's > perhaps a bit of mental gymnastics (e.g., what if we had committed the > inlining patch for v18?), but I believe that's how we've dealt with similar > problems in the past. But maybe there are ways to avoid even these > in-development regressions, too... I agree with you. However, unfortunately, I see regression on master + v10 compared to REL_18_3 (62d6c7d3df6). Thank you Kazar and Manni for benchmarks in [1] and [2]! I am still able to reproduce regression for the 'wide & CSV 1/3' case [3] by using Manni's benchmark script. I constantly see ~%5 regression, I am just curious if I am doing something wrong. I am a bit surprised because I didn't see this regression before, also Kazar and Manni don't see any regression in their [1] and [2] benchmarks. I am still investigating this regression. Hopefully, I will come back with more information soon. If anyone has any suggestions/ideas, please let me know! [1] https://postgr.es/m/CA%2BK2RukFH57QPAfTEzvy7PEyrLzav3HkyCiu-2yqR%2BuW_Niorw%40mail.gmail.com [2] https://postgr.es/m/CAKWEB6oT5KbyF%2BuRRhjjJi7p2PmRdOzxp3T6vFcN04BCR-%3DB2w%40mail.gmail.com [3] 1: current master without dc592a4155: 14400ms 2: current master: 13960ms (%3 improvement against #1) 3: current master + v10: 15123ms (%5 regression against #1 and %8 regression against #2) -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 25 Feb 2026 at 17:24, Nazir Bilal Yavuz wrote: > > I agree with you. However, unfortunately, I see regression on master + > v10 compared to REL_18_3 (62d6c7d3df6). > > Thank you Kazar and Manni for benchmarks in [1] and [2]! Kazar and Manni, if possible could you please share the build commands you use? I see regressions for an inlining patch (dc592a4155) too when I build postgres with -O2. My build commands are: -O2: meson setup buildtype=debugoptimized ... -O3: meson setup buildtype=release ... This is a wide benchmark only, old master means b2ff2a0b529 without 0001 inlining patch (dc592a4155): +--------------------------+---------------+---------------+ | | Text | CSV | +--------------------------+-------+-------+-------+-------+ | -O2 | None | 1/3 | None | 1/3 | +--------------------------+-------+-------+-------+-------+ | Old Master | 10440 | 11000 | 11940 | 13600 | +--------------------------+-------+-------+-------+-------+ | Old Master + 0001 | 10140 | 10800 | 11600 | 14300 | +--------------------------+-------+-------+-------+-------+ | Old Master + 0001 + 0002 | 9000 | 11000 | 8850 | 15300 | +--------------------------+-------+-------+-------+-------+ | | | | | | +--------------------------+-------+-------+-------+-------+ | | Text | | CSV | +--------------------------+-------+-------+-------+-------+ | -O3 | None | 1/3 | None | 1/3 | +--------------------------+-------+-------+-------+-------+ | Old Master | 10440 | 11200 | 12200 | 14390 | +--------------------------+-------+-------+-------+-------+ | Old Master + 0001 | 10000 | 10700 | 11540 | 13960 | +--------------------------+-------+-------+-------+-------+ | Old Master + 0001 + 0002 | 8880 | 10900 | 8900 | 15100 | +--------------------------+-------+-------+-------+-------+ This result shows that when we compare v18 and v18 + SIMD (0001 + 0002), there is only regression for the CSV 1/3 case. The regression is %12.5 for the -O2 and %5 for the -O3. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Wed, 25 Feb 2026 at 17:24, Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:
>
> I agree with you. However, unfortunately, I see regression on master +
> v10 compared to REL_18_3 (62d6c7d3df6).
>
> Thank you Kazar and Manni for benchmarks in [1] and [2]!
Kazar and Manni, if possible could you please share the build commands
you use? I see regressions for an inlining patch (dc592a4155) too when
I build postgres with -O2.
My build commands are:
-O2: meson setup buildtype=debugoptimized ...
-O3: meson setup buildtype=release ...
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hello,On Thu, Feb 26, 2026 at 1:19 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
On Wed, 25 Feb 2026 at 17:24, Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:
>
> I agree with you. However, unfortunately, I see regression on master +
> v10 compared to REL_18_3 (62d6c7d3df6).
>
> Thank you Kazar and Manni for benchmarks in [1] and [2]!
Kazar and Manni, if possible could you please share the build commands
you use? I see regressions for an inlining patch (dc592a4155) too when
I build postgres with -O2.
My build commands are:
-O2: meson setup buildtype=debugoptimized ...
-O3: meson setup buildtype=release ...All my builds are with CFLAGS='-O2 -g'Regards,Ayoub
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, Feb 26, 2026 at 8:31 AM KAZAR Ayoub <ma_kazar@esi.dz> wrote:Hello,On Thu, Feb 26, 2026 at 1:19 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:Hi,
On Wed, 25 Feb 2026 at 17:24, Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:
>
> I agree with you. However, unfortunately, I see regression on master +
> v10 compared to REL_18_3 (62d6c7d3df6).
>
> Thank you Kazar and Manni for benchmarks in [1] and [2]!
Kazar and Manni, if possible could you please share the build commands
you use? I see regressions for an inlining patch (dc592a4155) too when
I build postgres with -O2.
My build commands are:
-O2: meson setup buildtype=debugoptimized ...
-O3: meson setup buildtype=release ...All my builds are with CFLAGS='-O2 -g'Regards,AyoubHello!I have been building with this command:meson setup build --prefix=/home/mwood/compiled-pg-instances/${BRANCH} --buildtype=debugoptimizedAnd in my notes I have "If I use `--buildtype=debugoptimized` it optimizes `-O2` and uses `-g`"Best,-Manni---- Manni Wood EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
I have a thought and a question:My notes say "If I use `--buildtype=release` it optimizes `-O2` and the executable contains no debug symbols."
So, seeing as end users will presumably be seeing the performance generated by `--buildtype=release`, should we be building with that for all performance testing?
[1] https://mesonbuild.com/Builtin-options.html
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Feb 25, 2026 at 05:24:27PM +0300, Nazir Bilal Yavuz wrote:
> If anyone has any suggestions/ideas, please let me know!
A couple of random ideas:
* Additional inlining for callers. I looked around a little bit and didn't
see any great candidates, so I don't have much faith in this, but maybe
you'll see something I don't.
* Disable SIMD if we are consistently getting small rows. That won't help
your "wide & CSV 1/3" case in all likelihood, but perhaps it'll help with
the regression for narrow rows described elsewhere.
* Surround the variable initializations with "if (simd_enabled)".
Presumably compilers are smart enough to remove those in the non-SIMD paths
already, but it could be worth a try.
* Add simd_enabled function parameter to CopyReadLine(),
NextCopyFromRawFieldsInternal(), and CopyFromTextLikeOneRow(), and do the
bool literal trick in CopyFrom{Text,CSV}OneRow(). That could encourage the
compiler to do some additional optimizations to reduce branching.
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Mon, 2 Mar 2026 at 22:55, Nathan Bossart wrote:
>
> On Wed, Feb 25, 2026 at 05:24:27PM +0300, Nazir Bilal Yavuz wrote:
> > If anyone has any suggestions/ideas, please let me know!
I am able to fix the problem. My first assumption was that the
branching of SIMD code caused that problem, so I moved SIMD code to
the CopyReadLineTextSIMDHelper() function. Then I moved this
CopyReadLineTextSIMDHelper() to top of CopyReadLineText(), by doing
that we won't have any branching in the non-SIMD (scalar) code path.
This didn't solve the problem and then I realized that even though I
disable SIMD code path with 'if (false)', there is still regression
but if I comment all of the 'if (cstate->simd_enabled)' branch, then
there is no regression at all.
To find out more, I compared assembly outputs of both and found out
the possible reason. What I understood is that the compiler can't
promote a variable to register, instead these variables live in the
stack; which is slower. Please see the two different assembly outputs:
Slow code:
c = copy_input_buf[input_buf_ptr++];
db0: 48 8b 55 b8 mov -0x48(%rbp),%rdx
db4: 48 63 c6 movslq %esi,%rax
db7: 44 8d 66 01 lea 0x1(%rsi),%r12d
dbb: 44 89 65 cc mov %r12d,-0x34(%rbp)
dbf: 0f be 14 02 movsbl (%rdx,%rax,1),%edx
Fast code:
c = copy_input_buf[input_buf_ptr++];
d80: 49 63 c4 movslq %r12d,%rax
d83: 45 8d 5c 24 01 lea 0x1(%r12),%r11d
d88: 41 0f be 04 06 movsbl (%r14,%rax,1),%eax
And the reason for that is sending the address of input_buf_ptr to a
CopyReadLineTextSIMDHelper(..., &input_buf_ptr). If I change it to
this:
int temp_input_buf_ptr = input_buf_ptr;
CopyReadLineTextSIMDHelper(..., &temp_input_buf_ptr);
Then there is no regression. However, I am still not completely sure
if that is the same problem in the v10, I am planning to spend more
time debugging this.
> A couple of random ideas:
>
> * Additional inlining for callers. I looked around a little bit and didn't
> see any great candidates, so I don't have much faith in this, but maybe
> you'll see something I don't.
I agree with you. CopyReadLineText() is already quite a big function.
> * Disable SIMD if we are consistently getting small rows. That won't help
> your "wide & CSV 1/3" case in all likelihood, but perhaps it'll help with
> the regression for narrow rows described elsewhere.
I implemented this, two consecutive small rows disables SIMD.
> * Surround the variable initializations with "if (simd_enabled)".
> Presumably compilers are smart enough to remove those in the non-SIMD paths
> already, but it could be worth a try.
Done.
> * Add simd_enabled function parameter to CopyReadLine(),
> NextCopyFromRawFieldsInternal(), and CopyFromTextLikeOneRow(), and do the
> bool literal trick in CopyFrom{Text,CSV}OneRow(). That could encourage the
> compiler to do some additional optimizations to reduce branching.
I think we don't need this. At least the implementation with
CopyReadLineTextSIMDHelper() doesn't need this since branching will be
at the top and it will be once per line.
I think v11 looks better compared to v10. I liked the
CopyReadLineTextSIMDHelper() helper function. I also liked it being at
the top of CopyReadLineText(), not being in the scalar path. This
gives us more optimization options without affecting the scalar path.
Here are the new benchmark results, I benchmarked the changes with
both -O2 and -O3 and also both with and without 'changing
default_toast_compression to lz4' commit (65def42b1d5). Benchmark
results show that there is no regression and the performance
improvement is much bigger with 65def42b1d5, it is close to 2x for
text format and more than 2x for the csv format.
------------------------------
Benchmark results:
With 65def42b1d5:
+---------------------------------------------------------+
| Optimization: -O2 |
+--------------------------+--------------+---------------+
| | Text | CSV |
+--------------------------+------+-------+-------+-------+
| WIDE | None | 1/3 | None | 1/3 |
+--------------------------+------+-------+-------+-------+
| Old Master | 4220 | 4780 | 5930 | 8250 |
+--------------------------+------+-------+-------+-------+
| Old Master + 0001 + 0002 | 2520 | 4500 | 2520 | 7800 |
+--------------------------+------+-------+-------+-------+
| | | | | |
+--------------------------+------+-------+-------+-------+
| | Text | CSV |
+--------------------------+------+-------+-------+-------+
| NARROW | None | 1/3 | None | 1/3 |
+--------------------------+------+-------+-------+-------+
| Old Master | 9920 | 10100 | 10200 | 10470 |
+--------------------------+------+-------+-------+-------+
| Old Master + 0001 + 0002 | 9970 | 10000 | 10180 | 10350 |
+--------------------------+------+-------+-------+-------+
| |
+---------------------------------------------------------+
| |
+---------------------------------------------------------+
| Optimization: -O3 |
+--------------------------+--------------+---------------+
| | Text | CSV |
+--------------------------+------+-------+-------+-------+
| WIDE | None | 1/3 | None | 1/3 |
+--------------------------+------+-------+-------+-------+
| Old Master | 4100 | 4900 | 6200 | 8300 |
+--------------------------+------+-------+-------+-------+
| Old Master + 0001 + 0002 | 2470 | 4440 | 2570 | 7700 |
+--------------------------+------+-------+-------+-------+
| | | | | |
+--------------------------+------+-------+-------+-------+
| | Text | CSV |
+--------------------------+------+-------+-------+-------+
| NARROW | None | 1/3 | None | 1/3 |
+--------------------------+------+-------+-------+-------+
| Old Master | 9530 | 9690 | 9800 | 10080 |
+--------------------------+------+-------+-------+-------+
| Old Master + 0001 + 0002 | 9350 | 9450 | 9700 | 10000 |
+--------------------------+------+-------+-------+-------+
------------------------------
Without 65def42b1d5:
+----------------------------------------------------------+
| Optimization: -O2 |
+--------------------------+---------------+---------------+
| | Text | CSV |
+--------------------------+-------+-------+-------+-------+
| WIDE | None | 1/3 | None | 1/3 |
+--------------------------+-------+-------+-------+-------+
| Old Master | 10550 | 11030 | 12250 | 14400 |
+--------------------------+-------+-------+-------+-------+
| Old Master + 0001 + 0002 | 8890 | 10700 | 8870 | 14070 |
+--------------------------+-------+-------+-------+-------+
| | | | | |
+--------------------------+-------+-------+-------+-------+
| | Text | CSV |
+--------------------------+-------+-------+-------+-------+
| NARROW | None | 1/3 | None | 1/3 |
+--------------------------+-------+-------+-------+-------+
| Old Master | 9921 | 10205 | 10123 | 10420 |
+--------------------------+-------+-------+-------+-------+
| Old Master + 0001 + 0002 | 9880 | 10070 | 10150 | 10400 |
+--------------------------+-------+-------+-------+-------+
| |
+----------------------------------------------------------+
| |
+----------------------------------------------------------+
| Optimization: -O3 |
+--------------------------+---------------+---------------+
| | Text | CSV |
+--------------------------+-------+-------+-------+-------+
| WIDE | None | 1/3 | None | 1/3 |
+--------------------------+-------+-------+-------+-------+
| Old Master | 10500 | 11100 | 12600 | 14580 |
+--------------------------+-------+-------+-------+-------+
| Old Master + 0001 + 0002 | 8900 | 10660 | 8860 | 13990 |
+--------------------------+-------+-------+-------+-------+
| | | | | |
+--------------------------+-------+-------+-------+-------+
| | Text | CSV |
+--------------------------+-------+-------+-------+-------+
| NARROW | None | 1/3 | None | 1/3 |
+--------------------------+-------+-------+-------+-------+
| Old Master | 9600 | 9700 | 9800 | 10150 |
+--------------------------+-------+-------+-------+-------+
| Old Master + 0001 + 0002 | 9300 | 9470 | 9600 | 9880 |
+--------------------------+-------+-------+-------+-------+
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On 2026-03-04 We 10:15 AM, Nazir Bilal Yavuz wrote:
> Hi,
>
> On Mon, 2 Mar 2026 at 22:55, Nathan Bossart wrote:
>> On Wed, Feb 25, 2026 at 05:24:27PM +0300, Nazir Bilal Yavuz wrote:
>>> If anyone has any suggestions/ideas, please let me know!
> I am able to fix the problem. My first assumption was that the
> branching of SIMD code caused that problem, so I moved SIMD code to
> the CopyReadLineTextSIMDHelper() function. Then I moved this
> CopyReadLineTextSIMDHelper() to top of CopyReadLineText(), by doing
> that we won't have any branching in the non-SIMD (scalar) code path.
> This didn't solve the problem and then I realized that even though I
> disable SIMD code path with 'if (false)', there is still regression
> but if I comment all of the 'if (cstate->simd_enabled)' branch, then
> there is no regression at all.
>
> To find out more, I compared assembly outputs of both and found out
> the possible reason. What I understood is that the compiler can't
> promote a variable to register, instead these variables live in the
> stack; which is slower. Please see the two different assembly outputs:
>
> Slow code:
>
> c = copy_input_buf[input_buf_ptr++];
> db0: 48 8b 55 b8 mov -0x48(%rbp),%rdx
> db4: 48 63 c6 movslq %esi,%rax
> db7: 44 8d 66 01 lea 0x1(%rsi),%r12d
> dbb: 44 89 65 cc mov %r12d,-0x34(%rbp)
> dbf: 0f be 14 02 movsbl (%rdx,%rax,1),%edx
>
> Fast code:
>
> c = copy_input_buf[input_buf_ptr++];
> d80: 49 63 c4 movslq %r12d,%rax
> d83: 45 8d 5c 24 01 lea 0x1(%r12),%r11d
> d88: 41 0f be 04 06 movsbl (%r14,%rax,1),%eax
>
> And the reason for that is sending the address of input_buf_ptr to a
> CopyReadLineTextSIMDHelper(..., &input_buf_ptr). If I change it to
> this:
>
> int temp_input_buf_ptr = input_buf_ptr;
> CopyReadLineTextSIMDHelper(..., &temp_input_buf_ptr);
>
> Then there is no regression. However, I am still not completely sure
> if that is the same problem in the v10, I am planning to spend more
> time debugging this.
>
>> A couple of random ideas:
>>
>> * Additional inlining for callers. I looked around a little bit and didn't
>> see any great candidates, so I don't have much faith in this, but maybe
>> you'll see something I don't.
> I agree with you. CopyReadLineText() is already quite a big function.
>
>> * Disable SIMD if we are consistently getting small rows. That won't help
>> your "wide & CSV 1/3" case in all likelihood, but perhaps it'll help with
>> the regression for narrow rows described elsewhere.
> I implemented this, two consecutive small rows disables SIMD.
>
>> * Surround the variable initializations with "if (simd_enabled)".
>> Presumably compilers are smart enough to remove those in the non-SIMD paths
>> already, but it could be worth a try.
> Done.
>
>> * Add simd_enabled function parameter to CopyReadLine(),
>> NextCopyFromRawFieldsInternal(), and CopyFromTextLikeOneRow(), and do the
>> bool literal trick in CopyFrom{Text,CSV}OneRow(). That could encourage the
>> compiler to do some additional optimizations to reduce branching.
> I think we don't need this. At least the implementation with
> CopyReadLineTextSIMDHelper() doesn't need this since branching will be
> at the top and it will be once per line.
>
> I think v11 looks better compared to v10. I liked the
> CopyReadLineTextSIMDHelper() helper function. I also liked it being at
> the top of CopyReadLineText(), not being in the scalar path. This
> gives us more optimization options without affecting the scalar path.
>
> Here are the new benchmark results, I benchmarked the changes with
> both -O2 and -O3 and also both with and without 'changing
> default_toast_compression to lz4' commit (65def42b1d5). Benchmark
> results show that there is no regression and the performance
> improvement is much bigger with 65def42b1d5, it is close to 2x for
> text format and more than 2x for the csv format.
I spent some time exploring different ideas for improving this, but
found none that didn't cause regression in some cases, so good to go
from my POV.
cheers
andrew
--
Andrew Dunstan
EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
TXT : 25909.060500 ms
CSV : 28137.591250 ms
TXT with 1/3 escapes: 27794.177000 ms
CSV with 1/3 quotes: 34541.704750 ms
x86 NARROW v10
TXT : 26416.331500 ms -1.957890% regression
CSV : 25318.727500 ms 10.018142% improvement
TXT with 1/3 escapes: 28608.007500 ms -2.928061% regression
CSV with 1/3 quotes: 32805.627750 ms 5.026032% improvement
x86 NARROW v11
TXT : 27212.945750 ms -5.032545% regression
CSV : 26985.971250 ms 4.092817% improvement
TXT with 1/3 escapes: 27216.510000 ms 2.078374% improvement
CSV with 1/3 quotes: 32817.267500 ms 4.992334% improvement
x86 WIDE old master (18bcdb75)
TXT : 28778.426500 ms
CSV : 35671.908000 ms
TXT with 1/3 escapes: 32441.549750 ms
CSV with 1/3 quotes: 47024.416000 ms
x86 WIDE v10
TXT : 23067.046750 ms 19.846046% improvement
CSV : 23259.092250 ms 34.797174% improvement
TXT with 1/3 escapes: 31796.098250 ms 1.989583% improvement
CSV with 1/3 quotes: 42925.792250 ms 8.715948% improvement
x86 WIDE v11
TXT : 22571.305750 ms 21.568659% improvement
CSV : 22711.524750 ms 36.332184% improvement
TXT with 1/3 escapes: 29236.453000 ms 9.879604% improvement
CSV with 1/3 quotes: 40022.110750 ms 14.890786% improvement
arm NARROW old master (18bcdb75)
TXT : 10997.568250 ms
CSV : 10797.549000 ms
TXT with 1/3 escapes: 10299.047000 ms
CSV with 1/3 quotes: 12559.385750 ms
arm NARROW v10
TXT : 10467.816750 ms 4.816988% improvement
CSV : 9986.288000 ms 7.513381% improvement
TXT with 1/3 escapes: 10323.173750 ms -0.234262% regression
CSV with 1/3 quotes: 11843.611750 ms 5.699116% improvement
arm NARROW v11
TXT : 10340.966250 ms 5.970429% improvement
CSV : 10224.399500 ms 5.308144% improvement
TXT with 1/3 escapes: 10438.216750 ms -1.351288% regression
CSV with 1/3 quotes: 11865.934000 ms 5.521383% improvement
arm WIDE old master (18bcdb75)
TXT : 11825.771250 ms
CSV : 13907.074000 ms
TXT with 1/3 escapes: 13430.691250 ms
CSV with 1/3 quotes: 17557.954500 ms
arm WIDE v10
TXT : 9064.959000 ms 23.345727% improvement
CSV : 9019.553250 ms 35.144134% improvement
TXT with 1/3 escapes: 12344.497250 ms 8.087402% improvement
CSV with 1/3 quotes: 15495.863750 ms 11.744482% improvement
arm WIDE v11
TXT : 9001.442250 ms 23.882831% improvement
CSV : 8940.928750 ms 35.709490% improvement
TXT with 1/3 escapes: 12049.668500 ms 10.282589% improvement
CSV with 1/3 quotes: 15277.843250 ms 12.986201% improvement
On 2026-03-04 We 10:15 AM, Nazir Bilal Yavuz wrote:
> Hi,
>
> On Mon, 2 Mar 2026 at 22:55, Nathan Bossart <nathandbossart@gmail.com> wrote:
>> On Wed, Feb 25, 2026 at 05:24:27PM +0300, Nazir Bilal Yavuz wrote:
>>> If anyone has any suggestions/ideas, please let me know!
> I am able to fix the problem. My first assumption was that the
> branching of SIMD code caused that problem, so I moved SIMD code to
> the CopyReadLineTextSIMDHelper() function. Then I moved this
> CopyReadLineTextSIMDHelper() to top of CopyReadLineText(), by doing
> that we won't have any branching in the non-SIMD (scalar) code path.
> This didn't solve the problem and then I realized that even though I
> disable SIMD code path with 'if (false)', there is still regression
> but if I comment all of the 'if (cstate->simd_enabled)' branch, then
> there is no regression at all.
>
> To find out more, I compared assembly outputs of both and found out
> the possible reason. What I understood is that the compiler can't
> promote a variable to register, instead these variables live in the
> stack; which is slower. Please see the two different assembly outputs:
>
> Slow code:
>
> c = copy_input_buf[input_buf_ptr++];
> db0: 48 8b 55 b8 mov -0x48(%rbp),%rdx
> db4: 48 63 c6 movslq %esi,%rax
> db7: 44 8d 66 01 lea 0x1(%rsi),%r12d
> dbb: 44 89 65 cc mov %r12d,-0x34(%rbp)
> dbf: 0f be 14 02 movsbl (%rdx,%rax,1),%edx
>
> Fast code:
>
> c = copy_input_buf[input_buf_ptr++];
> d80: 49 63 c4 movslq %r12d,%rax
> d83: 45 8d 5c 24 01 lea 0x1(%r12),%r11d
> d88: 41 0f be 04 06 movsbl (%r14,%rax,1),%eax
>
> And the reason for that is sending the address of input_buf_ptr to a
> CopyReadLineTextSIMDHelper(..., &input_buf_ptr). If I change it to
> this:
>
> int temp_input_buf_ptr = input_buf_ptr;
> CopyReadLineTextSIMDHelper(..., &temp_input_buf_ptr);
>
> Then there is no regression. However, I am still not completely sure
> if that is the same problem in the v10, I am planning to spend more
> time debugging this.
>
>> A couple of random ideas:
>>
>> * Additional inlining for callers. I looked around a little bit and didn't
>> see any great candidates, so I don't have much faith in this, but maybe
>> you'll see something I don't.
> I agree with you. CopyReadLineText() is already quite a big function.
>
>> * Disable SIMD if we are consistently getting small rows. That won't help
>> your "wide & CSV 1/3" case in all likelihood, but perhaps it'll help with
>> the regression for narrow rows described elsewhere.
> I implemented this, two consecutive small rows disables SIMD.
>
>> * Surround the variable initializations with "if (simd_enabled)".
>> Presumably compilers are smart enough to remove those in the non-SIMD paths
>> already, but it could be worth a try.
> Done.
>
>> * Add simd_enabled function parameter to CopyReadLine(),
>> NextCopyFromRawFieldsInternal(), and CopyFromTextLikeOneRow(), and do the
>> bool literal trick in CopyFrom{Text,CSV}OneRow(). That could encourage the
>> compiler to do some additional optimizations to reduce branching.
> I think we don't need this. At least the implementation with
> CopyReadLineTextSIMDHelper() doesn't need this since branching will be
> at the top and it will be once per line.
>
> I think v11 looks better compared to v10. I liked the
> CopyReadLineTextSIMDHelper() helper function. I also liked it being at
> the top of CopyReadLineText(), not being in the scalar path. This
> gives us more optimization options without affecting the scalar path.
>
> Here are the new benchmark results, I benchmarked the changes with
> both -O2 and -O3 and also both with and without 'changing
> default_toast_compression to lz4' commit (65def42b1d5). Benchmark
> results show that there is no regression and the performance
> improvement is much bigger with 65def42b1d5, it is close to 2x for
> text format and more than 2x for the csv format.
I spent some time exploring different ideas for improving this, but
found none that didn't cause regression in some cases, so good to go
from my POV.
cheers
andrew
--
Andrew Dunstan
EDB: https://www.enterprisedb.com
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Fri, 6 Mar 2026 at 20:00, Manni Wood wrote: > > Hello. > > I ran Nazir's v11 patch on my x86 tower PC and my arm raspberry pi using the same build I've been using: meson with "debugoptimized", which translates to "-g -O2" gcc flags. Thanks for the benchmark! The results look nice. One question: does your benchmark include the 34dfca2934 LZ4 commit, and is LZ4 enabled on your system? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Fri, 6 Mar 2026 at 20:00, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> Hello.
>
> I ran Nazir's v11 patch on my x86 tower PC and my arm raspberry pi using the same build I've been using: meson with "debugoptimized", which translates to "-g -O2" gcc flags.
Thanks for the benchmark! The results look nice.
One question: does your benchmark include the 34dfca2934 LZ4 commit,
and is LZ4 enabled on your system?
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi Manni! On Fri, 6 Mar 2026 at 21:13, Manni Wood wrote: > > When I ran `meson setup build --buildtype=debugoptimized` on both my x86 machine and my arm machine, the response on both was: > > "External libraries > " lz4 : NO" > > However, I did not remove commit 34dfca2934 from any of my Postgres builds; I left that commit in place. > > Let me know if that helps! That definitely helps, thanks! If you have a chance, could you also run the benchmark with LZ4 enabled? I expect you may see significantly better performance, similar to what I observed. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi Manni!
On Fri, 6 Mar 2026 at 21:13, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> When I ran `meson setup build --buildtype=debugoptimized` on both my x86 machine and my arm machine, the response on both was:
>
> "External libraries
> " lz4 : NO"
>
> However, I did not remove commit 34dfca2934 from any of my Postgres builds; I left that commit in place.
>
> Let me know if that helps!
That definitely helps, thanks! If you have a chance, could you also
run the benchmark with LZ4 enabled? I expect you may see significantly
better performance, similar to what I observed.
--
Regards,
Nazir Bilal Yavuz
Microsoft
default_toast_compression
---------------------------
lz4
TXT : 25909.060500 ms
CSV : 28137.591250 ms
TXT with 1/3 escapes: 27794.177000 ms
CSV with 1/3 quotes: 34541.704750 ms
x86 NARROW v11
TXT : 27212.945750 ms -5.032545% regression
CSV : 26985.971250 ms 4.092817% improvement
TXT with 1/3 escapes: 27216.510000 ms 2.078374% improvement
CSV with 1/3 quotes: 32817.267500 ms 4.992334% improvement
x86 NARROW v11 lz4
TXT : 26471.776500 ms -2.171889% regression
CSV : 25607.026250 ms 8.993538% improvement
TXT with 1/3 escapes: 28628.729750 ms -3.002617% regression
CSV with 1/3 quotes: 34729.006750 ms -0.542249% regression
x86 WIDE old master
TXT : 28778.426500 ms
CSV : 35671.908000 ms
TXT with 1/3 escapes: 32441.549750 ms
CSV with 1/3 quotes: 47024.416000 ms
x86 WIDE v11
TXT : 22571.305750 ms 21.568659% improvement
CSV : 22711.524750 ms 36.332184% improvement
TXT with 1/3 escapes: 29236.453000 ms 9.879604% improvement
CSV with 1/3 quotes: 40022.110750 ms 14.890786% improvement
x86 WIDE v11 lz4
TXT : 8032.912750 ms 72.087033% improvement
CSV : 8047.098000 ms 77.441358% improvement
TXT with 1/3 escapes: 15428.139500 ms 52.443272% improvement
CSV with 1/3 quotes: 27517.084500 ms 41.483410% improvement
arm NARROW old master
TXT : 10997.568250 ms
CSV : 10797.549000 ms
TXT with 1/3 escapes: 10299.047000 ms
CSV with 1/3 quotes: 12559.385750 ms
arm NARROW v11
TXT : 10340.966250 ms 5.970429% improvement
CSV : 10224.399500 ms 5.308144% improvement
TXT with 1/3 escapes: 10438.216750 ms -1.351288% regression
CSV with 1/3 quotes: 11865.934000 ms 5.521383% improvement
arm NARROW v11 lz4
TXT : 9783.737000 ms 11.037270% improvement
CSV : 10122.890750 ms 6.248254% improvement
TXT with 1/3 escapes: 10298.780250 ms 0.002590% improvement
CSV with 1/3 quotes: 11738.992250 ms 6.532115% improvement
arm WIDE old master
TXT : 11825.771250 ms
CSV : 13907.074000 ms
TXT with 1/3 escapes: 13430.691250 ms
CSV with 1/3 quotes: 17557.954500 ms
arm WIDE v11
TXT : 9001.442250 ms 23.882831% improvement
CSV : 8940.928750 ms 35.709490% improvement
TXT with 1/3 escapes: 12049.668500 ms 10.282589% improvement
CSV with 1/3 quotes: 15277.843250 ms 12.986201% improvement
arm WIDE v11 lz4
TXT : 3186.825500 ms 73.051859% improvement
CSV : 3142.526500 ms 77.403396% improvement
TXT with 1/3 escapes: 6180.176000 ms 53.984677% improvement
CSV with 1/3 quotes: 9460.505500 ms 46.118407% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 06, 2026 at 03:25:46PM -0600, Manni Wood wrote: > Well, golly! Look at these numbers. Old master with no lz4, your v11 patch > with no lz4, and then your v11 patch with lz4 compiled in. I'm appreciative of all the benchmarking that you and others are doing, but wouldn't we be more interested in the difference between "old master with lz4" and "v11 with lz4"? Else, we have multiple variables in play. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 06, 2026 at 03:25:46PM -0600, Manni Wood wrote:
> Well, golly! Look at these numbers. Old master with no lz4, your v11 patch
> with no lz4, and then your v11 patch with lz4 compiled in.
I'm appreciative of all the benchmarking that you and others are doing, but
wouldn't we be more interested in the difference between "old master with
lz4" and "v11 with lz4"? Else, we have multiple variables in play.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Sat, 7 Mar 2026 at 02:31, KAZAR Ayoub wrote: > > On Sat, Mar 7, 2026 at 12:13 AM Nathan Bossart wrote: >> >> On Fri, Mar 06, 2026 at 03:25:46PM -0600, Manni Wood wrote: >> > Well, golly! Look at these numbers. Old master with no lz4, your v11 patch >> > with no lz4, and then your v11 patch with lz4 compiled in. >> >> I'm appreciative of all the benchmarking that you and others are doing, but >> wouldn't we be more interested in the difference between "old master with >> lz4" and "v11 with lz4"? Else, we have multiple variables in play. > > Yes I agree because the lz4 effect doesn't prove anything for the SIMD patch itself right ? So basically a comparison for the SIMD effect should be "master with/out lz4 vs patched with/out lz4, respectively and nothing more!", is this correct ? Yes, I think 'master with/out lz4 vs patched with/out lz4, respectively' is enough to determine the effect of the SIMD patch. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sat, 7 Mar 2026 at 02:31, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> On Sat, Mar 7, 2026 at 12:13 AM Nathan Bossart <nathandbossart@gmail.com> wrote:
>>
>> On Fri, Mar 06, 2026 at 03:25:46PM -0600, Manni Wood wrote:
>> > Well, golly! Look at these numbers. Old master with no lz4, your v11 patch
>> > with no lz4, and then your v11 patch with lz4 compiled in.
>>
>> I'm appreciative of all the benchmarking that you and others are doing, but
>> wouldn't we be more interested in the difference between "old master with
>> lz4" and "v11 with lz4"? Else, we have multiple variables in play.
>
> Yes I agree because the lz4 effect doesn't prove anything for the SIMD patch itself right ? So basically a comparison for the SIMD effect should be "master with/out lz4 vs patched with/out lz4, respectively and nothing more!", is this correct ?
Yes, I think 'master with/out lz4 vs patched with/out lz4,
respectively' is enough to determine the effect of the SIMD patch.
--
Regards,
Nazir Bilal Yavuz
Microsoft
TXT : 10362.799500 ms
CSV : 10288.791000 ms
TXT with 1/3 escapes: 10411.416250 ms
CSV with 1/3 quotes: 12318.385750 ms
arm NARROW master with inline with lz4 with v11patch
TXT : 10317.125750 ms 0.440747% improvement
CSV : 10418.020250 ms -1.256020% regression
TXT with 1/3 escapes: 10188.319500 ms 2.142809% improvement
CSV with 1/3 quotes: 12032.964500 ms 2.317035% improvement
arm WIDE master without inline with lz4
TXT : 5608.834500 ms
CSV : 8115.155000 ms
TXT with 1/3 escapes: 7037.290500 ms
CSV with 1/3 quotes: 10894.615750 ms
arm WIDE master with inline with lz4 with v11patch
TXT : 3190.268750 ms 43.120647% improvement
CSV : 3135.177000 ms 61.366394% improvement
TXT with 1/3 escapes: 6373.746750 ms 9.428966% improvement
CSV with 1/3 quotes: 10336.763500 ms 5.120440% improvement
x86 NARROW-master-without-inline-with-lz4.log
TXT : 26701.079250 ms
CSV : 26492.235500 ms
TXT with 1/3 escapes: 28590.508250 ms
CSV with 1/3 quotes: 34876.742750 ms
x86 NARROW-master-with-inline-with-lz4-with-v11patch.log
TXT : 26511.747750 ms 0.709078% improvement
CSV : 26261.269750 ms 0.871824% improvement
TXT with 1/3 escapes: 27702.964750 ms 3.104329% improvement
CSV with 1/3 quotes: 32339.393000 ms 7.275191% improvement
x86 WIDE-master-without-inline-with-lz4.log
TXT : 14485.563250 ms
CSV : 21392.582000 ms
TXT with 1/3 escapes: 18081.514750 ms
CSV with 1/3 quotes: 32547.086250 ms
x86 WIDE-master-with-inline-with-lz4-with-v11patch.log
TXT : 8080.378250 ms 44.217714% improvement
CSV : 8283.723000 ms 61.277591% improvement
TXT with 1/3 escapes: 15054.111000 ms 16.743087% improvement
CSV with 1/3 quotes: 25668.009750 ms 21.135768% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Sun, 8 Mar 2026 at 22:45, Manni Wood wrote: > > As requested, here are some numbers based on the latest master but with the copy code inlining excised (`git revert dc592a41557b072178f1798700bf9c69cd8e4235`), compared to master with copy code inlining left in place and the v11 patch applied. > Both results have lz4 compression in place. Thank you for the benchmark! > I have not run numbers without lz4. I assume I could use the two postgres instances that I have compiled with lz4, but just set `default_toast_compression = pglz` in postgesql.conf for both instances. Let me know if that is a mistaken assumption on my part. I am a bit confused. Are you asking that for the current benchmark you shared or future benchmarks? I assume your current benchmark has 'default_toast_compression = lz4' because your benchmark results are very similar to my benchmark with 'default_toast_compression = lz4' but I just wanted to make sure. What you said about editing postgresql.conf is correct but you need to make this change before creating the Postgres instance with 'pg_ctl ... start' command, otherwise it won't have an effect and you need to restart the instance to see the effect. Also, If you want to benchmark without lz4 change, you can just use the "SET default_toast_compression to 'pglz';" command in psql, then you don't need to edit postgresql.conf. Please note that this will affect only the psql instance you typed the command. To make things easier, you can run the 'SHOW default_toast_compression;' command to see the current value of 'default_toast_compression'. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Sun, 8 Mar 2026 at 22:45, Manni Wood <manni.wood@enterprisedb.com> wrote:
>
> As requested, here are some numbers based on the latest master but with the copy code inlining excised (`git revert dc592a41557b072178f1798700bf9c69cd8e4235`), compared to master with copy code inlining left in place and the v11 patch applied.
> Both results have lz4 compression in place.
Thank you for the benchmark!
> I have not run numbers without lz4. I assume I could use the two postgres instances that I have compiled with lz4, but just set `default_toast_compression = pglz` in postgesql.conf for both instances. Let me know if that is a mistaken assumption on my part.
I am a bit confused. Are you asking that for the current benchmark you
shared or future benchmarks? I assume your current benchmark has
'default_toast_compression = lz4' because your benchmark results are
very similar to my benchmark with 'default_toast_compression = lz4'
but I just wanted to make sure.
What you said about editing postgresql.conf is correct but you need to
make this change before creating the Postgres instance with 'pg_ctl
... start' command, otherwise it won't have an effect and you need to
restart the instance to see the effect. Also, If you want to benchmark
without lz4 change, you can just use the "SET
default_toast_compression to 'pglz';" command in psql, then you don't
need to edit postgresql.conf. Please note that this will affect only
the psql instance you typed the command. To make things easier, you
can run the 'SHOW default_toast_compression;' command to see the
current value of 'default_toast_compression'.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi Manni! On Mon, 9 Mar 2026 at 16:31, Manni Wood wrote: > > I was being too brief. > > The benchmarks I shared were absolutely with lz4 compiled in and 'default_toast_compression = lz4' set in postgresql.conf for every postgres instance I tested with. (Furthermore, I ran `show default_toast_compression` via `psql` on each postgres instance to be sure 'default_toast_compression = lz4' was really set!) > > Also, all were compiled using meson using `debugoptimized` which results in `-g -O2`. > > So those are the benchmarks that I shared. Thanks for the clarification. > OK, so my final question, hopefully clarified: If I run additional benchmarks where pglz is used for default_toast_compression, is it enough to use the instances I have already compiled with lz4 in them, but with 'default_toast_compression = pglz` explicitly set in postgresql.conf in a brand new data dir created by initdb? (In other words, existing data dir deleted, then initdb run to make a new data dir, then postgresql.conf edited to ensure 'default_toast_compression = pglz` explicitly set, then and only then starting up the cluster for the first time... and finally verifying via `show default_toast_compression` for good measure.) > > Or should I re-compile with the lz4-is-now-the-default commit completely excised? Yes, it is clear now; thanks. You don't need to compile without the lz4-is-now-the-default commit. You can compile with lz4 commit and set the 'default_toast_compression = pglz' in the postgresql.conf like you said. This should be enough. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Mar 04, 2026 at 06:15:53PM +0300, Nazir Bilal Yavuz wrote:
> +#ifndef USE_NO_SIMD
> +static bool CopyReadLineTextSIMDHelper(CopyFromState cstate, bool is_csv,
> + bool *temp_hit_eof, int *temp_input_buf_ptr);
> +#endif
Should we inline this, too?
> + /*
> + * Do not disable SIMD when we hit EOL or EOF characters. In
> + * practice, it does not matter for EOF because parsing ends
> + * there, but we keep the behavior consistent.
> + */
> + if (!(simd_hit_eof || simd_hit_eol))
> + cstate->simd_enabled = false;
nitpick: I would personally avoid disabling it for EOF. It probably
doesn't amount to much, but I don't see any point in the extra
complexity/work solely for consistency.
> + /*
> + * We encountered a EOL or EOF on the first vector. This means
> + * lines are not long enough to skip fully sized vector. If
> + * this happens two times consecutively, then disable the
> + * SIMD.
> + */
> + if (first_vector)
> + {
> + if (cstate->simd_failed_first_vector)
> + cstate->simd_enabled = false;
> +
> + cstate->simd_failed_first_vector = true;
> + }
The first time I saw this, my mind immediately went to the extreme case
where this likely regresses: alternating long and short lines. We might
just want to disable it the first time we see a short line, like we do for
special characters. This is another thing that we can improve
independently later on.
> + /* First try to run SIMD, then continue with the scalar path */
> + if (cstate->simd_enabled)
> + {
> + int temp_input_buf_ptr = input_buf_ptr;
> + bool temp_hit_eof = false;
> +
> + result = CopyReadLineTextSIMDHelper(cstate, is_csv, &temp_hit_eof,
> + &temp_input_buf_ptr);
> + input_buf_ptr = temp_input_buf_ptr;
> + hit_eof = temp_hit_eof;
Given CopyReadLineTextSIMDHelper() doesn't have too much duplicated code,
moving the SIMD stuff to its own function is nice. The temp variables seem
a bit too magical to me, though. If those really make a difference, IMHO
there ought to be a big comment explaining why.
--
nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Mar 04, 2026 at 06:15:53PM +0300, Nazir Bilal Yavuz wrote:
> +#ifndef USE_NO_SIMD
> +static bool CopyReadLineTextSIMDHelper(CopyFromState cstate, bool is_csv,
> + bool *temp_hit_eof, int *temp_input_buf_ptr);
> +#endif
Should we inline this, too?
> + /*
> + * Do not disable SIMD when we hit EOL or EOF characters. In
> + * practice, it does not matter for EOF because parsing ends
> + * there, but we keep the behavior consistent.
> + */
> + if (!(simd_hit_eof || simd_hit_eol))
> + cstate->simd_enabled = false;
nitpick: I would personally avoid disabling it for EOF. It probably
doesn't amount to much, but I don't see any point in the extra
complexity/work solely for consistency.
> + /*
> + * We encountered a EOL or EOF on the first vector. This means
> + * lines are not long enough to skip fully sized vector. If
> + * this happens two times consecutively, then disable the
> + * SIMD.
> + */
> + if (first_vector)
> + {
> + if (cstate->simd_failed_first_vector)
> + cstate->simd_enabled = false;
> +
> + cstate->simd_failed_first_vector = true;
> + }
The first time I saw this, my mind immediately went to the extreme case
where this likely regresses: alternating long and short lines. We might
just want to disable it the first time we see a short line, like we do for
special characters. This is another thing that we can improve
independently later on.
> + /* First try to run SIMD, then continue with the scalar path */
> + if (cstate->simd_enabled)
> + {
> + int temp_input_buf_ptr = input_buf_ptr;
> + bool temp_hit_eof = false;
> +
> + result = CopyReadLineTextSIMDHelper(cstate, is_csv, &temp_hit_eof,
> + &temp_input_buf_ptr);
> + input_buf_ptr = temp_input_buf_ptr;
> + hit_eof = temp_hit_eof;
Given CopyReadLineTextSIMDHelper() doesn't have too much duplicated code,
moving the SIMD stuff to its own function is nice. The temp variables seem
a bit too magical to me, though. If those really make a difference, IMHO
there ought to be a big comment explaining why.
--
nathan
arm NARROW master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 10055.141000 ms
CSV : 10549.174500 ms
TXT with 1/3 escapes: 10213.864750 ms
CSV with 1/3 quotes: 12188.039000 ms
arm NARROW master with inline with v11patch default_toast_compression = pglz
TXT : 10070.153750 ms -0.149304% regression
CSV : 10161.348750 ms 3.676361% improvement
TXT with 1/3 escapes: 10618.005000 ms -3.956781% regression
CSV with 1/3 quotes: 12279.366250 ms -0.749319% regression
arm WIDE master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 11355.602750 ms
CSV : 13893.110500 ms
TXT with 1/3 escapes: 12872.690500 ms
CSV with 1/3 quotes: 16722.262500 ms
arm WIDE master with inline with v11patch default_toast_compression = pglz
TXT : 9001.007250 ms 20.735099% improvement
CSV : 8988.679750 ms 35.301171% improvement
TXT with 1/3 escapes: 12191.137000 ms 5.294569% improvement
CSV with 1/3 quotes: 16297.541500 ms 2.539854% improvement
x86 NARROW master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 26243.084500 ms
CSV : 27719.564000 ms
TXT with 1/3 escapes: 29578.192750 ms
CSV with 1/3 quotes: 34467.571250 ms
x86 NARROW master with inline with v11patch default_toast_compression = pglz
TXT : 26371.996750 ms -0.491224% regression
CSV : 26137.186500 ms 5.708522% improvement
TXT with 1/3 escapes: 28080.201000 ms 5.064514% improvement
CSV with 1/3 quotes: 32557.377500 ms 5.542003% improvement
x86 WIDE master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 28734.774750 ms
CSV : 35700.485000 ms
TXT with 1/3 escapes: 32376.878250 ms
CSV with 1/3 quotes: 47024.985750 ms
x86 WIDE master with inline with v11patch default_toast_compression = pglz
TXT : 22753.755750 ms 20.814567% improvement
CSV : 22977.195500 ms 35.638982% improvement
TXT with 1/3 escapes: 29526.887000 ms 8.802551% improvement
CSV with 1/3 quotes: 40298.196750 ms 14.304712% improvement
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 10 Mar 2026 at 05:30, Manni Wood wrote: > > Here are some benchmarks showing what performance will look like for users who continue to use default_toast_compression = pglz. > > all compiled by meson with debugoptimized (-g -O2) > > arm NARROW master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz > TXT : 10055.141000 ms > CSV : 10549.174500 ms > TXT with 1/3 escapes: 10213.864750 ms > CSV with 1/3 quotes: 12188.039000 ms > > arm NARROW master with inline with v11patch default_toast_compression = pglz > TXT : 10070.153750 ms -0.149304% regression > CSV : 10161.348750 ms 3.676361% improvement > TXT with 1/3 escapes: 10618.005000 ms -3.956781% regression > CSV with 1/3 quotes: 12279.366250 ms -0.749319% regression > > arm WIDE master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz > TXT : 11355.602750 ms > CSV : 13893.110500 ms > TXT with 1/3 escapes: 12872.690500 ms > CSV with 1/3 quotes: 16722.262500 ms > > arm WIDE master with inline with v11patch default_toast_compression = pglz > TXT : 9001.007250 ms 20.735099% improvement > CSV : 8988.679750 ms 35.301171% improvement > TXT with 1/3 escapes: 12191.137000 ms 5.294569% improvement > CSV with 1/3 quotes: 16297.541500 ms 2.539854% improvement > > > x86 NARROW master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz > TXT : 26243.084500 ms > CSV : 27719.564000 ms > TXT with 1/3 escapes: 29578.192750 ms > CSV with 1/3 quotes: 34467.571250 ms > > x86 NARROW master with inline with v11patch default_toast_compression = pglz > TXT : 26371.996750 ms -0.491224% regression > CSV : 26137.186500 ms 5.708522% improvement > TXT with 1/3 escapes: 28080.201000 ms 5.064514% improvement > CSV with 1/3 quotes: 32557.377500 ms 5.542003% improvement > > x86 WIDE master without inline (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz > TXT : 28734.774750 ms > CSV : 35700.485000 ms > TXT with 1/3 escapes: 32376.878250 ms > CSV with 1/3 quotes: 47024.985750 ms > > x86 WIDE master with inline with v11patch default_toast_compression = pglz > TXT : 22753.755750 ms 20.814567% improvement > CSV : 22977.195500 ms 35.638982% improvement > TXT with 1/3 escapes: 29526.887000 ms 8.802551% improvement > CSV with 1/3 quotes: 40298.196750 ms 14.304712% improvement Thank you for the benchmark, results look nice! So, there is almost no regression for both pglz and lz4 toast compression modes. Best case is ~60% improvement for the lz4 and ~35% improvement for the pglz. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Mon, 9 Mar 2026 at 21:25, Nathan Bossart wrote:
>
> On Wed, Mar 04, 2026 at 06:15:53PM +0300, Nazir Bilal Yavuz wrote:
> > +#ifndef USE_NO_SIMD
> > +static bool CopyReadLineTextSIMDHelper(CopyFromState cstate, bool is_csv,
> > + bool *temp_hit_eof, int *temp_input_buf_ptr);
> > +#endif
>
> Should we inline this, too?
I think there is no need to inline this function. In the previous
version, SIMD code was in the main for loop which loops for every
character in the data. This means there was branching for every
character in the data. In the current version, SIMD code is outside of
this loop so there is no branching.
> > + /*
> > + * Do not disable SIMD when we hit EOL or EOF characters. In
> > + * practice, it does not matter for EOF because parsing ends
> > + * there, but we keep the behavior consistent.
> > + */
> > + if (!(simd_hit_eof || simd_hit_eol))
> > + cstate->simd_enabled = false;
>
> nitpick: I would personally avoid disabling it for EOF. It probably
> doesn't amount to much, but I don't see any point in the extra
> complexity/work solely for consistency.
Done. I thought that was a small change but this removed more
complexity than I thought.
>
> > + /*
> > + * We encountered a EOL or EOF on the first vector. This means
> > + * lines are not long enough to skip fully sized vector. If
> > + * this happens two times consecutively, then disable the
> > + * SIMD.
> > + */
> > + if (first_vector)
> > + {
> > + if (cstate->simd_failed_first_vector)
> > + cstate->simd_enabled = false;
> > +
> > + cstate->simd_failed_first_vector = true;
> > + }
>
> The first time I saw this, my mind immediately went to the extreme case
> where this likely regresses: alternating long and short lines. We might
> just want to disable it the first time we see a short line, like we do for
> special characters. This is another thing that we can improve
> independently later on.
I agree with you, done.
>
> > + /* First try to run SIMD, then continue with the scalar path */
> > + if (cstate->simd_enabled)
> > + {
> > + int temp_input_buf_ptr = input_buf_ptr;
> > + bool temp_hit_eof = false;
> > +
> > + result = CopyReadLineTextSIMDHelper(cstate, is_csv, &temp_hit_eof,
> > + &temp_input_buf_ptr);
> > + input_buf_ptr = temp_input_buf_ptr;
> > + hit_eof = temp_hit_eof;
>
> Given CopyReadLineTextSIMDHelper() doesn't have too much duplicated code,
> moving the SIMD stuff to its own function is nice. The temp variables seem
> a bit too magical to me, though. If those really make a difference, IMHO
> there ought to be a big comment explaining why.
I added a comment, please let me know if you wouldn't like it.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Mar 10, 2026 at 03:35:30PM +0300, Nazir Bilal Yavuz wrote: > Subject: [PATCH v12] Speed up COPY FROM text/CSV parsing using SIMD This looks pretty good to me. I'm hoping to take a closer look in the near future, but I think we are approaching something committable. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 10 Mar 2026 at 20:10, Nathan Bossart wrote: > > On Tue, Mar 10, 2026 at 03:35:30PM +0300, Nazir Bilal Yavuz wrote: > > Subject: [PATCH v12] Speed up COPY FROM text/CSV parsing using SIMD > > This looks pretty good to me. I'm hoping to take a closer look in the near > future, but I think we are approaching something committable. Thanks for looking into it! I am attaching v13 of the patch. 0001 is basically some typo fixes on top of v12, no functional changes. 0002 has an attempt to remove some branches from SIMD code but since it is kind of functional change, I wanted to attach that as another patch. I think we can apply some parts of this, if not all. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
0002 has an attempt to remove some branches from SIMD code but since
it is kind of functional change, I wanted to attach that as another
patch. I think we can apply some parts of this, if not all.
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 11 Mar 2026 at 15:19, KAZAR Ayoub wrote: > > On Wed, Mar 11, 2026, 12:36 PM Nazir Bilal Yavuz wrote: >> >> 0002 has an attempt to remove some branches from SIMD code but since >> it is kind of functional change, I wanted to attach that as another >> patch. I think we can apply some parts of this, if not all. > > 0002 sounds really good to have, haven't measured the diff but it's very logical. I agree with you. I saw very small speedups like 1%-2% but I think changes make sense regardless of the performance improvement. > Another quick question though, do we need USE_NO_SIMD for any reason? I just remembered that there's some simd paths like json that don't use it. vector8_eq() and vector8_highbit_mask() don't have non-SIMD implementations, so we need to use USE_NO_SIMD. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Wed, 11 Mar 2026 at 15:19, KAZAR Ayoub <ma_kazar@esi.dz> wrote:
>
> On Wed, Mar 11, 2026, 12:36 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:
>>
>> 0002 has an attempt to remove some branches from SIMD code but since
>> it is kind of functional change, I wanted to attach that as another
>> patch. I think we can apply some parts of this, if not all.
>
> 0002 sounds really good to have, haven't measured the diff but it's very logical.
I agree with you. I saw very small speedups like 1%-2% but I think
changes make sense regardless of the performance improvement.
> Another quick question though, do we need USE_NO_SIMD for any reason? I just remembered that there's some simd paths like json that don't use it.
vector8_eq() and vector8_highbit_mask() don't have non-SIMD
implementations, so we need to use USE_NO_SIMD.
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Mar 11, 2026 at 02:36:46PM +0300, Nazir Bilal Yavuz wrote: > 0002 has an attempt to remove some branches from SIMD code but since > it is kind of functional change, I wanted to attach that as another > patch. I think we can apply some parts of this, if not all. Could you describe what this is doing and what the performance impact is? -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Wed, 11 Mar 2026 at 21:09, Nathan Bossart wrote:
>
> On Wed, Mar 11, 2026 at 02:36:46PM +0300, Nazir Bilal Yavuz wrote:
> > 0002 has an attempt to remove some branches from SIMD code but since
> > it is kind of functional change, I wanted to attach that as another
> > patch. I think we can apply some parts of this, if not all.
>
> Could you describe what this is doing and what the performance impact is?
SIMD code check these characters:
csv mode: nl, cr, quote and possibly escape.
text mode: nl, cr and bs.
v12 checks them like that:
if (is_csv)
{
match = vector8_or(vector8_eq(chunk, nl),
vector8_eq(chunk, cr));
match = vector8_or(match, vector8_eq(chunk, quote));
if (unique_escapec)
match = vector8_or(match, vector8_eq(chunk, escape));
}
else
{
match = vector8_or(vector8_eq(chunk, nl),
vector8_eq(chunk, cr));
match = vector8_or(match, vector8_eq(chunk, bs));
}
But actually we know that we will definitely check nl, cr and one of
the quote or bs characters in the code. So, we can introduce a new
variable named bs_or_quote, it will be equal to bs if the mode is text
and it will be equal to quote if the mode is csv. Then, we can remove
the 'if (is_csv)' check and only check for escape ('if
(unique_escapec)'). Now code will look like that:
match = vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));
match = vector8_or(match, vector8_eq(chunk, bs_or_quote));
if (unique_escapec)
match = vector8_or(match, vector8_eq(chunk, escape));
That is what v13-0002 does. I saw 1%-2% speedups with this change and
there was no regression.
Regardless of introducing the bs_or_quote variable, we can move 'match
= vector8_or(vector8_eq(chunk, nl), vector8_eq(chunk, cr));' outside
of the if checks, though.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Mar 11, 2026 at 09:49:22PM +0300, Nazir Bilal Yavuz wrote: > That is what v13-0002 does. I saw 1%-2% speedups with this change and > there was no regression. Thanks for the explanation. Is there any reason _not_ to add this to 0001? -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 11 Mar 2026 at 22:02, Nathan Bossart wrote: > > On Wed, Mar 11, 2026 at 09:49:22PM +0300, Nazir Bilal Yavuz wrote: > > That is what v13-0002 does. I saw 1%-2% speedups with this change and > > there was no regression. > > Thanks for the explanation. Is there any reason _not_ to add this to 0001? I noticed this improvement today. To make it easier to review for anyone who may have already started looking at v12, I attached the new functional code changes as 0002. There was no other reason. Here is v14 which is v13-0001 + v13-0002. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Mar 11, 2026 at 10:22:18PM +0300, Nazir Bilal Yavuz wrote: > Here is v14 which is v13-0001 + v13-0002. Thanks! It's getting close. > + /* > + * Temporary variables are used here instead of passing the actual > + * variables (especially input_buf_ptr) directly to the helper. Taking > + * the address of a local variable might force the compiler to > + * allocate it on the stack rather than in a register. Because > + * input_buf_ptr is used heavily in the hot scalar path below, keeping > + * it in a register is important for performance. > + */ > + int temp_input_buf_ptr; > + bool temp_hit_eof = hit_eof; A few notes: * Does using a temporary variable for hit_eof actually make a difference? AFAICT that's only updated when loading more data. * Does inlining the function produce the same results? * Also, I'm curious what the usual benchmarks look like with and without this hack for the latest patch. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Wed, 11 Mar 2026 at 23:42, Nathan Bossart wrote: > > On Wed, Mar 11, 2026 at 10:22:18PM +0300, Nazir Bilal Yavuz wrote: > > Here is v14 which is v13-0001 + v13-0002. > > Thanks! It's getting close. > > > + /* > > + * Temporary variables are used here instead of passing the actual > > + * variables (especially input_buf_ptr) directly to the helper. Taking > > + * the address of a local variable might force the compiler to > > + * allocate it on the stack rather than in a register. Because > > + * input_buf_ptr is used heavily in the hot scalar path below, keeping > > + * it in a register is important for performance. > > + */ > > + int temp_input_buf_ptr; > > + bool temp_hit_eof = hit_eof; > > A few notes: > > * Does using a temporary variable for hit_eof actually make a difference? > AFAICT that's only updated when loading more data. > > * Does inlining the function produce the same results? > > * Also, I'm curious what the usual benchmarks look like with and without > this hack for the latest patch. I tried to benchmark all of these questions, here are the results: Old master means d841ca2d14 - inlining CopyReadLineText commit (dc592a4155). v14 means d841ca2d14 + v14. v14 + #1 means removing temporary variables. v14 + #2 means removing temp_hit_eof variable only. v14 + #3 means inlining CopyReadLineTextSIMDHelper(). v14 + #4 means inlining CopyReadLineTextSIMDHelper() + removing temporary variables (#1). ------------------------------------------------------------ Results for default_toast_compression = 'lz4': +-------------------------------------------+ | Optimization: -O2 | +------------+--------------+---------------+ | | Text | CSV | +------------+------+-------+-------+-------+ | WIDE | None | 1/3 | None | 1/3 | +------------+------+-------+-------+-------+ | Old master | 4260 | 4789 | 5930 | 8276 | +------------+------+-------+-------+-------+ | v14 | 2489 | 4439 | 2529 | 8098 | +------------+------+-------+-------+-------+ | v14 + #1 | 2472 | 5177 | 2479 | 9285 | +------------+------+-------+-------+-------+ | v14 + #2 | 2521 | 4252 | 2481 | 8050 | +------------+------+-------+-------+-------+ | v14 + #3 | 2632 | 4569 | 2458 | 8657 | +------------+------+-------+-------+-------+ | v14 + #4 | 2476 | 4239 | 2475 | 10544 | +------------+------+-------+-------+-------+ | | | | | | +------------+------+-------+-------+-------+ | | | | | | +------------+------+-------+-------+-------+ | | Text | CSV | +------------+------+-------+-------+-------+ | NARROW | None | 1/3 | None | 1/3 | +------------+------+-------+-------+-------+ | Old master | 9955 | 10056 | 10329 | 10872 | +------------+------+-------+-------+-------+ | v14 | 9917 | 10080 | 10104 | 10510 | +------------+------+-------+-------+-------+ | v14 + #1 | 9913 | 10090 | 10120 | 10532 | +------------+------+-------+-------+-------+ | v14 + #2 | 9937 | 10130 | 10072 | 10520 | +------------+------+-------+-------+-------+ | v14 + #3 | 9880 | 10258 | 10220 | 10604 | +------------+------+-------+-------+-------+ | v14 + #4 | 9827 | 10306 | 10308 | 10734 | +------------+------+-------+-------+-------+ ------------------------------------------------------------ Results for default_toast_compression = 'pglz': +-------------------------------------------+ | Optimization: -O2 | +------------+--------------+---------------+ | | Text | CSV | +------------+------+-------+-------+-------+ | WIDE | None | 1/3 | None | 1/3 | +------------+------+-------+-------+-------+ | Old master | 4260 | 4789 | 5930 | 8276 | +------------+------+-------+-------+-------+ | v14 | 2489 | 4439 | 2529 | 8098 | +------------+------+-------+-------+-------+ | v14 + #1 | 2472 | 5177 | 2479 | 9285 | +------------+------+-------+-------+-------+ | v14 + #2 | 2521 | 4252 | 2481 | 8050 | +------------+------+-------+-------+-------+ | v14 + #3 | 2632 | 4569 | 2458 | 8657 | +------------+------+-------+-------+-------+ | v14 + #4 | 2476 | 4239 | 2475 | 10544 | +------------+------+-------+-------+-------+ | | | | | | +------------+------+-------+-------+-------+ | | | | | | +------------+------+-------+-------+-------+ | | Text | CSV | +------------+------+-------+-------+-------+ | NARROW | None | 1/3 | None | 1/3 | +------------+------+-------+-------+-------+ | Old master | 9955 | 10056 | 10329 | 10872 | +------------+------+-------+-------+-------+ | v14 | 9917 | 10080 | 10104 | 10510 | +------------+------+-------+-------+-------+ | v14 + #1 | 9913 | 10090 | 10120 | 10532 | +------------+------+-------+-------+-------+ | v14 + #2 | 9937 | 10130 | 10072 | 10520 | +------------+------+-------+-------+-------+ | v14 + #3 | 9880 | 10258 | 10220 | 10604 | +------------+------+-------+-------+-------+ | v14 + #4 | 9827 | 10306 | 10308 | 10734 | +------------+------+-------+-------+-------+ ------------------------------------------------------------ By looking these results: v14 + #1 and v14 + #3 performs worse on wide & 1/3 cases. v14 + #4 performs worse on CSV & wide & 1/3 cases. v14 and v14 + #2 perform very similarly. They don't have regression. I think we can move forward with one of these. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Here is what I have staged for commit, which I'm planning to do tomorrow. Please review and/or test if you are able. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Here is what I have staged for commit, which I'm planning to do tomorrow.
Please review and/or test if you are able.
--
nathan
I found some time this evening to run some benchmarks using your v15 patch. I hope these help.
lz4 - arm
arm NARROW master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = lz4
TXT : 10203.493250 ms
CSV : 10217.946000 ms
TXT with 1/3 escapes: 10305.912750 ms
CSV with 1/3 quotes: 12339.182000 ms
arm NARROW v15 default_toast_compression = lz4
TXT : 10205.261500 ms -0.017330% regression
CSV : 10358.898500 ms -1.379460% regression
TXT with 1/3 escapes: 10053.073000 ms 2.453347% improvement
CSV with 1/3 quotes: 11881.337000 ms 3.710497% improvement
arm WIDE master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = lz4
TXT : 5613.525250 ms
CSV : 8069.692750 ms
TXT with 1/3 escapes: 7088.888250 ms
CSV with 1/3 quotes: 10902.545500 ms
arm WIDE v15 default_toast_compression = lz4
TXT : 3201.494500 ms 42.968200% improvement
CSV : 3146.033750 ms 61.014207% improvement
TXT with 1/3 escapes: 6677.907500 ms 5.797535% improvement
CSV with 1/3 quotes: 10766.909500 ms 1.244076% improvement
lz4 - x86
x86 NARROW master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = lz4
TXT : 26110.287750 ms
CSV : 27923.199750 ms
TXT with 1/3 escapes: 27984.483250 ms
CSV with 1/3 quotes: 34387.239000 ms
x86 NARROW v15 default_toast_compression = lz4
TXT : 26019.629000 ms 0.347215% improvement
CSV : 26379.889000 ms 5.526984% improvement
TXT with 1/3 escapes: 28865.322750 ms -3.147600% regression
CSV with 1/3 quotes: 33218.293250 ms 3.399359% improvement
x86 WIDE master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = lz4
TXT : 15829.765000 ms
CSV : 20479.146000 ms
TXT with 1/3 escapes: 18437.507500 ms
CSV with 1/3 quotes: 29749.379250 ms
x86 WIDE v15 default_toast_compression = lz4
TXT : 8056.305000 ms 49.106604% improvement
CSV : 7997.555500 ms 60.947808% improvement
TXT with 1/3 escapes: 16324.925500 ms 11.458067% improvement
CSV with 1/3 quotes: 29978.346500 ms -0.769654% regression
pglz - arm
arm NARROW master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 10334.666250 ms
CSV : 10978.851250 ms
TXT with 1/3 escapes: 11076.502750 ms
CSV with 1/3 quotes: 12582.679000 ms
arm NARROW v15 default_toast_compression = pglz
TXT : 10002.507750 ms 3.214023% improvement
CSV : 10017.436250 ms 8.756973% improvement
TXT with 1/3 escapes: 10179.949000 ms 8.094195% improvement
CSV with 1/3 quotes: 12088.836750 ms 3.924778% improvement
arm WIDE master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 11403.206000 ms
CSV : 13915.718750 ms
TXT with 1/3 escapes: 12888.060250 ms
CSV with 1/3 quotes: 16741.463000 ms
arm WIDE v15 default_toast_compression = pglz
TXT : 9005.868250 ms 21.023366% improvement
CSV : 8935.159250 ms 35.790889% improvement
TXT with 1/3 escapes: 12432.655250 ms 3.533542% improvement
CSV with 1/3 quotes: 16564.852250 ms 1.054930% improvement
pglz - x86
x86 NARROW master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 26404.516250 ms
CSV : 28138.719000 ms
TXT with 1/3 escapes: 28084.379750 ms
CSV with 1/3 quotes: 34502.702250 ms
x86 NARROW v15 default_toast_compression = pglz
TXT : 26438.415000 ms -0.128382% regression
CSV : 26869.718000 ms 4.509804% improvement
TXT with 1/3 escapes: 29379.299750 ms -4.610819% regression
CSV with 1/3 quotes: 33371.390250 ms 3.278908% improvement
x86 WIDE master (git revert dc592a41557b072178f1798700bf9c69cd8e4235) default_toast_compression = pglz
TXT : 30595.372000 ms
CSV : 35665.908500 ms
TXT with 1/3 escapes: 32746.252000 ms
CSV with 1/3 quotes: 44136.542750 ms
x86 WIDE v15 default_toast_compression = pglz
TXT : 22681.770750 ms 25.865354% improvement
CSV : 22692.153000 ms 36.375789% improvement
TXT with 1/3 escapes: 30638.978000 ms 6.435161% improvement
CSV with 1/3 quotes: 44330.233000 ms -0.438843% regression
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi,
On Thu, 12 Mar 2026 at 20:37, Nathan Bossart wrote:
>
> Here is what I have staged for commit, which I'm planning to do tomorrow.
> Please review and/or test if you are able.
Thank you!
Unfortunately, v15 causes a regression for a 'csv & wide & 1/3' case
on my end. v14 was taking 8000ms but v15 took ~9100ms. If we add the
tmp_hit_eof variable then the regression disappears. Also, if I use a
struct like below, regression disappears again.
typedef struct CopyReadLineSIMDResult
{
int input_buf_ptr;
bool hit_eof;
bool result;
} CopyReadLineSIMDResult;
When I removed the tmp_hit_eof variable on v14, I didn't encounter any
regression. I really don't understand why this is happening on my end.
Manni didn't encounter any regression on the benchmark [1].
I benchmarked v15 and both of the cases above:
------------------------------------------------------------
Results for default_toast_compression = 'lz4':
+--------------------------------------------------+
| Optimization: -O2 |
+-------------------+--------------+---------------+
| | Text | CSV |
+-------------------+------+-------+-------+-------+
| WIDE | None | 1/3 | None | 1/3 |
+-------------------+------+-------+-------+-------+
| Old master | 4260 | 4789 | 5930 | 8276 |
+-------------------+------+-------+-------+-------+
| v14 | 2489 | 4439 | 2529 | 8098 |
+-------------------+------+-------+-------+-------+
| v15 | 2494 | 4235 | 2490 | 9140 |
+-------------------+------+-------+-------+-------+
| v15 + tmp_hit_eof | 2487 | 4539 | 2478 | 8041 |
+-------------------+------+-------+-------+-------+
| v15 + struct | 2490 | 4531 | 2483 | 7756 |
+-------------------+------+-------+-------+-------+
| | | | | |
+-------------------+------+-------+-------+-------+
| | | | | |
+-------------------+------+-------+-------+-------+
| | Text | CSV |
+-------------------+------+-------+-------+-------+
| NARROW | None | 1/3 | None | 1/3 |
+-------------------+------+-------+-------+-------+
| Old master | 9955 | 10056 | 10329 | 10872 |
+-------------------+------+-------+-------+-------+
| v14 | 9917 | 10080 | 10104 | 10510 |
+-------------------+------+-------+-------+-------+
| v15 | 9898 | 10062 | 10232 | 10483 |
+-------------------+------+-------+-------+-------+
| v15 + tmp_hit_eof | 9847 | 10004 | 10192 | 10437 |
+-------------------+------+-------+-------+-------+
| v15 + struct | 9877 | 10008 | 10107 | 10521 |
+-------------------+------+-------+-------+-------+
------------------------------------------------------------
Results for default_toast_compression = 'pglz':
+---------------------------------------------------+
| Optimization: -O2 |
+-------------------+---------------+---------------+
| | Text | CSV |
+-------------------+-------+-------+-------+-------+
| WIDE | None | 1/3 | None | 1/3 |
+-------------------+-------+-------+-------+-------+
| Old master | 10579 | 10927 | 12276 | 14488 |
+-------------------+-------+-------+-------+-------+
| v14 | 8832 | 10646 | 8815 | 14352 |
+-------------------+-------+-------+-------+-------+
| v15 | 8859 | 10489 | 8835 | 15414 |
+-------------------+-------+-------+-------+-------+
| v15 + tmp_hit_eof | 8828 | 10829 | 8840 | 14297 |
+-------------------+-------+-------+-------+-------+
| v15 + struct | 8847 | 10829 | 8846 | 14003 |
+-------------------+-------+-------+-------+-------+
| | | | | |
+-------------------+-------+-------+-------+-------+
| | | | | |
+-------------------+-------+-------+-------+-------+
| | Text | CSV |
+-------------------+-------+-------+-------+-------+
| NARROW | None | 1/3 | None | 1/3 |
+-------------------+-------+-------+-------+-------+
| Old master | 9952 | 10342 | 10112 | 10861 |
+-------------------+-------+-------+-------+-------+
| v14 | 9907 | 10344 | 10103 | 10492 |
+-------------------+-------+-------+-------+-------+
| v15 | 9897 | 10261 | 10126 | 10490 |
+-------------------+-------+-------+-------+-------+
| v15 + tmp_hit_eof | 9848 | 10218 | 10184 | 10425 |
+-------------------+-------+-------+-------+-------+
| v15 + struct | 9858 | 10150 | 10116 | 10464 |
+-------------------+-------+-------+-------+-------+
------------------------------------------------------------
It can be seen that the 'csv & wide & 1/3' case is much better on 'v15
+ struct' and 'v15 + tmp_hit_eof' but 'text & wide & 1/3' case is a
bit worse but still better than master.
Regardless of the issues above, I encountered a compiler warning on
the v15, if 'USE_NO_SIMD' is defined, then this warning appears:
copyfromparse.c:1780:1: warning: label ‘out’ defined but not used
[-Wunused-label]
Rest of the changes look good to me. v16 is attached, it fixes the
warning by protecting 'out' with '#ifndef USE_NO_SIMD', no other
changes. In addition to that, I put 'using CopyReadLineSIMDResult
struct' as a 0002 to get an opinion.
[1] https://postgr.es/m/CAKWEB6pMbdMDvhfaX1Z0eSULVQFYhEhssaRHdOxAX_5OYubxKw%40mail.gmail.com
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Fri, 13 Mar 2026 at 14:57, Nazir Bilal Yavuz wrote: > > Unfortunately, v15 causes a regression for a 'csv & wide & 1/3' case > on my end. v14 was taking 8000ms but v15 took ~9100ms. If we add the > tmp_hit_eof variable then the regression disappears. Also, if I use a > struct like below, regression disappears again. > When I removed the tmp_hit_eof variable on v14, I didn't encounter any > regression. I really don't understand why this is happening on my end. > Manni didn't encounter any regression on the benchmark [1]. Problem might be related to gcc. I am using Debian Trixie and my current gcc version is 'gcc version 14.2.0 (Debian 14.2.0-19)'. If I compile Postgres with 'Debian clang version 19.1.7 (3+b1)', then there is no regression, which makes more sense IMO. Here is a comparison for csv & wide & 1/3 case. Postgres is compiled with buildtype=debugoptimized and default_toast_compression is lz4. +--------------------------------+ | CSV & WIDE & 1/3, LZ4, -O2 | +--------------+--------+--------+ | | gcc | clang | | | 14.0.2 | 19.1.7 | +--------------+--------+--------+ | old master | 8250 | 10400 | +--------------+--------+--------+ | v14 | 8100 | 9800 | +--------------+--------+--------+ | v15 | 9200 | 9800 | +--------------+--------+--------+ | v15 + struct | 7750 | 9800 | +--------------+--------+--------+ -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026 at 04:34:49PM +0300, Nazir Bilal Yavuz wrote: > On Fri, 13 Mar 2026 at 14:57, Nazir Bilal Yavuz wrote: >> Unfortunately, v15 causes a regression for a 'csv & wide & 1/3' case >> on my end. v14 was taking 8000ms but v15 took ~9100ms. If we add the >> tmp_hit_eof variable then the regression disappears. Also, if I use a >> struct like below, regression disappears again. > >> When I removed the tmp_hit_eof variable on v14, I didn't encounter any >> regression. I really don't understand why this is happening on my end. >> Manni didn't encounter any regression on the benchmark [1]. > > Problem might be related to gcc. I am using Debian Trixie and my > current gcc version is 'gcc version 14.2.0 (Debian 14.2.0-19)'. If I > compile Postgres with 'Debian clang version 19.1.7 (3+b1)', then there > is no regression, which makes more sense IMO. Let's just re-add the temporary variable for hit_eof. The struct idea is clever, but it's just a little more complicated than I think is necessary here. I've also removed the goto in favor of just duplicating the "out" code, like you had before. I'd like to avoid sporadic #ifndef USE_NO_SIMD uses, and goto is out of fashion, anyway. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Thu, Mar 12, 2026 at 09:39:38PM -0500, Manni Wood wrote: > I found some time this evening to run some benchmarks using your v15 patch. > I hope these help. Thanks! > x86 NARROW v15 default_toast_compression = lz4 > TXT : 26019.629000 ms 0.347215% improvement > CSV : 26379.889000 ms 5.526984% improvement > TXT with 1/3 escapes: 28865.322750 ms -3.147600% regression > CSV with 1/3 quotes: 33218.293250 ms 3.399359% improvement > x86 NARROW v15 default_toast_compression = pglz > TXT : 26438.415000 ms -0.128382% regression > CSV : 26869.718000 ms 4.509804% improvement > TXT with 1/3 escapes: 29379.299750 ms -4.610819% regression > CSV with 1/3 quotes: 33371.390250 ms 3.278908% improvement Those 3-5% regressions are interesting, but given there are similar "improvements" for the surrounding cases, I'm going to consider them as noise for now and proceed with the patch. If folks feel strongly about digging deeper here, I'm happy to revisit the subject. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026, at 10:05 AM, Nathan Bossart wrote: > On Fri, Mar 13, 2026 at 04:34:49PM +0300, Nazir Bilal Yavuz wrote: >> On Fri, 13 Mar 2026 at 14:57, Nazir Bilal Yavuz wrote: >>> Unfortunately, v15 causes a regression for a 'csv & wide & 1/3' case >>> on my end. v14 was taking 8000ms but v15 took ~9100ms. If we add the >>> tmp_hit_eof variable then the regression disappears. Also, if I use a >>> struct like below, regression disappears again. >> >>> When I removed the tmp_hit_eof variable on v14, I didn't encounter any >>> regression. I really don't understand why this is happening on my end. >>> Manni didn't encounter any regression on the benchmark [1]. >> >> Problem might be related to gcc. I am using Debian Trixie and my >> current gcc version is 'gcc version 14.2.0 (Debian 14.2.0-19)'. If I >> compile Postgres with 'Debian clang version 19.1.7 (3+b1)', then there >> is no regression, which makes more sense IMO. > > Let's just re-add the temporary variable for hit_eof. The struct idea is > clever, but it's just a little more complicated than I think is necessary > here. > > I've also removed the goto in favor of just duplicating the "out" code, > like you had before. I'd like to avoid sporadic #ifndef USE_NO_SIMD uses, > and goto is out of fashion, anyway. Hey Nathan, Nazir, et. al., I've always been a fan of these kinds of optimization so I couldn't resist reviewing, but I know you're ready to commit so I'll just check on some systems I have. :) At first glance the implementation seems conservative, but correct and safe. Local testing on on Linux/FreeBSD x86_64, and Win11/aarch64/MSVC seem good. I also tried IllumOS/SPARCv9 and with some fixes (from another active thread) to the build system and it worked just fine too. I'm sure the 10 people care will be thrilled. ;-P I ran into the "out:" label not defined because USE_NO_SIMD wasn't defined when I tested v15 on Linux/RISC-V, my build-farm animal "greenfly" which is an Orange Pi RV2 running Linux 24.04.4 LTS. Looks like changes this morning have fixed that. I would have reported earlier this morning but I detoured into RISC-V specific fixes and optimizations. Interestingly, simd.h uses a fixed-width Vector8 type (16 bytes on SSE2/NEON) and relies on sizeof(Vector8) throughout, however RISC-V Vector types like vuint8m1_t are "sizeless" meaning that you cannot use sizeof() on them. To model that properly would require overhauling all of simd.h, which is way out of scope for this patch so I'll start a new thread for that (and CRC, and popcnt). I also created a few tests (attached) to check boundary conditions, I might add some along with the RISC-V work. nice work, LGTM. -greg > -- > nathan > > Attachments: > * v17-0001-Optimize-COPY-FROM-FORMAT-text-csv-using-SIMD.patch
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, 13 Mar 2026 at 17:05, Nathan Bossart <nathandbossart@gmail.com> wrote:
>
> On Fri, Mar 13, 2026 at 04:34:49PM +0300, Nazir Bilal Yavuz wrote:
> > On Fri, 13 Mar 2026 at 14:57, Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:
> >> Unfortunately, v15 causes a regression for a 'csv & wide & 1/3' case
> >> on my end. v14 was taking 8000ms but v15 took ~9100ms. If we add the
> >> tmp_hit_eof variable then the regression disappears. Also, if I use a
> >> struct like below, regression disappears again.
> >
> >> When I removed the tmp_hit_eof variable on v14, I didn't encounter any
> >> regression. I really don't understand why this is happening on my end.
> >> Manni didn't encounter any regression on the benchmark [1].
> >
> > Problem might be related to gcc. I am using Debian Trixie and my
> > current gcc version is 'gcc version 14.2.0 (Debian 14.2.0-19)'. If I
> > compile Postgres with 'Debian clang version 19.1.7 (3+b1)', then there
> > is no regression, which makes more sense IMO.
>
> Let's just re-add the temporary variable for hit_eof. The struct idea is
> clever, but it's just a little more complicated than I think is necessary
> here.
>
> I've also removed the goto in favor of just duplicating the "out" code,
> like you had before. I'd like to avoid sporadic #ifndef USE_NO_SIMD uses,
> and goto is out of fashion, anyway.
Thanks! v17 LGTM. I didn't encounter any regressions.
--
Regards,
Nazir Bilal Yavuz
Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026 at 06:58:49PM +0300, Nazir Bilal Yavuz wrote: > Thanks! v17 LGTM. I didn't encounter any regressions. Committed. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Fri, 13 Mar 2026 at 19:08, Nathan Bossart wrote: > > On Fri, Mar 13, 2026 at 06:58:49PM +0300, Nazir Bilal Yavuz wrote: > > Thanks! v17 LGTM. I didn't encounter any regressions. > > Committed. Thank you for taking care of this! -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi Greg, On Fri, 13 Mar 2026 at 18:29, Greg Burd wrote: > > I've always been a fan of these kinds of optimization so I couldn't resist reviewing, but I know you're ready to commit so I'll just check on some systems I have. :) Thank you for the review! > At first glance the implementation seems conservative, but correct and safe. Local testing on on Linux/FreeBSD x86_64, and Win11/aarch64/MSVC seem good. I also tried IllumOS/SPARCv9 and with some fixes (from another active thread) to the build system and it worked just fine too. I'm sure the 10 people care will be thrilled. ;- Yes, we can probably improve this further with heuristics, but for now we wanted to avoid introducing any potential regressions. > I also created a few tests (attached) to check boundary conditions, I might add some along with the RISC-V work. Thank you for the tests! I have checked them and the output is the same on both v17 and master. Do you think it would make sense to add them as regression tests? -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026 at 07:14:06PM +0300, Nazir Bilal Yavuz wrote: > On Fri, 13 Mar 2026 at 18:29, Greg Burd wrote: >> I also created a few tests (attached) to check boundary conditions, I >> might add some along with the RISC-V work. > > Thank you for the tests! I have checked them and the output is the > same on both v17 and master. Do you think it would make sense to add > them as regression tests? Seems like a good idea. I was curious what the test coverage looked like without extra tests. Once there's a report, we could choose a subset of these to close any gaps. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026, at 12:14 PM, Nazir Bilal Yavuz wrote: > Hi Greg, Hello Nazir, > On Fri, 13 Mar 2026 at 18:29, Greg Burd wrote: >> >> I've always been a fan of these kinds of optimization so I couldn't resist reviewing, but I know you're ready to commit so I'll just check on some systems I have. :) > > Thank you for the review! Thank YOU for the work fixing this. :) >> At first glance the implementation seems conservative, but correct and safe. Local testing on on Linux/FreeBSD x86_64, and Win11/aarch64/MSVC seem good. I also tried IllumOS/SPARCv9 and with some fixes (from another active thread) to the build system and it worked just fine too. I'm sure the 10 people care will be thrilled. ;- > > Yes, we can probably improve this further with heuristics, but for now > we wanted to avoid introducing any potential regressions. >> I also created a few tests (attached) to check boundary conditions, I might add some along with the RISC-V work. > > Thank you for the tests! I have checked them and the output is the > same on both v17 and master. Do you think it would make sense to add > them as regression tests? If there are tests that materially add to the coverage that's a good thing to consider adding. I don't think all those tests are necessary. best. -greg > -- > Regards, > Nazir Bilal Yavuz > Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026, at 12:16 PM, Nathan Bossart wrote: > On Fri, Mar 13, 2026 at 07:14:06PM +0300, Nazir Bilal Yavuz wrote: >> On Fri, 13 Mar 2026 at 18:29, Greg Burd wrote: >>> I also created a few tests (attached) to check boundary conditions, I >>> might add some along with the RISC-V work. >> >> Thank you for the tests! I have checked them and the output is the >> same on both v17 and master. Do you think it would make sense to add >> them as regression tests? > > Seems like a good idea. I was curious what the test coverage looked like > without extra tests. Once there's a report, we could choose a subset of > these to close any gaps. +1, you said it better than I did! :) > -- > nathan best. -greg
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Fri, Mar 13, 2026 at 01:21:38PM -0400, Greg Burd wrote: > On Fri, Mar 13, 2026, at 12:14 PM, Nazir Bilal Yavuz wrote: >> On Fri, 13 Mar 2026 at 18:29, Greg Burd wrote: >>> I also created a few tests (attached) to check boundary conditions, I >>> might add some along with the RISC-V work. >> >> Thank you for the tests! I have checked them and the output is the >> same on both v17 and master. Do you think it would make sense to add >> them as regression tests? > > If there are tests that materially add to the coverage that's a good > thing to consider adding. I don't think all those tests are necessary. We seem to have good coverage on the new code [0]. I still wouldn't mind adding a couple of tests for correctness, if folks want them. [0] https://coverage.postgresql.org/src/backend/commands/copyfromparse.c.gcov.html -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Sep 08, 2026 at 01:11:00PM +0300, Nazir Bilal Yavuz wrote: > Here is an attempt to solve this problem. I ran Manni's script and saw > a 1-2% slowdown on TEXT-wide inputs. It is still faster compared to > the version without the SIMD patch; the slowdown is relative to the > current master branch (which includes the SIMD patch). Thanks for reporting. Fable 5.1 suggests moving the check to CopyLoadInputBuf() to avoid this small regression, which makes sense to me. I've attached a new patch that does that. This is definitely a bit of hack, and I'm assuming we'll need to revisit this if we want to re-attempt more sophisticated heuristics for when to use the SIMD path. But IMHO this is self-contained enough to be reasonable for v19. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Tue, Sep 08, 2026 at 11:58:42AM -0400, Greg Burd wrote: > The test writes one full input buffer (INPUT_BUF_SIZE, 65536 bytes) down a > COPY FROM PROGRAM pipe: a 65524-byte line, then a complete \. marker, then > 8 trailing bytes. That leaves 11 bytes unexamined when the SIMD path wants > to refill, which is under sizeof(Vector8) and already holds the whole > marker. The writer then dribbles a byte every 50ms and keeps the pipe open, > so a speculative load blocks rather than seeing EOF. statement_timeout > bounds the failure, so a regression fails the test instead of hanging the > suite. Skipped on Windows, where the blocking pipe and SIGPIPE behavior > isn't reliable. I'm not sure this particular corner case deserves its own TAP test. IMHO that is disproportionately expensive. It really only serves to prevent us from removing the backslash scan from the SIMD path, which I don't think we're likely to do. Furthermore, I'm not aware of any existing examples where a test writes and then invokes its own Perl script, and I can't get too excited about being the first to commit something like that. So, I'd prefer to leave this test out for now. If the new test fit cleanly into an existing suite or was less magical, I might be more eager to include it, but it's probably still unlikely to be worth the energy and maintenance risk. -- nathan
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 8 Sept 2026 at 18:25, Nathan Bossart wrote: > > On Tue, Sep 08, 2026 at 01:11:00PM +0300, Nazir Bilal Yavuz wrote: > > Here is an attempt to solve this problem. I ran Manni's script and saw > > a 1-2% slowdown on TEXT-wide inputs. It is still faster compared to > > the version without the SIMD patch; the slowdown is relative to the > > current master branch (which includes the SIMD patch). > > Thanks for reporting. Fable 5.1 suggests moving the check to > CopyLoadInputBuf() to avoid this small regression, which makes sense to me. > I've attached a new patch that does that. This is definitely a bit of > hack, and I'm assuming we'll need to revisit this if we want to re-attempt > more sophisticated heuristics for when to use the SIMD path. But IMHO this > is self-contained enough to be reasonable for v19. Thanks for v2; I agree that it is better. I confirm that it solves the problem and doesn't cause the 1-2% regression I mentioned. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
Hi, On Tue, 8 Sept 2026 at 23:58, Nathan Bossart wrote: > > On Tue, Sep 08, 2026 at 11:58:42AM -0400, Greg Burd wrote: > > The test writes one full input buffer (INPUT_BUF_SIZE, 65536 bytes) down a > > COPY FROM PROGRAM pipe: a 65524-byte line, then a complete \. marker, then > > 8 trailing bytes. That leaves 11 bytes unexamined when the SIMD path wants > > to refill, which is under sizeof(Vector8) and already holds the whole > > marker. The writer then dribbles a byte every 50ms and keeps the pipe open, > > so a speculative load blocks rather than seeing EOF. statement_timeout > > bounds the failure, so a regression fails the test instead of hanging the > > suite. Skipped on Windows, where the blocking pipe and SIGPIPE behavior > > isn't reliable. > > I'm not sure this particular corner case deserves its own TAP test. I have similar feelings. > IMHO > that is disproportionately expensive. It really only serves to prevent us > from removing the backslash scan from the SIMD path, which I don't think > we're likely to do. I too thought it would be expensive but it wasn't on my machine; it took 0.5s, which is lower than I expected. > Furthermore, I'm not aware of any existing examples > where a test writes and then invokes its own Perl script, and I can't get > too excited about being the first to commit something like that. So, I'd > prefer to leave this test out for now. If the new test fit cleanly into an > existing suite or was less magical, I might be more eager to include it, > but it's probably still unlikely to be worth the energy and maintenance > risk. Perhaps we can save this Perl script as another file and invoke that file. This seems better to me. However, I agree that adding this test might be overkill given the current circumstances. -- Regards, Nazir Bilal Yavuz Microsoft
Re: Speed up COPY FROM text/CSV parsing using SIMD
On Wed, Sep 9, 2026, at 6:12 AM, Nazir Bilal Yavuz wrote: > Hi, > > On Tue, 8 Sept 2026 at 23:58, Nathan Bossart wrote: >> >> On Tue, Sep 08, 2026 at 11:58:42AM -0400, Greg Burd wrote: >> > The test writes one full input buffer (INPUT_BUF_SIZE, 65536 bytes) down a >> > COPY FROM PROGRAM pipe: a 65524-byte line, then a complete \. marker, then >> > 8 trailing bytes. That leaves 11 bytes unexamined when the SIMD path wants >> > to refill, which is under sizeof(Vector8) and already holds the whole >> > marker. The writer then dribbles a byte every 50ms and keeps the pipe open, >> > so a speculative load blocks rather than seeing EOF. statement_timeout >> > bounds the failure, so a regression fails the test instead of hanging the >> > suite. Skipped on Windows, where the blocking pipe and SIGPIPE behavior >> > isn't reliable. >> >> I'm not sure this particular corner case deserves its own TAP test. > > I have similar feelings. It was just a first swing at a test, and I agree with both of you it felt heavyweight for the issue at hand. But it does capture the issue. >> IMHO >> that is disproportionately expensive. It really only serves to prevent us >> from removing the backslash scan from the SIMD path, which I don't think >> we're likely to do. > > I too thought it would be expensive but it wasn't on my machine; it > took 0.5s, which is lower than I expected. > > >> Furthermore, I'm not aware of any existing examples >> where a test writes and then invokes its own Perl script, and I can't get >> too excited about being the first to commit something like that. So, I'd >> prefer to leave this test out for now. If the new test fit cleanly into an >> existing suite or was less magical, I might be more eager to include it, >> but it's probably still unlikely to be worth the energy and maintenance >> risk. Yeah, that's a solid judgement call. One I should have made myself. > Perhaps we can save this Perl script as another file and invoke that > file. This seems better to me. However, I agree that adding this test > might be overkill given the current circumstances. It's overkill, let's focus and get the fix in. > -- > Regards, > Nazir Bilal Yavuz > Microsoft best. -greg