{
  "date": "2017-08-23T19:41:01",
  "slug": "improving-statspack-experience",
  "link": "https://www.dbi-services.com/blog/improving-statspack-experience/",
  "title": {
    "rendered": "Improving Statspack Experience"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nI&#8217;ve published a few month ago an article in the UKOUG OracleScene magazine on <a href=\"http://viewer.zmags.com/publication/dd9ed62b#/dd9ed62b/36\" target=\"_blank\" rel=\"noopener noreferrer\">Improving Statspack Experience</a>: quick setup script, changing settings, filling Idle Events,etc. In the article, I used dbms_job to schedule the snapshots, because I have this script for years and never took the time to do it with dbms_scheduler. Thanks to <a href=\"https://www.dbi-services.com/on-the-company-and-its-associates/our-associates-and-certified-experts/nicolas-jardot/\" target=\"_blank\" rel=\"noopener noreferrer\">Nicolas Jardot</a> here is the equivalent script using dbms_scheduler.<br />\n<!--more--><br />\nThe idea is to have a script to run on each instance (when in RAC) in order to have a job calling statspack.snap and statspack.purge on each instance.</p>\n<pre><code>\nDECLARE\n    instno NUMBER;\n    snapjob VARCHAR2(30);\n    purgejob VARCHAR2(30);\nBEGIN\n    select instance_number into instno from v$instance;\n    snapjob  := 'PERFSTAT.STATSPACK_SNAP_' || instno;\n    purgejob := 'PERFSTAT.STATSPACK_PURGE_' || instno;\n&nbsp;\n    DBMS_SCHEDULER.CREATE_JOB (\n       job_name =&gt; snapjob, \n       job_type =&gt; 'PLSQL_BLOCK', \n       job_action =&gt; 'statspack.snap;',\n       number_of_arguments =&gt; 0,\n       start_date =&gt; systimestamp,\n       repeat_interval =&gt; 'FREQ=HOURLY;BYTIME=0000;BYDAY=MON,TUE,WED,THU,FRI,SAT,SUN',\n       end_date =&gt; NULL,\n       enabled =&gt; TRUE,\n       auto_drop =&gt; FALSE,\n       comments =&gt; 'Take hourly Statspack snapshot');\n&nbsp;\n    DBMS_SCHEDULER.CREATE_JOB (\n       job_name =&gt; purgejob, \n       job_type =&gt; 'PLSQL_BLOCK', \n       job_action =&gt; 'statspack.purge(i_num_days=&gt;31,i_extended_purge=&gt;true);',\n       number_of_arguments =&gt; 0,\n       start_date =&gt; systimestamp,\n       repeat_interval =&gt; 'FREQ=WEEKLY;BYTIME=120000;BYDAY=SUN',\n       end_date =&gt; NULL,\n       enabled =&gt; TRUE,\n       auto_drop =&gt; FALSE,\n       comments =&gt; 'Weekly purge Statspack snapshot');\n&nbsp;\n    DBMS_SCHEDULER.SET_ATTRIBUTE( name =&gt; snapjob, attribute =&gt; 'logging_level', value =&gt; DBMS_SCHEDULER.LOGGING_OFF );\n    DBMS_SCHEDULER.SET_ATTRIBUTE( name =&gt; snapjob, attribute =&gt; 'INSTANCE_ID', value=&gt;instno);\n    DBMS_SCHEDULER.SET_ATTRIBUTE( name =&gt; snapjob, attribute =&gt; 'INSTANCE_STICKINESS', value=&gt;TRUE);\n&nbsp;\n    DBMS_SCHEDULER.SET_ATTRIBUTE( name =&gt; purgejob, attribute =&gt; 'logging_level', value =&gt; DBMS_SCHEDULER.LOGGING_OFF );\n    DBMS_SCHEDULER.SET_ATTRIBUTE( name =&gt; purgejob, attribute =&gt; 'INSTANCE_ID', value=&gt;instno);\n    DBMS_SCHEDULER.SET_ATTRIBUTE( name =&gt; purgejob, attribute =&gt; 'INSTANCE_STICKINESS', value=&gt;TRUE);  \nEND;\n/\n</code></pre>\n<p>I used the Oracle Cloud Service to provision quickly a two nodes RAC database to validate, and I&#8217;ll check the scheduling:</p>\n<pre><code>\n[oracle@rac-dg01-1 admin]$ alias sql='JAVA_HOME=$ORACLE_HOME/jdk bash $ORACLE_HOME/sqldeveloper/sqlcl/bin/sql'\n[oracle@rac-dg01-1 admin]$ TWO_TASK=//10.31.143.86/pdb1.a511644.oraclecloud.internal sql sys/\"Ach1z0#d\" as sysdba\n&nbsp;\nSQLcl: Release 12.2.0.1.0 RC on Wed Aug 23 18:57:12 2017\n&nbsp;\nCopyright (c) 1982, 2017, Oracle.  All rights reserved.\n&nbsp;\nConnected to:\nOracle Database 12c EE Extreme Perf Release 12.2.0.1.0 - 64bit Production\n&nbsp;\nSQL&gt; set sqlformat ansiconsole\nSQL&gt; select job_name, state, enabled, next_run_date, instance_stickiness, instance_id from dba_scheduler_jobs where owner='PERFSTAT';\nJOB_NAME           STATE      ENABLED  NEXT_RUN_DATE                           INSTANCE_STICKINESS  INSTANCE_ID\n--------           -----      -------  -------------                           -------------------  -----------\nSTATSPACK_SNAP_1   SCHEDULED  TRUE     23-AUG-17 07.00.00.981193000 PM +00:00  TRUE                 1\nSTATSPACK_PURGE_1  SCHEDULED  TRUE     27-AUG-17 12.00.00.074939000 PM +00:00  TRUE                 1\nSTATSPACK_SNAP_2   SCHEDULED  TRUE     23-AUG-17 07.00.00.644681000 PM +00:00  TRUE                 2\nSTATSPACK_PURGE_2  SCHEDULED  TRUE     27-AUG-17 12.00.00.755685000 PM +00:00  TRUE                 2\n</code></pre>\n<p>One hour later, the job has run on each instance:</p>\n<pre><code>\nJOB_NAME           STATE      ENABLED  NEXT_RUN_DATE                           INSTANCE_STICKINESS  INSTANCE_ID\n--------           -----      -------  -------------                           -------------------  -----------\nSTATSPACK_SNAP_1   SCHEDULED  TRUE     23-AUG-17 08.00.00.325755000 PM +00:00  TRUE                 1\nSTATSPACK_PURGE_1  SCHEDULED  TRUE     27-AUG-17 12.00.00.074939000 PM +00:00  TRUE                 1\nSTATSPACK_SNAP_2   SCHEDULED  TRUE     23-AUG-17 08.00.00.644681000 PM +00:00  TRUE                 2\nSTATSPACK_PURGE_2  SCHEDULED  TRUE     27-AUG-17 12.00.00.755685000 PM +00:00  TRUE                 2\n</code></pre>\n<p>Now running a spreport to see the instances having snapshots:</p>\n<pre><code>\n[oracle@rac-dg01-1 admin]$ TWO_TASK=//10.31.143.86/pdb1.a511644.oraclecloud.internal/cdb12 sqlplus sys/\"Ach1z0#d\" as sysdba @ spreport\n&nbsp;\nInstances in this Statspack schema\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n&nbsp;\n   DB Id    Inst Num DB Name      Instance     Host\n----------- -------- ------------ ------------ ------------\n  924704606        2 CDB1         cdb12        rac-dg01-2\n  924704606        1 CDB1         cdb11        rac-dg01-1\n&nbsp;\nUsing  924704606 for database Id\nUsing          2 for instance number\n</code></pre>\n<p>Here it is. dbms_job is deprecated. Let&#8217;s use dbms_scheduler.</p>\n",
    "protected": false
  }
}
