{
  "date": "2015-12-27T20:42:01",
  "slug": "phantom-session-environment-in-multitenant",
  "link": "https://www.dbi-services.com/blog/phantom-session-environment-in-multitenant/",
  "title": {
    "rendered": "Phantom session environment in multitenant?"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nIn multitenant a session can switch from one container to another. There are a few restrictions such as a transaction cannot span multiple containers. But how do sessions parameters behave? It seems that there is a small inconsistency.<br />\n<!--more--><br />\nI was looking at some adaptive plan and SPM behavior (see this tweeter <a href=\"https://twitter.com/MarisElsins/status/677990324308992000\" target=\"_blank\" rel=\"noopener noreferrer\">conversation</a>) and played with the optimizer environment to reproduce the plan I wanted, and then came to a strange behavior. I can understand that a session keep its parameter settings when switching to another container. But not when closing and re-opening the pluggable database. The default values should come then.</p>\n<p>Here is an example.</p>\n<h3>Change in memory value of optimizer parameter</h3>\n<p>This is in 12.1.0.2 latest PSU. I connect to PDB</p>\n<pre><code>\n19:13:10 SQL&gt; alter session set container=PDB;\nSession altered.\n</code></pre>\n<p>and check parameter optimizer_index_cost_adj from memory:</p>\n<pre><code>\n19:13:10 SQL&gt; show parameter optimizer_index_cost_adj;\n&nbsp;\nNAME                                 TYPE        VALUE\n------------------------------------ ----------- ------------------------------\noptimizer_index_cost_adj             integer     100\n</code></pre>\n<p>It&#8217;s the default.</p>\n<p>then I set it to one in memory only:</p>\n<pre><code>\n19:37:17 SQL&gt; alter system set optimizer_index_cost_adj=1 scope=memory;\nSystem altered.\n</code></pre>\n<p>And I run a query and check that the optimizer used that value</p>\n<pre><code>\n19:37:18 SQL&gt; select * from dual;\n&nbsp;\nD\n-\nX\n&nbsp;\n19:37:19 SQL&gt; select * from table(dbms_xplan.display_cursor(format=&gt;'+outline'));\n&nbsp;\nPLAN_TABLE_OUTPUT\n--------------------------------------------------------------------------\nSQL_ID  a5ks9fhw2v9s1, child number 0\n-------------------------------------\nselect * from dual\n&nbsp;\nPlan hash value: 272002086\n&nbsp;\n--------------------------------------------------------------------------\n| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |\n--------------------------------------------------------------------------\n|   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |\n|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |\n--------------------------------------------------------------------------\n&nbsp;\nOutline Data\n-------------\n&nbsp;\n  /*+\n      BEGIN_OUTLINE_DATA\n      IGNORE_OPTIM_EMBEDDED_HINTS\n      OPTIMIZER_FEATURES_ENABLE('12.1.0.2')\n      DB_VERSION('12.1.0.2')\n      OPT_PARAM('optimizer_index_cost_adj' 1)\n      ALL_ROWS\n      OUTLINE_LEAF(@\"SEL$1\")\n      FULL(@\"SEL$1\" \"DUAL\"@\"SEL$1\")\n      END_OUTLINE_DATA\n  */\n&nbsp;\n</code></pre>\n<p>Si this is expected, my custom value has been used to optimize the cursor.</p>\n<h3>close and re-open the PDB</h3>\n<p>I close and restart my PDB:</p>\n<pre><code>\n19:37:19 SQL&gt; shutdown immediate\nPluggable Database closed.\n19:37:21 SQL&gt; startup\nPluggable Database opened.\n</code></pre>\n<p>As the value was set in memory, it&#8217;s now back to default:</p>\n<pre><code>\n19:36:48 SQL&gt; show parameter optimizer_index_cost_adj;\n&nbsp;\nNAME                                 TYPE        VALUE\n------------------------------------ ----------- ------------------------------\noptimizer_index_cost_adj             integer     100\n&nbsp;\n</code></pre>\n<p>and of course, my cursor is not there anymore:</p>\n<pre><code>\n19:36:48 SQL&gt; select sql_id,child_number,first_load_time,con_id from v$sql where sql_id='a5ks9fhw2v9s1';\n&nbsp;\nno rows selected\n</code></pre>\n<p>So now I run again my query and expect the default optimizer environement:</p>\n<pre><code>\n19:36:48 SQL&gt; select dummy from dual;\n&nbsp;\nD\n-\nX\n&nbsp;\n19:36:48 SQL&gt; select * from table(dbms_xplan.display_cursor(format=&gt;'+outline'));\n&nbsp;\nPLAN_TABLE_OUTPUT\n---------------------------------------------------------------------------\nSQL_ID  4au7rzs3y6kzn, child number 0\n-------------------------------------\nselect dummy from dual\n&nbsp;\nPlan hash value: 272002086\n&nbsp;\n--------------------------------------------------------------------------\n| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |\n--------------------------------------------------------------------------\n|   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |\n|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |\n--------------------------------------------------------------------------\n&nbsp;\nOutline Data\n-------------\n&nbsp;\n  /*+\n      BEGIN_OUTLINE_DATA\n      IGNORE_OPTIM_EMBEDDED_HINTS\n      OPTIMIZER_FEATURES_ENABLE('12.1.0.2')\n      DB_VERSION('12.1.0.2')\n      OPT_PARAM('optimizer_index_cost_adj' 1)\n      ALL_ROWS\n      OUTLINE_LEAF(@\"SEL$1\")\n      FULL(@\"SEL$1\" \"DUAL\"@\"SEL$1\")\n      END_OUTLINE_DATA\n  */\n</code></pre>\n<p>And my new cursor has used the optimizer_index_cost_adj=1 which is not what I expected at all.<br />\nDoes that mean that session parameters remain across PDB close/start?<br />\nNo according to &#8216;show parameter&#8217;:</p>\n<pre><code>\n19:37:07 SQL&gt; show parameter optimizer_index_cost_adj;\n&nbsp;\nNAME                                 TYPE        VALUE\n------------------------------------ ----------- ------------------------------\noptimizer_index_cost_adj             integer     100\n&nbsp;\n</code></pre>\n<p>but yes according to V$SES_OPTIMIZER_ENV:</p>\n<pre><code>\n19:37:07 SQL&gt; select sid,name,value from v$ses_optimizer_env where name='optimizer_index_cost_adj' and sid=sys_context('userenv','sid');\n&nbsp;\n       SID NAME                                     VALUE\n---------- ---------------------------------------- -------------------------\n        35 optimizer_index_cost_adj                 1\n&nbsp;\n</code></pre>\n<h3>Conclusion</h3>\n<p>I&#8217;ve reproduced the same when re-connecting (instead of changing session container) and even when connecting to another PDB.<br />\nIn my opinion, this is a bug. The session parameters should be reinitialized when re-connecting. So be careful.</p>\n<h3>Update 12-JUL-2016</h3>\n<p>See Maciej Tokar <a href=\"http://dba24.pl/2016/07/07/alter-system-cdb-params-hack-from-pdb-a-bug-or-a-feature/\">post</a> about this issue (my case being only one effect)</p>\n",
    "protected": false
  }
}
