Logging slow queries

The log can also be used to track down individual slow queries. Back in the old days, this was pretty much the only way to spot performance problems.

How does it work? Basically, postgresql.conf has a variable called log_min_duration_statement. If this is set to a value greater than 0, every query exceeding our chosen setting will make it to the log:

# log_min_duration_statement = -1 

Most people see the slow query log as the ultimate source of wisdom. However, I would like to add a word of caution. There are many slow queries, and they just happen to eat up a lot of CPU: index creation, data exports, analytics, and so on.

These long-running queries are totally expected and are in many cases not the root of all evil. It is frequently the case that many shorter queries are to blame. Here is an example.

1,000 queries x 500 milliseconds is worse than 2 queries x 5 seconds. The slow query log can be misleading in some cases.

Still, it doesn't mean that it is pointless—it just means that it is a source of information and not the source of information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset