Skip to content
Keep Learning Keep Living
Keep Learning Keep Living
Keep Learning Keep Living

Hierarchical Blocking Session List

Mustafa, 2021-10-19

Hi,

Today, I needed to list all blocking sessions hierarchically. As you know, all information about sessions (and blocking sessions) are in gv$session (or just v$session if you are using single instance) view. I wanted to see who is blocking who and blocker at the top. you can write simple connect by (or with clause) query but it will list blockers at the bottom so I just developed it a little bit. if I remember correctly there were a small script under rdbms/admin but it was creating some tables etc etc. I just want to get it using a simple sql statements.

here is the sql:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
with blockers as (
    select /*+ materialize */ blocking_session from gv$session where blocking_session is not null
  ),
  blocker_hierarchy as (
    select /*+ materialize */ sid, blocking_session, lpad(sid, 10 + level*2, ' '), CONNECT_BY_ISLEAF leaf
    from   gv$session
    connect by nocycle prior sid = blocking_session and prior inst_id = inst_id
    start with sid in (select blocking_session from blockers)
  ),
  Main_Blockers as (
    select username, sid, blocking_session,
           lpad(' ', (level-1)*2, ' ') ||Decode(CONNECT_BY_ROOT sid, sid, 'BLOCKER', 'BLOCKED') type,
           'SID: ' || lpad('>', (level-1)*3, '-') || rpad(to_char(sid), 6, ' ') ||
           'User:' || username ||
           '  Current SQL: ' ||(Select substr(Sql_Text, 1, 40) from v$sqlarea where sql_id = s.sql_id) Blocking_info
    from   gv$session s
    connect by nocycle prior sid = blocking_session and prior inst_id = inst_id
    start with sid in (select sid from blocker_hierarchy
                       where  leaf = 0
                         and  blocking_session is null
                         and  type <> 'BACKGROUND')
    order siblings by sid)
select * from Main_Blockers;

first, I get all blockers’ sids (blockers data source in the query) then I use them to generate the hierarchy (blocker_hierarcy data source in the query) then I remove some blockers from the hierarchy because they are also blocked by some other sessions and will be listed under them.

for example, let’s say sid 10 is blocking 2 different sessions (sid 20 and 30) and also sid 30 is blocking some other session (sid 40). so this query will produce a result like this:

1
2
3
4
SID: 10  User:MUSTAFA  Current SQL:
SID: -->20  User:DAVID  Current SQL: update t set d = 3 where d = 2
SID: -->30  User:STEPHAN  Current SQL: update t set d = 1 where d = 2
SID: ----->40    User:JOOST  Current SQL: update t set d = 1 where d = 3

you can see the hierarch in a visual way and the first 40 character of their currently running sql statements.

Hope this will be useful for you.

wish you healthy days.

 

11g 12c 18c 19c 21c Administration SQL / PLSQL Useful Scripts blocked session listblockersblocking sessionhierarchical blocking session listhierarchical session listsession blockers

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Sessions with details
  • DBMS_PIPE Connecting the Sessions
  • DBCA Templates and Dangerous “General Purpose” Template
  • NON-CDB to PDB Migration Horror
  • 23c is Finally Here!
  • Not Deploying is also Important!
  • Damaged rowid is referenced! Health Monitor
  • Old Obsolete Backup is not Deleted

Recent Comments

  • Mustafa on DBMS_PIPE Connecting the Sessions
  • Hemant K Chitale on DBMS_PIPE Connecting the Sessions
  • Mustafa on DBMS_PIPE Connecting the Sessions
  • Hemant K Chitale on DBMS_PIPE Connecting the Sessions
  • Phong on ORA-00942: table or view does not exist and sequences as default value

Categories

  • 11g
  • 12c
  • 18c
  • 19c
  • 21c
  • 23c
  • Administration
  • Compression
  • Development
  • Materialized View
  • Multi-tenant
  • Performance
  • Security
  • SQL / PLSQL
  • Uncategorized
  • Undocumented
  • Useful Scripts

Archives

  • September 2023
  • August 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • October 2021
  • September 2021
  • August 2021
  • April 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • July 2019
  • June 2019
  • May 2019
  • March 2019
  • February 2019
  • June 2018

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

RSS Follow This Blog

  • Sessions with details
  • DBMS_PIPE Connecting the Sessions
  • DBCA Templates and Dangerous “General Purpose” Template
  • NON-CDB to PDB Migration Horror
  • 23c is Finally Here!
  • Not Deploying is also Important!
  • Damaged rowid is referenced! Health Monitor
  • Old Obsolete Backup is not Deleted
  • Autoupgrade doesn’t Upgrade Timezone Version While RU Patching
  • Unique Columns For V$Sql

Archives

  • September 2023
  • August 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • October 2021
  • September 2021
  • August 2021
  • April 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • July 2019
  • June 2019
  • May 2019
  • March 2019
  • February 2019
  • June 2018

RSS Follow This Blog

  • Sessions with details
  • DBMS_PIPE Connecting the Sessions
  • DBCA Templates and Dangerous “General Purpose” Template
  • NON-CDB to PDB Migration Horror
  • 23c is Finally Here!
  • Not Deploying is also Important!
  • Damaged rowid is referenced! Health Monitor
  • Old Obsolete Backup is not Deleted
  • Autoupgrade doesn’t Upgrade Timezone Version While RU Patching
  • Unique Columns For V$Sql
RSS Error: A feed could not be found at `http://www.mywebsite.com/feed/`; the status code is `200` and content-type is `text/html; charset="utf-8"`
©2023 Keep Learning Keep Living | WordPress Theme by SuperbThemes