18

I want to know the time that it takes to execute a query in Postgres. I see a lot of answers that suggest to use \timing, but I'm newbie in Postgres and I don't know how to use it.

1

1 Answer 1

27

You can use \timing only with the command line client psql, since this is a psql command.

It is a switch that turns execution time reporting on and off:

test=> \timing
Timing is on.
test=> SELECT 42;
┌──────────┐
│ ?column? │
├──────────┤
│       42 │
└──────────┘
(1 row)

Time: 0.745 ms
test=> \timing
Timing is off.
13
  • ok then if I want to test a select query how i can do that I tried to do what you have done, but instead of select 42I do select * from my_db and I get Commande \select invalide.
    – aName
    Nov 14, 2016 at 16:50
  • Try SELECT instead of \select, without the backslash. Nov 14, 2016 at 17:00
  • 2
    If you want to measure the time it takes to output the result to a file, you can use time on UNIX like this: time psql -P pager=off -c 'SELECT ...' >outfile Nov 15, 2016 at 7:27
  • 1
    That makes things harder. You can execute a batch file with the psql command line sandwiched between two echo %TIME%. Then a simple subtraction will tell how long it took. Nov 15, 2016 at 9:13
  • 1
    @TimurShtatland thanks a lot. But I switched to pgcli long back, which already provides timing along with many other features.
    – Krishna
    Apr 26, 2023 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.