{
  "date": "2016-11-24T20:53:29",
  "slug": "observing-dml-restarts-caused-by-invalidations",
  "link": "https://www.dbi-services.com/blog/observing-dml-restarts-caused-by-invalidations/",
  "title": {
    "rendered": "Observing DML restarts caused by invalidations"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nUsually, cursor invalidations do not interrupt the current execution of the cursor. You can even drop a table while a query on it is running and the query may end successfully. However some invalidations will throw internal errors that are catch and the cursor execution restarts transparently. I&#8217;ve reproduced one here to show the symptoms: it&#8217;s a different EXEC_SQL_ID so the restarts are not accounted within the same SQL Monitoring.<br />\n<!--more--><br />\nI had that question because I encountered a SQL Monitoring report where the &#8216;executions&#8217; count of the INSERT operation was more than one. I wondered if it can be a case of DML restart or if DML restart should be accounted in different EXEC_SQL_ID. I got the quick answer from a very reliable source:</p>\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\">\n<p lang=\"und\" dir=\"ltr\">Yep!</p>\n<p>&mdash; Stefan Koehler (@OracleSK) <a href=\"https://twitter.com/OracleSK/status/800693437876027392?ref_src=twsrc%5Etfw\">November 21, 2016</a></p></blockquote>\n<p><script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script></p>\n<p>However for my better understanding, I&#8217;m now reproducing a case of DML restart to show the symptoms on V$SQL and SQL Monitor.</p>\n<p>I need a table with some rows:</p>\n<pre><code>\nSQL&gt; create table DEMO as select rownum n from xmltable('1 to 3');\nTable created.\n</code></pre>\n<p>Initially my problem was with an insert into a GTT but now I&#8217;m reproducing the case with a partitioned table:</p>\n<pre><code>\nSQL&gt; create table DEMOGTT (n number) partition by hash(n);\nTable created.\n</code></pre>\n<p>For internal reasons, when a cursor is invalidated after it has acquired a lock on a partition, a DML restart occurs. If you have a doubt, &#8220;oerr ora 14403&#8221; explains that.</p>\n<p>The easiest way to invalidate while the cursor is running is to call a function that does it for each row. And as you will see that it can be restarted to infinity I set a timeout timestamp after 30 seconds.</p>\n<pre><code>\nSQL&gt; column limit new_value limit\nSQL&gt; select to_char(sysdate+30/24/60/60,'yyyymmddhh24miss') limit from dual;\n&nbsp;\nLIMIT\n--------------\n20161124212154\n&nbsp;\nSQL&gt; create function DEMOSLOW(n number) return number as\n  2    pragma autonomous_transaction;\n  3  begin\n  4   if sysdate &gt; to_date('&amp;limit','yyyymmddhh24miss')\n  5   then\n  6    dbms_stats.gather_table_stats(user,'DEMOGTT',no_invalidate=&gt;false);\n  7   end if;\n  8   return n;\n  9  end;\n 10  /\nold   4:  if sysdate &lt; to_date(&#039;&amp;limit&#039;,&#039;yyyymmddhh24miss&#039;)\nnew   4:  if sysdate &lt; to_date(&#039;20161124212154&#039;,&#039;yyyymmddhh24miss&#039;)\n&nbsp;\nFunction created.\n</code></pre>\n<p>Time to run the insert, calling the function for each row:</p>\n<pre><code>\nSQL&gt; alter session set statistics_level=all;\nSession altered.\n&nbsp;\nSQL&gt; set timing on time on\n21:21:24 SQL&gt; insert /*+ monitor */ into DEMOGTT select n from DEMO where n=DEMOSLOW(n);\n3 rows created.\n&nbsp;\nElapsed: 00:00:29.30\n21:21:54 SQL&gt; \n</code></pre>\n<p>This has taken 30 seconds. Without the timeout, it never stops.</p>\n<p>Execution plan for last run shows only one &#8216;Start&#8217;:</p>\n<pre><code>\n21:21:54 SQL&gt; select * from table(dbms_xplan.display_cursor('','','allstats last'));\n&nbsp;\nPLAN_TABLE_OUTPUT\n------------------------------------------------------------------------------------------------------------------------\nSQL_ID  3fk1hyq9vnuzx, child number 0\n-------------------------------------\ninsert /*+ monitor */ into DEMOGTT select n from DEMO where\nn=DEMOSLOW(n)\n&nbsp;\nPlan hash value: 4000794843\n&nbsp;\n----------------------------------------------------------------------------------------------\n| Id  | Operation                | Name    | Starts | E-Rows | A-Rows |   A-Time   | Buffers |\n----------------------------------------------------------------------------------------------\n|   0 | INSERT STATEMENT         |         |      1 |        |      0 |00:00:00.01 |     138 |\n|   1 |  LOAD TABLE CONVENTIONAL | DEMOGTT |      1 |        |      0 |00:00:00.01 |     138 |\n|*  2 |   TABLE ACCESS FULL      | DEMO    |      1 |      1 |      3 |00:00:00.01 |       9 |\n----------------------------------------------------------------------------------------------\n&nbsp;\nPredicate Information (identified by operation id):\n---------------------------------------------------\n&nbsp;\n   2 - filter(\"N\"=\"DEMOSLOW\"(\"N\"))\n</code></pre>\n<p>This is a proof that DML restarts are not accounted within the same execution. the &#8216;last&#8217; statistics are only from the last restart.</p>\n<p>V$SQL accounts only one execution but thousands of invalidations:</p>\n<pre><code>\n21:21:54 SQL&gt; select executions,invalidations from v$sql where sql_id='3fk1hyq9vnuzx';\n&nbsp;\nEXECUTIONS INVALIDATIONS\n---------- -------------\n         1          1571\n</code></pre>\n<p>ASH shows the different SQL_EXEC_ID:</p>\n<pre><code>\n21:21:54 SQL&gt; select sample_time,sql_id,sql_exec_id from v$active_session_history where sql_id='3fk1hyq9vnuzx' and sample_time&gt;sysdate-10/24/60/60 order by sample_time desc;\n&nbsp;\nSAMPLE_TIME                                                                 SQL_ID        SQL_EXEC_ID\n--------------------------------------------------------------------------- ------------- -----------\n24-NOV-16 09.21.53.773 PM                                                   3fk1hyq9vnuzx    16778774\n24-NOV-16 09.21.51.773 PM                                                   3fk1hyq9vnuzx\n24-NOV-16 09.21.48.773 PM                                                   3fk1hyq9vnuzx    16778501\n24-NOV-16 09.21.46.773 PM                                                   3fk1hyq9vnuzx    16778396\n24-NOV-16 09.21.45.773 PM                                                   3fk1hyq9vnuzx    16778341\n</code></pre>\n<p>and SQL Monitoring see different executions:</p>\n<pre><code>\n21:21:54 SQL&gt; select sql_id,sql_exec_id,status,last_refresh_time,plan_operation,starts from v$sql_plan_monitor where sql_id='3fk1hyq9vnuzx' and plan_line_id=0 order by last_refresh_time desc fetch first 10 rows only;\n&nbsp;\nSQL_ID        SQL_EXEC_ID STATUS              LAST_REFRESH_TI PLAN_OPERATION                     STARTS\n------------- ----------- ------------------- --------------- ------------------------------ ----------\n3fk1hyq9vnuzx    16778762 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778765 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778767 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778766 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778771 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778769 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778764 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778770 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778763 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n3fk1hyq9vnuzx    16778768 DONE                24-nov 21:21:53 INSERT STATEMENT                        1\n</code></pre>\n<p>So those are the symptoms of DML restart. And my initial problem is still there:</p>\n<ul>\n<li>This test case does not reproduce the issue on a GTT</li>\n<li>My issue on the GTT had only one SQL_EXEC_ID with multiple &#8216;starts&#8217; of the plan operations.</li>\n</ul>\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\"><a href=\"https://twitter.com/hashtag/sqlmon?src=hash&amp;ref_src=twsrc%5Etfw\">#sqlmon</a> <a href=\"https://twitter.com/hashtag/puzzled?src=hash&amp;ref_src=twsrc%5Etfw\">#puzzled</a><br />I expected to see &#39;1&#39; as &#39;executions for the first line of a plan,<br />Here I have executions with 1,12,19,64&#8230; ?!? <a href=\"https://t.co/yHPZXDprgX\">pic.twitter.com/yHPZXDprgX</a></p>\n<p>&mdash; Franck Pachot (@FranckPachot) <a href=\"https://twitter.com/FranckPachot/status/800678031908110336?ref_src=twsrc%5Etfw\">November 21, 2016</a></p></blockquote>\n<p><script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script></p>\n<p>Any comments greatly appreciated 🙂</p>\n",
    "protected": false
  }
}
