public inbox for pgsql-www@postgresql.org  
help / color / mirror / Atom feed
From: Andrew Morris <andrew.morris@lastyard.com>
To: pgsql-www@postgresql.org
Subject: Wiki editor request
Date: Wed, 6 Dec 2023 13:56:46 +0800
Message-ID: <8EACC548-F3FD-4CF0-AB6E-054D8D561E52@gridmon.com.au> (raw)

I would like editor access to the wiki, my username is amorris and I would like to modify https://wiki.postgresql.org/wiki/Lock_Monitoring

The existing query:
  SELECT blocked_locks.pid     AS blocked_pid,
         blocked_activity.usename  AS blocked_user,
         blocking_locks.pid     AS blocking_pid,
         blocking_activity.usename AS blocking_user,
         blocked_activity.query    AS blocked_statement,
         blocking_activity.query   AS current_statement_in_blocking_process
   FROM  pg_catalog.pg_locks         blocked_locks
    JOIN pg_catalog.pg_stat_activity blocked_activity  ON blocked_activity.pid = blocked_locks.pid
    JOIN pg_catalog.pg_locks         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
    JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
   WHERE NOT blocked_locks.granted;

does not work for cases where there are multiple transactions, it starts reporting additional bloced/blocking pairs that don't exist!

I have the following query that works for simple and complex cases with multiple transactions blocked by the same transaction:

SELECT
    blocker.pid AS blocker_id,
    blocker.datname AS blocker_db,
    blocker.application_name AS blocker_app,
    blocker.xact_start AS blocker_tx_start,
    blocker.query AS blocker_cur_query,
    --
    blocked.pid as blocked_pid,
    blocked.datname as blocked_db,
    blocked.application_name AS blocked_app,
    now() - blocked.query_start AS blocked_duration, 
    blocked.query as blocked_query
FROM pg_stat_activity AS blocked
JOIN pg_stat_activity AS blocker ON blocker.pid = ANY(pg_blocking_pids(blocked.pid))
order by blocked_duration desc;

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-www@postgresql.org
  Cc: andrew.morris@lastyard.com
  Subject: Re: Wiki editor request
  In-Reply-To: <8EACC548-F3FD-4CF0-AB6E-054D8D561E52@gridmon.com.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox