Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vU9yA-000p8b-0M for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Dec 2025 20:42:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vU9y7-009Lyl-0J for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Dec 2025 20:42:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vU9y6-009Lyd-1x for pgsql-hackers@lists.postgresql.org; Fri, 12 Dec 2025 20:42:27 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vU9y5-000MsA-1S for pgsql-hackers@postgresql.org; Fri, 12 Dec 2025 20:42:26 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-34a4079cfaeso1873747a91.0 for ; Fri, 12 Dec 2025 12:42:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765572143; x=1766176943; darn=postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=q6RCOJDfMxbaqgQ3p6LVGGRGekpBNZ7n7sNdGTvdlIk=; b=SvQ+ioVLgFQVqHTl0G/H4t/jAgGo4j+UrX0wvIYbpFy7fnDVB2GFCu7Ox94u8BrqpC fAFjWF2d/0QCteA1ZeDRWtj0GWk9yRnmTOEnsQX2su0AbVwTralXHar7jG5OE8NQqZsZ OwMlZ9aEhHjH6lcXLb6JBOFsEdDVdD4rMmo1yG3M727JIoyCk1EneZKX/oNBAwEbGI2d cWBnD4o1jP50U5J+NCKGC8TyU+R9+TBYB3SQ9jYCA8kLbP2phh+wqrO2Q0u/yvCd+4zK SriLu9hM0zWJhFWBVr7TC1MaBLGpaewSjtsMkqX1J+NZ8RYfD8HyiuF2cXU8pNmzIwdA 479g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765572143; x=1766176943; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q6RCOJDfMxbaqgQ3p6LVGGRGekpBNZ7n7sNdGTvdlIk=; b=QnP2j+MpVYF56U6d/HYzV7B8KBwcVoxErg2fhEWdVqMk0iwKqfq2dVXxa94aMKWGI3 3hb/yUguAACMn5cl1qHLJvTfms6UviKfpZLYYC6myyku0sOPIpVEkii0jJHoqbrPaZ5e JpGg4v/G99ZmvrZ99novgF0M9/mdmHHuN1eRDzvkwOBbnL2qQqX9BH5f0f+NtupkIFXH uXLwkIOgjD8eJM6YrkBUXcJd4WJI/I1xbihX5rTSy3ZxyCUjvSXZcPLrv6hmR3vvfB6u jgpRSwpeFkbTMtuv6HNmDejemnrAOzgxwTY346BOKbmH9uD/grQ6lv/iuUnb46z+GgI2 7NAQ== X-Forwarded-Encrypted: i=1; AJvYcCUKzN5BTHuthbjUyGgf/Misa/ncSqdVy6HUwibmovGmTy/6ZSC9ylSeLRyKDrfCf6qoEmfDk3QCRHrAaM+D@postgresql.org X-Gm-Message-State: AOJu0Yz7QeWf5+akHMny0PnI/8nldCnme6AXOvMjVVYFB9huorailSK2 V1//xJOhJUWmxP6zFPBElFv398MuM8TP7cT/3iD9GbFJGNYmN+Xgv8MO X-Gm-Gg: AY/fxX6Fib+P9WQAg8cq5yhHex8lAX4L+9+P1JC9p63XFSCY4qJ743NemCbgwB5Qat9 0UnE4Ul4QA6Q4ferJfXT00lc3SvGucbrABEFGEJI5x3OOKga3hG0Z2xpZLrDIV9E33iI7YYdblM lcQzWOElisZiVEqIkzUjGvAMqwoAmp9GcBObl/IaRMP4SSab8feecIic8uXJO0wNE63zFKuopNB J3yJns1zRSiFV5ySPJcdrCdB+n3il2Jr7HXThw9Vx5l/D2ohp++eiQYTzoOfaHGIm6zXftxW1M0 I+UPzqDgnqJ2XLEUkMgs+sWUCpn2NVIiiRcY2bNkVxPKCujOp3ED4XCiKalsAhEaZaByjvQLA8Y ULMJaiRSlxToj6gP1y24W3f/9k0Lsc3vNOhRGdCetnNONSSTieKWbM442ukInrTIAHysabUn+4F 9wQg01tdKFC0xMZnH//Zftg/RliZZkpg8= X-Google-Smtp-Source: AGHT+IFZ8jHvObIq+Dphk2pn8VZEmHnH1NESZuVb1vTHIREtGYK12GcVx1dcJagz/azvCgq2e9S4dQ== X-Received: by 2002:a05:7022:3d0d:b0:119:e569:fb91 with SMTP id a92af1059eb24-11f3530d484mr2543035c88.0.1765572143266; Fri, 12 Dec 2025 12:42:23 -0800 (PST) Received: from ltdrgnflg2 (71-34-92-171.ptld.qwest.net. [71.34.92.171]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-11f392500cdsm3372220c88.7.2025.12.12.12.42.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Dec 2025 12:42:22 -0800 (PST) Date: Fri, 12 Dec 2025 12:42:20 -0800 From: Mark Wong To: Bilal Yavuz Cc: Manni Wood , KAZAR Ayoub , Nathan Bossart , Andrew Dunstan , Shinya Kato , PostgreSQL-development Subject: Re: Speed up COPY FROM text/CSV parsing using SIMD Message-ID: References: <8e226753-57af-489a-bfbe-caa23dd71286@dunslane.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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