Blocking statistics


Job details

Name: Blocking statistics
Platform: Postgres
Category: Performance
Performance
Description: Checks whether there exists any blocked sessions.
Long description: Checks whether there exists any blocked sessions. To find out what processes are holding or waiting for locks pg_stat_activity and pg_locks are cross-referenced.
Version: 1.0
Default schedule: 0,5,10,15,20,25,30,35,40,45,50,55 * * *
Requires engine install: Yes
Compatibility tag: .[type=‘instance’ & databasetype=‘postgres’]/.[hasengine=‘YES’ & is_awsrds = ‘0′ & newer_than_ninefour = 1]

Parameters

Name Default value Description
keep data for 60 The number of days to keep blocking statistics for.
warning threshold 300 The maximum number of seconds a session can be blocked before a warning is triggered.
alarm threshold 1800 The maximum number of seconds a session can be blocked before an alarm is triggered.

Job Summary

SELECT a.datname as database_name, a.query_start as query_start, bl.pid as blocked_pid, a.usename as blocked_user, kl.pid as blocking_pid, ka.usename as blocking_user, a.application_name as blocked_application, ka.application_name as blocking_application, a.query as blocked_statement, ka.query as blocking_statement, EXTRACT(EPOCH FROM (NOW() - a.query_start)) as waiting_duration FROM pg_catalog.pg_locks bl JOIN pg_catalog.pg_stat_activity a ON a.pid = bl.pid JOIN pg_catalog.pg_locks kl ON kl.transactionid = bl.transactionid AND kl.pid != bl.pid JOIN pg_catalog.pg_stat_activity ka ON ka.pid = kl.pid WHERE NOT bl.granted ORDER BY waiting_duration DESC;

Details and Implementation

Reporting Structure

Column Description
Waiting duration sec. Duration a session has been waiting
Database Database where the lock is occurring
Query start Start time of the query causing block
Blocked user User who owns the blocked session
Blocking user User who owns the blocking session
Blocked statement SQL statement of the blocked session
Last known blocking statement SQL statement of the blocking session

This monitoring job enhances overall database performance visibility, easing the management and troubleshooting processes associated with database blocks in a PostgreSQL environment.