psql: Get the time it took a query to complete

1 min read

If you need to known the amount of time it took a query to complete on PostgreSQL, you can use the \timing function that psql has

It's usage it's straigforward: we can set it on and off. So we just need to set it on before running the query we are interested on. For example:

sonar=> \timing on
Timing is on.
sonar=> reindex database sonar;
REINDEX
Time: 4386577.151 ms
sonar=> 

The time is expressed in milliseconds. On this example 4386577.151 ms it would be approximately an hour and 13 minutes.

Once we no longer need to calculate the amount of time a query takes we can just turn it off:

sonar=> \timing off
Timing is off.

Posted on 31/05/2021

Categories