postgres.git / summary / log / commit / refs
commit d1895a11c8f1b19b3fe0db45cc10a9d26c56bc8c
Author: Nathan Bossart <nathan@postgresql.org>
Date: Wed Sep 09 21:02:24 2026 +0000
First released in: 19.0
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 testing.
Oversight in commit e0a3a3fd53.
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Reviewed-by: Greg Burd <greg@burd.me>
Discussion: https://postgr.es/m/CAN55FZ1qFb4Yo3-MWeQfGQnu1_Ksx4xcFC2m3hyw8a--%2BeHQYQ%40mail.gmail.com
Backpatch-through: 19
src/backend/commands/copyfromparse.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c
index 98bf30ef2e7..06450535779 100644
--- a/src/backend/commands/copyfromparse.c
+++ b/src/backend/commands/copyfromparse.c
@@ -654,14 +654,28 @@ CopyLoadRawBuf(CopyFromState cstate)
*
* 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.
+ * It also won't read past a backslash in text mode, since that might begin an
+ * end-of-copy marker (in which case there's no point in waiting for more data,
+ * which might not materialize anyway). 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);
+ /*
+ * If "speculative" is true and we're in text mode, refuse to wait for
+ * more input if there's a backslash in the buffer that the caller still
+ * needs to process. That might be the start of an end-of-copy marker. If
+ * it _is_ an end-of-copy marker, we don't need any more data, and more
+ * data might not show up, anyway (e.g., from a pipe that was left open).
+ */
+ 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