Add \timing patch to psql. Times all queries.
authorBruce Momjian <bruce@momjian.us>
Tue, 5 Mar 2002 00:01:03 +0000 (00:01 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 5 Mar 2002 00:01:03 +0000 (00:01 +0000)
Greg Sabino Mullane

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/command.c
src/bin/psql/common.c
src/bin/psql/help.c
src/bin/psql/settings.h
src/bin/psql/tab-complete.c

index 710c8fb6c86a6c00daddbc4fac25235673920f8c..8ea8a78a1a774c53d742781d6b1ebb3c7315ac56 100644 (file)
@@ -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>
index af78ba45089f1af8a252ec8818aadf31bfac06a6..90da4ee91c603437753e07d10319dc224e27685c 100644 (file)
@@ -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)
        {
index e7d11c4e7c8bf55f587a5fcafd285738a14fa5f5..c3a101a1ea561acaed9886fda388f986c482aec2 100644 (file)
@@ -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;
index 40cceab96b91a1bddc5784cd7ee919ae7c56206c..5ec25ff208ac6c47f111169a3c824c7368e3f680 100644 (file)
@@ -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"),
index c98d1f2e674966d9794e8f896863ff1942ba6207..e16c397d28cd9815afbbe5b35d31a9d255e6ec47 100644 (file)
@@ -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;
index 2e333ec8192c94d0984c56abbf666b5af106a535..79ae23b4985b9e268a59fda8aa36fc56c422820d 100644 (file)
@@ -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 */