Skip to content

Commit 25b0b09

Browse files
committed
Add \timing patch to psql. Times all queries.
Greg Sabino Mullane
1 parent 294f0d4 commit 25b0b09

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.61 2001/12/08 03:24:38 thomas Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.62 2002/03/05 00:00:58 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -1119,6 +1119,16 @@ lo_import 152801
11191119
</varlistentry>
11201120

11211121

1122+
<varlistentry>
1123+
<term><literal>\timing</literal>
1124+
<listitem>
1125+
<para>
1126+
Toggles a display of how long each query takes in seconds.
1127+
</para>
1128+
</listitem>
1129+
</varlistentry>
1130+
1131+
11221132
<varlistentry>
11231133
<term><literal>\w</literal> {<replaceable class="parameter">filename</replaceable> | <replaceable class="parameter">|command</replaceable>}</term>
11241134
<listitem>

src/bin/psql/command.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.66 2002/02/25 21:37:42 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.67 2002/03/05 00:01:00 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -715,6 +715,24 @@ exec_command(const char *cmd,
715715
free(value);
716716
}
717717

718+
/* \timing -- toggle timing of queries */
719+
else if (strcmp(cmd, "timing") == 0)
720+
{
721+
pset.timing = !pset.timing;
722+
if (!quiet)
723+
{
724+
if (pset.timing)
725+
{
726+
puts(gettext(("Timing is on.")));
727+
}
728+
else
729+
{
730+
puts(gettext(("Timing is off.")));
731+
732+
}
733+
}
734+
}
735+
718736
/* \unset */
719737
else if (strcmp(cmd, "unset") == 0)
720738
{

src/bin/psql/common.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.38 2001/11/05 17:46:30 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.39 2002/03/05 00:01:00 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

1010
#include "common.h"
1111

1212
#include <errno.h>
1313
#include <stdarg.h>
14+
#include <sys/time.h>
1415
#ifdef HAVE_TERMIOS_H
1516
#include <termios.h>
1617
#endif
@@ -406,6 +407,8 @@ SendQuery(const char *query)
406407
bool success = false;
407408
PGresult *results;
408409
PGnotify *notify;
410+
struct timeval before,after;
411+
struct timezone tz;
409412

410413
if (!pset.db)
411414
{
@@ -435,7 +438,15 @@ SendQuery(const char *query)
435438
}
436439

437440
cancelConn = pset.db;
441+
if (pset.timing)
442+
{
443+
gettimeofday(&before, &tz);
444+
}
438445
results = PQexec(pset.db, query);
446+
if (pset.timing)
447+
{
448+
gettimeofday(&after, &tz);
449+
}
439450
if (PQresultStatus(results) == PGRES_COPY_IN)
440451
copy_in_state = true;
441452
/* keep cancel connection for copy out state */
@@ -563,6 +574,13 @@ SendQuery(const char *query)
563574

564575
if (results)
565576
PQclear(results);
577+
}
578+
579+
/* Possible microtiming output */
580+
581+
if (pset.timing && success)
582+
{
583+
! printf(gettext("Total time: %.3fs\n"), ((after.tv_sec-before.tv_sec)*1000000 + after.tv_usec - before.tv_usec) / 1000000.0);
566584
}
567585

568586
return success;

src/bin/psql/help.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.42 2001/10/25 05:49:54 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.43 2002/03/05 00:01:01 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "help.h"
@@ -229,6 +229,8 @@ slashUsage(void)
229229
fprintf(fout, _(" \\t show only rows (currently %s)\n"),
230230
ON(pset.popt.topt.tuples_only));
231231
fprintf(fout, _(" \\T TEXT set HTML table tag attributes\n"));
232+
fprintf(fout, _(" \\timing toggle timing of queries (currently %s)\n"),
233+
ON(pset.timing));
232234
fprintf(fout, _(" \\unset NAME unset (delete) internal variable\n"));
233235
fprintf(fout, _(" \\w FILENAME write current query buffer to file\n"));
234236
fprintf(fout, _(" \\x toggle expanded output (currently %s)\n"),

src/bin/psql/settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/settings.h,v 1.12 2001/10/28 06:25:58 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/settings.h,v 1.13 2002/03/05 00:01:02 momjian Exp $
77
*/
88
#ifndef SETTINGS_H
99
#define SETTINGS_H
@@ -50,6 +50,7 @@ typedef struct _psqlSettings
5050

5151
bool issuper; /* is the current user a superuser? (used
5252
* to form the prompt) */
53+
bool timing; /* timing of all queries */
5354
} PsqlSettings;
5455

5556
extern PsqlSettings pset;

src/bin/psql/tab-complete.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.42 2002/03/02 21:39:34 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.43 2002/03/05 00:01:03 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -276,8 +276,8 @@ psql_completion(char *text, int start, int end)
276276
"\\e", "\\echo",
277277
"\\encoding", "\\g", "\\h", "\\i", "\\l",
278278
"\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
279-
"\\o", "\\p", "\\pset", "\\q", "\\qecho", "\\r", "\\set", "\\t", "\\unset",
280-
"\\x", "\\w", "\\z", "\\!", NULL
279+
"\\o", "\\p", "\\pset", "\\q", "\\qecho", "\\r", "\\set", "\\t",
280+
"\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
281281
};
282282

283283
(void) end; /* not used */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy