Skip to content

Commit 8e97a97

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 e8ec19c commit 8e97a97

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
@@ -1021,10 +1021,24 @@ testdb=>
10211021

10221022
<tip>
10231023
<para>
1024-
This operation is not as efficient as the <acronym>SQL</acronym>
1025-
<command>COPY</command> command because all data must pass
1026-
through the client/server connection. For large
1027-
amounts of data the <acronym>SQL</acronym> command might be preferable.
1024+
Another way to obtain the same result as <literal>\copy
1025+
... to</literal> is to use the <acronym>SQL</acronym> <literal>COPY
1026+
... TO STDOUT</literal> command and terminate it
1027+
with <literal>\g <replaceable>filename</replaceable></literal>
1028+
or <literal>\g |<replaceable>program</replaceable></literal>.
1029+
Unlike <literal>\copy</literal>, this method allows the command to
1030+
span multiple lines; also, variable interpolation and backquote
1031+
expansion can be used.
1032+
</para>
1033+
</tip>
1034+
1035+
<tip>
1036+
<para>
1037+
These operations are not as efficient as the <acronym>SQL</acronym>
1038+
<command>COPY</command> command with a file or program data source or
1039+
destination, because all data must pass through the client/server
1040+
connection. For large amounts of data the <acronym>SQL</acronym>
1041+
command might be preferable.
10281042
</para>
10291043
</tip>
10301044

src/bin/psql/common.c

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,20 +1044,49 @@ ProcessResult(PGresult **results)
10441044
* connection out of its COPY state, then call PQresultStatus()
10451045
* once and report any error.
10461046
*
1047-
* If pset.copyStream is set, use that as data source/sink,
1048-
* otherwise use queryFout or cur_cmd_source as appropriate.
1047+
* For COPY OUT, direct the output to pset.copyStream if it's set,
1048+
* otherwise to pset.gfname if it's set, otherwise to queryFout.
1049+
* For COPY IN, use pset.copyStream as data source if it's set,
1050+
* otherwise cur_cmd_source.
10491051
*/
1050-
FILE *copystream = pset.copyStream;
1052+
FILE *copystream;
10511053
PGresult *copy_result;
10521054

10531055
SetCancelConn();
10541056
if (result_status == PGRES_COPY_OUT)
10551057
{
1056-
if (!copystream)
1058+
bool need_close = false;
1059+
bool is_pipe = false;
1060+
1061+
if (pset.copyStream)
1062+
{
1063+
/* invoked by \copy */
1064+
copystream = pset.copyStream;
1065+
}
1066+
else if (pset.gfname)
1067+
{
1068+
/* invoked by \g */
1069+
if (openQueryOutputFile(pset.gfname,
1070+
&copystream, &is_pipe))
1071+
{
1072+
need_close = true;
1073+
if (is_pipe)
1074+
disable_sigpipe_trap();
1075+
}
1076+
else
1077+
copystream = NULL; /* discard COPY data entirely */
1078+
}
1079+
else
1080+
{
1081+
/* fall back to the generic query output stream */
10571082
copystream = pset.queryFout;
1083+
}
1084+
10581085
success = handleCopyOut(pset.db,
10591086
copystream,
1060-
&copy_result) && success;
1087+
&copy_result)
1088+
&& success
1089+
&& (copystream != NULL);
10611090

10621091
/*
10631092
* Suppress status printing if the report would go to the same
@@ -1069,11 +1098,25 @@ ProcessResult(PGresult **results)
10691098
PQclear(copy_result);
10701099
copy_result = NULL;
10711100
}
1101+
1102+
if (need_close)
1103+
{
1104+
/* close \g argument file/pipe */
1105+
if (is_pipe)
1106+
{
1107+
pclose(copystream);
1108+
restore_sigpipe_trap();
1109+
}
1110+
else
1111+
{
1112+
fclose(copystream);
1113+
}
1114+
}
10721115
}
10731116
else
10741117
{
1075-
if (!copystream)
1076-
copystream = pset.cur_cmd_source;
1118+
/* COPY IN */
1119+
copystream = pset.copyStream ? pset.copyStream : pset.cur_cmd_source;
10771120
success = handleCopyIn(pset.db,
10781121
copystream,
10791122
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)