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 1x3xh4-006n8x-0T for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 15:25:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x3xh3-0088zx-03 for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 15:25:05 +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 1x3xh2-0088zp-1x for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2026 15:25:04 +0000 Received: from mail-qv1-xf34.google.com ([2607:f8b0:4864:20::f34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x3xh0-00000004cJL-3Seh for pgsql-hackers@postgresql.org; Tue, 08 Sep 2026 15:25:03 +0000 Received: by mail-qv1-xf34.google.com with SMTP id 6a1803df08f44-90e7c71f86eso33326926d6.1 for ; Tue, 08 Sep 2026 08:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788881102; x=1789485902; darn=postgresql.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=bZLSiHYOe4rpiOjuihqLH23uG9zJfu9ZvZltVaGolsA=; b=VGT++M6YSjaSQ0KKXm9EQXzvqpl8i8csMiunL58XJ64IQjUCIEJLPeIBqGSFvLbf7D rTkL3UxdmDnxcGP/nAgVDhInslmBZUneGpdbjcKepBty7w3wMG6leFDyIMgF63mz5FBZ DobKfJ8b/2Ww0YVOSUBk548PQHaVH7Rlho7y24aGnAjRj5OjqNSQDeEff+ePryne9ml5 5gs0xbiGCQr1UCaAWQk/q1qibKfVvcGkuuI+WcroCkj4FNrKJIJsqXydfCXwAyT4tD5E Q8YFZl8tLfeMahKGi8BWXMxW37JhjfxW4ngF+YSKuwjrq28HlxR5sIdfyrauHR9u86ZG z2mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788881102; x=1789485902; h=in-reply-to:content-disposition:content-type: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 :content-type; bh=bZLSiHYOe4rpiOjuihqLH23uG9zJfu9ZvZltVaGolsA=; b=V+at4a4dAdYRfjP3PYfDNsTBGpFaD9zPwQ/d8tVCzYYPs5tNanIIcb672A2fYJdxfD 4bXN3J6J/Nfv76ZHBcTFHT9Hrq0U2sg9ld/t1FHq6xESZtuXSp3xJ3vfUFVXE5UNxeuo IoRaqh4N4H/ZBajhRK1lu1RCoZ8Ad11W+tiUMA9sPW5y9fSntXEs7qjLd1XUmo2PcKqy EzwuiMNFe2jZdlPcZfKUXqdGW7nK25MFw86ifqB8ByFJjXiNdYQOH4MHkZ3MQo4IRL9i schEuOE+B1NBvSDnmmw3Mv0qdXqeNX4q9OQ+9vztLT7YJTTqlq6EZC1s6RYE92izZlKP JXDQ== X-Forwarded-Encrypted: i=1; AKwUvBzp/LVvczdrggHHLrrQD+8t0Co+S5l2IZTdxBovP0Gcr0rNq6akh2tVEzWQqZ1HRIyB6f1gDfRHLPSNNuTI@postgresql.org X-Gm-Message-State: AFuF++kDhk4YwkIwnwLjn4F5zHwMgrZTLp+8EqAohCcGTcAmg2J7L+v+ jTPq81ppQjWJ1+PPa5UOiqZ8+fdY7My8+8d/T5KSfWzeiaVpIjRj7SwY X-Gm-Gg: AYBFou2mDJ+Dtk1HtFEFrxA7YrNy8DxAMj/UAU73WerE/2QtaEuDT9ZHw0kywOWswmn i4IieJzmbbuRGVUQ+JhqEeRInSadLyUUOm9tJepo0FX94gfTHaeW8St4ZNWhCL/nfEyNhC8KQGq rcDsxr8I6khKmwYORDFr2+IncRz8Bg44vV/ulge/rpc3d3LgTacfoU5n3I6IyKJ833yKwITCa+g KrzGjX7ajrDC7ih1A2+RWGkEHrt0JvNiY6p+CvIiyQV83i3uHJt1b4bLSwchAsym/tLrU3+hFUO 3jncBpXfsWRg5AjQFJ56AVJOp/iN16pn6WtnHBVnjvBm1OH3+FyS7We4Tsd/jw+JiPfu2hsqmYA Io0jL0XAr9X4UvtiXBPKpr8DMbNABT5SzfGFA89CsoDTvjKRMscbN8iVF68vu3eW+WTjtHPN1At OO8DK5K6KBsVnUc633W5vc4wzAxvbn59wvqWHcXueMjhiqLkGodeg5AEcRxz+wYE6/jAGeC4vix HrUas/CrvPY5vOqae8XROGQf2NvgZ2TWMlOr2Q3d/7bMyuxEmgPs51WLsAVvgvxGEzQ0uK2M9kU zGcXT3dhp5YolGo= X-Received: by 2002:a05:6214:29c3:b0:90e:980b:9859 with SMTP id 6a1803df08f44-9103ef97107mr355927546d6.17.1788881101802; Tue, 08 Sep 2026 08:25:01 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-91058416847sm52627126d6.4.2026.09.08.08.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 08:25:00 -0700 (PDT) Date: Tue, 8 Sep 2026 10:24:59 -0500 From: Nathan Bossart To: Nazir Bilal Yavuz Cc: Greg Burd , KAZAR Ayoub , Neil Conway , Andrew Dunstan , Shinya Kato , pgsql-hackers Subject: Re: Speed up COPY FROM text/CSV parsing using SIMD Message-ID: References: <78e736c8-7f4e-46c2-9fcf-a6839e393d64@app.fastmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="6iEgN7+SXNUlzhJ5" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --6iEgN7+SXNUlzhJ5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --6iEgN7+SXNUlzhJ5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v2-0001-Fix-hangs-in-COPY-FROM-FORMAT-text.patch From 2323f8bbc97467238859ece05f3b6da5524c7593 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 8 Sep 2026 10:24:31 -0500 Subject: [PATCH v2 1/1] Fix hangs in COPY FROM (FORMAT text). The SIMD path for this command reads ahead via CopyLoadInputBuf() whenever fewer than sizeof(Vector8) bytes remain in the input buffer, even if those bytes hold a complete end-of-copy marker. If the input is a pipe whose writer has sent the marker but not closed the pipe, that read blocks, and COPY waits for data it will never use. The scalar loop asks only for the bytes it needs, so it stops at the marker without reading any further. To fix, teach CopyLoadInputBuf() to decline a speculative load when the caller's remaining bytes contain a backslash, which in text mode might begin such a marker. (CSV mode doesn't treat \. as special, so it is unaffected.) The SIMD path then hands those bytes to the scalar loop, as it already does when a load comes up short. Checking for the marker in the SIMD helper itself would be more direct, but a call there costs the compiler registers on every line, which measurably slowed COPY of short lines in my testing. Oversight in commit e0a3a3fd53. Author: Nazir Bilal Yavuz Discussion: https://postgr.es/m/CAN55FZ1qFb4Yo3-MWeQfGQnu1_Ksx4xcFC2m3hyw8a--%2BeHQYQ%40mail.gmail.com Backpatch-through: 19 --- src/backend/commands/copyfromparse.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index 98bf30ef2e7..68088108c7c 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c @@ -652,16 +652,30 @@ CopyLoadRawBuf(CopyFromState cstate) * If INPUT_BUF_BYTES(cstate) > 0, the unprocessed bytes are moved to the start * of the buffer and then we load more data after that. * - * If "speculative" is true, this function skips reporting any encoding or - * conversion errors, provided there are still data for the caller to process. - * Such callers must be prepared for this function to return without loading - * anything. + * If "speculative" is true, the caller has not yet examined the data left in + * input_buf and needn't load more to make progress. In that case, this + * function skips reporting any encoding or conversion errors, and in text + * mode it declines to read past a backslash, as that might begin an + * end-of-copy marker. Such callers must be prepared for this function to + * return without loading anything. */ static void CopyLoadInputBuf(CopyFromState cstate, bool speculative) { int nbytes = INPUT_BUF_BYTES(cstate); + /* + * In text mode, a backslash among the bytes a speculative caller has yet + * to examine might begin an end-of-copy marker. The caller must find + * that on its own, without waiting on input that may never arrive, as + * from a pipe whose writer has sent the marker but not closed the pipe. + * So in that case, just return without loading anything. + */ + if (speculative && cstate->opts.format == COPY_FORMAT_TEXT && + memchr(cstate->input_buf + cstate->input_buf_index, '\\', + nbytes) != NULL) + return; + /* * The caller has updated input_buf_index to indicate how much of the * input has been consumed and isn't needed anymore. If input_buf is the -- 2.55.0 --6iEgN7+SXNUlzhJ5--