{
  "date": "2014-03-14T06:44:00",
  "slug": "investigating-oracle-lock-issues-with-event-10704",
  "link": "https://www.dbi-services.com/blog/investigating-oracle-lock-issues-with-event-10704/",
  "title": {
    "rendered": "Investigating Oracle lock issues with event 10704"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nDid you ever encounter unexplained Oracle lock issues? They may be coming from unindexed foreign keys (which is worse in 11g). It&#8217;s not easy to monitor. Of course you can check Oracle locks from V$LOCKED_OBJECT, but that is a solution only for locks that remain. Some Oracle locks are there only for a short duration. How do you check which lock is acquired by a statement?</p>\n<p>Event 10704 is the solution. I&#8217;ll show some examples in order to explain which lines from the trace are interresting, and how to interpret them.</p>\n<p>In this posting, I&#8217;ll illustrate how to use event 10704 to understand locking on referential integrity (the well know index on foreign key issue) in 12c.</p>\n<p>I set the trace (10704 level 3 for the locks, 10046 to see the statements):</p>\n<pre><code>SQL&gt; alter session set events='10046 trace name context forever, level 1 : 10704 trace name context forever, level 3' tracefile_identifier='ForeignKey';\nSession altered.</code></pre>\n<p>I delete from the DEPT table:</p>\n<pre><code>SQL&gt; DELETE FROM SCOTT.DEPT WHERE DEPTNO=0;\n0 rows deleted.</code></pre>\n<p>and then terminate the transaction:</p>\n<pre><code>SQL&gt; ROLLBACK;\nRollback complete.</code></pre>\n<p>and stop the trace:</p>\n<pre><code>SQL&gt; alter session set events='10046 trace name context off : 10704 trace name context off ';\nSession altered.</code></pre>\n<p>Now let&#8217;s get the trace file name:</p>\n<pre><code>SQL&gt; column tracefile new_value tracefile\nSQL&gt; select tracefile from v$process where addr=(select paddr from v$session where sid=sys_context('USERENV','SID'));\n\nTRACEFILE\n--------------------------------------------------------------------------------\n/u01/app/oracle/diag/rdbms/demo/DEMO/trace/DEMO_ora_28042_ForeignKey.trc</code></pre>\n<p>and grep the interresting lines from the trace file:</p>\n<pre><code>SQL&gt; host grep -A 1 -E 'ksqgtl [*]{3}|ksqrcl: [A-Z]|ksqcnv: [A-Z]|XCTEND|PARSE ERROR|PARSING' &amp;tracefile</code></pre>\n<p>The output is:</p>\n<pre><code>--\nPARSING IN CURSOR #140064855052048 len=37 dep=0 uid=0 oct=7 lid=0 tim=294484898588 hv=3450586461 ad='822782c8' sqlid='c1fnpd76urjax'\nDELETE FROM SCOTT.DEPT WHERE DEPTNO=0\n--\nksqgtl *** TM-00017EA8-00000000-00000000-00000000 mode=3 flags=0x401 timeout=21474836 ***\nksqgtl: xcb=0x8eb5bcf8, ktcdix=2147483647, topxcb=0x8eb5bcf8\n--\nksqgtl *** TM-00017EAA-00000000-00000000-00000000 mode=4 flags=0x401 timeout=21474836 ***\nksqgtl: xcb=0x8eb5bcf8, ktcdix=2147483647, topxcb=0x8eb5bcf8\n--\nksqrcl: TM-00017EAA-00000000-00000000-00000000\nksqcmi: TM-00017EAA-00000000-00000000-00000000 mode=0 timeout=0\n--\nPARSING IN CURSOR #140064855052048 len=8 dep=0 uid=0 oct=45 lid=0 tim=294484900906 hv=2761672982 ad='0' sqlid='8sst43uk9rk8q'\nROLLBACK\n--\nXCTEND rlbk=1, rd_only=1, tim=294484900964\nksqrcl: TM-00017EA8-00000000-00000000-00000000\nksqcmi: TM-00017EA8-00000000-00000000-00000000 mode=0 timeout=0\n--</code></pre>\n<p>In order to interpret it, we need one more information &#8211; the OBJECT_ID in hexadecimal:</p>\n<pre><code>SQL&gt; column object_name format a20\nSQL&gt; column object_type format a20\nSQL&gt; select object_id , to_char(object_id,'0XXXXXXX') , object_name,object_type from all_objects where owner='SCOTT' order by 2;\n\n OBJECT_ID  TO_CHAR(O OBJECT_NAME          OBJECT_TYPE\n---------- --------- -------------------- --------------------\n 97960      00017EA8  DEPT                 TABLE\n 97961      00017EA9  PK_DEPT              INDEX\n 97962      00017EAA  EMP                  TABLE\n 97963      00017EAB  PK_EMP               INDEX\n 97964      00017EAC  BONUS                TABLE\n 97965      00017EAD  SALGRADE             TABLE\n\n7 rows selected.</code></pre>\n<p>Great. Now let&#8217;s interpret that.<br />\nYou see the DELETE statement in the trace file (written by event 10046 which is the sql_trace). Then we have:</p>\n<div style=\"text-align: left\"><samp>  ksqgtl *** TM-00017EA8-00000000-00000000-00000000 mode=3</samp></div>\n<p>&#8230;which means we get lock (ksqgtl) on table (lock_type=TM) SCOTT.DEPT (object_id=17EA8) in mode Row-X (mode=3).<br />\nThis is expected as we have the intention to delete rows, thus we request Row-X lock on the table.<br />\nAnd next to it you see:</p>\n<div style=\"text-align: left\"><samp>  TM-00017EAA-00000000-00000000-00000000 mode=4</samp></div>\n<p>which is a table lock on SCOTT.EMP (object_id=17EAA) in Share mode (mode=4) &#8211; the child lock that we see since 8.1.7 when the foreign key is not indexed.<br />\nThat lock is released immediately (which is the behaviour since 9.2) after the delete:</p>\n<div style=\"text-align: left\"><samp style=\"color: #000000;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;line-height: normal;text-indent: 0px\">ksqrcl: TM-00017EAA-00000000-00000000-00000000</samp></div>\n<p>ksqrcl is the function that releases lock. That is done before the end of the transaction (XCTEND from sql_trace).<br />\nAnd the Row-X on DEPT is released once the transaction is ended:</p>\n<div style=\"text-align: left\"><samp style=\"color: #000000;font-style: normal;font-variant: normal;font-weight: normal;letter-spacing: normal;line-height: normal;text-indent: 0px\">ksqrcl: TM-00017EA8-00000000-00000000-00000000</samp></div>\n<p>If we create a index in order to avoid the Share mode lock:</p>\n<pre><code>SQL&gt; CREATE INDEX SCOTT.FK_DEPTNO on SCOTT.EMP(DEPTNO);\nIndex created.SQL&gt; DELETE FROM SCOTT.DEPT WHERE DEPTNO=0;\n0 rows deleted.\nSQL&gt; ROLLBACK;\nRollback complete.</code></pre>\n<p>&#8230;then here is what we get from the 10704 trace:</p>\n<p><samp>ksqgtl *** TM-00017EAA-00000000-00000000-00000000 mode=4 flags=0x401 timeout=0 ***</samp></p>\n<p>This is the Share mode lock on the table when creating the index.<br />\nThis is what a delete on the parent generates:</p>\n<pre><code>ksqgtl *** TM-00017EA8-00000000-00000000-00000000 mode=3 flags=0x401 timeout=21474836 ***\nksqgtl *** TM-00017EAA-00000000-00000000-00000000 mode=3 flags=0x401 timeout=21474836 ***\nXCTEND rlbk=1, rd_only=1, tim=294485532138\nksqrcl: TM-00017EAA-00000000-00000000-00000000\nksqrcl: TM-00017EA8-00000000-00000000-00000000</code></pre>\n<p>&#8230;which are only Row-X (mode=3) locks, but are released only at the end of the transaction.</p>\n<p>And besides requesting a lock (ksqgtl) and releasing a lock (ksqrcl), the third interresting function is when a lock is converted (ksqcnv) from one mode to another mode.</p>\n<p>Let&#8217;s see an exemple when we first delete rows from the child  (without the index on the foreign key):</p>\n<pre><code>SQL&gt; DELETE FROM SCOTT.EMP WHERE DEPTNO=0\nksqgtl *** TM-00017EA8-00000000-00000000-00000000 mode=2 flags=0x401 timeout=21474836 ***\nksqgtl *** TM-00017EAA-00000000-00000000-00000000 mode=3 flags=0x401 timeout=21474836 ***</code></pre>\n<p>DML on EMP (object_id=17EAA) requests a Row-X and because of the referential integrity it request also a Row-S on the opposite side DEPT (object_id=17EA8)</p>\n<p>Note that I did this example on 12c. That behaviour has changed in each Oracle version (and you can use event 10704 on your version in order to check on yours). Basically it was Row-S before 11g. Then 11g changed it to Row-X which introduced a lot of issues (see <a href=\"http://hoopercharles.wordpress.com/2010/01/07/deadlock-on-oracle-11g-but-not-on-10g/\" target=\"_blank\" rel=\"noopener noreferrer\">here</a> and <a href=\"http://jonathanlewis.wordpress.com/2010/02/15/lock-horror/\" target=\"_blank\" rel=\"noopener noreferrer\">here</a> for example). Fortunately 12c get it back to Row-S for two situations: insert into parent and delete from child.</p>\n<pre><code>SQL&gt; DELETE FROM SCOTT.DEPT WHERE DEPTNO=0\nksqcnv: TM-00017EA8-00000000-00000000-00000000 mode=3 timeout=21474836\nksqcnv: TM-00017EAA-00000000-00000000-00000000 mode=5 timeout=21474836\nksqcnv: TM-00017EAA-00000000-00000000-00000000 mode=3 timeout=21474836</code></pre>\n<p>Here we see the lock conversions. On DEPT (object_id=17EA8) we had a Row-S (mode=2) and now requesting a Row-X (mode=3) because of the DML on it.<br />\nAnd because of the unindexed foreign key we temporarily need a Share lock in addition to the Row-X we had. This is Share Row-X (mode=5). And it is converted back to Row-X as soon as the delete is done.</p>\n<p>So remember the following in order to interpret the 10704 trace dump:</p>\n<ul>\n<li>ksqgtl: lock request</li>\n<li>ksqcnv: lock conversion</li>\n<li>ksqrrcl: lock release</li>\n</ul>\n<p>For more reference, you can get the lock type description, such as TM , from V$LOCK_TYPE:</p>\n<pre><code>SQL&gt; select * from v$lock_type where type='TM';</code></pre>\n<table style=\"width: 90%\" border=\"0\" summary=\"Script output\" align=\"center\">\n<tbody>\n<tr>\n<th scope=\"col\"><samp>TYPE </samp></th>\n<th scope=\"col\"><samp>NAME</samp></th>\n<th scope=\"col\"><samp>ID1_TAG</samp></th>\n<th scope=\"col\"><samp>ID2_TAG</samp></th>\n<th scope=\"col\"><samp>IS_</samp></th>\n<th scope=\"col\"><samp>IS_</samp></th>\n<th scope=\"col\"><samp>DESCRIPTION</samp></th>\n<th scope=\"col\"><samp>CON_ID</samp></th>\n</tr>\n<tr>\n<td><samp>TM</samp></td>\n<td><samp>DML</samp></td>\n<td><samp>object #</samp></td>\n<td><samp>table/partition</samp></td>\n<td><samp>YES</samp></td>\n<td><samp>NO</samp></td>\n<td><samp>Synchronizes accesses to an object</samp></td>\n<td align=\"right\"><samp>0</samp></td>\n</tr>\n</tbody>\n</table>\n<p>And when ID1 is an object# then the first hexadecimal is the OBJECT_ID from DBA_OBJECTS.<br />\nAbout the lock modes, you have the intended row locks which have the goal to block concurrent DDL:</p>\n<p>mode=2 is Row-S, mode=3 is Row-X<br />\nand the table locks which have the goal to prevent concurrent row locks :<br />\nmode=4 is Share, mode=5 is Row-X + Share, mode=6 is eXclusive.</p>\n<p>You can also check the blocking matrix:</p>\n<p><a class=\"easyblog-thumb-preview\" title=\"CaptureLockMatrix.PNG\" href=\"http://prezi.com/cdckwsgqxeyi/oracle-table-lock-modes/\"><img decoding=\"async\" style=\"margin-left: auto;margin-right: auto\" title=\"CaptureLockMatrix.PNG\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CaptureLockMatrix.png\" alt=\"CaptureLockMatrix.PNG\" width=\"50%\" height=\"50%\" /></a></p>\n<p style=\"text-align: center\"><a href=\"http://prezi.com/cdckwsgqxeyi/oracle-table-lock-modes/\">http://prezi.com/cdckwsgqxeyi/oracle-table-lock-modes/</a></p>\n",
    "protected": false
  }
}
