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

Commit351adb8

Browse files
committed
Fix order of operations within SendQuery() so that the time spent in
data transfer during COPY is included in the \timing display. Alsoavoid portability problems if tv_usec is unsigned on some platform.
1 parenta1dcd8f commit351adb8

File tree

1 file changed

+78
-28
lines changed

1 file changed

+78
-28
lines changed

‎src/bin/psql/common.c

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.75 2003/10/05 22:36:00 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.76 2003/10/06 01:11:12 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"common.h"
@@ -45,14 +45,19 @@
4545
typedefstructtimevalTimevalStruct;
4646

4747
#defineGETTIMEOFDAY(T) gettimeofday(T, NULL)
48-
#defineDIFF_MSEC(T,U) ((((T)->tv_sec - (U)->tv_sec) * 1000000.0 + (T)->tv_usec - (U)->tv_usec) / 1000.0)
48+
#defineDIFF_MSEC(T,U) \
49+
((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 + \
50+
((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
4951

5052
#else
5153

5254
typedefstruct_timebTimevalStruct;
5355

5456
#defineGETTIMEOFDAY(T) _ftime(T)
55-
#defineDIFF_MSEC(T,U) ((((T)->time - (U)->time) * 1000.0 + (T)->millitm - (U)->millitm))
57+
#defineDIFF_MSEC(T,U) \
58+
(((T)->time - (U)->time) * 1000.0 + \
59+
((T)->millitm - (U)->millitm))
60+
5661
#endif
5762

5863
externboolprompt_state;
@@ -478,18 +483,15 @@ PrintQueryTuples(const PGresult *results)
478483
}
479484

480485

481-
482486
/*
483-
*PrintQueryResults: analyze query results and print them out
487+
*ProcessCopyResult: if command was a COPY FROM STDIN/TO STDOUT, handle it
484488
*
485489
* Note: Utility function for use by SendQuery() only.
486490
*
487491
* Returns true if the query executed successfully, false otherwise.
488492
*/
489493
staticbool
490-
PrintQueryResults(PGresult*results,
491-
constTimevalStruct*before,
492-
constTimevalStruct*after)
494+
ProcessCopyResult(PGresult*results)
493495
{
494496
boolsuccess= false;
495497

@@ -499,11 +501,58 @@ PrintQueryResults(PGresult *results,
499501
switch (PQresultStatus(results))
500502
{
501503
casePGRES_TUPLES_OK:
502-
success=PrintQueryTuples(results);
503-
break;
504+
casePGRES_COMMAND_OK:
504505
casePGRES_EMPTY_QUERY:
506+
/* nothing to do here */
505507
success= true;
506508
break;
509+
510+
casePGRES_COPY_OUT:
511+
success=handleCopyOut(pset.db,pset.queryFout);
512+
break;
513+
514+
casePGRES_COPY_IN:
515+
if (pset.cur_cmd_interactive&& !QUIET())
516+
puts(gettext("Enter data to be copied followed by a newline.\n"
517+
"End with a backslash and a period on a line by itself."));
518+
519+
success=handleCopyIn(pset.db,pset.cur_cmd_source,
520+
pset.cur_cmd_interactive ?get_prompt(PROMPT_COPY) :NULL);
521+
break;
522+
523+
default:
524+
break;
525+
}
526+
527+
/* may need this to recover from conn loss during COPY */
528+
if (!CheckConnection())
529+
return false;
530+
531+
returnsuccess;
532+
}
533+
534+
535+
/*
536+
* PrintQueryResults: print out query results as required
537+
*
538+
* Note: Utility function for use by SendQuery() only.
539+
*
540+
* Returns true if the query executed successfully, false otherwise.
541+
*/
542+
staticbool
543+
PrintQueryResults(PGresult*results)
544+
{
545+
boolsuccess= false;
546+
547+
if (!results)
548+
return false;
549+
550+
switch (PQresultStatus(results))
551+
{
552+
casePGRES_TUPLES_OK:
553+
success=PrintQueryTuples(results);
554+
break;
555+
507556
casePGRES_COMMAND_OK:
508557
{
509558
charbuf[10];
@@ -525,17 +574,15 @@ PrintQueryResults(PGresult *results,
525574
SetVariable(pset.vars,"LASTOID",buf);
526575
break;
527576
}
528-
casePGRES_COPY_OUT:
529-
success=handleCopyOut(pset.db,pset.queryFout);
577+
578+
casePGRES_EMPTY_QUERY:
579+
success= true;
530580
break;
531581

582+
casePGRES_COPY_OUT:
532583
casePGRES_COPY_IN:
533-
if (pset.cur_cmd_interactive&& !QUIET())
534-
puts(gettext("Enter data to be copied followed by a newline.\n"
535-
"End with a backslash and a period on a line by itself."));
536-
537-
success=handleCopyIn(pset.db,pset.cur_cmd_source,
538-
pset.cur_cmd_interactive ?get_prompt(PROMPT_COPY) :NULL);
584+
/* nothing to do here */
585+
success= true;
539586
break;
540587

541588
default:
@@ -544,19 +591,10 @@ PrintQueryResults(PGresult *results,
544591

545592
fflush(pset.queryFout);
546593

547-
/* may need this to recover from conn loss during COPY */
548-
if (!CheckConnection())
549-
return false;
550-
551-
/* Possible microtiming output */
552-
if (pset.timing&&success)
553-
printf(gettext("Time: %.3f ms\n"),DIFF_MSEC(after,before));
554-
555594
returnsuccess;
556595
}
557596

558597

559-
560598
/*
561599
* SendQuery: send the query string to the backend
562600
* (and print out results)
@@ -621,13 +659,25 @@ SendQuery(const char *query)
621659

622660
if (pset.timing)
623661
GETTIMEOFDAY(&before);
662+
624663
results=PQexec(pset.db,query);
664+
665+
/* these operations are included in the timing result: */
666+
OK= (AcceptResult(results)&&ProcessCopyResult(results));
667+
625668
if (pset.timing)
626669
GETTIMEOFDAY(&after);
627670

628-
OK= (AcceptResult(results)&&PrintQueryResults(results,&before,&after));
671+
/* but printing results isn't: */
672+
if (OK)
673+
OK=PrintQueryResults(results);
674+
629675
PQclear(results);
630676

677+
/* Possible microtiming output */
678+
if (OK&&pset.timing)
679+
printf(gettext("Time: %.3f ms\n"),DIFF_MSEC(&after,&before));
680+
631681
/* check for events that may occur during query execution */
632682

633683
if (pset.encoding!=PQclientEncoding(pset.db)&&

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp