/**
 * Retuns the current status about blocked user and query information
 *
 * @author: "Stefanie Janine Stölting"<stefanie@proopensource.eu>
 * @license: PostgreSQL https://opensource.org/licenses/postgresql
 */

CREATE OR REPLACE VIEW monitoring_blocked_and_blocking_activity AS
SELECT blocked_locks.pid AS blocked_pid
	, blocking_locks.database AS database_name
	, blocked_activity.usename AS blocked_rolename
	, blocking_locks.pid AS blocking_pid
	, blocking_activity.usename AS blocking_rolename
	, blocked_activity.query AS blocked_statement
	, blocking_activity.query AS current_statement_in_blocking_procblocked_statementess
	, age(now(), blocked_activity.query_start) AS blocked_query_age
	, age(now(), blocking_activity.query_start) AS blocking_query_age
FROM pg_catalog.pg_locks AS blocked_locks
INNER JOIN pg_catalog.pg_stat_activity AS blocked_activity
	ON blocked_activity.pid = blocked_locks.pid
INNER JOIN pg_catalog.pg_locks AS blocking_locks
	ON blocking_locks.locktype = blocked_locks.locktype
	AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database
	AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
	AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
	AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
	AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
	AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
	AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
	AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
	AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
	AND blocking_locks.pid != blocked_locks.pid
INNER JOIN pg_catalog.pg_stat_activity AS blocking_activity
	ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.GRANTED
;

COMMENT ON VIEW monitoring_blocked_and_blocking_activity IS 'Retuns the current status about blocked user and query information.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocked_pid IS 'The connection identifier, that is blocked.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.database_name IS 'The database name in which the query has been executed.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocked_rolename IS 'The role/user who is blocked.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocking_pid IS 'The connection identifier, that is blocking.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocking_rolename IS 'The role/user who is blocking.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocked_statement IS 'This is the SQL source of the query that is blocked.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.current_statement_in_blocking_procblocked_statementess IS 'This is the SQL source of the query that is currently blocking.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocked_query_age IS 'That is the duration, that the query is blocked.';
COMMENT ON COLUMN monitoring_blocked_and_blocking_activity.blocking_query_age IS 'That is the duration, that the query is blocking.';
