Thomas' Tech Tips

Show query execution time in PostgreSQL

12 April 2023 - Thomas Damgaard

One of the features that can be incredibly helpful for optimizing your PostgreSQL database performance, is the \timing option in psql.

Enabling \timing will cause psql to display the amount of time it took for a statement to execute in milliseconds.

By default, the \timing option is disabled in psql. However, you can enable it at any time by entering \timing at the psql prompt.

Usage:

\timing [on|off]       toggle timing of commands (currently off)

Example:

finance=# \timing
Timing is on.
finance=# SELECT COUNT(*) FROM account;
 count
-------
 10257
(1 row)

Time: 36,114 ms
finance=#

The \timing option can be useful for benchmarking different approaches to a problem. For example, you could write two different SQL statements that accomplish the same task and measure the execution time of each to see which one is faster.

Filed under: howto, performance, postgresql, pqsl, sql, tips

Back to article list