June 11th, 2008 by depesz | Tags: , , , | 7 comments »
Did it help? If yes - maybe you can help me? Donate BTC to 19zPa5diT2LZqGtTi8f8bfApLn8rw9zBHx

Yes, finally! David Fetter wrote, and Heikki Linnakangas committed patch which adds on/off arguments to \timing:

Before you could simply do:

\timing

to switch timing information:

# select 1;
?column?
----------
1
(1 row)
# \timing
Timing is on.
# select 1;
?column?
----------
1
(1 row)
Time: 0.364 ms
# \timing
Timing is off.
# select 1;
?column?
----------
1
(1 row)
#

The problem with this approach is that if you use .psqlrc to set your preferred psql environment, you might happen to put “\timing" there.

It means – that from start, all queries show they timing information.

And now, let's consider a simple case, when I have a simple .sql file, which contains some selects, but I wanted psql to print timings every time I ran it.

So, I wrote the file as:

\timing
SELECT 1;
SELECT 2;
\timing

Usually it would output:

=> psql -f z.sql
Timing is on.
?column?
----------
1
(1 row)
Time: 0.795 ms
?column?
----------
2
(1 row)
Time: 0.342 ms
Timing is off.

Which is pretty cool.

But if you did turn on timing in your .psqlrc, output will be different:

=> psql -f z.sql
Timing is off.
?column?
----------
1
(1 row)
?column?
----------
2
(1 row)
Timing is on.

Effectively I was “punished" by being user which cares about making my work environment most productive.

Luckily, David's patch, changes this. Now I can simply change the sql file to:

\timing on
SELECT 1;
SELECT 2;
\timing off

And get desired results even if my .psqlrc tells psql to initially turn timings on. Great. Thanks David.

  1. 7 comments

  2. Jun 12, 2008

    Happy to help :)

  3. # Vincenzo Romano
    Jun 12, 2008

    How many of those interesting not-so-complex patches will be backported to 8.3?

  4. Jun 12, 2008

    @Vincenzo Romano:
    according to my knowledge – 0.

    8.3 line is “closed” in terms of features. Only bugfixes are allowed.

  5. Jun 13, 2008

    But if i have /timing turnned on in .psqlrc file and i run your four lines from the last example timings are now turrned off — and it’s not what I want.

    What about something that turns it on (regardless if it’s on or off) just for the next query? Something like

    > \timing single
    Timing is on.
    > SELECT …
    Timing is back to whatever it was before.

    I don’t realy like the “single” keyword but anyone with a better suggestion is welcome.

  6. Jun 14, 2008

    Denis, the patch lets you set \timing to your desired setting unconditionally, which is what you want, if I’ve understood correctly.

  7. Jun 15, 2008

    Nope, I want it to turn of the timing for next statement and don’t set it off (or leave it on) on the end of the script.

    Maybe it would be a better idea if you get a fresh “session” (all settings read from your config files are made default for your session) each time you enter the psql and you create another (sub)session each time you run something with \i. On the end of this (sub)session every changed setting would be restored to the default state/value of the “parent” session.

    This could be a solution that wouldn’t break my config even if the .sql file i run change the env completely.

  8. # Adeline Boronat
    Apr 27, 2009

    good goodthis post deserves nothing :( hahaha just joking :P nice post :P

Leave a comment