database_sessions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| database_sessions [2019/02/16 13:53] – [Setting sessions and transactions parameters using processes] stuart | database_sessions [2021/03/18 21:11] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | * [[https:// | ||
| + | |||
| + | ==== List Users Created on the Database (including dba role grantee) ==== | ||
| + | < | ||
| + | CLEAR BREAKS | ||
| + | CLEAR COLUMNS | ||
| + | CLEAR COMPUTES | ||
| + | |||
| + | COL username HEA " | ||
| + | COL default_tablespace HEA " Default Tablespace " FOR a20; | ||
| + | COL temporary_tablespace HEA " | ||
| + | COL grole HEA " DBA " FOR a5; | ||
| + | |||
| + | SELECT | ||
| + | LPAD( DECODE( p.granted_role, | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | ( | ||
| + | | ||
| + | grantee, granted_role | ||
| + | FROM | ||
| + | dba_role_privs | ||
| + | WHERE | ||
| + | granted_role = ' | ||
| + | ) p | ||
| + | WHERE | ||
| + | | ||
| + | ORDER BY | ||
| + | | ||
| + | / | ||
| + | </ | ||
| + | ==== List logged on users with O/S info ==== | ||
| + | < | ||
| + | COL orauser HEA " | ||
| + | COL osuser HEA " O/S User " FOR a10 TRUNC | ||
| + | COL ssid HEA " | ||
| + | COL sserial HEA " | ||
| + | COL ospid HEA "O/S Pid" FOR a7 | ||
| + | COL slogon HEA " | ||
| + | COL sstat HEA " | ||
| + | COL auth HEA " | ||
| + | COL conn HEA " | ||
| + | |||
| + | SELECT | ||
| + | ' | ||
| + | ' | ||
| + | LPAD( s.sid, 4 ) ssid, LPAD( s.serial#, 6 ) sserial, | ||
| + | LPAD( p.spid, 6 ) ospid, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | ( | ||
| + | | ||
| + | DISTINCT sid, authentication_type | ||
| + | FROM | ||
| + | v$session_connect_info | ||
| + | ) sc | ||
| + | WHERE | ||
| + | | ||
| + | ORDER BY | ||
| + | | ||
| + | / | ||
| + | |||
| + | </ | ||
| + | ==== Check if Session Tracing is On ==== | ||
| + | < | ||
| + | set lines 180 | ||
| + | col module for a45 | ||
| + | col sql_trace_waits for a20 | ||
| + | col sql_trace_binds for a20 | ||
| + | col sql_trace for a20 | ||
| + | |||
| + | select username, | ||
| + | / | ||
| + | </ | ||
| + | ==== Enable Session Tracing ==== | ||
| + | < | ||
| + | EXEC DBMS_MONITOR.SESSION_TRACE_ENABLE(& | ||
| + | </ | ||
| + | ==== Disable Session Tracing ==== | ||
| + | </ | ||
| + | EXEC DBMS_MONITOR.SESSION_TRACE_DISABLE(& | ||
| + | </ | ||
| + | ==== Lists all Locked Objects ==== | ||
| + | < | ||
| + | SET PAGESIZE 60 | ||
| + | SET LINESIZE 300 | ||
| + | |||
| + | COLUMN owner FORMAT A20 | ||
| + | COLUMN username FORMAT A20 | ||
| + | COLUMN object_owner FORMAT A20 | ||
| + | COLUMN object_name FORMAT A30 | ||
| + | COLUMN locked_mode FORMAT A15 | ||
| + | |||
| + | SELECT b.session_id AS sid, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | 1, 'Null (NULL)', | ||
| + | 2, 'Row-S (SS)', | ||
| + | 3, 'Row-X (SX)', | ||
| + | 4, 'Share (S)', | ||
| + | 5, ' | ||
| + | 6, ' | ||
| + | | ||
| + | | ||
| + | FROM | ||
| + | | ||
| + | WHERE a.object_id = b.object_id | ||
| + | ORDER BY 1, 2, 3, 4 | ||
| + | / | ||
| + | </ | ||
| + | |||
| ==== Setting sessions and transactions parameters using processes ==== | ==== Setting sessions and transactions parameters using processes ==== | ||
| If building a new database,.. | If building a new database,.. | ||
| Line 20: | Line 138: | ||
| from dual; | from dual; | ||
| undef processes | undef processes | ||
| + | </ | ||
| + | Then again, you could just set processes and let the database work out what to set sessions and transactions to... | ||
| + | < | ||
| + | alter system reset sessions scope=spfile sid=' | ||
| + | alter system reset transactions scope=spfile sid=' | ||
| </ | </ | ||
| Line 180: | Line 303: | ||
| order by s.sid | order by s.sid | ||
| / | / | ||
| + | </ | ||
| + | |||
| + | ==== Drop a schema but drop its sessions first ==== | ||
| + | Sometimes dropping a user proves difficult because database processes owned by the user are still running. This script will kill all sessions owned by the schema before dropping it. | ||
| + | < | ||
| + | set serveroutput on | ||
| + | variable schema2drop varchar2(50); | ||
| + | execute : | ||
| + | declare | ||
| + | open_count integer; | ||
| + | begin | ||
| + | dbms_output.enable(null); | ||
| + | -- prevent any further connections | ||
| + | execute immediate 'alter user ' | ||
| + | |||
| + | --kill all sessions | ||
| + | for session in ( | ||
| + | select sid, serial# | ||
| + | from v$session | ||
| + | where username = ': | ||
| + | ) | ||
| + | loop | ||
| + | dbms_output.put_line (' | ||
| + | execute immediate 'alter system disconnect session ''' | ||
| + | end loop; | ||
| + | |||
| + | -- killing is done in the background, so we need to wait a bit | ||
| + | loop | ||
| + | select count(*) | ||
| + | into open_count | ||
| + | from v$session where username = ''' | ||
| + | exit when open_count = 0; | ||
| + | dbms_lock.sleep(0.5); | ||
| + | end loop; | ||
| + | |||
| + | -- finally, it is safe to issue the drop statement | ||
| + | dbms_output.put_line ('drop user ' | ||
| + | execute immediate 'drop user ' | ||
| + | end; | ||
| + | </ | ||
| + | or same thing in SQL*Plus | ||
| + | < | ||
| </ | </ | ||
| Line 257: | Line 422: | ||
| where 1=1 | where 1=1 | ||
| and a.sql_address = b.address; | and a.sql_address = b.address; | ||
| + | </ | ||
| + | |||
| + | ==== Incative sessions still holding lots of memory can probably be killed (after consulting the relevant user) ==== | ||
| + | < | ||
| + | SELECT | ||
| + | s.sid, | ||
| + | ,s.status | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | -- idle time | ||
| + | -- days separately | ||
| + | , | ||
| + | -- hours | ||
| + | substr(' | ||
| + | -- minutes | ||
| + | substr(' | ||
| + | --seconds | ||
| + | substr(' | ||
| + | FROM v$session s, v$process p | ||
| + | WHERE s.username IS NOT NULL | ||
| + | -- use outer join to show sniped sessions in | ||
| + | -- v$session that don't have an OS process | ||
| + | AND p.addr(+) = s.paddr | ||
| + | and s.sid=nvl(& | ||
| + | ORDER BY | ||
| + | idle_time desc; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Show SQL transaction usage ==== | ||
| + | For example after killing a session with alter system disconnect session immediate, it takes a long time to die, use this script to see how long it will take to disappear by checking how many undo blocks still remain occupied. | ||
| + | < | ||
| + | select vs.sid | ||
| + | , vs.username | ||
| + | , vt.xidusn | ||
| + | , vt.used_urec | ||
| + | , vt.used_ublk | ||
| + | from | ||
| + | , v$transaction vt | ||
| + | where vs.saddr = vt.ses_addr | ||
| + | / | ||
| + | </ | ||
| + | or a little more detailed... | ||
| + | < | ||
| + | select s.username username, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | , t.used_ublk | ||
| + | from | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | where s.paddr = p.addr | ||
| + | and s.taddr = t.addr (+) | ||
| + | and t.xidusn = r.usn (+) | ||
| + | and r.usn = n.usn (+) | ||
| + | and s.username=' | ||
| + | order by 1 | ||
| + | / | ||
| + | </ | ||
| + | produces | ||
| + | < | ||
| + | USERNAME | ||
| + | --------------- -------------------------------------------------- ---------------------- ---------- | ||
| + | SYS | ||
| + | SYS | ||
| + | </ | ||
| + | |||
| + | ==== Other useful tables to look at for blocking objects ==== | ||
| + | < | ||
| + | V$ACCESS | ||
| + | DBA_DDL_LOCKS | ||
| + | V$LOCKED_OBJECT | ||
| + | </ | ||
| + | ==== List Database Sessions including any blocking ==== | ||
| + | < | ||
| + | SET HEADING ON | ||
| + | SET LINESIZE 300 | ||
| + | SET PAGESIZE 60 | ||
| + | COLUMN username FORMAT A21 | ||
| + | COLUMN osuser FORMAT A8 | ||
| + | COLUMN sid FORMAT 9,999,999 | ||
| + | COLUMN serial# FORMAT 9,999,999 | ||
| + | COLUMN lockwait FORMAT A6 | ||
| + | COLUMN status FORMAT A8 | ||
| + | COLUMN module FORMAT A20 | ||
| + | COLUMN machine FORMAT A24 | ||
| + | COLUMN program FORMAT A20 | ||
| + | COLUMN logon_time FORMAT A20 | ||
| + | |||
| + | SELECT LPAD(' ', (level-1)*2, | ||
| + | s.osuser, | ||
| + | s.sid, | ||
| + | s.serial#, | ||
| + | s.lockwait, | ||
| + | s.status, | ||
| + | s.module, | ||
| + | s.machine, | ||
| + | s.program, | ||
| + | TO_CHAR(s.logon_Time,' | ||
| + | FROM | ||
| + | CONNECT BY PRIOR s.sid = s.blocking_session | ||
| + | START WITH s.blocking_session IS NULL | ||
| + | / | ||
| + | </ | ||
| + | ==== Blocking Locks ==== | ||
| + | * DBA_LOCKS | ||
| + | * DBA_DML_LOCKS | ||
| + | * DBA_DDL_LOCKS | ||
| + | * DBA_WAITERS | ||
| + | * DBA_BLOCKERS | ||
| + | |||
| + | Sessions blocking others | ||
| + | < | ||
| + | SET LINESIZE 300 | ||
| + | SET PAGESIZE 60 | ||
| + | COLUMN username FORMAT A15 | ||
| + | COLUMN osuser FORMAT A8 | ||
| + | COLUMN sid FORMAT 99999 | ||
| + | COLUMN serial# FORMAT 99999 | ||
| + | COLUMN process_id FORMAT A5 | ||
| + | COLUMN wait_class FORMAT A12 | ||
| + | COLUMN seconds_in_wait FORMAT 9999 | ||
| + | COLUMN state FORMAT A17 | ||
| + | COLUMN blocking_session 9999 | ||
| + | COLUMN blocking_session_state a10 | ||
| + | COLUMN module FORMAT a10 | ||
| + | COLUMN logon_time FORMAT A20 | ||
| + | |||
| + | SELECT | ||
| + | NVL(a.username, | ||
| + | a.osuser, | ||
| + | a.inst_id, | ||
| + | a.sid, | ||
| + | a.serial#, | ||
| + | a.sql_id, | ||
| + | d.spid AS process_id, | ||
| + | a.wait_class, | ||
| + | a.seconds_in_wait, | ||
| + | a.state, | ||
| + | a.blocking_instance, | ||
| + | a.blocking_session, | ||
| + | a.blocking_session_status, | ||
| + | a.module, | ||
| + | TO_CHAR(a.logon_Time,' | ||
| + | FROM | ||
| + | gv$session a, | ||
| + | gv$process d | ||
| + | WHERE | ||
| + | a.paddr | ||
| + | AND | ||
| + | | ||
| + | AND | ||
| + | a.status = ' | ||
| + | AND | ||
| + | a.blocking_session IS NOT NULL | ||
| + | ORDER BY 1,2 | ||
| + | / | ||
| + | </ | ||
| + | ==== Blocking locks (verbose) ==== | ||
| + | < | ||
| + | -- Shows blocked session, blocking session and details of the programs and machines they are running from. | ||
| + | -- In addition, shows the CTIME value which details how long the blocking session has had hold of the lock | ||
| + | SELECT | ||
| + | sysdate, | ||
| + | c.blocker_inst, | ||
| + | c.blocker_sid, | ||
| + | c.blocker_username, | ||
| + | c.blocker_sql_id, | ||
| + | c.blocker_program, | ||
| + | c.blocker_machine, | ||
| + | c.blocker_info, | ||
| + | c.blocker_action, | ||
| + | c.blocker_client, | ||
| + | c.blockee_inst, | ||
| + | c.blockee_sid, | ||
| + | c.blockee_username, | ||
| + | c.blockee_sql_id, | ||
| + | c.blockee_program, | ||
| + | c.blockee_machine, | ||
| + | c.blockee_action, | ||
| + | c.blockee_info, | ||
| + | c.blockee_client, | ||
| + | d.os_user_name locked_os_user_name, | ||
| + | d.oracle_username locked_username, | ||
| + | b.owner object_owner, | ||
| + | b.object_name, | ||
| + | b.object_type, | ||
| + | c.ctime "Time Locked (s)" | ||
| + | FROM | ||
| + | gv$locked_object d, | ||
| + | dba_objects b, | ||
| + | ( | ||
| + | SELECT | ||
| + | ( SELECT username FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_username, | ||
| + | ( SELECT sql_id FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_sql_id, | ||
| + | ( SELECT program FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_program, | ||
| + | ( SELECT machine FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_machine, | ||
| + | ( SELECT action FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_action, | ||
| + | ( SELECT client_info FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_info, | ||
| + | ( SELECT client_identifier FROM gv$session WHERE sid=y.sid AND inst_id=y.inst_id) blocker_client, | ||
| + | y.sid blocker_sid, | ||
| + | y.inst_id blocker_inst, | ||
| + | ( SELECT username FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_username, | ||
| + | ( SELECT sql_id FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_sql_id, | ||
| + | ( SELECT program FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_program, | ||
| + | ( SELECT machine FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_machine, | ||
| + | ( SELECT action FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_action, | ||
| + | ( SELECT client_info FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_info, | ||
| + | ( SELECT client_identifier FROM gv$session WHERE sid=z.sid AND inst_id=z.inst_id) blockee_client, | ||
| + | z.sid blockee_sid, | ||
| + | z.inst_id blockee_inst, | ||
| + | z.ctime | ||
| + | FROM | ||
| + | gv$lock y, | ||
| + | gv$lock z | ||
| + | WHERE | ||
| + | y.block = 1 AND | ||
| + | z.request > 0 AND | ||
| + | y.id1 = z.id1 AND | ||
| + | y.id2 = z.id2 | ||
| + | ) c | ||
| + | WHERE | ||
| + | | ||
| + | | ||
| + | | ||
| + | / | ||
| </ | </ | ||
| Line 271: | Line 684: | ||
| ==== Show blocking sessions ==== | ==== Show blocking sessions ==== | ||
| < | < | ||
| - | select s1.username || ' | + | select s1.username||' |
| - | | + | |
| - | s2.username || ' | + | from |
| - | | + | , v$session s1 |
| - | | + | , v$lock l2 |
| - | , | + | , v$session s2 |
| - | , | + | where 1=1 |
| - | , | + | and s1.sid |
| - | | + | and s2.sid |
| - | | + | and l1.block |
| - | | + | and l2.request > 0 |
| - | | + | and l1.id1 |
| - | | + | and l2.id2 |
| - | | + | / |
| - | | + | |
| </ | </ | ||
| Line 324: | Line 736: | ||
| ==== Blocking sessions in tree layout ==== | ==== Blocking sessions in tree layout ==== | ||
| - | from www.guyharrison.net | + | from www.guyharrison.net. this one seems to be the best! |
| < | < | ||
| set wrap off | set wrap off | ||
| Line 366: | Line 778: | ||
| , row_wait_obj# | , row_wait_obj# | ||
| , sql_id | , sql_id | ||
| - | , 'alter system disconnect session | + | , 'alter system disconnect session |
| from | from | ||
| ) | ) | ||
| Line 381: | Line 793: | ||
| start with blocking_session | start with blocking_session | ||
| / | / | ||
| + | </ | ||
| + | |||
| + | ==== Kill a session with a force timeout! ==== | ||
| + | Saw this flashing by in a database upgrade log. Maybe a useful hidden trick. | ||
| + | < | ||
| + | ALTER SYSTEM KILL SESSION ' | ||
| </ | </ | ||
| Line 452: | Line 870: | ||
| ==== Generate kill statement for ddl locking sessions ==== | ==== Generate kill statement for ddl locking sessions ==== | ||
| < | < | ||
| - | select 'alter system disconnect session | + | col killer for a65 |
| + | select 'alter system disconnect session | ||
| | | ||
| , | , | ||
| Line 459: | Line 878: | ||
| | | ||
| </ | </ | ||
| + | ==== Generate killer statements for a particular schema / user ==== | ||
| + | < | ||
| + | set lines 1000 pages 100 | ||
| + | col username for a15 | ||
| + | col status | ||
| + | select ses.username | ||
| + | , ses.status | ||
| + | , 'alter system disconnect session ''' | ||
| + | from | ||
| + | where 1=1 | ||
| + | and lower (ses.username) like lower(' | ||
| + | order by ses.status | ||
| + | / | ||
| + | </ | ||
| + | ==== Generate killer statements from PL/SQL for INACTIVE sessions ==== | ||
| + | < | ||
| + | begin | ||
| + | for i in (select * from v$session where status=' | ||
| + | LOOP | ||
| + | EXECUTE IMMEDIATE(q' | ||
| + | END LOOP; | ||
| + | end; | ||
| + | </ | ||
| ==== Display any long operations ==== | ==== Display any long operations ==== | ||
| < | < | ||
| Line 621: | Line 1062: | ||
| group by p.value; | group by p.value; | ||
| </ | </ | ||
| + | |||
database_sessions.1550325234.txt.gz · Last modified: 2019/02/16 13:53 by stuart
