Fix psql's \copy command to ensure that it cycles libpq back to the idle state
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 24 Nov 2006 23:07:07 +0000 (23:07 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 24 Nov 2006 23:07:07 +0000 (23:07 +0000)
(in particular, causing the ReadyForQuery message to be eaten) before
returning from do_copy.  The only known consequence of failing to do so is
that get_prompt might show a wrong result for the %x transaction status
escape, as reported by Bernd Helmle; but it's possible there are other issues.

Back-patch as far as 7.4, the oldest version supporting %x.

src/bin/psql/copy.c

index 132926131d0d653580e3f329baf9874ef45ed357..5cbc8d11a765a52960c1382b60c327ccbda64501 100644 (file)
@@ -416,6 +416,18 @@ do_copy(const char *args)
 
        PQclear(result);
 
+       /*
+        * Make sure we have pumped libpq dry of results; else it may still be
+        * in ASYNC_BUSY state, leading to false readings in, eg, get_prompt().
+        */
+       while ((result = PQgetResult(pset.db)) != NULL)
+       {
+               success = false;
+               psql_error("\\copy: unexpected response (%d)\n",
+                                  PQresultStatus(result));
+               PQclear(result);
+       }
+
        if (copystream != stdout && copystream != stdin)
                fclose(copystream);
        free_copy_options(options);