On Thu, Feb 23, 2023 at 2:05 PM Kirk Wolak <wol...@gmail.com> wrote:
> On Thu, Feb 23, 2023 at 9:52 AM Tom Lane <t...@sss.pgh.pa.us> wrote: > >> Heikki Linnakangas <hlinn...@iki.fi> writes: >> > On 23/02/2023 13:20, Peter Eisentraut wrote: >> >> If you don't have \timing turned on before the query starts, psql won't >> >> record what the time was before the query, so you can't compute the run >> >> time afterwards. This kind of feature would only work if you always >> >> take the start time, even if \timing is turned off. >> >> > Correct. That seems acceptable though? gettimeofday() can be slow on >> > some platforms, but I doubt it's *that* slow, that we couldn't call it >> > two times per query. >> >> Yeah, you'd need to capture both the start and stop times even if >> \timing isn't on, in case you get asked later. But the backend is >> going to call gettimeofday at least once per query, likely more >> depending on what features you use. And there are inherently >> multiple kernel calls involved in sending a query and receiving >> a response. I tend to agree with Heikki that this overhead would >> be unnoticeable. (Of course, some investigation proving that >> wouldn't be unwarranted.) >> >> regards, tom lane >> > > Note, for this above feature, I was thinking we have a ROW_COUNT variable > I use \set to see. > The simplest way to add this is maybe a set variable: EXEC_TIME > And it's set when ROW_COUNT gets set. > +1 > > == > Now, since this opened a lively discussion, I am officially submitting my > first patch. > This includes the small change to prompt.c and the documentation. I had > help from Andrey Borodin, > and Pavel Stehule, who have supported me in how to propose, and use > gitlab, etc. > > We are programmers... It's literally our job to sharpen our tools. And > PSQL is one of my most used. > A small frustration, felt regularly was the motive. > > Regards, Kirk > PS: If I am supposed to edit the subject to say there is a patch here, I > did not know > PPS: I appreciate ANY and ALL feedback... This is how we learn! > Patch Posted with one edit, for line editings (Thanks Jim!)
From b9db157177bbdeeeb6d35c3623ca9355141419d7 Mon Sep 17 00:00:00 2001 From: Jim Jones <jim.jo...@uni-muenster.de> Date: Wed, 1 Mar 2023 00:07:55 +0100 Subject: [PATCH v2] Time option added to psql prompt This adds a useful time option to the prompt: %T. Which does not require a wasteful backquoted shell command which is also not compatible between operating systems. The format is simply HH24:MI:SS no other options available by design! Author: Kirk Wolak <wol...@gmail.com> Reviewed-By: Andrey Borodin <amboro...@acm.org> Reviewed-By: Nikolay Samokhvalov <samokhva...@gmail.com> Thread: https://postgr.es/m/CACLU5mSRwHr_8z%3DenMj-nXF1tmC7%2BJn5heZQNiKuLyxYUtL2fg%40mail.gmail.com --- doc/src/sgml/ref/psql-ref.sgml | 9 +++++++++ src/bin/psql/prompt.c | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index dc6528dc11..04ab9eeb8c 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -4575,6 +4575,15 @@ testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput> </listitem> </varlistentry> + <varlistentry id="app-psql-prompting-t-uc"> + <term><literal>%T</literal></term> + <listitem> + <para> + The current time on the client in HH24:MI:SS format. + </para> + </listitem> + </varlistentry> + <varlistentry id="app-psql-prompting-x"> <term><literal>%x</literal></term> <listitem> diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 969cd9908e..0c0c725df5 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -41,6 +41,7 @@ * or a ! if session is not connected to a database; * in prompt2 -, *, ', or "; * in prompt3 nothing + * %T - time in HH24:MI:SS format * %x - transaction status: empty, *, !, ? (unknown or no connection) * %l - The line number inside the current statement, starting from 1. * %? - the error code of the last query (not yet implemented) @@ -223,7 +224,14 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) break; } break; - + /* output HH24:MI:SS */ + case 'T': + { + time_t current_time = time(NULL); + struct tm *tm_info = localtime(¤t_time); + sprintf(buf, "%02d:%02d:%02d", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec); + } + break; case 'x': if (!pset.db) buf[0] = '?'; -- 2.25.1