diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 230e11013bfad1b408f023dded4afab97b1f1160..0d84d01b7ca7ba750dff7b89f7da20c140bffde9 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ <!-- -$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.61 2001/12/08 03:24:38 thomas Exp $ +$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.62 2002/03/05 00:00:58 momjian Exp $ PostgreSQL documentation --> @@ -1119,6 +1119,16 @@ lo_import 152801 </varlistentry> + <varlistentry> + <term><literal>\timing</literal> + <listitem> + <para> + Toggles a display of how long each query takes in seconds. + </para> + </listitem> + </varlistentry> + + <varlistentry> <term><literal>\w</literal> {<replaceable class="parameter">filename</replaceable> | <replaceable class="parameter">|command</replaceable>}</term> <listitem> diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 2a078bb6b9596986d5d498cb507a37667ef98ae7..398ac00b8ac2d54ad38cce764a225380631969ef 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.66 2002/02/25 21:37:42 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.67 2002/03/05 00:01:00 momjian Exp $ */ #include "postgres_fe.h" #include "command.h" @@ -715,6 +715,24 @@ exec_command(const char *cmd, free(value); } + /* \timing -- toggle timing of queries */ + else if (strcmp(cmd, "timing") == 0) + { + pset.timing = !pset.timing; + if (!quiet) + { + if (pset.timing) + { + puts(gettext(("Timing is on."))); + } + else + { + puts(gettext(("Timing is off."))); + + } + } + } + /* \unset */ else if (strcmp(cmd, "unset") == 0) { diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 3a92f9642fb6482861f7fac61d613efe1b7cb747..43293a0cfbeb8750a70b97c60d57b2e638e9398d 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.38 2001/11/05 17:46:30 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.39 2002/03/05 00:01:00 momjian Exp $ */ #include "postgres_fe.h" @@ -11,6 +11,7 @@ #include <errno.h> #include <stdarg.h> +#include <sys/time.h> #ifdef HAVE_TERMIOS_H #include <termios.h> #endif @@ -406,6 +407,8 @@ SendQuery(const char *query) bool success = false; PGresult *results; PGnotify *notify; + struct timeval before,after; + struct timezone tz; if (!pset.db) { @@ -435,7 +438,15 @@ SendQuery(const char *query) } cancelConn = pset.db; + if (pset.timing) + { + gettimeofday(&before, &tz); + } results = PQexec(pset.db, query); + if (pset.timing) + { + gettimeofday(&after, &tz); + } if (PQresultStatus(results) == PGRES_COPY_IN) copy_in_state = true; /* keep cancel connection for copy out state */ @@ -563,6 +574,13 @@ SendQuery(const char *query) if (results) PQclear(results); + } + + /* Possible microtiming output */ + + if (pset.timing && success) + { + ! printf(gettext("Total time: %.3fs\n"), ((after.tv_sec-before.tv_sec)*1000000 + after.tv_usec - before.tv_usec) / 1000000.0); } return success; diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index b4614627a7f21039c0d1401beb36181ac1b66074..3c28441181fc94b8b77092a19f70ab949e95ce64 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.42 2001/10/25 05:49:54 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.43 2002/03/05 00:01:01 momjian Exp $ */ #include "postgres_fe.h" #include "help.h" @@ -229,6 +229,8 @@ slashUsage(void) fprintf(fout, _(" \\t show only rows (currently %s)\n"), ON(pset.popt.topt.tuples_only)); fprintf(fout, _(" \\T TEXT set HTML table tag attributes\n")); + fprintf(fout, _(" \\timing toggle timing of queries (currently %s)\n"), + ON(pset.timing)); fprintf(fout, _(" \\unset NAME unset (delete) internal variable\n")); fprintf(fout, _(" \\w FILENAME write current query buffer to file\n")); fprintf(fout, _(" \\x toggle expanded output (currently %s)\n"), diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 21faca32332961c6252022089136386195b89601..cb41920eb9de0a81573cbee15a9f7e8681f4ba2a 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/settings.h,v 1.12 2001/10/28 06:25:58 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/settings.h,v 1.13 2002/03/05 00:01:02 momjian Exp $ */ #ifndef SETTINGS_H #define SETTINGS_H @@ -50,6 +50,7 @@ typedef struct _psqlSettings bool issuper; /* is the current user a superuser? (used * to form the prompt) */ + bool timing; /* timing of all queries */ } PsqlSettings; extern PsqlSettings pset; diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index f76921bd17af6c7b511c3da213e88c35898584fe..85296fc1999007f08be45c4bb4fd974609b9a15a 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.42 2002/03/02 21:39:34 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.43 2002/03/05 00:01:03 momjian Exp $ */ /*---------------------------------------------------------------------- @@ -276,8 +276,8 @@ psql_completion(char *text, int start, int end) "\\e", "\\echo", "\\encoding", "\\g", "\\h", "\\i", "\\l", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink", - "\\o", "\\p", "\\pset", "\\q", "\\qecho", "\\r", "\\set", "\\t", "\\unset", - "\\x", "\\w", "\\z", "\\!", NULL + "\\o", "\\p", "\\pset", "\\q", "\\qecho", "\\r", "\\set", "\\t", + "\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL }; (void) end; /* not used */