*** a/doc/src/sgml/ref/copy.sgml --- b/doc/src/sgml/ref/copy.sgml *************** *** 369,374 **** COPY count --- 369,382 ---- The count is the number of rows copied. + + + + In case of COPY ... TO STDOUT, + command tag is not displayed. + + + *** a/doc/src/sgml/ref/psql-ref.sgml --- b/doc/src/sgml/ref/psql-ref.sgml *************** *** 904,909 **** testdb=> --- 904,925 ---- + + + On successful completion, a COPY command returns a command + tag of the form + + COPY count + + The count is the number + of rows copied. + + + But in case of \COPY ... TO ... , + command tag is not displayed. + + + *** a/src/bin/psql/common.c --- b/src/bin/psql/common.c *************** *** 631,638 **** StoreQueryTuple(const PGresult *result) * When the command string contained no such COPY command, this function * degenerates to an AcceptResult() call. * ! * Changes its argument to point to the last PGresult of the command string, ! * or NULL if that result was for a COPY FROM STDIN or COPY TO STDOUT. * * Returns true on complete success, false otherwise. Possible failure modes * include purely client-side problems; check the transaction status for the --- 631,637 ---- * When the command string contained no such COPY command, this function * degenerates to an AcceptResult() call. * ! * Changes its argument to point to the last PGresult of the command string. * * Returns true on complete success, false otherwise. Possible failure modes * include purely client-side problems; check the transaction status for the *************** *** 693,737 **** ProcessResult(PGresult **results) * otherwise use queryFout or cur_cmd_source as appropriate. */ FILE *copystream = pset.copyStream; SetCancelConn(); if (result_status == PGRES_COPY_OUT) { ! if (!copystream) copystream = pset.queryFout; success = handleCopyOut(pset.db, ! copystream) && success; } else { ! if (!copystream) copystream = pset.cur_cmd_source; success = handleCopyIn(pset.db, copystream, ! PQbinaryTuples(*results)) && success; } ResetCancelConn(); /* ! * Call PQgetResult() once more. In the typical case of a ! * single-command string, it will return NULL. Otherwise, we'll ! * have other results to process that may include other COPYs. */ ! PQclear(*results); ! *results = next_result = PQgetResult(pset.db); } else if (first_cycle) /* fast path: no COPY commands; PQexec visited all results */ break; ! else if ((next_result = PQgetResult(pset.db))) { /* non-COPY command(s) after a COPY: keep the last one */ PQclear(*results); *results = next_result; } first_cycle = false; ! } while (next_result); /* may need this to recover from conn loss during COPY */ if (!first_cycle && !CheckConnection()) --- 692,764 ---- * otherwise use queryFout or cur_cmd_source as appropriate. */ FILE *copystream = pset.copyStream; + PGresult *copy_result; SetCancelConn(); if (result_status == PGRES_COPY_OUT) { ! /* ! * If this is second cycle, then it will definitely not be ! * \copy, so pset.copyStream should not be used. ! */ ! if (!pset.copyStream || !first_cycle) copystream = pset.queryFout; + success = handleCopyOut(pset.db, ! copystream, ! ©_result) && success; } else { ! if (!pset.copyStream || !first_cycle) copystream = pset.cur_cmd_source; + success = handleCopyIn(pset.db, copystream, ! PQbinaryTuples(*results), ! ©_result) && success; } ResetCancelConn(); + /* replace the COPY_OUT/IN result with COPY command exit status */ + PQclear(*results); + *results = copy_result; + /* ! * Check PQgetResult() again. In the typical case of a single-command ! * string, it will return NULL. Otherwise, we'll have other results ! * to process that may include other COPYs. ! * If there is other result to process then to print status or not ! * will be decided on next command handling otherwise decide here ! * only. */ ! if ((next_result = PQgetResult(pset.db))) ! { ! PQclear(*results); ! *results = next_result; ! } ! else if (result_status == PGRES_COPY_OUT) ! { ! /* If last command is COPY TO then no need to print status. */ ! PQclear(*results); ! *results = NULL; ! } } else if (first_cycle) + { /* fast path: no COPY commands; PQexec visited all results */ break; ! } ! else if (next_result = PQgetResult(pset.db)) { /* non-COPY command(s) after a COPY: keep the last one */ + /* Printing of status for any other command will be always done*/ PQclear(*results); *results = next_result; } first_cycle = false; ! }while(next_result); /* may need this to recover from conn loss during COPY */ if (!first_cycle && !CheckConnection()) *** a/src/bin/psql/copy.c --- b/src/bin/psql/copy.c *************** *** 429,444 **** 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. * * result is true if successful, false if not. */ bool ! handleCopyOut(PGconn *conn, FILE *copystream) { bool OK = true; char *buf; int ret; - PGresult *res; for (;;) { --- 429,445 ---- * 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. + * The final status for the COPY is returned into *res (but note + * we already reported the error, if it's not a success result). * * result is true if successful, false if not. */ bool ! handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res) { bool OK = true; char *buf; int ret; for (;;) { *************** *** 485,497 **** handleCopyOut(PGconn *conn, FILE *copystream) * but hasn't exited COPY_OUT state internally. So we ignore the * possibility here. */ ! res = PQgetResult(conn); ! if (PQresultStatus(res) != PGRES_COMMAND_OK) { psql_error("%s", PQerrorMessage(conn)); OK = false; } - PQclear(res); return OK; } --- 486,497 ---- * but hasn't exited COPY_OUT state internally. So we ignore the * possibility here. */ ! *res = PQgetResult(conn); ! if (PQresultStatus(*res) != PGRES_COMMAND_OK) { psql_error("%s", PQerrorMessage(conn)); OK = false; } return OK; } *************** *** 504,509 **** handleCopyOut(PGconn *conn, FILE *copystream) --- 504,511 ---- * and got back a PGRES_COPY_IN result. * copystream is the file stream to read the data from. * isbinary can be set from PQbinaryTuples(). + * The final status for the COPY is returned into *res (but note + * we already reported the error, if it's not a success result). * * result is true if successful, false if not. */ *************** *** 512,523 **** handleCopyOut(PGconn *conn, FILE *copystream) #define COPYBUFSIZ 8192 bool ! handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary) { bool OK; const char *prompt; char buf[COPYBUFSIZ]; - PGresult *res; /* * Establish longjmp destination for exiting from wait-for-input. (This is --- 514,524 ---- #define COPYBUFSIZ 8192 bool ! handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res) { bool OK; const char *prompt; char buf[COPYBUFSIZ]; /* * Establish longjmp destination for exiting from wait-for-input. (This is *************** *** 679,699 **** copyin_cleanup: * connection is lost. But that's fine; it will get us out of COPY_IN * state, which is what we need.) */ ! while (res = PQgetResult(conn), PQresultStatus(res) == PGRES_COPY_IN) { OK = false; ! PQclear(res); /* We can't send an error message if we're using protocol version 2 */ PQputCopyEnd(conn, (PQprotocolVersion(conn) < 3) ? NULL : _("trying to exit copy mode")); } ! if (PQresultStatus(res) != PGRES_COMMAND_OK) { psql_error("%s", PQerrorMessage(conn)); OK = false; } - PQclear(res); return OK; } --- 680,699 ---- * connection is lost. But that's fine; it will get us out of COPY_IN * state, which is what we need.) */ ! while (*res = PQgetResult(conn), PQresultStatus(*res) == PGRES_COPY_IN) { OK = false; ! PQclear(*res); /* We can't send an error message if we're using protocol version 2 */ PQputCopyEnd(conn, (PQprotocolVersion(conn) < 3) ? NULL : _("trying to exit copy mode")); } ! if (PQresultStatus(*res) != PGRES_COMMAND_OK) { psql_error("%s", PQerrorMessage(conn)); OK = false; } return OK; } *** a/src/bin/psql/copy.h --- b/src/bin/psql/copy.h *************** *** 12,22 **** /* handler for \copy */ ! bool do_copy(const char *args); /* lower level processors for copy in/out streams */ ! bool handleCopyOut(PGconn *conn, FILE *copystream); ! bool handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary); #endif --- 12,24 ---- /* handler for \copy */ ! extern bool do_copy(const char *args); /* lower level processors for copy in/out streams */ ! extern bool handleCopyOut(PGconn *conn, FILE *copystream, ! PGresult **res); ! extern bool handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, ! PGresult **res); #endif