*** a/doc/src/sgml/ref/copy.sgml
--- b/doc/src/sgml/ref/copy.sgml
***************
*** 369,374 **** COPY <replaceable class="parameter">count</replaceable>
--- 369,382 ----
  </screen>
     The <replaceable class="parameter">count</replaceable> is the number
     of rows copied.
+    
+     <note>
+     <para>
+     In case of <literal>COPY ... TO STDOUT</literal>, 
+     command tag is not displayed.  	
+     </para>
+     </note>
+     
    </para>
   </refsect1>
  
*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 904,909 **** testdb=&gt;
--- 904,925 ----
          </para>
          </tip>
  
+         <note>
+         <para>
+         On successful completion, a <command>COPY</> command returns a command
+         tag of the form
+         <screen>
+             COPY <replaceable class="parameter">count</replaceable>
+         </screen>
+         The <replaceable class="parameter">count</replaceable> is the number
+         of rows copied.
+         </para>
+         <para>
+         But in case of <literal>\COPY ... TO ... </literal>, 
+         command tag is not displayed.  	
+         </para>
+         </note>
+ 
          </listitem>
        </varlistentry>
  
*** 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,
! 										&copy_result) && success;
  			}
  			else
  			{
! 				if (!pset.copyStream || !first_cycle)
  					copystream = pset.cur_cmd_source;
+ 
  				success = handleCopyIn(pset.db,
  									   copystream,
! 									   PQbinaryTuples(*results),
! 									   &copy_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
