Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6f654f

Browse files
committed
Allow \watch to display query execution time if \timing is enabled.
Previously \watch could not display the query execution time evenwhen \timing was enabled because it used PSQLexec instead ofSendQuery and that function didn't support \timing. This patchintroduces PSQLexecWatch and changes \watch so as to use it, instead.PSQLexecWatch is the function to run the query, print its results anddisplay how long it took (only when \timing is enabled).This patch also changes --echo-hidden so that it doesn't printthe query that \watch executes. Since \watch cannot executebackslash command queries, they should not be printed evenwhen --echo-hidden is set.Patch by me, review by Heikki Linnakangas and Michael Paquier
1 parent4b91ade commitf6f654f

File tree

3 files changed

+109
-53
lines changed

3 files changed

+109
-53
lines changed

‎src/bin/psql/command.c

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,7 +2687,7 @@ do_watch(PQExpBuffer query_buf, long sleep)
26872687

26882688
for (;;)
26892689
{
2690-
PGresult*res;
2690+
intres;
26912691
time_ttimer;
26922692
longi;
26932693

@@ -2700,65 +2700,22 @@ do_watch(PQExpBuffer query_buf, long sleep)
27002700
sleep,asctime(localtime(&timer)));
27012701
myopt.title=title;
27022702

2703-
/*
2704-
* Run the query. We use PSQLexec, which is kind of cheating, but
2705-
* SendQuery doesn't let us suppress autocommit behavior.
2706-
*/
2707-
res=PSQLexec(query_buf->data, false);
2708-
2709-
/* PSQLexec handles failure results and returns NULL */
2710-
if (res==NULL)
2711-
break;
2703+
/* Run the query and print out the results */
2704+
res=PSQLexecWatch(query_buf->data,&myopt);
27122705

27132706
/*
2714-
* If SIGINT is sent while the query is processing, PSQLexec will
2715-
* consume the interrupt. The user's intention, though, is to cancel
2716-
* the entire watch process, so detect a sent cancellation request and
2717-
* exit in this case.
2707+
* PSQLexecWatch handles the case where we can no longer
2708+
* repeat the query, and returns 0 or -1.
27182709
*/
2719-
if (cancel_pressed)
2720-
{
2721-
PQclear(res);
2710+
if (res==0)
27222711
break;
2723-
}
2724-
2725-
switch (PQresultStatus(res))
2726-
{
2727-
casePGRES_TUPLES_OK:
2728-
printQuery(res,&myopt,pset.queryFout,pset.logfile);
2729-
break;
2730-
2731-
casePGRES_COMMAND_OK:
2732-
fprintf(pset.queryFout,"%s\n%s\n\n",title,PQcmdStatus(res));
2733-
break;
2734-
2735-
casePGRES_EMPTY_QUERY:
2736-
psql_error(_("\\watch cannot be used with an empty query\n"));
2737-
PQclear(res);
2738-
return false;
2739-
2740-
casePGRES_COPY_OUT:
2741-
casePGRES_COPY_IN:
2742-
casePGRES_COPY_BOTH:
2743-
psql_error(_("\\watch cannot be used with COPY\n"));
2744-
PQclear(res);
2745-
return false;
2746-
2747-
default:
2748-
/* other cases should have been handled by PSQLexec */
2749-
psql_error(_("unexpected result status for \\watch\n"));
2750-
PQclear(res);
2751-
return false;
2752-
}
2753-
2754-
PQclear(res);
2755-
2756-
fflush(pset.queryFout);
2712+
if (res==-1)
2713+
return false;
27572714

27582715
/*
27592716
* Set up cancellation of 'watch' via SIGINT. We redo this each time
2760-
* through the loop since it's conceivable something inside PSQLexec
2761-
* could change sigint_interrupt_jmp.
2717+
* through the loop since it's conceivable something inside
2718+
*PSQLexecWatchcould change sigint_interrupt_jmp.
27622719
*/
27632720
if (sigsetjmp(sigint_interrupt_jmp,1)!=0)
27642721
break;

‎src/bin/psql/common.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,102 @@ PSQLexec(const char *query, bool start_xact)
497497
}
498498

499499

500+
/*
501+
* PSQLexecWatch
502+
*
503+
* This function is used for \watch command to send the query to
504+
* the server and print out the results.
505+
*
506+
* Returns 1 if the query executed successfully, 0 if it cannot be repeated,
507+
* e.g., because of the interrupt, -1 on error.
508+
*/
509+
int
510+
PSQLexecWatch(constchar*query,constprintQueryOpt*opt)
511+
{
512+
PGresult*res;
513+
doubleelapsed_msec=0;
514+
instr_timebefore;
515+
instr_timeafter;
516+
517+
if (!pset.db)
518+
{
519+
psql_error("You are currently not connected to a database.\n");
520+
return0;
521+
}
522+
523+
SetCancelConn();
524+
525+
if (pset.timing)
526+
INSTR_TIME_SET_CURRENT(before);
527+
528+
res=PQexec(pset.db,query);
529+
530+
ResetCancelConn();
531+
532+
if (!AcceptResult(res))
533+
{
534+
PQclear(res);
535+
return0;
536+
}
537+
538+
if (pset.timing)
539+
{
540+
INSTR_TIME_SET_CURRENT(after);
541+
INSTR_TIME_SUBTRACT(after,before);
542+
elapsed_msec=INSTR_TIME_GET_MILLISEC(after);
543+
}
544+
545+
/*
546+
* If SIGINT is sent while the query is processing, the interrupt
547+
* will be consumed. The user's intention, though, is to cancel
548+
* the entire watch process, so detect a sent cancellation request and
549+
* exit in this case.
550+
*/
551+
if (cancel_pressed)
552+
{
553+
PQclear(res);
554+
return0;
555+
}
556+
557+
switch (PQresultStatus(res))
558+
{
559+
casePGRES_TUPLES_OK:
560+
printQuery(res,opt,pset.queryFout,pset.logfile);
561+
break;
562+
563+
casePGRES_COMMAND_OK:
564+
fprintf(pset.queryFout,"%s\n%s\n\n",opt->title,PQcmdStatus(res));
565+
break;
566+
567+
casePGRES_EMPTY_QUERY:
568+
psql_error(_("\\watch cannot be used with an empty query\n"));
569+
PQclear(res);
570+
return-1;
571+
572+
casePGRES_COPY_OUT:
573+
casePGRES_COPY_IN:
574+
casePGRES_COPY_BOTH:
575+
psql_error(_("\\watch cannot be used with COPY\n"));
576+
PQclear(res);
577+
return-1;
578+
579+
default:
580+
psql_error(_("unexpected result status for \\watch\n"));
581+
PQclear(res);
582+
return-1;
583+
}
584+
585+
PQclear(res);
586+
587+
fflush(pset.queryFout);
588+
589+
/* Possible microtiming output */
590+
if (pset.timing)
591+
printf(_("Time: %.3f ms\n"),elapsed_msec);
592+
593+
return1;
594+
}
595+
500596

501597
/*
502598
* PrintNotifications: check for asynchronous notifications, and print them out

‎src/bin/psql/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include<setjmp.h>
1313
#include"libpq-fe.h"
1414

15+
#include"print.h"
16+
1517
#defineatooid(x) ((Oid) strtoul((x), NULL, 10))
1618

1719
externboolsetQFout(constchar*fname);
@@ -37,6 +39,7 @@ extern void SetCancelConn(void);
3739
externvoidResetCancelConn(void);
3840

3941
externPGresult*PSQLexec(constchar*query,boolstart_xact);
42+
externintPSQLexecWatch(constchar*query,constprintQueryOpt*opt);
4043

4144
externboolSendQuery(constchar*query);
4245

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp