aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/command.c
diff options
context:
space:
mode:
authorFujii Masao <fujii@postgresql.org>2014-09-04 12:31:48 +0900
committerFujii Masao <fujii@postgresql.org>2014-09-04 12:31:48 +0900
commitf6f654ff12c527310ddbeaf53d463d22ab46ee2c (patch)
tree438737ad356ce13ccfff87ea128447a8ae796824 /src/bin/psql/command.c
parent4b91ade41fcda805a50e136edad7f95d8f35a7a9 (diff)
downloadpostgresql-f6f654ff12c527310ddbeaf53d463d22ab46ee2c.tar.gz
postgresql-f6f654ff12c527310ddbeaf53d463d22ab46ee2c.zip
Allow \watch to display query execution time if \timing is enabled.
Previously \watch could not display the query execution time even when \timing was enabled because it used PSQLexec instead of SendQuery and that function didn't support \timing. This patch introduces PSQLexecWatch and changes \watch so as to use it, instead. PSQLexecWatch is the function to run the query, print its results and display how long it took (only when \timing is enabled). This patch also changes --echo-hidden so that it doesn't print the query that \watch executes. Since \watch cannot execute backslash command queries, they should not be printed even when --echo-hidden is set. Patch by me, review by Heikki Linnakangas and Michael Paquier
Diffstat (limited to 'src/bin/psql/command.c')
-rw-r--r--src/bin/psql/command.c63
1 files changed, 10 insertions, 53 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index e16b4d54c24..a66093abb3a 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2687,7 +2687,7 @@ do_watch(PQExpBuffer query_buf, long sleep)
for (;;)
{
- PGresult *res;
+ int res;
time_t timer;
long i;
@@ -2700,65 +2700,22 @@ do_watch(PQExpBuffer query_buf, long sleep)
sleep, asctime(localtime(&timer)));
myopt.title = title;
- /*
- * Run the query. We use PSQLexec, which is kind of cheating, but
- * SendQuery doesn't let us suppress autocommit behavior.
- */
- res = PSQLexec(query_buf->data, false);
-
- /* PSQLexec handles failure results and returns NULL */
- if (res == NULL)
- break;
+ /* Run the query and print out the results */
+ res = PSQLexecWatch(query_buf->data, &myopt);
/*
- * If SIGINT is sent while the query is processing, PSQLexec will
- * consume the interrupt. The user's intention, though, is to cancel
- * the entire watch process, so detect a sent cancellation request and
- * exit in this case.
+ * PSQLexecWatch handles the case where we can no longer
+ * repeat the query, and returns 0 or -1.
*/
- if (cancel_pressed)
- {
- PQclear(res);
+ if (res == 0)
break;
- }
-
- switch (PQresultStatus(res))
- {
- case PGRES_TUPLES_OK:
- printQuery(res, &myopt, pset.queryFout, pset.logfile);
- break;
-
- case PGRES_COMMAND_OK:
- fprintf(pset.queryFout, "%s\n%s\n\n", title, PQcmdStatus(res));
- break;
-
- case PGRES_EMPTY_QUERY:
- psql_error(_("\\watch cannot be used with an empty query\n"));
- PQclear(res);
- return false;
-
- case PGRES_COPY_OUT:
- case PGRES_COPY_IN:
- case PGRES_COPY_BOTH:
- psql_error(_("\\watch cannot be used with COPY\n"));
- PQclear(res);
- return false;
-
- default:
- /* other cases should have been handled by PSQLexec */
- psql_error(_("unexpected result status for \\watch\n"));
- PQclear(res);
- return false;
- }
-
- PQclear(res);
-
- fflush(pset.queryFout);
+ if (res == -1)
+ return false;
/*
* Set up cancellation of 'watch' via SIGINT. We redo this each time
- * through the loop since it's conceivable something inside PSQLexec
- * could change sigint_interrupt_jmp.
+ * through the loop since it's conceivable something inside
+ * PSQLexecWatch could change sigint_interrupt_jmp.
*/
if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
break;