diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2019-01-26 14:15:42 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2019-01-26 14:15:42 -0500 |
commit | 2c50c9f23d1dbb36b4fed50409ec590a0e9adaa2 (patch) | |
tree | 5e23fe131bee17bb1338195770c8c6fca4dfd1e7 /src/bin/psql/copy.c | |
parent | c0aed6959541f1ce3930977c8cf8dd874308a1b5 (diff) | |
download | postgresql-2c50c9f23d1dbb36b4fed50409ec590a0e9adaa2.tar.gz postgresql-2c50c9f23d1dbb36b4fed50409ec590a0e9adaa2.zip |
Fix psql's "\g target" meta-command to work with COPY TO STDOUT.
Previously, \g would successfully execute the COPY command, but
the target specification if any was ignored, so that the data was
always dumped to the regular query output target. This seems like
a clear bug, so let's not just fix it but back-patch it.
While at it, adjust the documentation for \copy to recommend
"COPY ... TO STDOUT \g foo" as a plausible alternative.
Back-patch to 9.5. The problem exists much further back, but the
code associated with \g was refactored enough in 9.5 that we'd
need a significantly different patch for 9.4, and it doesn't
seem worth the trouble.
Daniel Vérité, reviewed by Fabien Coelho
Discussion: https://postgr.es/m/15dadc39-e050-4d46-956b-dcc4ed098753@manitou-mail.org
Diffstat (limited to 'src/bin/psql/copy.c')
-rw-r--r-- | src/bin/psql/copy.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 555c6331a36..20a744509b6 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -425,7 +425,10 @@ do_copy(const char *args) * * conn should be a database connection that you just issued COPY TO on * and got back a PGRES_COPY_OUT result. + * * copystream is the file stream for the data to go to. + * copystream can be NULL to eat the data without writing it anywhere. + * * The final status for the COPY is returned into *res (but note * we already reported the error, if it's not a success result). * @@ -447,7 +450,7 @@ handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res) if (buf) { - if (OK && fwrite(buf, 1, ret, copystream) != ret) + if (OK && copystream && fwrite(buf, 1, ret, copystream) != ret) { psql_error("could not write COPY data: %s\n", strerror(errno)); @@ -458,7 +461,7 @@ handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res) } } - if (OK && fflush(copystream)) + if (OK && copystream && fflush(copystream)) { psql_error("could not write COPY data: %s\n", strerror(errno)); |