diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-09-03 15:29:03 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-09-03 15:29:03 -0400 |
commit | 6591f4226c81104f7746da6a5c00519919c560ae (patch) | |
tree | 28c7b49a8cbed6a0c3d8240eb034cc12071eec80 /src | |
parent | 600dc4c0da3b8c094ccc1ae75b47c8320898c714 (diff) | |
download | postgresql-6591f4226c81104f7746da6a5c00519919c560ae.tar.gz postgresql-6591f4226c81104f7746da6a5c00519919c560ae.zip |
Improve readability of the output of psql's \timing command.
In addition to the existing decimal-milliseconds output value,
display the same value in mm:ss.fff format if it exceeds one second.
Tack on hours and even days fields if the interval is large enough.
This avoids needing mental arithmetic to convert the values into
customary time units.
Corey Huinker, reviewed by Gerdan Santos; bikeshedding by many
Discussion: <CADkLM=dbC4R8sbbuFXQVBFWoJGQkTEW8RWnC0PbW9nZsovZpJQ@mail.gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/psql/common.c | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 73999502847..a7789dfa53f 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -10,6 +10,7 @@ #include <ctype.h> #include <limits.h> +#include <math.h> #include <signal.h> #ifndef WIN32 #include <unistd.h> /* for write() */ @@ -532,6 +533,57 @@ ClearOrSaveResult(PGresult *result) /* + * Print microtiming output. Always print raw milliseconds; if the interval + * is >= 1 second, also break it down into days/hours/minutes/seconds. + */ +static void +PrintTiming(double elapsed_msec) +{ + double seconds; + double minutes; + double hours; + double days; + + if (elapsed_msec < 1000.0) + { + /* This is the traditional (pre-v10) output format */ + printf(_("Time: %.3f ms\n"), elapsed_msec); + return; + } + + /* + * Note: we could print just seconds, in a format like %06.3f, when the + * total is less than 1min. But that's hard to interpret unless we tack + * on "s" or otherwise annotate it. Forcing the display to include + * minutes seems like a better solution. + */ + seconds = elapsed_msec / 1000.0; + minutes = floor(seconds / 60.0); + seconds -= 60.0 * minutes; + if (minutes < 60.0) + { + printf(_("Time: %.3f ms (%02d:%06.3f)\n"), + elapsed_msec, (int) minutes, seconds); + return; + } + + hours = floor(minutes / 60.0); + minutes -= 60.0 * hours; + if (hours < 24.0) + { + printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"), + elapsed_msec, (int) hours, (int) minutes, seconds); + return; + } + + days = floor(hours / 24.0); + hours -= 24.0 * days; + printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"), + elapsed_msec, days, (int) hours, (int) minutes, seconds); +} + + +/* * PSQLexec * * This is the way to send "backdoor" queries (those not directly entered @@ -679,7 +731,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt) /* Possible microtiming output */ if (pset.timing) - printf(_("Time: %.3f ms\n"), elapsed_msec); + PrintTiming(elapsed_msec); return 1; } @@ -1332,7 +1384,7 @@ SendQuery(const char *query) /* Possible microtiming output */ if (pset.timing) - printf(_("Time: %.3f ms\n"), elapsed_msec); + PrintTiming(elapsed_msec); /* check for events that may occur during query execution */ |