Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9485516

Browse files
committed
Make psql handle EOF during COPY FROM STDIN properly on all platforms.
When stdin is a terminal, it's possible to end a COPY FROM STDIN witha keyboard EOF signal (typically control-D), and then keep on issuingSQL commands. One would expect another COPY FROM STDIN to work as well,but on some platforms it did not. This turns out to be because we werenot resetting the stream's feof() flag, and BSD-ish versions of fread()and fgets() won't attempt to read more data if that's set.The misbehavior is observed on BSDen (including macOS), but not Linux,Windows, or SysV-ish Unixen, which makes this a portability bug notjust a missing feature.Add a clearerr() call to fix the behavior, and improve the prompt that'sissued when copying from a TTY to mention that EOF signals work.It's been like this forever, so back-patch to all supported branches.Thomas MunroDiscussion:https://postgr.es/m/CAEepm=0MCGfYf=JAMiYhO6JPtv9-3ZfBo8fcGeCZ8oMzaw+Z+Q@mail.gmail.com
1 parent944dc0f commit9485516

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

‎src/bin/psql/copy.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res)
540540
showprompt= true;
541541
if (!pset.quiet)
542542
puts(_("Enter data to be copied followed by a newline.\n"
543-
"End with a backslash and a period on a line by itself."));
543+
"End with a backslash and a period on a line by itself, or an EOF signal."));
544544
}
545545
else
546546
showprompt= false;
@@ -673,6 +673,16 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res)
673673

674674
copyin_cleanup:
675675

676+
/*
677+
* Clear the EOF flag on the stream, in case copying ended due to an EOF
678+
* signal. This allows an interactive TTY session to perform another COPY
679+
* FROM STDIN later. (In non-STDIN cases, we're about to close the file
680+
* anyway, so it doesn't matter.) Although we don't ever test the flag
681+
* with feof(), some fread() implementations won't read more data if it's
682+
* set. This also clears the error flag, but we already checked that.
683+
*/
684+
clearerr(copystream);
685+
676686
/*
677687
* Check command status and return to normal libpq state.
678688
*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp