155 DBMS_SQLDIAG
The DBMS_SQLDIAG package provides an interface to the SQL Diagnosability functionality.
This chapter contains the following topics:
See Also:
Oracle Database Administrator's Guide for more information about "Managing Diagnostic Data"
155.1 DBMS_SQLDIAG Overview
In the rare case that a SQL statement fails with a critical error, you can run the SQL Repair Advisor to try to repair the failed statement.
This section covers the following topics:
About the SQL Repair Advisor
You run the SQL Repair Advisor after a SQL statement fails with a critical error. The advisor analyzes the statement and in many cases recommends a patch to repair the statement. If you implement the recommendation, the applied SQL patch circumvents the failure by causing the query optimizer to choose an alternate execution plan for future executions.
Running the SQL Repair Advisor
You run the SQL Repair Advisor by creating and executing a diagnostic task using the CREATE_DIAGNOSIS_TASK
and EXECUTE_DIAGNOSIS_TASK
respectively. The SQL Repair Advisor first reproduces the critical error and then tries to produce a workaround in the form of SQL patch.
-
Identify the problem SQL statement
Consider the SQL statement that gives a critical error:
DELETE FROM t t1 WHERE t1.a = 'a' AND ROWID <> (SELECT MAX(ROWID) FROM t t2 WHERE t1.a= t2.a AND t1.b = t2.b AND t1.d=t2.d)
You use the SQL Repair advisor to repair this critical error.
-
Create a diagnosis task
Invoke
DBMS_SQLDIAG
.CREATE_DIAGNOSIS_TASK
. You can specify an optional task name, an optional time limit for the advisor task, and problem type. In the example below, we specify the SQL text, the task name as 'error_task
' and a problem type as 'DBMS_SQLDIAG
.PROBLEM_TYPE_COMPILATION_ERROR
'.DECLARE rep_out CLOB; t_id VARCHAR2(50); BEGIN t_id := DBMS_SQLDIAG.CREATE_DIAGNOSIS_TASK( sql_text => 'DELETE FROM t t1 WHERE t1.a = ''a'' AND ROWID <> (SELECT MAX(ROWID) FROM t t2 WHERE t1.a= t2.a AND t1.b = t2.b AND t1.d=t2.d)', task_name => 'error_task', problem_type =>DBMS_SQLDIAG.PROBLEM_TYPE_COMPILATION_ERROR);
-
Execute the diagnosis task
To execute the workaround generation and analysis phase of the SQL Repair Advisor, you call
DBMS_SQLDIAG
.EXECUTE_DIAGNOSIS_TASK
with the task ID returned by theCREATE_DIAGNOSIS_TASK
. After a short delay, the SQL Repair Advisor returns. As part of its execution, the SQL Repair Advisor keeps a record of its findings which can be accessed through the reporting facilities of SQL Repair Advisor.DBMS_SQLDIAG.EXECUTE_DIAGNOSIS_TASK (t_id);
-
Report the diagnosis task
The analysis of the diagnosis task is accessed through dbms_sqldiag.report_diagnosis_task. If the SQL Repair Advisor was able to find a workaround, it recommends a SQL Patch. A SQL Patch is similar to a SQL profile but unlike the SQL Profile, it is used to workaround compilation or execution errors.
rep_out := DBMS_SQLDIAG.REPORT_DIAGNOSIS_TASK (t_id, DBMS_SQLDIAG.TYPE_TEXT); DBMS_OUTPUT.PUT_LINE ('Report : ' || rep_out); END; /
-
Applying the patch
If a patch recommendation is present in the report, you can run the
ACCEPT_SQL_PATCH
command to accept the patch by invokingDBMS_SQLDIAG
.ACCEPT_SQL_PATCH
. This procedure takes thetask_name
as an argument.EXECUTE DBMS_SQLDIAG.ACCEPT_SQL_PATCH(task_name => 'error_task', task_owner => 'SYS', replace => TRUE);
-
Test the patch
Now that you have accepted the patch, you can rerun the SQL statement. This time, it will not give you the critical error. If you run 'explain plan' for this statement, you will see that a SQL patch was used to generate the plan.
DELETE FROM t t1 WHERE t1.a = 'a' AND ROWID <> (select max(rowid) FROM t t2 WHERE t1.a= t2.a AND t1.b = t2.b AND t1.d=t2.d);
Removing a SQL Patch
In a situation where you obtained an official patch from Oracle to fix an error, or upgraded to the next patchset or release of Oracle which included the fix for the error, you call DBMS_SQLDIAG
.DROP_SQL_PATCH
with the patch name to drop the SQL patch. The patch name can be obtained from the explain plan section or by querying the view DBA_SQL_PATCHES
.
155.2 DBMS_SQLDIAG Security Model
You must have the ADVISOR
role to execute the DBMS_SQLDIAG
package.
155.3 DBMS_SQLDIAG Constants
DBMS_SQLDIAG
defines constants to use when specifying parameter values.
These constants are shown in the following tables:
-
Table 155-1 describes the name of SQL repair advisor as seen by the advisor framework
-
Table 155-2 describes SQLDIAG advisor task scope parameter values
-
Table 155-3 describes SQLDIAG advisor
time_limit
constants -
Table 155-4 describes possible formats for a report
-
Table 155-5 describes possible levels of detail in the report
-
Table 155-6 describes possible report sections (comma delimited)
-
Table 155-7 describes possible values for the
problem_type
parameter of the CREATE_DIAGNOSIS_TASK Functions -
Table 155-8 describes possible values for the
_sql_findings_mode
parameter
Table 155-1 DBMS_SQLDIAG Constants - SQLDIAG Advisor Name
Constant | Type | Value | Description |
---|---|---|---|
|
|
SQL Repair Advisor |
Name of SQL repair advisor as seen by the advisor framework |
Table 155-2 DBMS_SQLDIAG Constants - SQLDIAG Advisor Task Scope Parameter Values
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
Detailed analysis of the problem which may take more time to execute |
|
|
|
Brief analysis of the problem |
Table 155-3 DBMS_SQLDIAG Constants - SQLDIAG Advisor time_limit Constants
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
Default time limit for analysis of the problem |
Table 155-4 DBMS_SQLDIAG Constants - Report Type (possible values) Constants
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
Report from the REPORT_DIAGNOSIS_TASK Function in HTML form |
|
|
|
Report from the REPORT_DIAGNOSIS_TASK Function in text form |
|
|
|
Report from the REPORT_DIAGNOSIS_TASK Function in XML form |
Table 155-5 DBMS_SQLDIAG Constants - Report Level (possible values) Constants
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
Complete report including annotations about statements skipped over |
|
|
|
Shows information about every statement analyzed, including recommendations not implemented |
|
|
|
Simple report shows only information about the actions taken by the advisor. |
Table 155-6 DBMS_SQLDIAG Constants - Report Section (possible values) Constants
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
All statements |
|
|
|
Statements with errors |
|
|
|
Tuning findings |
|
|
|
General information |
|
|
|
Explain plans |
|
|
|
Summary information |
Table 155-7 DBMS_SQLDIAG Constants - Problem Type Constants
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
User suspects this is a performance problem |
|
|
|
User suspects the query is giving inconsistent results |
|
|
|
User sees a crash in compilation |
|
|
|
User sees a crash in execution |
|
|
|
User to explore all alternative plans |
Table 155-8 DBMS_SQLDIAG Constants - Findings Filter Constants
Constant | Type | Value | Description |
---|---|---|---|
|
|
|
Show all possible findings |
|
|
|
Show status of validation rules over structures |
|
|
|
Show only features used by the query |
|
|
|
Show the alternative plans generated by the advisor |
|
|
|
Show difference between two plans |
|
|
|
Mask info for testing |
|
|
|
Show features usage history |
|
|
|
Show the alternative plans generated by the advisor |
155.4 DBMS_SQLDIAG Examples
Patches can be exported out of one system and imported into another by means of a staging table, provided by subprograms in this package. Like with SQL diagnosis sets, the operation of inserting into the staging table is called a "pack", and the operation of creating patches from staging table data is termed the "unpack".
Patch Pack / Unpack
DBAs should perform a pack/unpack as follows:
-
Create a staging table owned by user '
SH
' through a call toCREATE_STGTAB_SQLPATCH:
EXEC DBMS_SQLDIAG.CREATE_STGTAB_SQLPATCH( table_name => 'STAGING_TABLE', schema_name => 'SH');
-
Call
PACK_STGTAB_SQLPATCH
one or more times to write SQL patch data into the staging table. In this case, copy data for all SQL patches in the DEFAULT category into a staging table owned by the current schema owner:EXEC DBMS_SQLDIAG.PACK_STGTAB_SQLPATCH( staging_table_name => 'STAGING_TABLE');
-
In this case, only a single SQL patch
SP_FIND_EMPLOYEE
is copied into a staging table owned by the current schema owner:EXEC DBMS_SQLDIAG.PACK_STGTAB_SQLPATCH( patch_name => 'SP_FIND_EMPLOYEE', staging_table_name => 'STAGING_TABLE');
The staging table can then be moved to another system using either datapump, import/export commands or through a databaselink.
-
Call
UNPACK_STGTAB_SQLPATCH
to create SQL patches on the new system from the patch data in the staging table. In this case, change the name in the data for theSP_FIND_EMPLOYEE
patch stored in the staging table to 'SP_FIND_EMP_PROD
':exec dbms_sqldiag.remap_stgtab_sqlpatch( old_patch_name => 'SP_FIND_EMPLOYEE', new_patch_name => 'SP_FIND_EMP_PROD',
155.5 Summary of DBMS_SQLDIAG Subprograms
This table lists the DBMS_SQLDIAG
subprograms and briefly describes them.
Table 155-9 DBMS_SQLDIAG Package Subprograms
Subprogram | Description |
---|---|
Accepts a recommended SQL patch as recommended by the specified SQL diagnosis task |
|
Alters specific attributes of an existing SQL patch object |
|
Cancels a diagnostic task |
|
Creates a diagnostic task in order to diagnose a single SQL statement |
|
Creates an SQL patch based on a set of user specified hints for specific statements identified by SQL text. |
|
Creates the staging table used for transporting SQL patches from one system to another |
|
Drops a diagnostic task |
|
Drops the named SQL patch from the database |
|
Executes a diagnostic task |
|
Explains a SQL test case |
|
Exports a SQL test case to a directory |
|
Generates a SQL Test Case corresponding to the incident ID passed as an argument. |
|
Generates a SQL Test Case corresponding to the SQL passed as an argument |
|
Returns the value of fix control for a given bug number |
|
Imports a SQL test case |
|
Imports a SQL test case into a schema |
|
Initializes a |
|
Interrupts a diagnostic task |
|
Loads a |
|
SQL patches into the staging table created by the CREATE_STGTAB_SQLPATCH Procedure |
|
Reports on a diagnostic task |
|
Reports on a diagnostic task |
|
Resets a diagnostic task |
|
Resumes a diagnostic task |
|
Sets a diagnosis task parameter |
|
Unpacks from the staging table populated by a call to the PACK_STGTAB_SQLPATCH Procedure, using the patch data stored in the staging table to create patches on this system |
155.5.1 ACCEPT_SQL_PATCH Function & Procedure
This procedure accepts a recommended SQL patch as recommended by the specified SQL diagnosis task.
Syntax
DBMS_SQLDIAG.ACCEPT_SQL_PATCH ( task_name IN VARCHAR2, object_id IN NUMBER := NULL, name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, category IN VARCHAR2 := NULL, task_owner IN VARCHAR2 := NULL, replace IN BOOLEAN := FALSE, force_match IN BOOLEAN := FALSE) RETURN VARCHAR2;
DBMS_SQLDIAG.ACCEPT_SQL_PATCH ( task_name IN VARCHAR2, object_id IN NUMBER := NULL, name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, category IN VARCHAR2 := NULL, task_owner IN VARCHAR2 := NULL, replace IN BOOLEAN := FALSE, force_match IN BOOLEAN := FALSE);
Parameters
Table 155-10 ACCEPT_SQL_PATCH Function & Procedure Parameters
Parameter | Description |
---|---|
|
Name of the SQL diagnosis task |
|
Identifier of the advisor framework object representing the SQL statement associated to the diagnosis task |
|
Name of the patch. It cannot contain double quotation marks. The name is case sensitive. If not specified, the system will generate a unique name for the SQL patch. |
|
User specified string describing the purpose of this SQL patch. Maximum size of description is |
|
Category name which must match the value of the |
|
Owner of the diagnosis task. This is an optional parameter that has to be specified to accept a SQL Patch associated to a diagnosis task owned by another user. The current user is the default value. |
|
If the patch already exists, it will be replaced if this argument is |
|
If |
Return Values
Name of the SQL patch
Usage Notes
Requires CREATE
ANY
SQL
PROFILE
privilege
155.5.2 ALTER_SQL_PATCH Procedure
This procedure alters specific attributes of an existing SQL patch object.
Syntax
DBMS_SQLDIAG.ALTER_SQL_PATCH ( name IN VARCHAR2, attribute_name IN VARCHAR2, value IN VARCHAR2);
Parameters
Table 155-11 ALTER_SQL_PATCH Procedure Parameters
Parameter | Description |
---|---|
|
Name of SQL patch to alter. |
|
Name of SQL patch to alter. Possible values:
This parameter is mandatory and is case sensitive. |
|
New value of the attribute. See |
Usage Notes
Requires ALTER
ANY
SQL
PATCH
privilege
155.5.3 CANCEL_DIAGNOSIS_TASK Procedure
This procedure cancels a diagnostic task.
Syntax
DBMS_SQLDIAG.CANCEL_DIAGNOSIS_TASK ( taskname IN VARCHAR2);
Parameters
Table 155-12 CANCEL_DIAGNOSIS_TASK Procedure Parameters
Parameter | Description |
---|---|
|
Name of task |
155.5.4 CREATE_DIAGNOSIS_TASK Functions
This function creates a diagnostic task in order to diagnose a single SQL statement. It returns a SQL diagnosis task unique name
Syntax
Prepares the diagnosis of a single statement given its text:
DBMS_SQLDIAG.CREATE_DIAGNOSIS_TASK ( sql_text IN CLOB, bind_list IN sql_binds := NULL, user_name IN VARCHAR2 := NULL, scope IN VARCHAR2 := SCOPE_COMPREHENSIVE, time_limit IN NUMBER := TIME_LIMIT_DEFAULT, task_name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, problem_type IN NUMBER := PROBLEM_TYPE_PERFORMANCE) RETURN VARCHAR2;
Prepares the diagnosis of a single statement from the Cursor Cache given its identifier:
DBMS_SQLDIAG.CREATE_DIAGNOSIS_TASK ( sql_id IN VARCHAR2, plan_hash_value IN NUMBER := NULL, scope IN VARCHAR2 := SCOPE_COMPREHENSIVE, time_limit IN NUMBER := TIME_LIMIT_DEFAULT, task_name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, problem_type IN NUMBER := PROBLEM_TYPE_PERFORMANCE) RETURN VARCHAR2;
Prepares the diagnosis of a Sqlset:
DBMS_SQLDIAG.CREATE_DIAGNOSIS_TASK ( sqlset_name IN VARCHAR2, basic_filter IN VARCHAR2 := NULL, object_filter IN VARCHAR2 := NULL, rank1 IN VARCHAR2 := NULL, rank2 IN VARCHAR2 := NULL, rank3 IN VARCHAR2 := NULL, result_percentage IN NUMBER := NULL, result_limit IN NUMBER := NULL, scope IN VARCHAR2 := SCOPE_COMPREHENSIVE, time_limit IN NUMBER := TIME_LIMIT_DEFAULT, task_name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, plan_filter IN VARCHAR2 := 'MAX_ELAPSED_TIME', sqlset_owner IN VARCHAR2 := NULL, problem_type IN NUMBER := PROBLEM_TYPE_PERFORMANCE) RETURN VARCHAR2;
Parameters
Table 155-13 CREATE_DIAGNOSIS_TASK Function Parameters
Parameter | Description |
---|---|
|
Text of a SQL statement |
|
Set of bind values |
|
Username for who the statement/sqlset will be diagnosed |
|
Diagnosis scope (limited/comprehensive) |
|
Maximum duration in seconds for the diagnosis session |
|
Optional diagnosis task name |
description |
Maximum of 256 SQL diagnosis session description |
|
Determines the goal of the task. Possible values are:
|
|
Identifier of the statement |
|
Hash value of the SQL execution plan |
|
Sqlset name |
|
SQL predicate to filter the SQL from the SQL tuning set (STS) |
|
Object filter |
|
Order-by clause on the selected SQL |
|
Percentage on the sum of a ranking measure |
|
Top L(imit) SQL from (filtered/ranked) SQL |
|
Plan filter. It is applicable in case there are multiple plans (
|
|
Owner of the sqlset, or null for current schema owner |
155.5.5 CREATE_SQL_PATCH Function
This function creates a SQL patch based on a set of user specified hints for specific statements identified by SQL text.
A SQL patch is usually created automatically by the SQL Repair Advisor to prevent any errors during the compilation or execution of a SQL statement. This function provides a way to manually create a SQL patch based on a set of hints that resolves the error.
Syntax
DBMS_SQLDIAG.CREATE_SQL_PATCH ( sql_text IN CLOB, hint_text IN CLOB, name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, category IN VARCHAR2 := NULL, validate IN BOOLEAN := TRUE) RETURN VARCHAR2;
DBMS_SQLDIAG.CREATE_SQL_PATCH ( sql_id IN VARCHAR2, hint_text IN CLOB, name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL, category IN VARCHAR2 := NULL, validate IN BOOLEAN := TRUE) RETURN VARCHAR2;
Parameters
Table 155-14 CREATE_SQL_PATCH Function Parameters
Parameter | Description |
---|---|
|
Text of the SQL statement |
|
The SQL identifier for the SQL statement |
|
Hints to include in the SQL patch |
|
Optional SQL patch name |
|
Description of the SQL patch |
|
Category name |
|
Whether to validate the provided hints |
Return Values
Both functions return the SQL patch name.
155.5.6 CREATE_STGTAB_SQLPATCH Procedure
This procedure creates the staging table used for transporting SQL patches from one system to another.
Syntax
DBMS_SQLDIAG.CREATE_STGTAB_SQLPATCH ( table_name IN VARCHAR2, schema_name IN VARCHAR2 := NULL, tablespace_name IN VARCHAR2 := NULL);
Parameters
Table 155-15 CREATE_STGTAB_SQLPATCH Procedure Parameters
Parameter | Description |
---|---|
|
(Mandatory) Name of the table to create (case-sensitive) |
|
Schema to create the table in, or |
|
Tablespace to store the staging table within, or |
155.5.7 DROP_DIAGNOSIS_TASK Procedure
This procedure drops a diagnostic task.
Syntax
DBMS_SQLDIAG.DROP_DIAGNOSIS_TASK ( taskname IN VARCHAR2);
Parameters
Table 155-16 DROP_DIAGNOSIS_TASK Procedure Parameters
Parameter | Description |
---|---|
|
Name of task |
155.5.8 DROP_SQL_PATCH Procedure
This procedure drops the named SQL patch from the database.
Syntax
DBMS_SQLDIAG.DROP_SQL_PATCH ( name IN VARCHAR2, ignore IN BOOLEAN := FALSE);
Parameters
Table 155-17 DROP_SQL_PATCH Function & Procedure Parameters
Parameter | Description |
---|---|
|
Name of patch to be dropped. The name is case sensitive. |
|
Ignore errors due to object not existing. |
Usage Notes
Requires DROP
ANY
SQL
PATCH
privilege
155.5.9 EXECUTE_DIAGNOSIS_TASK Procedure
This procedure executes a diagnostic task.
Syntax
DBMS_SQLDIAG.EXECUTE_DIAGNOSIS_TASK ( taskname IN VARCHAR2);
Parameters
Table 155-18 EXECUTE_DIAGNOSIS_TASK Procedure Parameters
Parameter | Description |
---|---|
|
Name of task |
155.5.10 EXPLAIN_SQL_TESTCASE Function
This procedure explains a SQL test case.
Syntax
DBMS_SQLDIAG.EXPLAIN_SQL_TESTCASE ( sqlTestCase IN CLOB) RETURN CLOB;
Parameters
Table 155-19 EXPLAIN_SQL_TESTCASE Function Parameters
Parameter | Description |
---|---|
|
XML document describing the SQL test case |
155.5.11 EXPORT_SQL_TESTCASE Procedures
This procedure exports a SQL test case to a directory.
Syntax
This variant has to be provided with the SQL information.
DBMS_SQLDIAG.EXPORT_SQL_TESTCASE ( directory IN VARCHAR2, sql_text IN CLOB, user_name IN VARCHAR2 := NULL, bind_list IN sql_binds := NULL, exportEnvironment IN BOOLEAN := TRUE, exportMetadata IN BOOLEAN := TRUE, exportData IN BOOLEAN := FALSE, exportPkgbody IN BOOLEAN := FALSE, samplingPercent IN NUMBER := 100, ctrlOptions IN VARCHAR2 := NULL, timeLimit IN NUMBER := 0, testcase_name IN VARCHAR2 := NULL, testcase IN OUT NOCOPY CLOB, preserveSchemaMapping IN BOOLEAN := FALSE, version IN VARCHAR2 := 'COMPATIBLE');
This variant extracts the SQL information from an incident file.
DBMS_SQLDIAG.EXPORT_SQL_TESTCASE ( directory IN VARCHAR2, incident_id IN VARCHAR2, exportEnvironment IN BOOLEAN := TRUE, exportMetadata IN BOOLEAN := TRUE, exportData IN BOOLEAN := FALSE, exportPkgbody IN BOOLEAN := FALSE, samplingPercent IN NUMBER := 100, ctrlOptions IN VARCHAR2 := NULL, timeLimit IN NUMBER := DBMS_SQLDIAG.TIME_LIMIT_DEFAULT, testcase_name IN VARCHAR2 := NULL, testcase IN OUT NOCOPY CLOB, preserveSchemaMapping IN BOOLEAN := FALSE) version IN VARCHAR2 := 'COMPATIBLE');
This variant allow the SQL Test case to be generated from a cursor present in the cursor cache. Use V$SQL
to get the SQL identifier and the SQL hash value.
DBMS_SQLDIAG.EXPORT_SQL_TESTCASE ( directory IN VARCHAR2, sql_id IN VARCHAR2, plan_hash_value IN NUMBER := NULL, exportEnvironment IN BOOLEAN := TRUE, exportMetadata IN BOOLEAN := TRUE, exportData IN BOOLEAN := FALSE, exportPkgbody IN BOOLEAN := FALSE, samplingPercent IN NUMBER := 100, ctrlOptions IN VARCHAR2 := NULL, timeLimit IN NUMBER := DBMS_SQLDIAG.TIME_LIMIT_DEFAULT, testcase_name IN VARCHAR2 := NULL, testcase IN OUT NOCOPY CLOB, preserveSchemaMapping IN BOOLEAN := FALSE) version IN VARCHAR2 := 'COMPATIBLE');
Parameters
Table 155-20 EXPORT_SQL_TESTCASE Procedure Parameters
Parameter | Description |
---|---|
|
Directory to store the various generated files |
|
Text of the SQL statement to export |
|
Incident ID containing the offending SQL |
|
Identifier of the statement in the cursor cache |
|
Name of the user schema to use to parse the SQL, defaults to |
|
List of bind values associated to the statement |
|
|
|
|
|
|
|
|
|
If is |
|
Opaque control parameters. For example, to execute three times, set
|
|
How much time should we spend exporting the SQL test case |
|
An optional name for the SQL test case. This is used to prefix all the generated scripts |
|
Resulting testcase |
|
|
|
Version of database objects to be extracted. This option is only valid for
|
Usage Notes
-
A SQL test case generates a set of files needed to help reproduce a SQL failure on a different machine. It contains:
-
a dump file containing schemas objects and statistics (
.dmp
) -
the explain plan for the statements (in advanced mode)
-
diagnostic information gathered on the offending statement
-
an import script to execute to reload the objects
-
a SQL script to replay system statistics of the source
-
a table of contents file describing the SQL test case
-
metadata. (
xxxxmain.xml
) -
a
README.txt
file that explain the usage of the TCB -
the outlines used by the statement (
ol.xml
) -
a list of parameters set in the exporting db/env (
prmimp.sql
) -
a SQL monitor report, if any (
smrpt.html
) -
an AWR report, if any (
awrrpt.html
) -
a list of binds used in this statement (
bndlst.xml
)
-
-
You should not run Test Case Builder (TCB) under user
SYS
. Instead, use another user who can be granted theDBA
role. -
The default setting for TCB is that data is not exported. However, in some cases data is required, such as to diagnose an outcome with a result that is not optimal. To export data, call
EXPORT_SQL_TESTCASE
withexportData=>TRUE
and the data will be imported by default, unless turnedOFF
byimportData=>FALSE
. -
TCB includes PL/SQL package spec by default, but not the PL/SQL package body. However, you may need to have the package body as well, for example, to invoke the PL/SQL functions, or because you have a Virtual Private Database (VPD) function defined in a package. To export a PL/SQL package body, call
EXPORT_SQL_TESTCASE
withexportPkgbody=>TRUE
. To import a PL/SQL package body, call IMPORT_SQL_TESTCASE Procedures with importPkgbody=>TRUE
. -
To export objects statistics history, the database compatibility should be set to 12.0 or higher.
-
This procedure does not export data and statistics on a Global Temporary Table (GTT).
Examples
The user can specify multiple parameters in the ctrlOptions encapsulated either by using the <parameters>
parent tag or without the parent tag.
Using the <parameters> tag
<parameters> <parameter name="capture">with_runtime_info</parameter> <parameter name="mexec_count">1</parameter> </parameters>
Without the <parameters> tag
<parameter name="capture">with_runtime_info</parameter> <parameter name="mexec_count">1</parameter>'
155.5.12 EXPORT_SQL_TESTCASE_DIR_BY_INC Function
This function generates a SQL test case corresponding to the incident ID passed as an argument. It creates a set of scripts and dump file in the directory passed as an argument.
Syntax
DBMS_SQLDIAG.EXPORT_SQL_TESTCASE_DIR_BY_INC ( incident_id IN NUMBER, directory IN VARCHAR2, exportEnvironment IN VARCHAR2 := 'TRUE', exportMetadata IN VARCHAR2 := 'TRUE', exportData IN VARCHAR2 := 'FALSE', samplingPercent IN VARCHAR2 := '100', ctrlOptions IN VARCHAR2 := NULL version IN VARCHAR2 := 'COMPATIBLE') RETURN BOOLEAN;
Parameters
Table 155-21 EXPORT_SQL_TESTCASE_DIR_BY_INC Function Parameters
Parameter | Description |
---|---|
|
Incident ID containing the offending SQL. For more information about Incidents, see Oracle Database Performance Tuning Guide. |
|
Directory path to the generated files |
|
|
|
|
|
|
|
If is |
|
Opaque control parameters. For example, to execute three times, set
|
|
Version of database objects to be extracted. This option is only valid for
|
155.5.13 EXPORT_SQL_TESTCASE_DIR_BY_TXT Function
This function generates a SQL Test Case corresponding to the SQL passed as an argument. It creates a set of scripts and dump files in the directory passed as an argument.
Syntax
DBMS_SQLDIAG.EXPORT_SQL_TESTCASE_DIR_BY_TXT ( incident_id IN NUMBER, directory IN VARCHAR2, sql_text IN CLOB, user_name IN VARCHAR2 := 'SYS', exportEnvironment IN VARCHAR2 := 'TRUE', exportMetadata IN VARCHAR2 := 'TRUE', exportData IN VARCHAR2 := 'FALSE', samplingPercent IN VARCHAR2 := '100', ctrlOptions IN VARCHAR2 := NULL version IN VARCHAR2 := 'COMPATIBLE') RETURN BOOLEAN;
Parameters
Table 155-22 EXPORT_SQL_TESTCASE_DIR_BY_TXT Function Parameters
Parameter | Description |
---|---|
|
Incident ID containing the offending SQL |
|
Directory to store the various generated files |
|
Text of the SQL statement to explain |
|
Name of the user schema to use to parse the SQL, defaults to |
|
|
|
|
|
|
|
If is |
|
Opaque control parameters. For example, to execute three times, set
|
|
Version of database objects to be extracted. This option is only valid for
|
155.5.14 GET_FIX_CONTROL Function
This function returns the value of fix control for a given bug number.
Syntax
DBMS_SQLDIAG.GET_FIX_CONTROL ( bug_number IN NUMBER) RETURN NUMBER;
Parameters
Table 155-23 GET_FIX_CONTROL Function Parameters
Parameter | Description |
---|---|
|
Bug number |
155.5.15 GET_SQL Function
This function loads a sql_setrow
from the trace file associated to an the given incident ID.
Syntax
DBMS_SQLDIAG.GET_SQL ( incident_id IN VARCHAR2) RETURN SQLSET_ROW;
Parameters
Table 155-24 GET_SQL Function Parameters
Parameter | Description |
---|---|
|
Identifier of the incident |
155.5.16 IMPORT_SQL_TESTCASE Procedures
This procedure imports a SQL test case into a schema.
Syntax
This variant requires a source directory and SQL Testcase metadata object (in XML format).
DBMS_SQLDIAG.IMPORT_SQL_TESTCASE ( directory IN VARCHAR2, sqlTestCase IN CLOB, importEnvironment IN BOOLEAN := TRUE, importMetadata IN BOOLEAN := TRUE, importData IN BOOLEAN := TRUE, importPkgbody IN BOOLEAN := FALSE, importDiagnosis IN BOOLEAN := TRUE, ignoreStorage IN BOOLEAN := TRUE, ctrlOptions IN VARCHAR2 := NULL, preserveSchemaMapping IN BOOLEAN := FALSE);
This variant requires a source directory name of SQL Testcase metadata file.
DBMS_SQLDIAG.IMPORT_SQL_TESTCASE ( directory IN VARCHAR2, filename IN VARCHAR2, importEnvironment IN BOOLEAN := TRUE, importMetadata IN BOOLEAN := TRUE, importData IN BOOLEAN := TRUE, importPkgbody IN BOOLEAN := FALSE, importDiagnosis IN BOOLEAN := TRUE, ignoreStorage IN BOOLEAN := TRUE, ctrlOptions IN VARCHAR2 := NULL, preserveSchemaMapping IN BOOLEAN := FALSE);
Parameters
Table 155-25 IMPORT_SQL_TESTCASE Procedure Parameters
Parameter | Description |
---|---|
|
Directory containing test case files |
|
Name of a file containing an XML document describing the SQL test case |
|
|
|
|
|
|
|
|
|
|
|
|
|
Opaque control parameters, of which only
|
|
|
Usage Notes
-
A SQL test case generates a set of files needed to help reproduce a SQL failure on a different machine. It contains:
-
a dump file containing schemas objects and statistics (
.dmp
) -
the explain plan for the statements (in advanced mode)
-
diagnostic information gathered on the offending statement
-
an import script to execute to reload the objects
-
a SQL script to replay system statistics of the source
-
a table of contents file describing the SQL test case
-
metadata. (
xxxxmain.xml
) -
a
README.txt
file that explain the usage of the TCB -
the outlines used by the statement (
ol.xml
) -
a list of parameters set in the exporting db/env (
prmimp.sql
) -
a SQL monitor report, if any (
smrpt.html
) -
an AWR report, if any (
awrrpt.html
) -
a list of binds used in this statement (
bndlst.xml
)
-
-
You should not run Test Case Builder (TCB) under user
SYS
. Instead, use another user who can be granted theSYSDBA
privilege -
The default setting for TCB is that data is not exported. However, in some cases data is required, such as to diagnose an outcome with a result that is not optimal. To export data, call EXPORT_SQL_TESTCASE Procedures with
exportData=>TRUE
and the data will be imported by default, unless turnedOFF
byimportData=>FALSE
. -
TCB includes PL/SQL package spec by default, but not the PL/SQL package body. However, you may need to have the package body as well, for example, to invoke the PL/SQL functions, or because you have a Virtual Private Database (VPD) function defined in a package. To export a PL/SQL package body, call EXPORT_SQL_TESTCASE Procedures with
exportPkgbody=>TRUE
. To import a PL/SQL package body, callIMPORT_SQL_TESTCASE
Procedures with importPkgbody=>TRUE
. -
The
capture
value used when invoking the EXPORT_SQL_TESTCASE Procedures must be used when calling this procedure.
155.5.17 INCIDENTID_2_SQL Procedure
This procedure initializes a sql_setrow
from an incident ID.
Syntax
DBMS_SQLDIAG.INCIDENTID_2_SQL ( incident_id IN VARCHAR2, sql_stmt OUT SQLSET_ROW, problem_type OUT NUMBER, err_code OUT BINARY_INTEGER, err_mesg OUT VARCHAR2);
Parameters
Table 155-26 INCIDENTID_2_SQL Procedure Parameters
Parameter | Description |
---|---|
|
Identifier of the incident |
|
Resulting SQL |
|
Tentative type of SQL problem (currently among |
|
Error code if any otherwise it is set to |
|
Error message if any otherwise it is set to |
155.5.18 INTERRUPT_DIAGNOSIS_TASK Procedure
This procedure interrupts a diagnostic task.
Syntax
DBMS_SQLDIAG.INTERRUPT_DIAGNOSIS_TASK ( taskname IN VARCHAR2);
Parameters
Table 155-27 INTERRUPT_DIAGNOSIS_TASK Procedure Parameters
Parameter | Description |
---|---|
|
Name of task |
155.5.19 LOAD_SQLSET_FROM_TCB Function
This function loads a SQLSET
from a Test Case Builder file.
Syntax
DBMS_SQLDIAG.LOAD_SQLSET_FROM_TCB ( directory IN VARCHAR2, filename IN VARCHAR2, sqlset_name IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2;
Parameters
Table 155-28 LOAD_SQLSET_FROM_TCB Function Parameters
Parameter | Description |
---|---|
|
Name of directory |
|
Name of file |
|
Name of |
155.5.20 PACK_STGTAB_SQLPATCH Procedure
This procedure packs SQL patches into the staging table created by a call to the CREATE_STGTAB_SQLPATCH Procedure.
Syntax
DBMS_SQLDIAG.UPPACK_STGTAB_SQLPATCH ( patch_name IN VARCHAR2 := '%', patch_category IN VARCHAR2 := 'DEFAULT', staging_table_name IN VARCHAR2, staging_schema_owner IN VARCHAR2 := NULL);
Parameters
Table 155-29 UPPACK_STGTAB_SQLPATCH Procedure Parameters
Parameter | Description |
---|---|
|
Name of patch to pack (% wildcards acceptable, case-sensitive) |
|
Category to which to pack patches (% wildcards acceptable, case-insensitive) |
|
(Mandatory) Name of the table to use (case-sensitive) |
|
Schema where the table resides, or |
Usage Notes
-
Requires:
ADMINISTER
SQL
PLAN MANAGEMENT
OBJECT
privilege andINSERT
privilege on the staging table -
By default, we move all SQL patches in category
DEFAULT
. See the Examples for details. Note that the subprogram issues aCOMMIT
after packing each SQL patch, so if an error is raised in mid-execution, some patches may be in the staging table.
Related Topics
155.5.21 REPLAY_SQL_TESTCASE Function
This function automates the reproduction of the SQL Test Case.
Syntax
DBMS_SQLDIAG.REPLAY_SQL_TESTCASE ( directory IN VARCHAR2, filename IN VARCHAR2, ctrlOptions IN VARCHAR2 := NULL, format IN VARCHAR2 := 'TEXT') RETURN CLOB; DBMS_SQLDIAG.REPLAY_SQL_TESTCASE ( directory IN VARCHAR2, sqlTestCase IN CLOB, ctrlOptions IN VARCHAR2 := NULL, format IN VARCHAR2 := 'TEXT') RETURN CLOB;
Parameters
Table 155-30 REPLAY_SQL_TESTCASE Function Parameters
Parameter | Description |
---|---|
|
Directory containing test case files |
|
Name of a file containing an XML document describing the SQL test case |
|
Opaque control parameters. For example, to execute three times, set
|
|
SQL test case |
|
Format of the replay report. Possible formats are: |
Examples
TCB Replay Mode: Execute SELECT /* tcbdynpl_1 */ /*+ gather_plan_statistics */ * FROM (SELECT * FROM emp where emp.sal > 100) emp, dept WHERE emp.deptno = dept.deptno And emp.sal > 1000 /* tcbdynpl_1 */ Explain Plan Plan Hash Value : 2219294842 ----------------------------------------------------------------- | Id | Operation | Name | Rows | ----------------------------------------------------------------- | 0 | SELECT STATEMENT | | 13 | | * 1 | HASH JOIN | | 13 | | 2 | NESTED LOOPS | | | | 3 | NESTED LOOPS | | 13 | | 4 | STATISTICS COLLECTOR | | | | 5 | TABLE ACCESS FULL | DEPT | 4 | | * 6 | INDEX RANGE SCAN | EMP_IDX_DEPTNO | | | * 7 | TABLE ACCESS BY INDEX ROWID | EMP | 3 | | * 8 | TABLE ACCESS FULL | EMP | 13 | ----------------------------------------------------------------- Predicate Information (identified by operation id): ------------------------------------------ * 1 - access("EMP"."DEPTNO"="DEPT"."DEPTNO") * 6 - access("EMP"."DEPTNO"="DEPT"."DEPTNO") * 7 - filter("EMP"."SAL">1000) * 8 - filter("EMP"."SAL">1000) Runtime Plan Plan Hash Value : 2219294842 ------------------------------------------------------- | Id | Operation | Name | E-Card | A-Card | ------------------------------------------------------- | 0 | SELECT STATEMENT | | | 0 | | * 1 | HASH JOIN | | 13 | 0 | | 2 | TABLE ACCESS FULL | DEPT | 4 | 0 | | * 3 | TABLE ACCESS FULL | EMP | 13 | 0 | ------------------------------------------------------- Predicate Information (identified by operation id): ------------------------------------------ * 1 - access("EMP"."DEPTNO"="DEPT"."DEPTNO") * 3 - filter("EMP"."SAL">1000) REPLAY Note: ----------- - Replay used dynamic sampling - Replay forced Dynamic plan
155.5.22 REPORT_DIAGNOSIS_TASK Function
This function reports on a diagnostic task. It returns a CLOB
containing the desired report.
Syntax
DBMS_SQLDIAG.REPORT_DIAGNOSIS_TASK ( taskname IN VARCHAR2, type IN VARCHAR2 := TYPE_TEXT, level IN VARCHAR2 := LEVEL_TYPICAL, section IN VARCHAR2 := SECTION_ALL, object_id IN NUMBER := NULL, result_limit IN NUMBER := NULL, owner_name IN VARCHAR2 := NULL) RETURN CLOB;
Parameters
Table 155-31 REPORT_DIAGNOSIS_TASK Function Parameters
Parameter | Description |
---|---|
|
Name of task to report |
|
Type of the report. Possible values are: TEXT, HTML, XML (see Table 155-4). |
|
Format of the recommendations. Possible values are TYPICAL, BASIC, ALL (Table 155-5). |
|
Particular section in the report. Possible values are: SUMMARY, FINDINGS, PLAN, INFORMATION, ERROR, ALL (Table 155-6). |
|
Identifier of the advisor framework object that represents a given statement in a SQL Tuning Set (STS). |
|
Number of statements in a STS for which the report is generated |
|
Name of the task execution to use. If |
155.5.23 RESET_DIAGNOSIS_TASK Procedure
This procedure resets a diagnostic task.
Syntax
DBMS_SQLDIAG.RESET_DIAGNOSIS_TASK ( taskname IN VARCHAR2);
Parameters
Table 155-32 RESET_DIAGNOSIS_TASK Procedure Parameters
Parameter | Description |
---|---|
|
Name of task |
155.5.24 RESUME_DIAGNOSIS_TASK Procedure
This procedure resumes a diagnostic path.
Syntax
DBMS_SQLDIAG.RESUME_DIAGNOSIS_TASK ( taskname IN VARCHAR2);
Parameters
Table 155-33 RESUME_DIAGNOSIS_TASK Procedure Parameters
Parameter | Description |
---|---|
|
Name of task |
155.5.25 SET_DIAGNOSIS_TASK_PARAMETER Procedure
This procedure is called to update the value of a SQL diagnosis parameter of type VARCHAR2
.
The task must be set to its initial state before calling this procedure. The diagnosis parameters that can be set by this procedure are:
-
MODE
: diag scope (comprehensive, limited) -
_SQLDIAG_FINDING_MODE
: findings in the report (see "Table 155-8" for possible values)
Syntax
DBMS_SQLDIAG.SET_DIAGNOSIS_TASK_PARAMETER ( taskname IN VARCHAR2, parameter IN VARCHAR2, value IN NUMBER);
Parameters
Table 155-34 SET_DIAGNOSIS_TASK_PARAMETER Procedure Parameters
Parameter | Description |
---|---|
|
Identifier of the task to execute |
|
Name of the parameter to set |
|
New value of the specified parameter |
155.5.26 UNPACK_STGTAB_SQLPATCH Procedure
This procedure unpacks from the staging table populated by a call to the PACK_STGTAB_SQLPATCH Procedure. It uses the patch data stored in the staging table to create patches on this system. Users can opt to replace existing patches with patch data when they exist already. In this case, note that it is only possible to replace patches referring to the same statement if the names are the same (see the ACCEPT_SQL_PATCH Function & Procedure).
Syntax
DBMS_SQLDIAG.UPPACK_STGTAB_SQLPATCH ( patch_name IN VARCHAR2 := '%', patch_category IN VARCHAR2 := '%', replace IN BOOLEAN, staging_table_name IN VARCHAR2, staging_schema_owner IN VARCHAR2 := NULL);
Parameters
Table 155-35 UPPACK_STGTAB_SQLPATCH Procedure Parameters
Parameter | Description |
---|---|
|
Name of patch to unpack (% wildcards acceptable, case-sensitive) |
|
Category from which to unpack patches (% wildcards acceptable, case-insensitive) |
|
Replace patches if they already exist. Note that patches cannot be replaced if there is one in the staging table with the same name as an active patch on different SQL. The subprogram raises an error if there an attempt to create a patch that already exists. |
|
(Mandatory) Name of the table to use (case-sensitive) |
|
Schema where the table resides, or |
Usage Notes
-
Requires:
ADMINISTER
SQL
MANAGEMENT
OBJECT
privilege andSELECT
orREAD
privilege on the staging table -
By default, all SQL patches in the staging table are moved. The function commits after successfully loading each patch. If it fails in creating an individual patch, it raises an error and does not proceed to those remaining in the staging table.