pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Burd <greg@burd.me>
To: Nathan Bossart <nathandbossart@gmail.com>
Cc: Nazir Bilal Yavuz <byavuz81@gmail.com>
Cc: KAZAR Ayoub <ma_kazar@esi.dz>
Cc: Neil Conway <neil.conway@gmail.com>
Cc: Andrew Dunstan <andrew@dunslane.net>
Cc: Shinya Kato <shinya11.kato@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: Speed up COPY FROM text/CSV parsing using SIMD
Date: Tue, 08 Sep 2026 11:58:42 -0400
Message-ID: <633c2a60-e5b9-49d2-a93d-946d184d946d@app.fastmail.com> (raw)
In-Reply-To: <aqArgOhtHfM2_iJe@nathan>
References: <b26a1cf8-335d-4054-90ad-d94457c45947@app.fastmail.com>
	<abRKUiInG6Gu728Q@nathan>
	<apWOr1kgVSqweBnU@nathan>
	<CAN55FZ1utgaKb1DbW7tr+2=s4Hm1yiCEeNDY9m=qP_M_drafCg@mail.gmail.com>
	<78e736c8-7f4e-46c2-9fcf-a6839e393d64@app.fastmail.com>
	<CAN55FZ0UJg+9wcyTCUG656c9DKCNvTGVuuVe4EMMVCHPwGS4aQ@mail.gmail.com>
	<apcmTvBmA27RoyYL@nathan>
	<CAN55FZ1qFb4Yo3-MWeQfGQnu1_Ksx4xcFC2m3hyw8a--+eHQYQ@mail.gmail.com>
	<CAN55FZ3U2waD=5ZuVmju-HtZ4cO4onxHN4jXskk8Y11M6o3NOQ@mail.gmail.com>
	<9434f7bb-5865-4828-9f23-1dd30e50aaac@app.fastmail.com>
	<aqArgOhtHfM2_iJe@nathan>


On Tue, Sep 8, 2026, at 11:36 AM, Nathan Bossart wrote:
> On Tue, Sep 08, 2026 at 11:08:36AM -0400, Greg Burd wrote:
>> One thing I'd like to see land with the fix: a test. I'll see if I can
>> knock one out if you don't first.
>
> I'm open to adding a test, but I'm not going to hold up the patch for it.
> I've been reluctant to add too many tests for corner cases here because I'm
> concerned about portability and fragility.  But I will admit that I haven't
> tried too hard...

Hey Nathan, I like your approach better.

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 confirmed both directions, with the fix reverse-applied it fails with
"canceling statement due to statement timeout" at COPY line 2, and with the
fix in place it passes in under 2s. I also instrumented the early return in
CopyLoadInputBuf() to be sure the test actually reaches it, and it fires
once with nbytes=11.

Attached is a TAP test and your patch as v3.

-greg

> -- 
> nathan

Attachments: download all (2 files, .tar.gz)

v3-0001-Fix-hangs-in-COPY-FROM-FORMAT-text.patch (application/octet-stream, 3.3K) download From e8ec3f600493a38dcaa02625b302ca6214848edf Mon Sep 17 00:00:00 2001 From: Nathan Bossart <nathan@postgresql.org> Date: Tue, 8 Sep 2026 10:24:31 -0500 Subject: [PATCH v3 1/2] 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 <byavuz81@gmail.com> 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.50.1 (Apple Git-155)
v3-0002-Add-TAP-test-for-COPY-SIMD-buffered-end-of-copy-m.patch (application/octet-stream, 5.1K) download From 8b93b6a63fcf9ee3ec5a9095bc47bd203e9976ba Mon Sep 17 00:00:00 2001 From: Greg Burd <greg@burd.me> Date: Tue, 8 Sep 2026 11:10:34 -0400 Subject: [PATCH v3 2/2] Add TAP test for COPY SIMD buffered end-of-copy marker The earlier commit fixed a hang where CopyReadLineTextSIMDHelper() refilled the input buffer past a complete "\." marker already sitting in the short tail, blocking on a still-open pipe. No tests in the tree cover this code. The test writes one full input buffer with the marker in that tail, then keeps the pipe open with a slow dribble so a bad refill blocks rather than seeing EOF. statement_timeout bounds the failure, so a regression fails instead of hanging the suite. Skipped on Windows, where the blocking pipe and SIGPIPE behavior is unreliable. Discussion: https://postgr.es/m/CAN55FZ3U2waD%3D5ZuVmju-HtZ4cO4onxHN4jXskk8Y11M6o3NOQ%40mail.gmail.com --- src/test/modules/test_misc/meson.build | 1 + .../test_misc/t/016_copy_simd_eoc_marker.pl | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/test/modules/test_misc/t/016_copy_simd_eoc_marker.pl diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build index 5d81f5b13be..ec41a7a373b 100644 --- a/src/test/modules/test_misc/meson.build +++ b/src/test/modules/test_misc/meson.build @@ -24,6 +24,7 @@ tests += { 't/013_temp_obj_multisession.pl', 't/014_log_statement_max_length.pl', 't/015_temp_schema_exit_deferrable.pl', + 't/016_copy_simd_eoc_marker.pl', ], # The injection points are cluster-wide, so disable installcheck 'runningcheck': false, diff --git a/src/test/modules/test_misc/t/016_copy_simd_eoc_marker.pl b/src/test/modules/test_misc/t/016_copy_simd_eoc_marker.pl new file mode 100644 index 00000000000..31ce83e0b2c --- /dev/null +++ b/src/test/modules/test_misc/t/016_copy_simd_eoc_marker.pl @@ -0,0 +1,90 @@ + +# Copyright (c) 2021-2026, PostgreSQL Global Development Group + +# Regression test for a COPY FROM (FORMAT text) hang introduced by the SIMD +# input scanner (commit e0a3a3fd536). +# +# CopyReadLineTextSIMDHelper() refills the input buffer once fewer than +# sizeof(Vector8) bytes remain, before falling back to the scalar parser. If +# those buffered bytes already hold a complete "\." end-of-copy marker and the +# source is a still-open pipe with no further data ready, the refill blocks and +# COPY fails to recognize the marker until the writer produces more data or +# closes the pipe. The fix checks the short tail for a backslash and defers to +# the scalar parser, which recognizes the marker without reading ahead. +# +# The writer program below emits exactly one full input buffer +# (INPUT_BUF_SIZE = 65536 bytes): a long line, a complete "\." marker, and a +# few trailing bytes so the marker lands in the short (< sizeof(Vector8)) tail. +# It then keeps the pipe open with a slow dribble so a buggy refill blocks +# (rather than seeing EOF) until statement_timeout would fire. With the fix, +# COPY recognizes the marker immediately and the writer exits on SIGPIPE. + +use strict; +use warnings FATAL => 'all'; +use File::Spec; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($windows_os) +{ + plan skip_all => + 'COPY FROM PROGRAM pipe-blocking scenario is unreliable on Windows'; +} + +# The writer script, run by COPY ... FROM PROGRAM. Single-quoted heredoc: the +# contents are written to the file verbatim, so the "\." below reaches the +# child perl as a literal backslash-dot end-of-copy marker. +my $writer = <<'END_WRITER'; +$SIG{PIPE} = sub { exit 0 }; +my $data = ("a" x 65524) . "\n\\.\n" . ("x" x 8); +die "payload is not 65536 bytes" unless length($data) == 65536; +my $offset = 0; +while ($offset < length($data)) +{ + my $written = syswrite(STDOUT, $data, length($data) - $offset, $offset); + die "write failed: $!" unless defined $written; + $offset += $written; +} +while (1) +{ + select(undef, undef, undef, 0.05); + syswrite(STDOUT, "x"); +} +END_WRITER + +# Absolute path: COPY ... FROM PROGRAM runs in the backend's data directory, +# not the test's working directory. +my $script = File::Spec->rel2abs( + "${PostgreSQL::Test::Utils::tmp_check}/copy_simd_eoc_writer.pl"); +append_to_file($script, $writer); + +my $node = PostgreSQL::Test::Cluster->new('primary'); +$node->init; +$node->start; + +# statement_timeout bounds the failure: a buggy build blocks in the refill and +# aborts here; a fixed build recognizes the marker and returns immediately, +# independent of the timeout. +my $sql = qq{ +SET statement_timeout = '30s'; +CREATE TEMP TABLE copy_simd_eoc (a text); +COPY copy_simd_eoc FROM PROGRAM '$^X "$script"'; +SELECT count(*) || '|' || min(length(a)) FROM copy_simd_eoc; +}; + +my ($stdout, $stderr) = ('', ''); +my $ret = $node->psql( + 'postgres', $sql, + stdout => \$stdout, + stderr => \$stderr, + timeout => $PostgreSQL::Test::Utils::timeout_default); + +is($ret, 0, + 'COPY FROM pipe with a buffered end-of-copy marker does not block'); +is($stdout, '1|65524', 'exactly one row of the expected length was copied'); +diag("psql stderr: $stderr") if $ret != 0; + +$node->stop; + +done_testing(); -- 2.50.1 (Apple Git-155)

view thread (178+ messages)  latest in thread

Message-ID: <633c2a60-e5b9-49d2-a93d-946d184d946d@app.fastmail.com>
Permalink:  ../633c2a60-e5b9-49d2-a93d-946d184d946d@app.fastmail.com/
Also on:    postgresql.org/message-id/633c2a60-e5b9-49d2-a93d-946d184d946d@app.fastmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: greg@burd.me, nathandbossart@gmail.com, byavuz81@gmail.com, ma_kazar@esi.dz, neil.conway@gmail.com, andrew@dunslane.net, shinya11.kato@gmail.com
  Subject: Re: Speed up COPY FROM text/CSV parsing using SIMD
  In-Reply-To: <633c2a60-e5b9-49d2-a93d-946d184d946d@app.fastmail.com>
  References: <b26a1cf8-335d-4054-90ad-d94457c45947@app.fastmail.com> <abRKUiInG6Gu728Q@nathan> <apWOr1kgVSqweBnU@nathan> <CAN55FZ1utgaKb1DbW7tr+2=s4Hm1yiCEeNDY9m=qP_M_drafCg@mail.gmail.com> <78e736c8-7f4e-46c2-9fcf-a6839e393d64@app.fastmail.com> <CAN55FZ0UJg+9wcyTCUG656c9DKCNvTGVuuVe4EMMVCHPwGS4aQ@mail.gmail.com> <apcmTvBmA27RoyYL@nathan> <CAN55FZ1qFb4Yo3-MWeQfGQnu1_Ksx4xcFC2m3hyw8a--+eHQYQ@mail.gmail.com> <CAN55FZ3U2waD=5ZuVmju-HtZ4cO4onxHN4jXskk8Y11M6o3NOQ@mail.gmail.com> <9434f7bb-5865-4828-9f23-1dd30e50aaac@app.fastmail.com> <aqArgOhtHfM2_iJe@nathan> <633c2a60-e5b9-49d2-a93d-946d184d946d@app.fastmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox