24

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

36

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.
Sign up to request clarification or add additional context in comments.

13 Comments

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.
Try SELECT instead of \select, without the backslash.
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
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.
@TimurShtatland thanks a lot. But I switched to pgcli long back, which already provides timing along with many other features.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.