\timing is great. \timing is helpful. \timing has made me really good at mentally estimating numbers modulo 3600000.
Wouldn't it be great if we had a way of printing timing in more human friendly formats? Attached is a patch that allows the following (new/interesting bits in bold): # \timing off Timing is off. # select pg_sleep(1); pg_sleep ---------- (1 row) # \timing Timing is on. # select pg_sleep(1); pg_sleep ---------- (1 row) Time: 1002.959 ms *# \timing interval* *Timing is interval.* # select pg_sleep(1); pg_sleep ---------- (1 row) *Time: 00:00:01.003* # \timing Timing is off. As demonstrated, "interval" toggles to "off". There is no way to toggle to "interval". I'm pretty flexible on how something like this gets invoked. We could leave timing alone and create a format variable. We could actually leverage the pre-existing interval-to-string code, etc. Note: the current patch includes no doc changes. I'd figure I'd wait to do that after this patch or another gains some traction.
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 3f2cebf..1e6686f 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1516,12 +1516,23 @@ exec_command(const char *cmd, OT_NORMAL, NULL, false); if (opt) - pset.timing = ParseVariableBool(opt, "\\timing"); + if (strcmp(opt,"interval") == 0) + pset.timing = PSQL_TIMING_INTERVAL; + else if (ParseVariableBool(opt, "\\timing")) + pset.timing = PSQL_TIMING_ON; + else + pset.timing = PSQL_TIMING_OFF; else - pset.timing = !pset.timing; + if (pset.timing == PSQL_TIMING_OFF) + pset.timing = PSQL_TIMING_ON; + else + pset.timing = PSQL_TIMING_OFF; + if (!pset.quiet) { - if (pset.timing) + if (pset.timing == PSQL_TIMING_INTERVAL) + puts(_("Timing is interval.")); + else if (pset.timing == PSQL_TIMING_ON) puts(_("Timing is on.")); else puts(_("Timing is off.")); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 2450b9c..c79843f 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -587,6 +587,47 @@ PSQLexec(const char *query) return res; } +/* + * PrintTimingInterval + * + * Output timing an interval format + */ +static void +PrintTimingInterval(double elapsed_msec) +{ + double seconds = elapsed_msec / 1000.0; + int minutes; + int hours; + int days; + + if (seconds < 60.0) + { + printf(_("Time: 00:00:%06.3f\n"), seconds); + return; + } + + minutes = (int)seconds / 60; + seconds = seconds - (60.0 * minutes); + if (minutes < 60) + { + printf(_("Time: 00:%02d:%06.3f\n"), minutes, seconds); + return; + } + + hours = minutes / 60; + minutes = minutes % 60; + + if (hours < 24) + { + printf(_("Time: %2d:%02d:%06.3f\n"), hours, minutes, seconds); + return; + } + + days = hours / 24; + hours = hours % 24; + + printf(_("Time: %d, %2d:%02d:%06.3f\n"), days, hours, minutes, seconds); +} /* * PSQLexecWatch @@ -613,7 +654,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt) SetCancelConn(); - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) INSTR_TIME_SET_CURRENT(before); res = PQexec(pset.db, query); @@ -626,7 +667,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt) return 0; } - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) { INSTR_TIME_SET_CURRENT(after); INSTR_TIME_SUBTRACT(after, before); @@ -677,7 +718,9 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt) fflush(pset.queryFout); /* Possible microtiming output */ - if (pset.timing) + if (pset.timing == PSQL_TIMING_INTERVAL) + PrintTimingInterval(elapsed_msec); + else if (pset.timing == PSQL_TIMING_ON) printf(_("Time: %.3f ms\n"), elapsed_msec); return 1; @@ -1228,7 +1271,7 @@ SendQuery(const char *query) instr_time before, after; - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) INSTR_TIME_SET_CURRENT(before); results = PQexec(pset.db, query); @@ -1237,7 +1280,7 @@ SendQuery(const char *query) ResetCancelConn(); OK = ProcessResult(&results); - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) { INSTR_TIME_SET_CURRENT(after); INSTR_TIME_SUBTRACT(after, before); @@ -1327,7 +1370,9 @@ SendQuery(const char *query) ClearOrSaveResult(results); /* Possible microtiming output */ - if (pset.timing) + if (pset.timing == PSQL_TIMING_INTERVAL) + PrintTimingInterval(elapsed_msec); + else if (pset.timing == PSQL_TIMING_ON) printf(_("Time: %.3f ms\n"), elapsed_msec); /* check for events that may occur during query execution */ @@ -1412,7 +1457,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) my_popt.topt.stop_table = false; my_popt.topt.prior_records = 0; - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) INSTR_TIME_SET_CURRENT(before); /* if we're not in a transaction, start one */ @@ -1440,7 +1485,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (!OK) goto cleanup; - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) { INSTR_TIME_SET_CURRENT(after); INSTR_TIME_SUBTRACT(after, before); @@ -1482,13 +1527,13 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) for (;;) { - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) INSTR_TIME_SET_CURRENT(before); /* get fetch_count tuples at a time */ results = PQexec(pset.db, fetch_cmd); - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) { INSTR_TIME_SET_CURRENT(after); INSTR_TIME_SUBTRACT(after, before); @@ -1583,7 +1628,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) } cleanup: - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) INSTR_TIME_SET_CURRENT(before); /* @@ -1609,7 +1654,7 @@ cleanup: ClearOrSaveResult(results); } - if (pset.timing) + if (pset.timing != PSQL_TIMING_OFF) { INSTR_TIME_SET_CURRENT(after); INSTR_TIME_SUBTRACT(after, before); diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index 9e6d67b..1e2a229 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -45,6 +45,7 @@ */ #define ON(var) (var ? _("on") : _("off")) + void usage(unsigned short int pager) { @@ -288,8 +289,8 @@ slashUsage(unsigned short int pager) fprintf(output, _("Operating System\n")); fprintf(output, _(" \\cd [DIR] change the current working directory\n")); fprintf(output, _(" \\setenv NAME [VALUE] set or unset environment variable\n")); - fprintf(output, _(" \\timing [on|off] toggle timing of commands (currently %s)\n"), - ON(pset.timing)); + fprintf(output, _(" \\timing [on|off|interval] toggle timing of commands (currently %s)\n"), + ( (pset.timing == PSQL_TIMING_INTERVAL) ? _("interval") : ON(pset.timing))); fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n")); fprintf(output, "\n"); diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 8cfe9d2..cc4e5eb 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -64,6 +64,13 @@ typedef enum typedef enum { + PSQL_TIMING_OFF, + PSQL_TIMING_ON, + PSQL_TIMING_INTERVAL +} PSQL_TIMING; + +typedef enum +{ hctl_none = 0, hctl_ignorespace = 1, hctl_ignoredups = 2, @@ -108,7 +115,7 @@ typedef struct _psqlSettings uint64 lineno; /* also for error reporting */ uint64 stmt_lineno; /* line number inside the current statement */ - bool timing; /* enable timing of all queries */ + PSQL_TIMING timing; /* enable timing of all queries */ FILE *logfile; /* session log file handle */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers