Oracle之杀会话kill session的相关问题

0    320    2

Tags:

👉 本文共约7671个字,系统预计阅读时间或需29分钟。

前言部分

导读和注意事项

① killed状态的会话如何释放(如何找到后台进程)--重点?
② 授予普通用户可以kill自己用户session的权限
③ kill session和disconnect session的区别
④ v$session.CREATOR_ADDR列的使用
⑤ 对inactive会话的处理(1、sqlnet.ora文件中设置expire_time 参数 2、用户profile的idle_time 参数 3、找到很久没有响应的会话然后kill掉)
⑥ v$session.LAST_CALL_ET的使用
⑦ PMON的清理周期隐含参数"_PKT_PMON_INTERVAL"介绍

本文简介

今天同事杀会话的时候采用了alter system kill session ‘xxx,xxx’的方式,结果杀完后,v$session中还可以查到,就求助我,因为我之前杀会话都是带的immediate的,杀完后会立刻释放,v$session中也查询不到,同事现在的情况就只能杀后台进程了,但paddr列关联不到后台进程,查了下MOS还是给出了一些办法,整理了一下,分享给大家。

相关知识点扫盲

得到当前会话的几个SQL

Session 状态说明

Oracle session 有如下几种状态:

ACTIVE - Session currently executing SQL

INACTIVE

KILLED - Session marked to be killed

CACHED - Session temporarily cached for use by Oracle*XA

SNIPED - Session inactive, waiting on the client
有关状态的说明:
(1)active 处于此状态的会话,表示正在执行,处于活动状态。
官方文档说明:
Any session that is connected to the database and is waiting for an event that does not belong to the Idle wait class is considered as an active session.
(2)killed处于此状态的会话,被标注为删除,表示出现了错误,正在回滚。
当然,也是占用系统资源的。还有一点就是,killed的状态一般会持续较长时间,而且用windows下的工具pl/sql developer来kill掉,是不管用的,要用命令:alter system kill session 'sid,serial#' ;
(3)inactive 处于此状态的会话表示不是正在执行的
该状态处于等待操作(即等待需要执行的SQL语句),通常当DML语句已经完成。 但连接没有释放,这个可能是程序中没有释放,如果是使用中间件来连接的话,也可能是中间件的配置或者是bug 导致。inactive对数据库本身没有什么影响,但是如果程序没有及时commit,那么就会造成占用过多会话。容易是DB 的session 达到极限值。
一般不处理inactive 状态的session, 如果达到了session 的最大值, 就增加processes 和 sessions 参数。 对于Inactive 状态的session,可以设置过期时间:
(1)sqlnet.ora文件中设置expire_time 参数
(2)设置用户profile的idle_time 参数
(3)找到很久没有响应的会话然后kill掉
当设置了resource_limit=true 。通过idle_time限制session idle 时间。session idle超过设置时间,状态为sniped (v$session).,然而OS下的process并不会释放,当session(user process) 再次与server process 通讯,将关闭相应的server process.
sqlnet.expire_time 的原理不一样,Oracle Server 发送包探测dead connection ,如果连接关闭,或者不再用,则关闭相应的server process.
以上两者组合使用,减少server process,防止process超过init$ORACLE_SID极限值。
本文会对这3种方式分别说明的。

清理inactive会话的3种方式

设置sqlnet.expire_time

可以在sqlnet.ora文件里面加上sqlnet.expire_time这个参数来解决,设置一个分钟数,这是ORACLE建议的DCD解决方法。
在 sqlnet.ora文件中设置expire_time 参数
官网有关这个参数的说明:
http://download.oracle.com/docs/cd/B19306_01/network.102/b14213/sqlnet.htm
Oracle® Database Net Services Reference 11g Release 2 (11.2)E10835-10
SQLNET.EXPIRE_TIME
Purpose
Use parameter SQLNET.EXPIRE_TIME to specify a the time interval, in minutes, to send a probe to verify that client/server connections are active. Setting a value greater than 0 ensures that connections are not left open indefinitely, due to an abnormal client termination. If the probe finds a terminated connection, or a connection that is no longer in use, it returns an error, causing the server process to exit. This parameter is primarily intended for the database server, which typically handles multiple connections at any one time.
sqlnet.expire_time 的原理:Oracle Server 发送包探测dead connection ,如果连接关闭,或者不再用,则关闭相应的server process.
Limitations on using this terminated connection detection feature are:
(1)It is not allowed on bequeathed connections.
(2)Though very small, a probe packet generates additional traffic that may downgrade network performance.
(3)Depending on which operating system is in use, the server may need to perform additional processing to distinguish the connection probing event from other events that occur. This can also result in degraded network performance.
Default :0
Minimum Value :0
Recommended Value :10
Example
SQLNET.EXPIRE_TIME=10

设置用户profile的idle_time 参数
Oracle 用户 profile 属性
http://blog.csdn.net/tianlesoftware/archive/2011/03/10/6238279.aspx
注意,要启用idle_time 要先启用RESOURCE_LIMIT参数。 该参数默认是False。 官网说明如下:
RESOURCE_LIMIT
PropertyDescription
Parameter typeBoolean
Default valuefalse
ModifiableALTER SYSTEM
Range of valuestrue | false

RESOURCE_LIMIT determines whether resource limits are enforced in database profiles.
Values:
TRUE:Enables the enforcement of resource limits
FALSE:Disables the enforcement of resource limits

找到很久没有响应的会话然后kill掉

关于v$session中LAST_CALL_ET列的理解:http://blog.itpub.net/26736162/viewspace-1762403/
根据v$session中LAST_CALL_ET列的意义我们可以写出如下的SQL脚本,没一个小时清理10个小时没有响应的会话:

本人提供Oracle(OCP、OCM)、MySQL(OCP)、PostgreSQL(PGCA、PGCE、PGCM)等数据库的培训和考证业务,私聊QQ646634621或微信db_bao,谢谢!

Oracle之杀会话kill session的相关问题
运行日志:

Oracle之杀会话kill session的相关问题

oracle死连接(DC)和不活动会话(INACTIVE)

This note explains the difference between a dead connection and an INACTIVE session in v$session. It also discusses the mechanisms provided to automate the cleanup of each.
这里解释死连接和不活动会话的区别,也会讨论自动清除的机制。
Difference between INACTIVE sessions and Dead Connections

Dead connections and INACTIVE sessions are different issues. Oracle provides separate mechanisms to automate the cleanup of each.
死连接和不活动会话的不同问题,oracle提供了分离的机制去自动清理。
(1) Dead connections:死连接
These are previously valid connections with the database but the connection between the client and server processes has terminated abnormally.
有些之前合法的连接,但是由于客户端和服务器进程的异常中断。
Examples of a dead connection:

  • A user reboots/turns-off their machine without logging off or disconnecting from the database.
  • A network problem prevents communication between the client and the server.
    1.用户没有注销就关闭机器。
    2.网络在客户端和服务器端终端连接。
    In these cases, the shadow process running on the server and the session in the database may not terminate. To automate the cleanup of these sessions, you can use the Dead Connection Detection (DCD) feature of Net8.
    这种情况下,后台进程跑在服务器端,而会话在数据库端不会中断。
    When DCD is enabled, Net8 (server-side) sends a packet to the client. If the client is active, the packet is discarded. If the client has terminated, the server will receive an error and Net8 (server-side) will end that session.
    当死连接启动,NET8(服务端)会发送一个包到客户端。如果客户端是活动的,这个包就被丢掉。如果客户端已经被中断,服务器端将接收一个错误,将会中断该会话。SQLNET.EXPIRE_TIME.
    Refer to Note:151972.1: Dead Connection Detection (DCD) Explained, for details regarding DCD.
    (2) INACTIVE Sessions:
    These are sessions that remain connected to the database with a status in v$session of INACTIVE.
    会话与服务器端保持连接,但是状态为inactive.
    Example of an INACTIVE session:
  • A user starts a program/session, then leaves it running and idle for an extended period of time.
    用户开始一个会话,运行一段时间后,保持相当一段时间的空闲。
    To automate cleanup of INACTIVE sessions you can create a profile with an appropriate IDLE_TIME setting and assign that profile to the users.
    自动清理不活动的会话,你可以创一个profile.然后设置恰当的IDLE_TIME,分配给指定用户。
    Note:159978.1: How To Automate Disconnection of Idle Sessions, outlines the steps to setup IDLE_TIME for this.

    kill session相关内容

    kill session 是DBA经常碰到的事情之一。如果kill 掉了不该kill 的session,则具有破坏性,因此尽可能的避免这样的错误发生。同时也应当注意,如果kill 的session属于Oracle 后台进程,则容易导致数据库实例宕机。
    一、获得需要kill session的信息

    二、使用ALTER SYSTEM KILL SESSION 命令实现
    语法:
    SQL> ALTER SYSTEM KILL SESSION 'sid,serial#';
    SQL> ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
    对于RAC环境下的kill session ,需要搞清楚需要kill 的session 位于哪个节点,可以查询GV$SESSION视图获得。11g杀掉集群环境下的某个会话:
    alter system kill session'1228,42549,@实例号';
    例如:alter system kill session '1228, 42549, @2';
    10g下应登录到某个特定的实例才可以。
    Kill session 命令实际不会kill session,比如等待远程数据库的反应或者回滚事务,那么session 就不会立即kill其必须等待当前的操作结束才能执行,在这种情况下,session 就会被标记为killed 状态。
    我们可以在kill 命令中添加immediate,语法如下:
    SQL> ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
    这个命令不会影响性能,但它会立即返回到当前的session,处理kill操作,而不是等待其他的信息完成。 如果session 一直处于killed 状态,那么可以考虑在操作系统级别kill掉相关的进程。不过在操作之前,要先确认session 是否在执行rollback 操作。 可以使用如下SQL 来确认。

如果有我们的session,那么就要等rollback 先完成,然后才能在操作系统级别kill session。
kill session 的时候仅仅是将会话杀掉。在有些时候,由于较大的事务或需要运行较长的SQL语句将导致需要kill的session并不能立即杀掉。对于这种情况将收到 "marked for kill"提示(如下),一旦会话当前事务或操作完成,该会话被立即杀掉。

在下面的操作中将杀掉会话146,144

注意:在查询中可以看到被杀掉的会话的PADDR地址发生了变化,参照查询结果中的红色字体。如果多个session被kill 掉,则多个session的PADDR被改为相同的进程地址,被杀掉的会话的server列变为PSEUDO。

oracle killed会话不释放的问题

一般情况下,在杀一个会话的时候,直接执行alter system kill session ‘sid,serial#’;
Administrator's Guide说,当session是active的时候,alter system kill session 只是将session的状态标识为killed,server变为pseudo状态,并不会释放session持有的资源,所以我们在执行完alter system kill session 后,看会话还是一直存在。
这种情况下可以使用 immediate选项,强制立即Kill会话,如下:
SQL> alter system kill session '3964,51752' immediate;
SQL Language Reference(http://docs.oracle.com/cd/B28359_01/server.111/b28286/toc.htm#BEGIN
里对Immediate的解释是:IMMEDIATE Specify IMMEDIATE to instruct Oracle
Database to roll back ongoing transactions, release all session locks, recover the entire session state,and return control to you immediately.

killed状态的会话如何找到spid列

一般情况下我们查询会话的后台进程是通过如下的SQL,即通过v$session的paddr列关联v$process的addr列,但是killed状态的v$session的paddr列都变成了一样的,所以已经没有办法通过如下的SQL去查询了。

如果会话已经在v$sesion里是killed状态,那么我们通过上面的SQL已经查不出spid,可以用下面的SQL查出SPID:
-----方法1

-----方法2 我自己常用的方法

As a result of the bug, 2 additional columns have been added to V$SESSION from 11g on:
V$SESSION
CREATOR_ADDR - state object address of creating process
CREATOR_SERIAL# - serial number of creating process
CREATOR_ADDR is the column that can be joined with the ADDR column in V$PROCESS to uniquely identify the killed process corresponding to the former session.
Following the previous example, this would identify the killed session
-----方法3

-----方法4

-----方法4

为何killed状态的进程一直在v$session中能查询到?

[ID 100859.1]这篇文章中提到pmon如何清理killed的会话:
PMON will not delete the session object itself until the client connected to
that session notices that it has been killed. Therefore, the sequence of
events is:
1) alter system kill session is issued - the STATUS of the session object in
V$SESSION becomes KILLED, its server becomes PSEUDO.
2) PMON cleans up the resources allocated to the session
(i.e., rolls back its transaction, releases its locks, etc).
3) the entry in V$SESSION remains there until the client of that session (the
client is the process associated with the OSUSER,MACHINE,PROCESS columns in
the V$SESSION view) tries to do another request.
4) the client attempts another SQL statement and gets back ORA-28.
5) PMON can now remove the entry from V$SESSION.
This behavior is necessary because the client still has pointers to the
session object even though the session has been killed. Therefore, the
object cannot be deleted until the client is no longer pointing at it.
alter system kill session后,会话状态变为killed,pmon回收资源后,会话的信息仍然保留在v$session中,直到客户端再次请求,数据库返回ORA-28错误:"your session has been killed"。
此时pmon才从v$session中移除这些会话的信息。而上述中,客户端机器重启,无法再向数据库发送请求,因此从v$session中一直可以查到,只有通过操作系统强制杀掉进程,才能触发pmon从v$session中清除。
然后Oracle就等待PMON去清除这些Session.所以通常等待一个被标记为Killed的Session退出需要花费很长的时间.如果此时被Kill的process,重新尝试执行任务,那么马上会收到进程中断的提示,process退出,此时Oracle会立即启动PMON来清除该session.这被作为一次异常中断处理.

为何kill session后,paddr变成同一个值了?

[ID 387077.1]这篇文章中提到:
Bug 5453737 WHEN A SESSION IS KILLED, PADDR CHANGES IN V$SESSION BUT ADDR NOT IN V$PROCESS closed as not a bug with the following explanation:
When a session is killed, the session state object(and all the child state objects under the session state object) move out from under the original parent process state object, and are placed under the pseudo process state object (which is expected, given the parent/child process mechanism on Unix). PMON will clean up all the state objects found under the pseudo process state object. That explains why PADDR changes in V$SESSION when a session is killed. New PADDR you are seeing in v$SESSION is the address of the pseudo process state object. This shows up in system state under PSEUDO PROCESS for group DEFAULT: V$PROCESS still maintains the record of the original parent process. This is expected.
使用以下sql查找killed状态的spid:
select spid, program from v$process
where program!= 'PSEUDO'
and addr not in (select paddr from v$session)
and addr not in (select paddr from v$bgprocess)
and addr not in (select paddr from v$shared_server);
通过底层表x$ksupr关联v$session也可以,11.1.0.6版本及更高在v$session增加了2个字段CREATOR_ADDR和CREATOR_SERIAL#用于标记这种情况。然后就可以通过操作系统命令kill -9来杀掉这些进程了。

ALTER SYSTEM KILL SESSION 权限不足

需要赋权限,dba角色不用,grant alter system to lhr;
Oracle之杀会话kill session的相关问题

授予普通用户杀自己session的权限

若没有dba角色或者没有alter system权限的话,业务用户就不能自己杀自己会话了,这样一来,只能dba来介入了,其实这个功能我们可以通过如下的SQL脚本来完成。
------- user replace XXXXXX
------- 普通用户可以kill自己的session 授予应用用户杀自己session的权限

ALTER SYSTEM DISCONNECT SESSION 说明

Alter system disconnect session 是一个可选的kill session 的方法。 与kill session 命令不同,disconnect session 命令会kill 掉 dedicated server process, 该命令等同于在操作系统级别kill 掉server process。
具体语法如下:
SQL> ALTER SYSTEM DISCONNECT SESSION'sid,serial#' POST_TRANSACTION;
SQL> ALTER SYSTEM DISCONNECT SESSION'sid,serial#' IMMEDIATE;
POST_TRANSACTION 选项会等待事务完成之后在断开连接。
IMMEDIATE 选项会立即断开连接,然后事务会进行recover操作。
这2个选项也可以一起使用,但是必须指定其中一个,否则就会报错:
SQL> alter system disconnect session'30,7';
alter system disconnect session '30,7'
*
ERROR at line 1:
ORA-02000: missing POST_TRANSACTION orIMMEDIATE keyword
SQL>
SQL> alter system disconnect session'15,12' post_transaction immediate;
System altered.
使用alter system disconnectsession 命令就不需要切换到系统来kill session,也从而减少了kill 错进程的几率。

PMON 清理间隔

PMON进程负责处理异常结束进程相关资源的释放。PMON周期性地被唤醒,可以对"_PKT_PMON_INTERVAL"这个隐藏参数来进行修改,默认为50秒。也可以通过查找出进程的PID,然后在oradebug中,执行命令oradebug wakeup orapid(oracle进程的PID,不是OS的PID)来手动唤醒PMON进程。可以用alter session set events '100246 trace name conext forever,level 4'来查看PMON的相关操作。

MOS上的一些资料

于是metalink和google到以下一些资料:
Removing Sessions in Killed Status on Unix [ID 274216.1]
ALTER SYSTEM KILL Session Marked for Killed Forever [ID 1020720.102]
KILLING INACTIVE SESSIONS DOES NOT REMOVE SESSION ROW FROM V$SESSION [ID 1041427.6]
ALTER SYSTEM KILL SESSION does not Release Locks Killing a Thread on Windows NT [ID 100859.1]
http://blog.csdn.net/tianlesoftware/article/details/7417058
http://www.eygle.com/faq/Kill_Session.htm
---比较有用的
How To Find The Process Identifier (pid, spid) After The Corresponding Session Is Killed? [ID 387077.1]
HOW TO HAVE ORACLE CLEAN-UP OLD USER INFO AFTER KILLING SESSION UNDER MTS Note:1023442.6

实验部分

实验环境介绍

项目primary db
db 类型单实例
db version11.2.0.3.0
db 存储ASM
主机IP地址/hosts配置192.168.59.129
OS版本及kernel版本rhel 6.5

实验内容

实验序号实验内容
1设置用户profile的idle_time 参数
2kill session的时候加immediate和不加的区别
3授予普通用户kill自己用户的权限
4KILLED状态的会话如何找到相关的SPID

实验过程

设置用户profile的idle_time 参数

可以参考:
http://blog.csdn.net/leshami/article/details/9184917
http://blog.csdn.net/tianlesoftware/article/details/6238279
设置resource_limit 为true,该参数默认为false。

授予普通用户杀自己session的权限

创建普通用户lhrtest,授予resource和connect权限。

可以看到普通用户也可以杀掉自己用户的会话了。

kill session的时候加immediate和不加immediate的区别

kill session 的测试:

可以看到,杀掉的2个会话在v$session中都可以查到,只是①其status变为了KILLED,②server列变为了PSEUDO,③paddr列都变为了一样的了,因此这个时候如若还使用paddr列来关联process列必然查询不到spid号了,这个时候可以使用前边我们介绍的几种SQL来查询会话的spid,然后用kill -9杀掉会话。
我们用kill -9杀掉会话,等待后台PMON来自动清理进程,若是PMON很慢,我们可以手动来用oradebug wakeup 2来唤醒PMON进程,还可以设置PMON的清理间隔alter system set "_PKT_PMON_INTERVAL"=5;:

可以看到若加上immediate的话会话没有事务的情况下会立即释放,且v$session视图也被清理了。

Windows平台杀后端进程orakill

由于oracle在windows平台采用了单进程多线程的实现方式,unix/linux上的server process在windows上实际是一个thread。在unix平台上,有时使用alter system kill的方式杀死一个用户会话后,可能会标记为killed而不是立即释放该会话所占有的所有资源,或者由于某些原因,某些会话处于假死状态,这时可能要在os级别强行kill对应的process。但在windows上,进程管理器中只能看到一个oracle进程,而无法看到并且杀死具体的线程。这种情况下,我们当然可以借助第三方的线程管理工具来实现我们杀某个指定线程的目的,但实际上,oracle本身也是提供了这种的工具的。这就是orakill工具。

在windows上运行的oracle,在操作系统中只会有一个oracle.exe进程,如果在关库的时候alert日志中告警hang在某个进程上,此时可以通过orakill来杀除该会话。

当手工使用“alter system kill session ‘sid,serial#’ immediate;”命令杀数据库中的session后,会话信息可能还会驻留在数据库中。使用这个orakill命令便可在操作系统一级可以彻底清除之;

C:>orakill

Usage: orakill sid thread

where sid = the Oracle instance to target
thread = the thread id of the thread to kill

The thread id should be retrieved from the spid column of a query such as:

常用SQL

标签:

Avatar photo

小麦苗

学习或考证,均可联系麦老师,请加微信db_bao或QQ646634621

您可能还喜欢...

发表回复

嘿,我是小麦,需要帮助随时找我哦。
  • 18509239930
  • 个人微信

  • DB宝
  • 个人邮箱
  • 点击加入QQ群
  • 个人微店

  • 回到顶部