On Mon, Jul 11, 2016 at 8:35 AM, Peter Eisentraut < peter.eisentr...@2ndquadrant.com> wrote:
> On 7/9/16 4:00 PM, Andrew Gierth wrote: > >> How about >> >> Time: 1234567.666 ms (20m 34.6s) >> > > That's similar to what I had in mind, so I'd be happy with that. > > > -- > Peter Eisentraut http://www.2ndQuadrant.com/ > PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services > Ok, here's what I came up with (with time to test it). If the query took less than a second, timing display is unchanged. Otherwise, print the ms time, followed by a more human readable form accurate to 0.001s. # select 1; select pg_sleep(1); select pg_sleep(71); select pg_sleep (3601); select pg_sleep(24*3601); ?column? ---------- 1 (1 row) Time: 1.575 ms pg_sleep ---------- (1 row) Time: 1002.568 ms (1.003s) pg_sleep ---------- (1 row) Time: 71041.022 ms (1m 11.041s) pg_sleep ---------- (1 row) Time: 3601083.544 ms (1h 0m 1.084s) pg_sleep ---------- (1 row) Time: 86424018.416 ms (1d 0h 0m 24.018s) As-is, there isn't much that could be done for regression or documentation changes, so I'll just leave this here.
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 2450b9c..7a87314 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -529,6 +529,58 @@ ClearOrSaveResult(PGresult *result) } } +/* + * Print the time elapsed in both raw milliseconds and if the time elapsed + * was at least 1 second, also print a format normalized to days, hours, + * minutes, and seconds. + */ +static void +PrintTiming(double elapsed_msec) +{ + double seconds; + int minutes; + int hours; + int days; + + if (elapsed_msec < 1000.0) + { + printf(_("Time: %.3f ms\n"), elapsed_msec); + return; + } + + seconds = elapsed_msec / 1000.0; + if (seconds < 60.0) + { + printf(_("Time: %.3f ms (%.3fs)\n"), elapsed_msec, seconds); + return; + } + + minutes = (int)seconds / 60; + seconds = seconds - (60.0 * minutes); + if (minutes < 60) + { + printf(_("Time: %.3f ms (%dm %.3fs)\n"), + elapsed_msec, minutes, seconds); + return; + } + + hours = minutes / 60; + minutes = minutes % 60; + + if (hours < 24) + { + printf(_("Time: %.3f ms (%dh %dm %.3fs)\n"), + elapsed_msec, hours, minutes, seconds); + return; + } + + days = hours / 24; + hours = hours % 24; + + printf(_("Time: %.3f ms (%dd %dh %dm %.3fs)\n"), + days, elapsed_msec, hours, minutes, seconds); +} + /* * PSQLexec @@ -678,7 +730,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; } @@ -1328,7 +1380,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 */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers