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

Commit f64f4c3

Browse files
committed
Fix tracking of psql script line numbers during \copy from another place.
Commit 0814677 changed do_copy() to temporarily scribble on pset.cur_cmd_source. That was a mighty ugly bit of code in any case, but in particular it broke handleCopyIn's ability to tell whether it was reading from the current script source file (in which case pset.lineno should be incremented for each line of COPY data), or from someplace else (in which case it shouldn't). The former case still worked, the latter not so much. The visible effect was that line numbers reported for errors in a script file would be wrong if there were an earlier \copy that was reading anything other than inline-in-the-script-file data. To fix, introduce another pset field that holds the file do_copy wants the COPY code to use. This is a little bit ugly, but less so than passing the file down explicitly through several layers that aren't COPY-specific. Extracted from a larger patch by Kumar Rajeev Rastogi; that patch also changes printing of COPY command tags, which is not a bug fix and shouldn't get back-patched. This particular idea was from a suggestion by Amit Khandekar, if I'm reading the thread correctly. Back-patch to 9.2 where the faulty code was introduced.
1 parent 73f0483 commit f64f4c3

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/bin/psql/common.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ StoreQueryTuple(const PGresult *result)
630630
* command. In that event, we'll marshal data for the COPY and then cycle
631631
* through any subsequent PGresult objects.
632632
*
633-
* When the command string contained no affected COPY command, this function
633+
* When the command string contained no such COPY command, this function
634634
* degenerates to an AcceptResult() call.
635635
*
636636
* Changes its argument to point to the last PGresult of the command string,
@@ -690,13 +690,28 @@ ProcessResult(PGresult **results)
690690
* Marshal the COPY data. Either subroutine will get the
691691
* connection out of its COPY state, then call PQresultStatus()
692692
* once and report any error.
693+
*
694+
* If pset.copyStream is set, use that as data source/sink,
695+
* otherwise use queryFout or cur_cmd_source as appropriate.
693696
*/
697+
FILE *copystream = pset.copyStream;
698+
694699
SetCancelConn();
695700
if (result_status == PGRES_COPY_OUT)
696-
success = handleCopyOut(pset.db, pset.queryFout) && success;
701+
{
702+
if (!copystream)
703+
copystream = pset.queryFout;
704+
success = handleCopyOut(pset.db,
705+
copystream) && success;
706+
}
697707
else
698-
success = handleCopyIn(pset.db, pset.cur_cmd_source,
708+
{
709+
if (!copystream)
710+
copystream = pset.cur_cmd_source;
711+
success = handleCopyIn(pset.db,
712+
copystream,
699713
PQbinaryTuples(*results)) && success;
714+
}
700715
ResetCancelConn();
701716

702717
/*

src/bin/psql/copy.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,8 @@ do_copy(const char *args)
271271
{
272272
PQExpBufferData query;
273273
FILE *copystream;
274-
FILE *save_file;
275-
FILE **override_file;
276274
struct copy_options *options;
277275
bool success;
278-
struct stat st;
279276

280277
/* parse options */
281278
options = parse_slash_copy(args);
@@ -289,8 +286,6 @@ do_copy(const char *args)
289286

290287
if (options->from)
291288
{
292-
override_file = &pset.cur_cmd_source;
293-
294289
if (options->file)
295290
{
296291
if (options->program)
@@ -310,8 +305,6 @@ do_copy(const char *args)
310305
}
311306
else
312307
{
313-
override_file = &pset.queryFout;
314-
315308
if (options->file)
316309
{
317310
if (options->program)
@@ -347,6 +340,8 @@ do_copy(const char *args)
347340

348341
if (!options->program)
349342
{
343+
struct stat st;
344+
350345
/* make sure the specified file is not a directory */
351346
fstat(fileno(copystream), &st);
352347
if (S_ISDIR(st.st_mode))
@@ -370,11 +365,10 @@ do_copy(const char *args)
370365
if (options->after_tofrom)
371366
appendPQExpBufferStr(&query, options->after_tofrom);
372367

373-
/* Run it like a user command, interposing the data source or sink. */
374-
save_file = *override_file;
375-
*override_file = copystream;
368+
/* run it like a user command, but with copystream as data source/sink */
369+
pset.copyStream = copystream;
376370
success = SendQuery(query.data);
377-
*override_file = save_file;
371+
pset.copyStream = NULL;
378372
termPQExpBuffer(&query);
379373

380374
if (options->file != NULL)

src/bin/psql/settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ typedef struct _psqlSettings
7070
FILE *queryFout; /* where to send the query results */
7171
bool queryFoutPipe; /* queryFout is from a popen() */
7272

73+
FILE *copyStream; /* Stream to read/write for \copy command */
74+
7375
printQueryOpt popt;
7476

7577
char *gfname; /* one-shot file output argument for \g */

src/bin/psql/startup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ main(int argc, char *argv[])
120120
pset.encoding = PQenv2encoding();
121121
pset.queryFout = stdout;
122122
pset.queryFoutPipe = false;
123+
pset.copyStream = NULL;
123124
pset.cur_cmd_source = stdin;
124125
pset.cur_cmd_interactive = false;
125126

0 commit comments

Comments
 (0)