82

In the MySQL command line interface, when you execute a query it will tell you how long the query took to execute after printing out the results.

In the Postgres command line interface (psql) it does not tell you. I know how to configure the logging so I can get the information from logs, but it would be more convenient to have it print to standard output like it does in MySQL.

Can this be done?

4
  • 4
    It's pointless when many Google hits are answers saying "use Google" - about as useful as a chocolate teapot! Commented Jul 15, 2013 at 12:14
  • 1
    \timing --it's fun to talk Commented Jul 15, 2013 at 12:17
  • 2
    It's one way of a couple. But still, if you tried it and it doesn't work, describe it in your question. Commented Jul 15, 2013 at 12:19
  • 8
    @dragoste: I think a much better place to start finding information about a product is the manual rather than a search engine. Commented Jul 15, 2013 at 12:35

3 Answers 3

135

Use \timing as explained by "How can I time SQL-queries using psql?".

See also the manual for psql.

If you want server-side execution times that don't include the time to transfer the result to the client, you can set log_min_duration_statement = 0 in the configuration, then SET client_min_messages = log so you get the log info in the console.

You can also use EXPLAIN ANALYZE to get detailed execution timings. There's some timing overhead for this unless you use EXPLAIN (ANALYZE TRUE, TIMING FALSE), which is only in newer versions, and disables detailed timing to give only an aggregate execution time instead.

PgBadger, especially when combined with the auto_explain module, can provide useful aggregate statistics from log analysis.

Finally, there's pg_stat_statements, which can collect handy aggregate information on the running system.

Sign up to request clarification or add additional context in comments.

2 Comments

and how to use the \timing
@aName: Type \timing in psql console and hit enter. :) To disabe enter \timing again.
67

I think that EXPLAIN ANALYZE might be helpful to you

Syntax:

EXPLAIN ANALYZE query;

Example;

EXPLAIN ANALYZE 
SELECT  * 
FROM    demotable;

Output:

"Seq Scan on demotable  (cost=0.00..12.10 rows=210 width=356) 
                        (actual time=0.020..0.021 rows=8 loops=1)"
"Planning time: 18.477 ms"
"Execution time: 0.042 ms"

1 Comment

what if there are triggers, this won't work
8

You can use \timing in pgsql as follows to have the execution time printed to standard output:

psql -d db -c '\timing' -c 'select 1'

Taken from this comment.

Comments

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.