Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analysing/Profiling queries on PostgreSQL

I've just inherited an old PostgreSQL installation and need to do some diagnostics to find out why this database is running slow. On MS SQL you would use a tool such as Profiler to see what queries are running and then see how their execution plan looks like.

What tools, if any, exist for PostgreSQL that I can do this with? I would appreciate any help since I´m quite new with Postgres.

like image 646
Andy.l Avatar asked Mar 05 '15 14:03

Andy.l


People also ask

How do you analyze a query performance in PostgreSQL?

The ANALYZE option causes the statement to be actually executed, not only planned. The total elapsed time expended within each plan node (in milliseconds) and total number of rows it actually returned are added to the display. This is useful for seeing whether the planner's estimates are close to reality.

How do you analyze data in PostgreSQL?

With the help of ANALYZE command, you can get the stats of all the Postgres databases. For this, log in to your Postgres console and then type ANALYZE to execute the command. Upon successful execution of the command, it would return ANALYZE.

Does PostgreSQL have a Profiler?

Query Profiler functionality helps trace, recreate, and troubleshoot problems in PostgreSQL Server. With the PostgreSQL Profiler tool, you can quickly and easily identify productivity bottlenecks and thus boost your database performance.


1 Answers

My general approach is usually a mixture of approaches. This requires no extensions.

  1. set the log_min_duration_statement to catch long-running queries. https://dba.stackexchange.com/questions/62842/log-min-duration-statement-setting-is-ignored should get you started.

  2. Use profiling of client applications to see which queries they are spending their time on. Sometimes one has queries which take a small duration but are so frequently repeated to cause performance problems.

Of course then explain analyze can help. If you are looking inside plpgsql functions however, often you need to pull out the queries and run explain analyze on them directly.

Note: ALWAYS run explain analyze in a transaction that rolls back or a read-only transaction unless you know that it does not write to the database.

like image 52
Chris Travers Avatar answered Oct 17 '22 06:10

Chris Travers