Sunday, 29 November 2020

NEW LOGMINER - Based on archivelog generation

Scenario Preview:

We have archive log of particular date, with the help of  logminer we can extract the value based on archive log .

In general we can extract object value based on archive log | Timestamp date |  SCN Number

Operating System: OEL 7.7 64 bit

Database Version : 12.2.0.1 

Database : SMRUPGR12QA

Schema : infodba

Log Miner table : PFND0GENERALAUDIT

SQL> @d

NAME                             INSTANCE_NAME    DB_UNIQUE_NAME                 OPEN_MODE            DATABASE_ROLE    CURRENT_SCN        FAILOVER_SCN

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

SMRUPGR1                         SMRUPGR12QA      SMRUPGR12QA                    READ WRITE           PRIMARY          NO                 0


Check UTL_FILE_DIR location below.

SQL> sho parameter utl_file_dir

NAME                                 TYPE        VALUE

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

utl_file_dir                         string

  • We need to take bounce of  database as parameter value is not set as dyanamics to set UTL_FILE_DIR  location.

SQL> alter system set utl_file_dir='/u04/db_backup/expdp/SMRUPGR12QA/logminer' scope=spfile;

System altered.

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup

ORACLE instance started.

Total System Global Area 2147483648 bytes

Fixed Size                  3712904 bytes

Variable Size             822085752 bytes

Database Buffers         1308622848 bytes

Redo Buffers               13062144 bytes

Database mounted.

Database opened.

SQL> @d

NAME                             INSTANCE_NAME    DB_UNIQUE_NAME                 OPEN_MODE            DATABASE_ROLE    CURRENT_SCN        FAILOVER_SCN

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

SMRUPGR1                         SMRUPGR12QA      SMRUPGR12QA                    READ WRITE           PRIMARY          NO                 0

SQL> sho parameter utl_file_dir

NAME                                 TYPE        VALUE

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

utl_file_dir                         string      /u04/db_backup/expdp/SMRUPGR12

                                                 QA/logminer

Create directory for storing logminer file location 

SQL> CREATE OR REPLACE DIRECTORY LOG_DIR AS '/u04/db_backup/expdp/SMRUPGR12QA/logminer';

Directory created.

Optional if we wants to exract from particular users

SQL> GRANT READ, WRITE ON DIRECTORY LOG_DIR TO infodba;

Grant succeeded.

SQL> select SUPPLEMENTAL_LOG_DATA_MIN from v$database;

SUPPLEME

--------

NO

SQL> ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;


Database altered.

  • Start log miner specifying name and location 

SQL> BEGIN

sys.DBMS_LOGMNR_D.build (

dictionary_filename => 'lgmnrdict.ora',

dictionary_location => 'LOG_DIR');

END;

/  2    3    4    5    6

PL/SQL procedure successfully completed.

SQL> SQL>

SQL> SQL> select member from v$logfile;

MEMBER
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/u01/app/oracle/oradata/SMRUPGR12QA/redo01.log
/u01/app/oracle/oradata/SMRUPGR12QA/redo02.log
/u01/app/oracle/oradata/SMRUPGR12QA/redo03.log
  • Using mention archive log  values to log. 
SQL> BEGIN
DBMS_LOGMNR.add_logfile (
options => DBMS_LOGMNR.new,
logfilename => '/u04/db_backup/Flash_recovery_area/SMRUPGR12QA/SMRUPGR12QA/archivelog/2020_11_19/o1_mf_1_737_hvcwzj71_.arc');

DBMS_LOGMNR.add_logfile (
  2    3    4    5    6    7  options => DBMS_LOGMNR.addfile,
logfilename => '/u04/db_backup/Flash_recovery_area/SMRUPGR12QA/SMRUPGR12QA/archivelog/2020_11_19/o1_mf_1_738_hvd82hth_.arc');

DBMS_LOGMNR.add_logfile (
  8    9   10   11  options => DBMS_LOGMNR.addfile,
 12  logfilename => '/u04/db_backup/Flash_recovery_area/SMRUPGR12QA/SMRUPGR12QA/archivelog/2020_11_19/o1_mf_1_739_hvfq7xpq_.arc');
 13
 14  END;
/  15

PL/SQL procedure successfully completed.

SQL> sho user
USER is "SYS"
SQL>
  • Start log mining location 
SQL> sho user
USER is "SYS"
SQL> BEGIN
-- Start using all logs
DBMS_LOGMNR.start_logmnr (
dictfilename => '/u04/db_backup/expdp/SMRUPGR12QA/logminer/lgmnrdict.ora');
END;
/   2    3    4    5    6

PL/SQL procedure successfully completed.

SQL>
  • Check the log mining contents.
SELECT seg_owner, seg_name, count(*) AS Hits
FROM   v$logmnr_contents
WHERE  seg_name NOT LIKE '%$'
GROUP BY seg_owner, seg_name;

  • Check Tablevalue operation information details

SELECT OPERATION, STATUS, SQL_REDO FROM V$LOGMNR_CONTENTS WHERE SEG_OWNER = 'INFODBA' AND TABLE_NAME = 'PFND0GENERALAUDIT';
  • End log miner execution

SQL> EXECUTE DBMS_LOGMNR.END_LOGMNR();

PL/SQL procedure successfully completed.

SQL>

Wednesday, 25 November 2020

PT - Row level lock contention TX blockers and waiters

 Handling 'enq: TX - row lock contention'

  • Client session 1 :  Recording current time and update customers row

SQL> SELECT TO_CHAR(SYSDATE, 'MM/DD/YY HH24:MI:SS') ctime FROM DUAL;


CTIME

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

11/24/20 20:41:27


SQL> var V_CUSTOMER_ID number

SQL> exec :V_CUSTOMER_ID := 100


PL/SQL procedure successfully completed.


SQL> UPDATE CUSTOMERS

  2  SET CUST_EMAIL = CUST_EMAIL || '' WHERE CUSTOMER_ID = :V_CUSTOMER_ID;


1 row updated.

  • client Session 2 :  Same update statement run on session.


SQL> UPDATE CUSTOMERS SET CUST_EMAIL = CUST_EMAIL || '' WHERE CUSTOMER_ID =100;

output 
C:\Users\varunyadav>sqlplus sys/system123@orcl2 as sysdba

SQL*Plus: Release 12.2.0.1.0 Production on Tue Nov 24 20:46:49 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.


Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

  • Query to check waiter and blocker session 

SQL> col SESSIONS format A20
SQL> SELECT DECODE(REQUEST,0,'Holder SID: ','Waiter SID: ') ||
  2  SID SESSIONS, ID1, ID2, LMODE, REQUEST, TYPE
  3  FROM V$LOCK
  4  WHERE (ID1, ID2, TYPE) IN (SELECT ID1, ID2, TYPE FROM V$LOCK WHERE REQUEST > 0)
  5  ORDER BY ID1, REQUEST;

SESSIONS                    ID1        ID2      LMODE    REQUEST TY
-------------------- ---------- ---------- ---------- ---------- --
Holder SID: 126          131085       1379          6          0 TX
Waiter SID: 867          131085       1379          0          6 TX

  • Information about waiter sessions details
SQL> set pagesize 20
SQL> col INFO for a200
SQL> SELECT
  2  'SID: '|| S.SID || CHR(10) ||
  3  'USERNAME: '|| S.USERNAME || CHR(10) ||
  4  'EVENT: ' || S.EVENT || CHR(10) ||
  5  'DESCRIPTION: ' || T.DESCRIPTION || CHR(10) ||
  6  'CURRENT STATEMENT: ' || Q.SQL_TEXT || CHR(10) ||
  7  'WAITING TIME (s): ' || S.SECONDS_IN_WAIT || CHR(10) ||
  8  'P1TEXT: ' || S.P1TEXT || CHR(10) ||
  9  'P1: ' || S.P1 || CHR(10) ||
 10  'P2TEXT: ' || S.P2TEXT || CHR(10) ||
 11  'P2: ' || S.P2 || CHR(10) ||
 12  'P3TEXT: ' || S.P3TEXT || CHR(10) ||
 13  'P3: ' || S.P3
 14  AS INFO
 15  FROM V$SESSION S, V$LOCK L, V$LOCK_TYPE T, V$SQL Q
 16  WHERE S.SID = L.SID AND T.TYPE=L.TYPE AND S.SQL_ID = Q.SQL_ID
 17  AND L.REQUEST>0;

INFO
--------------------------------------------------------------------------------
SID: 867
USERNAME: SOE
EVENT: enq: TX - row lock contention
DESCRIPTION: Lock held by a transaction to allow other transactions to wait for
it
CURRENT STATEMENT: UPDATE CUSTOMERS SET CUST_EMAIL = CUST_EMAIL || '' WHERE CUST
OMER_ID =100
WAITING TIME (s): 291
P1TEXT: name|mode
P1: 1415053318
P2TEXT: usn<<16 | slot
P2: 131085
P3TEXT: sequence
P3: 1379
  • Query to retrieve particular affected lock row

SQL> SELECT 'SELECT * FROM "' || O.OWNER || '"."' || O.OBJECT_NAME || '"
  2  WHERE ROWID = DBMS_ROWID.ROWID_CREATE(1, ' || S.ROW_WAIT_OBJ# || ', ' ||
  3  S.ROW_WAIT_FILE# || ', ' || ROW_WAIT_BLOCK# || ', ' ||
  4  ROW_WAIT_ROW# || ');'
  5  FROM DBA_OBJECTS O, V$SESSION S
  6  WHERE S.ROW_WAIT_OBJ# = O.OBJECT_ID AND S.SID = &V_WSID;
Enter value for v_wsid: 867
old   6: WHERE S.ROW_WAIT_OBJ# = O.OBJECT_ID AND S.SID = &V_WSID
new   6: WHERE S.ROW_WAIT_OBJ# = O.OBJECT_ID AND S.SID = 867

'SELECT*FROM"'||O.OWNER||'"."'||O.OBJECT_NAME||'"WHEREROWID=DBMS_ROWID.ROWID_CRE
--------------------------------------------------------------------------------
SELECT * FROM "SOE"."CUSTOMERS"
WHERE ROWID = DBMS_ROWID.ROWID_CREATE(1, 170508, 8, 4816, 38);


  • Rollback client session 1:

SQL>rollback;



SQL> SELECT DECODE(REQUEST,0,'Holder SID: ','Waiter SID: ') ||
  2  SID SESSIONS, ID1, ID2, LMODE, REQUEST, TYPE
  3  FROM V$LOCK
  4  WHERE (ID1, ID2, TYPE) IN (SELECT ID1, ID2, TYPE FROM V$LOCK WHERE REQUEST > 0)
  5  ORDER BY ID1, REQUEST;

no rows selected

SQL>

  • ASH report of particular locking periods: 


define dbid = '';
define inst_num = '';
define report_type = 'html';
define begin time
define begin_time ='-10'
define duration = 10;
define report_name = 'C:\Users\varunyadav\ash_enqtx.html';
define slot_width = '';
define target_session_id = '';
define target_sql_id = '';
define target_wait_class = '';
define target_service_hash = '';
define target_module_name = '';
define target_action_name = '';
define target_client_id = '';
define target_plsql_entry = '';
define target_container = '';
@?\rdbms\admin\ashrpti.sql


  • Query for enqueue wait events since last database start
SQL> col EVENT format a40
SQL> col WAIT_CLASS format a11
SQL> SELECT EVENT, AVERAGE_WAIT,
  2  TO_CHAR(ROUND(TIME_WAITED/100),'999,999,999') TIME_SECONDS, WAIT_CLASS
  3  FROM V$SYSTEM_EVENT
  4  WHERE EVENT LIKE 'enq%'
  5  ORDER BY TIME_WAITED;

EVENT                                    AVERAGE_WAIT TIME_SECONDS WAIT_CLASS
---------------------------------------- ------------ ------------ -----------
enq: SQ - contention                              .14            0 Configurati
                                                                   on

enq: PV - syncstart                              1.35            0 Other
enq: CF - contention                             5.34            0 Other
enq: WF - contention                            20.17            0 Other
enq: CR - block range reuse ckpt                  .21            1 Other
enq: RO - fast object reuse                       .29            3 Application
enq: PR - contention                            18.46            6 Other
enq: JG - queue lock                             5.76           12 Other
enq: TX - row lock contention               233833.06        7,015 Application

9 rows selected.

SQL>
  • Query to check object waited for lock to be released

SQL> col OBJECT_NAME for a10
SQL> col stats for a20
SQL> SELECT OBJECT_NAME, SUBSTR(STATISTIC_NAME, 1, 30) STATS, VALUE
  2  FROM V$SEGMENT_STATISTICS
  3  WHERE ( STATISTIC_NAME IN( 'ITL waits' , 'row lock waits' ))
  4  AND VALUE>0 AND OBJECT_NAME NOT LIKE 'BIN$%'
  5  ORDER BY VALUE DESC;

OBJECT_NAM STATS                     VALUE
---------- -------------------- ----------
EMP        row lock waits                2
CUSTOMERS  row lock waits                1

SQL>
  • Query for most waited wait events of application class
SQL> col SQL_TEXT for a30
SQL> SELECT ROUND(APPLICATION_WAIT_TIME / 1000000) WAIT_TIME_S,
  2  SQL_ID, SUBSTR(SQL_TEXT,1,30) SQL_TEXT
  3  FROM V$SQLSTATS
  4  WHERE APPLICATION_WAIT_TIME >0
  5  ORDER BY APPLICATION_WAIT_TIME DESC FETCH FIRST 10 ROW ONLY;

WAIT_TIME_S SQL_ID        SQL_TEXT
----------- ------------- ------------------------------
       5372 9b21yxhcn6r9d UPDATE EMP SET SALARY=SALARY W
        948 fwwngr2fq76ap  UPDATE EMP SET SALARY=SALARY
        695 1d0m79rpx5h4j UPDATE CUSTOMERS SET CUST_EMAI
          2 6mcpb06rctk0x call dbms_space.auto_space_adv
          1 b6usrg82hwsa3 call dbms_stats.gather_databas

SQL>

Use Case: Using Session Waiting Events with Hung or Very Slow Sessions

Use Case: Using Session Waiting Events with Hung or Very Slow Sessions 

Scenario Overview:

we have 2 client session 1 &2 . We will update same row on table in both session and do not commit after session 1execution . and try to update same query on session 2 .

Checking the sessions details of waiter and blocker of session.

 SESSION 1

C:\Users\varunyadav>sqlplus soe/soe@orcl2

SQL*Plus: Release 12.2.0.1.0 Production on Tue Nov 24 16:30:41 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Last Successful login time: Tue Nov 24 2020 16:30:13 +05:30

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL>  UPDATE EMP SET SALARY=SALARY WHERE EMP_NO=104;

1 row updated.

SQL>

 SESSION 2

C:\Users\varunyadav>sqlplus soe/soe@orcl2

SQL*Plus: Release 12.2.0.1.0 Production on Tue Nov 24 16:30:41 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Last Successful login time: Tue Nov 24 2020 16:30:13 +05:30

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL>  UPDATE EMP SET SALARY=SALARY WHERE EMP_NO=104;

( Session above stuck as session 1 is not commit there update)

  • Checking the idle or hung  session details using query

C:\Users\varunyadav>sqlplus soe/soe@orcl2

SQL*Plus: Release 12.2.0.1.0 Production on Tue Nov 24 16:35:10 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Last Successful login time: Tue Nov 24 2020 16:30:41 +05:30

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> SELECT SID, EVENT
  2  FROM V$SESSION
  3  WHERE STATE='WAITING' AND USERNAME ='SOE' AND WAIT_CLASS<>'Idle';

       SID EVENT
---------- ----------------------------------------------------------------
       126 enq: TX - row lock contention

  • Hung session information 

SQL> col SESSION_WAITS format a100
SQL> SELECT 'SID: '|| SID||
  2  CHR(10)||'USERNAME: '|| USERNAME||
  3  CHR(10)||'STATE: '|| STATE||
  4  CHR(10)||'EVENT: '|| EVENT||
  5  CHR(10)||'WAIT_TIME: '|| WAIT_TIME||
  6  CHR(10)||'SECONDS_IN_WAIT: '|| SECONDS_IN_WAIT||
  7  CHR(10)||'WAIT_CLASS: '|| WAIT_CLASS||
  8  CHR(10)||'P1TEXT: '|| P1TEXT||
  9  CHR(10)||'P1: '|| P1||
 10  CHR(10)||'P2TEXT: '|| P2TEXT||
 11  CHR(10)||'P2: '|| P2 ||
 12  CHR(10)||'P3TEXT: '|| P3TEXT||
 13  CHR(10)||'P3: ' || P3 AS SESSION_WAITS
 14  FROM V$SESSION
 15  WHERE USERNAME='SOE' AND EVENT LIKE 'enq: TX%'
 16  ORDER BY WAIT_TIME;

SESSION_WAITS
--------------------------------------------------------------------------------
SID: 126
USERNAME: SOE
STATE: WAITING
EVENT: enq: TX - row lock contention
WAIT_TIME: 0
SECONDS_IN_WAIT: 280
WAIT_CLASS: Application
P1TEXT: name|mode
P1: 1415053318
P2TEXT: usn<<16 | slot
P2: 524304

SESSION_WAITS
--------------------------------------------------------------------------------
P3TEXT: sequence
P3: 1381


SQL> col EVENT for a30
SQL> SELECT E.EVENT,
  2  TO_CHAR(ROUND(E.TIME_WAITED/100),'999,999,999') TIME_SECONDS
  3  FROM V$SESSION_EVENT E, V$SESSION S
  4  WHERE E.SID=S.SID AND S.USERNAME='SOE' AND E.EVENT LIKE 'enq: TX%'
  5  ORDER BY TIME_WAITED;

EVENT                          TIME_SECONDS
------------------------------ ------------
enq: TX - row lock contention           307

  • Retrieve session history details

SQL> SELECT COUNT(*) FROM V$SESSION_WAIT_HISTORY
  2  WHERE EVENT LIKE 'enq: TX%';

  COUNT(*)
----------
         0

SQL>

<16 --------------------------------------------------------------------------------="" 1381="" 524304="" p2:="" p3:="" p3text:="" sequence="" session_waits="" slot="" sql="">ROLLBACK

<16 --------------------------------------------------------------------------------="" 1381="" 524304="" p2:="" p3:="" p3text:="" sequence="" session_waits="" slot="" sql="">Session 1
SQL> UPDATE EMP SET SALARY=SALARY WHERE EMP_NO=104; 
 1 row updated. 

 SQL> rollback; 
 Rollback complete. SQL>

<16 --------------------------------------------------------------------------------="" 1381="" 524304="" p2:="" p3:="" p3text:="" sequence="" session_waits="" slot="" sql="">Session 2
SQL> UPDATE EMP SET SALARY=SALARY WHERE EMP_NO=104; 
 1 row updated. 

 SQL> rollback; 
 Rollback complete. 

  • Wait event of sessions of schema
SQL>
SQL> col EVENT format a40
SQL> col WAIT_CLASS format a10
SQL> SELECT SID, EVENT, WAIT_CLASS
  2  FROM V$SESSION
  3  WHERE USERNAME='SOE';

       SID EVENT                                    WAIT_CLASS
---------- ---------------------------------------- ----------
        10 SQL*Net message to client                Network
       126 SQL*Net message from client              Idle
       867 SQL*Net message from client              Idle

SQL>