*** a/src/bin/psql/common.c --- b/src/bin/psql/common.c *************** *** 689,699 **** ProcessResult(PGresult **results) * connection out of its COPY state, then call PQresultStatus() * once and report any error. */ SetCancelConn(); if (result_status == PGRES_COPY_OUT) ! success = handleCopyOut(pset.db, pset.queryFout) && success; else ! success = handleCopyIn(pset.db, pset.cur_cmd_source, PQbinaryTuples(*results)) && success; ResetCancelConn(); --- 689,706 ---- * connection out of its COPY state, then call PQresultStatus() * once and report any error. */ + + /* + * If this is second copy; then it will be definately not \copy, + * and also it can not be from any user given file. So pass the + * value of copystream as NULL, so that read/wrie happens on + * already set cur_cmd_source/queryFout. + */ SetCancelConn(); if (result_status == PGRES_COPY_OUT) ! success = handleCopyOut(pset.db, (first_cycle ? pset.copyStream : NULL)) && success; else ! success = handleCopyIn(pset.db, (first_cycle ? pset.copyStream : NULL), PQbinaryTuples(*results)) && success; ResetCancelConn(); *** a/src/bin/psql/copy.c --- b/src/bin/psql/copy.c *************** *** 269,276 **** do_copy(const char *args) { PQExpBufferData query; FILE *copystream; - FILE *save_file; - FILE **override_file; struct copy_options *options; bool success; struct stat st; --- 269,274 ---- *************** *** 287,294 **** do_copy(const char *args) if (options->from) { - override_file = &pset.cur_cmd_source; - if (options->file) { if (options->program) --- 285,290 ---- *************** *** 308,315 **** do_copy(const char *args) } else { - override_file = &pset.queryFout; - if (options->file) { if (options->program) --- 304,309 ---- *************** *** 369,378 **** do_copy(const char *args) appendPQExpBufferStr(&query, options->after_tofrom); /* Run it like a user command, interposing the data source or sink. */ ! save_file = *override_file; ! *override_file = copystream; success = SendQuery(query.data); ! *override_file = save_file; termPQExpBuffer(&query); if (options->file != NULL) --- 363,371 ---- appendPQExpBufferStr(&query, options->after_tofrom); /* Run it like a user command, interposing the data source or sink. */ ! pset.copyStream = copystream; success = SendQuery(query.data); ! pset.copyStream = NULL; termPQExpBuffer(&query); if (options->file != NULL) *************** *** 440,445 **** handleCopyOut(PGconn *conn, FILE *copystream) --- 433,441 ---- int ret; PGresult *res; + if (!copystream) + copystream = pset.queryFout; + for (;;) { ret = PQgetCopyData(conn, &buf, 0); *************** *** 530,535 **** handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary) --- 526,534 ---- char buf[COPYBUFSIZ]; PGresult *res; + if (!copystream) + copystream = pset.cur_cmd_source; + /* * Establish longjmp destination for exiting from wait-for-input. (This is * only effective while sigint_interrupt_enabled is TRUE.) *************** *** 656,663 **** handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary) } } ! if (copystream == pset.cur_cmd_source) ! pset.lineno++; } } --- 655,661 ---- } } ! pset.lineno++; } } *** a/src/bin/psql/settings.h --- b/src/bin/psql/settings.h *************** *** 70,75 **** typedef struct _psqlSettings --- 70,77 ---- FILE *queryFout; /* where to send the query results */ bool queryFoutPipe; /* queryFout is from a popen() */ + FILE *copyStream; /* Stream to read/write for copy command */ + printQueryOpt popt; char *gfname; /* one-shot file output argument for \g */