Re: Performance degradation on concurrent COPY into a single relation in PG16.
Dean Rasheed <dean.a.rasheed@gmail.com>
From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>,
Andres Freund <andres@anarazel.de>, Jakub Wartak <jakub.wartak@enterprisedb.com>,
Andrew Dunstan <andrew@dunslane.net>, Peter Eisentraut <peter@eisentraut.org>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2023-07-20T08:36:48Z
Lists: pgsql-hackers
Attachments
- pg_strtoint32_base_10_first.v2.patch (text/x-patch) patch v2
On Thu, 20 Jul 2023 at 00:56, David Rowley <dgrowleyml@gmail.com> wrote: > > I noticed that 6fcda9aba added quite a lot of conditions that need to > be checked before we process a normal decimal integer string. I think > we could likely do better and code it to assume that most strings will > be decimal and put that case first rather than last. That sounds sensible, but ... > In the attached, I've changed that for the 32-bit version only. A > more complete patch would need to do the 16 and 64-bit versions too. > > Without that, we need to check if the first digit is '0' a total of 3 > times and also check if the 2nd digit is any of 'x', 'X', 'o', 'O', > 'b' or 'B'. That's not what I see. For me, the compiler (gcc 11, using either -O2 or -O3) is smart enough to spot that the first part of each of the 3 checks is the same, and it only tests whether the first digit is '0' once, before immediately jumping to the decimal parsing code. I didn't test other compilers though, so I can believe that different compilers might not be so smart. OTOH, this test in your patch: + /* process decimal digits */ + if (isdigit((unsigned char) ptr[0]) && + (isdigit((unsigned char) ptr[1]) || ptr[1] == '_' || ptr[1] == '\0' || isspace(ptr[1]))) is doing more work than it needs to, and actually makes things noticeably worse for me. It needs to do a minimum of 2 isdigit() checks before it will parse the input as a decimal, whereas before (for me at least) it just did one simple comparison of ptr[0] against '0'. I agree with the principal though. In the attached updated patch, I replaced that test with a simpler one: + /* + * Process the number's digits. We optimize for decimal input (expected to + * be the most common case) first. Anything that doesn't start with a base + * prefix indicator must be decimal. + */ + + /* process decimal digits */ + if (likely(ptr[0] != '0' || !isalpha((unsigned char) ptr[1]))) So hopefully any compiler should only need to do the one comparison against '0' for any non-zero decimal input. Doing that didn't give any measurable performance improvement for me, but it did at least not make it noticeably worse, and seems more likely to generate better code with not-so-smart compilers. I'd be interested to know how that performs for you (and if your compiler really does generate 3 "first digit is '0'" tests for the unpatched code). Regards, Dean --- Here were my test results (where P1 is the "fix_COPY_DEFAULT.patch"), and I tested COPY loading 50M rows: HEAD + P1 ========= 7137.966 ms 7193.190 ms 7094.491 ms 7123.520 ms HEAD + P1 + pg_strtoint32_base_10_first.patch ============================================= 7561.658 ms 7548.282 ms 7551.360 ms 7560.671 ms HEAD + P1 + pg_strtoint32_base_10_first.v2.patch ================================================ 7238.775 ms 7184.937 ms 7123.257 ms 7159.618 ms
Commits
-
Avoid edge case in pg_visibility test with small shared_buffers
- 408209d6a9ae 16.3 landed
- 3a4837fc809a 17.0 landed
-
Fix bulk table extension when copying into multiple partitions
- 0002feb82096 16.1 landed
- 22655aa23132 17.0 landed
-
hio: Take number of prior relation extensions into account
- 82a4edabd272 17.0 landed
- d37ab378b6e7 16.0 landed
-
Fix performance regression in pg_strtointNN_safe functions
- 4e2e75cd29eb 16.0 landed
- 3845577cb55e 17.0 landed
-
Fix performance problem with new COPY DEFAULT code
- c1308ce2d922 16.0 landed
- b635ac03e802 17.0 landed
-
hio: Use ExtendBufferedRelBy() to extend tables more efficiently
- 00d1e02be249 16.0 cited
-
Add VACUUM/ANALYZE BUFFER_USAGE_LIMIT option
- 1cbbee033857 16.0 cited