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? postgresql.conf has a variable called log_min_duration_statement. If this is set to a value greater than zero, 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.

Those long-running queries are totally expected and are in many cases not the root of all evil. It happens ...

Get Mastering PostgreSQL 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.