Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ae4c7d5

Browse files
committed
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
1 parent 0d5b273 commit ae4c7d5

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,10 +984,24 @@ testdb=>
984984

985985
<tip>
986986
<para>
987-
This operation is not as efficient as the <acronym>SQL</acronym>
988-
<command>COPY</command> command because all data must pass
989-
through the client/server connection. For large
990-
amounts of data the <acronym>SQL</acronym> command might be preferable.
987+
Another way to obtain the same result as <literal>\copy
988+
... to</literal> is to use the <acronym>SQL</acronym> <literal>COPY
989+
... TO STDOUT</literal> command and terminate it
990+
with <literal>\g <replaceable>filename</replaceable></literal>
991+
or <literal>\g |<replaceable>program</replaceable></literal>.
992+
Unlike <literal>\copy</literal>, this method allows the command to
993+
span multiple lines; also, variable interpolation and backquote
994+
expansion can be used.
995+
</para>
996+
</tip>
997+
998+
<tip>
999+
<para>
1000+
These operations are not as efficient as the <acronym>SQL</acronym>
1001+
<command>COPY</command> command with a file or program data source or
1002+
destination, because all data must pass through the client/server
1003+
connection. For large amounts of data the <acronym>SQL</acronym>
1004+
command might be preferable.
9911005
</para>
9921006
</tip>
9931007

src/bin/psql/common.c

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,20 +946,49 @@ ProcessResult(PGresult **results)
946946
* connection out of its COPY state, then call PQresultStatus()
947947
* once and report any error.
948948
*
949-
* If pset.copyStream is set, use that as data source/sink,
950-
* otherwise use queryFout or cur_cmd_source as appropriate.
949+
* For COPY OUT, direct the output to pset.copyStream if it's set,
950+
* otherwise to pset.gfname if it's set, otherwise to queryFout.
951+
* For COPY IN, use pset.copyStream as data source if it's set,
952+
* otherwise cur_cmd_source.
951953
*/
952-
FILE *copystream = pset.copyStream;
954+
FILE *copystream;
953955
PGresult *copy_result;
954956

955957
SetCancelConn();
956958
if (result_status == PGRES_COPY_OUT)
957959
{
958-
if (!copystream)
960+
bool need_close = false;
961+
bool is_pipe = false;
962+
963+
if (pset.copyStream)
964+
{
965+
/* invoked by \copy */
966+
copystream = pset.copyStream;
967+
}
968+
else if (pset.gfname)
969+
{
970+
/* invoked by \g */
971+
if (openQueryOutputFile(pset.gfname,
972+
&copystream, &is_pipe))
973+
{
974+
need_close = true;
975+
if (is_pipe)
976+
disable_sigpipe_trap();
977+
}
978+
else
979+
copystream = NULL; /* discard COPY data entirely */
980+
}
981+
else
982+
{
983+
/* fall back to the generic query output stream */
959984
copystream = pset.queryFout;
985+
}
986+
960987
success = handleCopyOut(pset.db,
961988
copystream,
962-
&copy_result) && success;
989+
&copy_result)
990+
&& success
991+
&& (copystream != NULL);
963992

964993
/*
965994
* Suppress status printing if the report would go to the same
@@ -971,11 +1000,25 @@ ProcessResult(PGresult **results)
9711000
PQclear(copy_result);
9721001
copy_result = NULL;
9731002
}
1003+
1004+
if (need_close)
1005+
{
1006+
/* close \g argument file/pipe */
1007+
if (is_pipe)
1008+
{
1009+
pclose(copystream);
1010+
restore_sigpipe_trap();
1011+
}
1012+
else
1013+
{
1014+
fclose(copystream);
1015+
}
1016+
}
9741017
}
9751018
else
9761019
{
977-
if (!copystream)
978-
copystream = pset.cur_cmd_source;
1020+
/* COPY IN */
1021+
copystream = pset.copyStream ? pset.copyStream : pset.cur_cmd_source;
9791022
success = handleCopyIn(pset.db,
9801023
copystream,
9811024
PQbinaryTuples(*results),

src/bin/psql/copy.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,10 @@ do_copy(const char *args)
425425
*
426426
* conn should be a database connection that you just issued COPY TO on
427427
* and got back a PGRES_COPY_OUT result.
428+
*
428429
* copystream is the file stream for the data to go to.
430+
* copystream can be NULL to eat the data without writing it anywhere.
431+
*
429432
* The final status for the COPY is returned into *res (but note
430433
* we already reported the error, if it's not a success result).
431434
*
@@ -447,7 +450,7 @@ handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res)
447450

448451
if (buf)
449452
{
450-
if (OK && fwrite(buf, 1, ret, copystream) != ret)
453+
if (OK && copystream && fwrite(buf, 1, ret, copystream) != ret)
451454
{
452455
psql_error("could not write COPY data: %s\n",
453456
strerror(errno));
@@ -458,7 +461,7 @@ handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res)
458461
}
459462
}
460463

461-
if (OK && fflush(copystream))
464+
if (OK && copystream && fflush(copystream))
462465
{
463466
psql_error("could not write COPY data: %s\n",
464467
strerror(errno));

0 commit comments

Comments
 (0)