HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to get slow query on PostgreSQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlqueryslowgethow

Problem

Basically I want to log those query which is taking more than 300ms to execute. I don't want to log those query which is less than 300ms. But at PostgreSQL showing all type of log whether it is below 300ms or above 300ms.

I configured below parameter:

  • log_directory = 'pg_log'



  • log_filename = 'postgresql-%a.log'



  • log_statement = 'all'



  • logging_collector = on



  • log_min_duration_statement = 300



Why PostgreSQL log all type of statement, though I set parameter log_min_duration_statement=300?
It is confusing to me, Is there any better configuration to log only 300ms SQL statement?

Solution

log_statement being set to all is telling PostgreSQL to log the text of all statements. When used together with log_min_duration_statement, statement text is not repeated in logs, forcing manual correlations by process/session identifier for analysis. If you truly want log_statement set to all (you want the text of all statements to be logged), be sure to be logging the process and/or session identifier (see log_line_prefix and its %p and %c "escape sequences"). Otherwise, consider setting log_statement to none. Note that log_duration will also lead to logging for all statements.

On a loosely related note, consider checking out pg_stat_statements, a PostgreSQL-supplied module, function, and view that provides planning and execution statistics for executed queries. This module has proven itself very useful in the past. If interested, check out PostgreSQL: pg_stat_statements.

Context

StackExchange Database Administrators Q#314657, answer score: 2

Revisions (0)

No revisions yet.