Mysql – Measuring SQL execution time in PostgreSQL

MySQLpostgresql

MySQL has a performance_schema database that allows one to capture SQL statement execution time data in a table (e.g. performance_schema.events_statements_history_long ; useful MySQL link).

I was wondering if a similar set of tools existed in PostgreSQL?

Best Answer

There isn't a reasonably easy way to do this, but you can:

  1. Log slow queries

  2. Use EXPLAIN ANALYZE to see where the query is actually spending its time.

You could probably store plans in the database somehow but I am not quite sure how this would be done (maybe with a pl/perl function?). You'd have to roll your own solution though.

Storing plans in the database however is not easy. One of the huge problems that you will have is getting the recursive structure right so you can query it. A simple approach might be to take a JSON format and store that, or store XML.