|
3 | 3 | * |
4 | 4 | * Copyright (c) 2000-2010, PostgreSQL Global Development Group |
5 | 5 | * |
6 | | - * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.144 2010/02/16 22:34:50 tgl Exp $ |
| 6 | + * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.145 2010/05/28 20:02:32 tgl Exp $ |
7 | 7 | */ |
8 | 8 | #include"postgres_fe.h" |
9 | 9 | #include"common.h" |
@@ -982,6 +982,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) |
982 | 982 | charfetch_cmd[64]; |
983 | 983 | instr_timebefore, |
984 | 984 | after; |
| 985 | +intflush_error; |
985 | 986 |
|
986 | 987 | *elapsed_msec=0; |
987 | 988 |
|
@@ -1045,6 +1046,9 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) |
1045 | 1046 | } |
1046 | 1047 | } |
1047 | 1048 |
|
| 1049 | +/* clear any pre-existing error indication on the output stream */ |
| 1050 | +clearerr(pset.queryFout); |
| 1051 | + |
1048 | 1052 | for (;;) |
1049 | 1053 | { |
1050 | 1054 | if (pset.timing) |
@@ -1096,19 +1100,29 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) |
1096 | 1100 |
|
1097 | 1101 | printQuery(results,&my_popt,pset.queryFout,pset.logfile); |
1098 | 1102 |
|
1099 | | -/* |
1100 | | - * Make sure to flush the output stream, so intermediate results are |
1101 | | - * visible to the client immediately. |
1102 | | - */ |
1103 | | -fflush(pset.queryFout); |
| 1103 | +PQclear(results); |
1104 | 1104 |
|
1105 | 1105 | /* after the first result set, disallow header decoration */ |
1106 | 1106 | my_popt.topt.start_table= false; |
1107 | 1107 | my_popt.topt.prior_records+=ntuples; |
1108 | 1108 |
|
1109 | | -PQclear(results); |
| 1109 | +/* |
| 1110 | + * Make sure to flush the output stream, so intermediate results are |
| 1111 | + * visible to the client immediately. We check the results because |
| 1112 | + * if the pager dies/exits/etc, there's no sense throwing more data |
| 1113 | + * at it. |
| 1114 | + */ |
| 1115 | +flush_error=fflush(pset.queryFout); |
1110 | 1116 |
|
1111 | | -if (ntuples<pset.fetch_count||cancel_pressed) |
| 1117 | +/* |
| 1118 | + * Check if we are at the end, if a cancel was pressed, or if |
| 1119 | + * there were any errors either trying to flush out the results, |
| 1120 | + * or more generally on the output stream at all. If we hit any |
| 1121 | + * errors writing things to the stream, we presume $PAGER has |
| 1122 | + * disappeared and stop bothering to pull down more data. |
| 1123 | + */ |
| 1124 | +if (ntuples<pset.fetch_count||cancel_pressed||flush_error|| |
| 1125 | +ferror(pset.queryFout)) |
1112 | 1126 | break; |
1113 | 1127 | } |
1114 | 1128 |
|
|