ORACLE查看会话的大小及终止会话

一、出现PGA不足时,我们可以查看用户会话大小,结束相应会话

方法一

Select Server, Osuser, Name, Value / 1024 / 1024 Mb, s.Sql_Id, Spid, s.*
From V$session s, V$sesstat St, V$statname Sn, V$process p
Where St.Sid = s.Sid
And St.Statistic# = Sn.Statistic#
And Sn.Name Like 'session pga memory'
And p.Addr = s.Paddr
Order By Value Desc;

方法二

也可以通过下面语句模糊查询

select sid,serial#,username,status,osuser,machine,action from v$session where username like '%TEST%'

二、我们在停机时间停库或者drop用户时经常会遇到如下情况

SQL>drop user test

drop user test

*

ERROR at line 1:

ORA-01940:cannot drop a user that is currently connected

2.终止会话 kill session

alter system kill session 'sid,serial#';

3.终止会话 disconnect session

 用法:

alter system disconnect session 'sid,serial#' immediate ;(立即断开用户session,未完成的事务自动会滚。)

alter system disconnect session 'sid,serial#' post_transaction;(事务断开用户session,等待未完成的事务提交后(commit后),断开连接。)

sid(会话ID)和serial#(session序列号)位置填写查询出该用户对应的数值

区别:有的时候我们会遇到会话kill不掉,可以尝试disconnect session

SQL> alter system kill session '137,7818';

alter system kill session '137,7818'

*

ERROR at line 1:

ORA-00031: session marked for kill

SQL> select status,event from v$session where sid = 137;

STATUS   EVENT

-------- ----------------------------------------------------------------

KILLED   SQL*Net more data from dblink

 

SQL>  select object_id,locked_mode,session_id from v$locked_object;

OBJECT_ID LOCKED_MODE SESSION_ID

---------- ----------- ----------

       165           3        137

    104489           3        137

       212           3        137

 

SQL> select TYPE,LMODE,REQUEST,BLOCK from v$lock where sid=137;

TY      LMODE    REQUEST      BLOCK

-- ---------- ---------- ----------

JQ          6          0          0

JI          6          0          0

TM          3          0          0

TM          3          0          0

TM          3          0          0

TX          6          0          0

 

SQL> select t.status, s.status from v$transaction t, v$session s where s.taddr = t.addr and s.sid=137;

STATUS           STATUS

---------------- --------

ACTIVE           KILLED

 

该session已经被标志为killed,但是其对应的transaction依旧为active,且对应的lock没有被释放;

又因为该instance由其他OS用户启动,当前登录的用户没有权限执行kill -9

ora_10@justin_$ ps -ef | grep 15616

ora_xxx 15616     1  0   Jul 06 ?        0:22 ora_j001_GLIMSP

ora_10  20035 17648  0 08:23:18 pts/7    0:00 grep 15616

ora_10@justin_$ kill -9 15616

kill: 15616: permission denied

不是太清楚到底发生了什么事情,但此时可使用disconnect session,请参考以下解释

The KILL SESSION command doesn’t actually kill the session. It merely asks the session to kill itself. In some situations, like waiting for a reply from a remote database or rolling back transactions, the session will not kill itself immediately and will wait for the current operation to complete. In these cases the session will have a status of “marked for kill”. It will then be killed as soon as possible.

The ALTER SYSTEM DISCONNECT SESSION syntax as an alternative method for killing Oracle sessions. Unlike the KILL SESSION command which asks the session to kill itself, the DISCONNECT SESSION command kills the dedicated server process (or virtual circuit when using Shared Sever), which is equivalent to killing the server process from the operating system. The basic syntax is similar to the KILL SESSION command with the addition of the POST_TRANSACTION clause. The SID and SERIAL# values of the relevant session can be substituted into one of the following statements.

The POST_TRANSACTION clause waits for ongoing transactions to complete before disconnecting the session, while the IMMEDIATE clause disconnects the session and ongoing transactions are recovered immediately.

http://fatihacar.com/blog/show-and-kill-transaction-lock-in-oracle/

SQL> alter system disconnect session '137,7818' immediate;

 

System altered.

 

SQL> select serial#,status,event from v$session where sid=137;

 

   SERIAL# STATUS

---------- --------

EVENT

----------------------------------------------------------------

      7822 ACTIVE

jobq slave wait

 

 

SQL> alter system disconnect session '137,7822' immediate;

 

System altered.

 

SQL> select serial#,status,event from v$session where sid=137;

 

no rows selected

 

SQL> select object_id,locked_mode,session_id from v$locked_object;

 

 OBJECT_ID LOCKED_MODE SESSION_ID

---------- ----------- ----------

       165           3        132

    104489           3        132

       212           3        132

 

SQL> select serial#,event,status,sql_id from v$session where sid=132;

 

   SERIAL# EVENT

---------- ----------------------------------------------------------------

STATUS   SQL_ID

-------- -------------

     24231 jobq slave wait

ACTIVE

 

 

SQL> alter system disconnect session '132,24231' immediate;

 

System altered.

 

SQL> select object_id,locked_mode,session_id from v$locked_object;

 

no rows selected

 

--此时session被彻底清除,对应的lock

也已释放

原文地址:https://www.cnblogs.com/zhm1985/p/12843905.html