B13 Investigating oracle by Julian Dyke

87
1 Investigating Oracle Julian Dyke Independent Consultant DB Tech Showcase - Osaka May 2013 juliandyke.com © 2013 Julian Dyke

Transcript of B13 Investigating oracle by Julian Dyke

Page 1: B13 Investigating oracle by Julian Dyke

1

Investigating Oracle Julian Dyke Independent Consultant

DB Tech Showcase - Osaka May 2013

juliandyke.com © 2013 Julian Dyke

Page 2: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

About Me Worked with Oracle databases since 1989

Independent Consultant for 8 years

Regular presenter at Conferences, Seminars and Special Interest Groups Co-Author of Pro Oracle Database 10g RAC on Linux Member of Oak Table Network since 2003

Member of UKOUG Council

Former UKOUG director Former chair of UKOUG RAC & HA SIG

Oracle ACE Expert

Oracle Certified DBA – 7.3, 8, 8i, 9i and 10i

2

Page 3: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 3

Investigating Oracle Introduction This presentation describes various techniques for investigating Oracle

features

Investigative techniques differ depending on feature under investigation

Why investigate? Feature evaluation

Performance

Compatibility

Licensing costs

Page 4: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Investigating Oracle Agenda

Initial Research Advanced Research Oracle Tools

Execution Plans Trace Dumps

Internals Operating System Tools Custom Tools

4

Page 5: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 5

Initial Research

Page 6: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 6

Initial Research Read the Oracle Documentation Read ALL of the Oracle documentation for the feature

Useful sources include:

New Features Guide 11g Release 1 (11.2) Part Number E10881-01

Other Oracle documents

What's New in... sections White Papers

Oracle Technology Network

http://www.oracle.com/technetwork My Oracle Support

http://support.oracle.com

Review the SQL syntax that supports the feature in: SQL Language Reference PL/SQL Packages and Types Reference

Page 7: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Initial Research Read Third Party Documentation Read any available third party documentation for the feature

Blogs Websites Books Presentations White Papers

Make information is accurate and credible

7

Page 8: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Initial Research Learn how the new feature works Build a test environment

Build VMs Configure storage Install Oracle software Install required patch sets / one-offs Create database(s)

Configure feature

Set parameters

Test feature DDL DML etc

8

Page 9: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Initial Research Licensing Check the licensing requirements for the feature being investigated

Almost all recent new features have been licensed options Investigation may not be cost-effective

Check the Oracle Licensing document

Check the global pricing document

Check your existing license agreement

Gotchas include:

VMWare environments Primary and standby configurations RAC/Exadata environments

9

Page 10: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 10

Advanced Research

Page 11: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 11

Advanced Research Supported Parameters Supported parameter values are reported in

V$PARAMETER V$SYSTEM_PARAMETER V$SPPARAMETER

Normalized lists of values are reported in

V$PARAMETER2 V$SYSTEM_PARAMETER2

In Oracle 10.1 and above, valid values for supported parameters are reported

in V$PARAMETER_VALID_VALUES

Page 12: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 12

Advanced Research Unsupported Parameters Unsupported parameter values are reported in

X$KSPPI X$KSPPSV X$KSPPCV

To print parameter name, description and values use:

In Oracle 10.1 and above, valid values for unsupported parameters are reported in X$KSPVLD_VALUES

SELECT i.ksppinm, i.ksppdesc, sv.ksppstvl FROM x$ksppi i, x$ksppsv sv WHERE i.indx = sv.indx ORDER BY i.ksppinm;

Page 13: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 13

Advanced Research Optimizer Parameters In Oracle 10.1 and above, optimizer parameter values are reported at

Instance-level Session-level Statement-level

Dynamic performance views only report supported parameters

Fixed (X$) tables include unsupported parameters:

Level Supported Parameters Unsupported Parameters

Instance V$SYS_OPTIMIZER_ENV X$QKSCESYS

Session V$SES_OPTIMIZER_ENV X$QKSCESES

Statement V$SQL_OPTIMIZER_ENV X$KQLFSQCE

Page 14: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 14

Advanced Research Data Dictionary View Definitions Data dictionary views are based on data dictionary objects

DBA_% Accessible by all users

ALL_% Accessible by current user

USER_% Accessible by users with DBA privilege only

View definitions can be found in DBA_VIEWS

SET LONG 50000 SELECT text FROM dba_views WHERE view_name = 'DBA_INDEXES';

Many views are defined in terms of other views e.g. DBA_SEGMENTS is now based on SYS_DBA_SEGS

Page 15: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 15

Advanced Research Data Dictionary Objects The data dictionary is created in the SYSTEM tablespace

Objects are owned by SYS

Data dictionary tables include:

Table Name Description OBJ$ Objects

TAB$ Tables

IND$ Indexes

COL$ Columns

USER$ Users

SEG$ Segments

FET$ Free Extents

UET$ Used Extents

TS$ Tablespaces

FILE$ Files

Page 16: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 16

Advanced Research Data Dictionary Index Clusters Many data dictionary objects are stored in index clusters For example C_OBJ# is a cluster containing the following tables:

ASSEMBLY$ ICOLDEP$ REFCON$

ATTRCOL$ IND$ SUBCOLTYPE$

CLU$ LIBRARY$ TAB$

COL$ LOB$ TYPE_MISC$

COLTYPE$ NTAB$ VIEWTRCOL$

ICOL$ OPQTYPE$

ICOLDEP$

Other clusters include: C_USER# C_FILE#_BLOCK# C_TS#

Index clusters increase complexity of: Trace files Redo/undo changes

Page 17: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 17

Advanced Research SQL.BSQ During database creation, data dictionary objects are defined in

$ORACLE_HOME/rdbms/admin/sql.bsq

In Oracle 11.1 and above, sql.bsq has been divided into the following files:

dcore.bsq drac.bsq daw.bsq

dsqlddl.bsq dsec.bsq dsummgt.bsq

dmanage.bsq doptim.bsq dtools.bsq

dplsql.bsq dobj.bsq dexttab.bsq

dtxnspc.bsq djava.bsq ddm.bsq

dfmap.bsq dpart.bsq dlmnr.bsq

denv.bsq drep.bsq

The core data dictionary tables (obj$, tab$, ind$, col$ etc) are specified in dcore.bsq

Page 18: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 18

Advanced Research Dynamic Performance View Definitions Definitions reported by

V$FIXED_VIEW_DEFINITION

Global (GV$) view selects from underlying fixed (X$) tables

SELECT view_definition FROM v$fixed_view_definition WHERE view_name = 'GV$RESULT_CACHE_MEMORY';

VIEW_DEFINITION SELECT inst_id, qesrcmem_lad, qesrcmem_cnk, qesrcmem_blk, DECODE (qesrcmem_sta, 0, 'NO', 1, 'YES'), qesrcmem_oid, qesrcmem_pos FROM x$qesrcmem;

SELECT view_definition FROM v$fixed_view_definition WHERE view_name = 'V$RESULT_CACHE_MEMORY';

VIEW_DEFINITION SELECT id, chunk, offset, free, object_id, position FROM gv$result_cache_memory WHERE inst_id = USERENV ('Instance')

Instance (V$) view selects from global (GV$) view

Page 19: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Advanced Research PL/SQL Package Definitions PL/SQL package headers are distributed in $ORACLE_HOME/rdbms/admin For example:

dbmsmntr.sql contains package header for DBMS_MONITOR dbmsxplan.sql contains package header for DBMS_XPLAN

Names are not intuitive

Use grep to find specific packages Package headers contain comments that can be useful when documentation

is inadequate e.g.: new parameters ambiguity

Most package bodies are wrapped:

Unwrap tools can be found on the internet Probably break terms of licence agreements

19

Page 20: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 20

Advanced Research Error Messages Error messages describe what a feature does not support

Can be useful to understand how feature is implemented See $ORACLE_HOME/rdbms/mesg/oraus.msg

Contains all message and event definitions Individual messages reported by oerr on Unix platforms Binary file - on Unix use strings utility to inspect contents

$ strings $ORACLE_HOME/rdbms/mesg/oraus.msg > /tmp/oraus.txt

A range of error numbers is initially allocated for each feature Some features have more than one range

Page 21: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 21

Advanced Research Fixed Tables and Views V$FIXED_TABLES reports all fixed objects including

Fixed (X$) tables Derived fixed (X$) tables (fixed views) Global (GV$) dynamic performance views Instance (V$) dynamic performance views

V$INDEXED_FIXED_COLUMN reports all indexed columns for

Fixed (X$) tables

Page 22: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 22

Advanced Research Fixed Tables and Views Fixed (X$) tables are described in X$KQFTA

Fixed (X$) derived tables are described in X$KQFDT

Fixed (X$) table columns arev described in X$KQFCO

Internal data type reported in KQFCODTY Indexes reported in KQFCOIDX and KQFCOIPO

SELECT co.kqfconam, CASE co.kqfcodty WHEN 2 THEN 'NUMBER' WHEN 23 THEN 'RAW ('||kqfcosiz||')' END FROM x$kqfco co, x$kqfta ta WHERE co.kqfcotab = ta.indx AND ta.kqftanam = 'X$QESRCMEM' ORDER BY co.kqfcocno;

Page 23: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 23

Advanced Research Fixed Tables X$KQFCO.KQFCODTY reports internal column data type Current values in Oracle 11.1

Value Description 1 VARCHAR2

2 NUMBER

12 DATE

23 RAW

112 CLOB

180 TIMESTAMP

181 TIMESTAMP WITH TIME ZONE

See http://www.juliandyke.com for more comprehensive list of internal data types (Internals/Internal Data Types)

Page 24: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 24

Advanced Research Shared Memory V$SGA

reports SGA memory sizes

V$SGAINFO reports SGA memory pool sizes introduced in Oracle 10.1

V$SGASTAT reports SGA memory area sizes enhanced in Oracle 10.1

Page 25: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 25

Advanced Research Shared Memory X$KSMFSV

Contents of fixed SGA

X$KSMGE Reports granule allocation

X$KSMMEM

Contents of SGA presented as an array Usually fails in Windows Usually fails with Automatic Memory Management

X$KSMSP

Reports shared pool memory usage

X$KSMHP Reports heap allocation

Page 26: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Advanced Research AWR Tables Some AWR tables contain useful information including:

DBA_HIST_PLAN_OPERATION_NAME

Execution plan operations

DBA_HIST_PLAN_OPTION_NAME Execution plan operation options

DBA_HIST_SQLCOMMAND_NAME

Command types and names – similar to AUDIT_ACTIONS

DBA_HIST_TOPLEVELCALL_NAME Top level calls

26

Page 27: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 27

Advanced Research Dynamic Performance Views V$BGPROCESS (X$KSBDD)

List of background processes

V$LATCHNAME (X$KSLLD) List of parent latches

V$EVENT_NAME (X$KSLED)

List of wait events

V$LOCK_TYPE (X$KSIRESTYP) List of lock/enqueue types (Oracle 10.1 and above)

V$SQL_HINT

List of supported hints

V$SQL_FEATURE List of supported features

Page 28: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 28

Advanced Research Dynamic Performance Views V$TYPE_SIZE (X$KQFSZ)

Size in bytes of various data types

V$RESERVED_WORDS (X$KWDDEF) List of keywords / reserved words

X$MESSAGES

List of messages exchanged between processes

Page 29: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 29

Advanced Research Standard Package $ORACLE_HOME/rdbms/admin/standard.sql

Contains definitions of standard PL/SQL types, built-in functions and exceptions

For example: DECODE SUBSTR INSTR UPPER LOWER

In Oracle 11.2 standard.sql calls

stdspec.sql stdbody.sql

Page 30: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 30

Oracle Tools Execution Plans

Page 31: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 31

Execution Plans AUTOTRACE SQL*Plus feature Prints execution plans and execution statistics

$ORACLE_HOME/rdbms/admin/utlxplan.sql

Requires PLUS_TRACE role

$ORACLE_HOME/sqlplus/admin/plustrce.sql

GRANT plustrce TO user1;

In Oracle 10.1 and above execution plan output uses DBMS_XPLAN format: Automatically includes:

Partition information Parallel execution information

Requires access to a PLAN_TABLE

Page 32: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 32

Execution Plans AUTOTRACE Options are:

SET AUTOTRACE OFF No report generated - default SET AUTOTRACE ON EXPLAIN Shows execution plan

SET AUTOTRACE ON STATISTICS Shows execution statistics SET AUTOTRACE ON Shows output, execution plan and statistics SET AUTOTRACE ON TRACEONLY Suppresses query output if executed

The EXPLAIN option generates a new execution plan Optimizer environment may differ from user sessions

SET AUTOTRACE TRACEONLY EXPLAIN

Shows execution plan for SELECT statements Shows execution plan AND executes DML statements

Note that SET AUTOT TRACE EXP executes DML statements Any changes must be rolled back manually

Alternatively explain the SELECT subquery only

Page 33: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 33

Execution Plans DBMS_XPLAN Introduced in Oracle 9.2

Enhanced in subsequent releases

Useful subroutines include

DISPLAY - display a plan from a PLAN_TABLE DISPLAY_AWR - display a plan from the AWR DISPLAY_CURSOR - display a plan from the library cache DISPLAY_PLAN – returns plan as a CLOB DISPLAY_SQLSET – displays a plan from a SQL tuning set DISPLAY_SQLPLAN_BASELIN E – displays a plan from SQL baseline

For example to display execution plan for statement most recently executed

by the current session:

SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR);

Page 34: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Execution Plans DBMS_XPLAN The FORMAT parameter specifies the output format for the execution plan

There are four options:

BASIC – minimum information - operation ID, operation name and option

only

TYPICAL – default – most relevant information in plan – operation id, operation name, option, #rows, # bytes, optimizer cost. Also pruning, parallel and predicate information where applicable

SERIAL – same as TYPICAL except parallel information is not displayed even if plan executes in parallel

ALL – same as TYPICAL. Also includes PROJECTION, ALIAS and REMOTE SQL if applicable

34

Page 35: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Execution Plans DBMS_XPLAN The following keywords can be added to the FORMAT options for more fine-

grained control of output ROWS – number of rows estimated by optimizer BYTES – number of bytes estimated by optimizer COST – optimizer cost information PARTITION – partition pruning information PARALLEL – parallel execution information PREDICATE – predicate section PROJECTION – projection section ALIAS – Query Block Name / Object Alias section REMOTE – distributed query information NOTE - note section of plan

Sections are only displayed if relevant To exclude a section prefix with minus e.g. -PROJECTION

35

Page 36: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Execution Plans DBMS_XPLAN For DISPLAY_CURSOR and DISPLAY_SQLSET, additional data can be output

using the following keywords: IOSTATS – shows IO statistics for all executions of the cursor MEMSTATS – displays memory management statistics including

execution mode of operator how much memory was used number of bytes spilled to disk

ALLSTATS – both IOSTATS and MEMSTATS LAST – only report statistics for last execution of cursor

IOSTATS, ALLSTATS and LAST require basic plan statistics which are

collected by setting STATISTICS_LEVEL parameter to ALL specifying GATHER_PLAN_STATISTICS hint

MEMSTATS requires PGA memory management to be enabled by:

setting PGA_AGGREGATE_TARGET to non-zero value 36

Page 37: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 37

Oracle Tools Trace

Page 38: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 38

SQL Trace Diagnostics Area In Oracle 11.1 and above trace files and alert logs are written to the

diagnostics area: Default location is $ORACLE_HOME/diag

Trace files and alter log are written to

$ORACLE_BASE/diag/rdbms/<database>/<instance>/trace

For example for database TEST $ORACLE_BASE/diag/rdbms/test/TEST/trace

BACKGROUND_DUMP_DEST and USER_DUMP_DEST parameters still

available in Oracle 11.2

New dynamic performance view V$DIAG_INFO reports trace locations Trace file for current process can be identified using:

SELECT value FROM v$diag_info WHERE name = 'Default Trace File';

Page 39: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 39

SQL Trace Trace File Identifier In Oracle 8.1.5 and above a suffix can be added to the trace file name using

TRACEFILE_IDENTIFIER parameter For example given the following current trace filename:

ALTER SESSION SET tracefile_identifier = 'TEST1';

TEST_ora_24672.trc

The following statement:

will add the suffix "_TEST1" to the file name e.g.

TEST_ora_24672_TEST1.trc

Page 40: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 40

DBMS_MONITOR Overview Introduced in Oracle 10.1

Contains the following subroutines:

SESSION_TRACE_ENABLE SESSION_TRACE_DISABLE DATABASE_TRACE_ENABLE DATABASE_TRACE_DISABLE CLIENT_ID_TRACE_ENABLE CLIENT_ID_TRACE_DISABLE CLIENT_ID_STAT_ENABLE CLIENT_ID_STAT_DISABLE SERV_MOD_ACT_TRACE_ENABLE SERV_MOD_ACT_TRACE_DISABLE SERV_MOD_ACT_STAT_ENABLE SERV_MOD_ACT_STAT_DISABLE

DBMS_MONITOR trace configuration is reported in DBA_ENABLED_TRACES

Page 41: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 41

DBMS_MONITOR Enabling and Disabling Trace To enable and disable trace in the current session use:

EXECUTE DBMS_MONITOR.SESSION_TRACE_ENABLE;

EXECUTE DBMS_MONITOR.SESSION_TRACE_DISABLE;

To enable and disable trace in another session e.g. 42 use:

EXECUTE DBMS_MONITOR.SESSION_TRACE_ENABLE (SESSION_ID => 42);

EXECUTE DBMS_MONITOR.SESSION_TRACE_DISABLE (SESSION_ID => 42);

To enable and disable trace for instance RAC1 use:

EXECUTE DBMS_MONITOR.DATABASE_TRACE_ENABLE (INSTANCE_NAME => 'RAC1');

EXECUTE DBMS_MONITOR.DATABASE_TRACE_DISABLE (INSTANCE_NAME => 'RAC1');

To enable and disable trace for the entire database use:

EXECUTE DBMS_MONITOR.DATABASE_TRACE_ENABLE;

EXECUTE DBMS_MONITOR.DATABASE_TRACE_DISABLE;

Page 42: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 42

DBMS_MONITOR Client Identifiers Trace can be enabled for using client identifiers

Useful when many sessions connect using the same Oracle user Useful with connection caches

To set a client identifier use DBMS_SESSION.SET_IDENTIFIER e.g.:

The client identifier is reported in V$SESSION.CLIENT_IDENTIFIER

DBMS_SESSION.SET_IDENTIFIER ('CLIENT42');

To enable and disable trace for CLIENT42 use:

DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE (CLIENT_ID => 'CLIENT42');

DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE (CLIENT_ID => 'CLIENT42');

To enable and disable statistics collection for CLIENT42 use:

DBMS_MONITOR.CLIENT_ID_STAT_ENABLE (CLIENT_ID => 'CLIENT42');

DBMS_MONITOR.CLIENT_ID_STAT_DISABLE (CLIENT_ID => 'CLIENT42');

Client statistics are reported in V$CLIENT_STATS

Page 43: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 43

DBMS_MONITOR Services, Modules & Actions Trace can be enabled for a specific

service service and module service, module and action

Current module and action reported by V$SESSION.MODULE V$SESSION.ACTION

To specify a new action within the current module use:

DBMS_APPLICATION_INFO.SET_ACTION (ACTION_NAME=> 'ACTION2');

DBMS_APPLICATION_INFO.SET_MODULE (MODULE_NAME => 'MODULE1', ACTION_NAME=> 'ACTION1');

To specify a module and action with a session use:

Page 44: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 44

DBMS_MONITOR Services, Modules & Actions To enable trace for use:

To enable statistics collection use:

DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE (SERVICE_NAME => 'SERVICE1',MODULE_NAME => 'MODULE1',ACTION_NAME => 'ACTION1‘);

DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE (SERVICE_NAME => 'SERVICE1',MODULE_NAME => 'MODULE1‘);

DBMS_MONITOR.SERV_MOD_ACT_STAT_ENABLE (SERVICE_NAME => 'SERVICE1',MODULE_NAME => 'MODULE1',ACTION_NAME => 'ACTION1‘);

DBMS_MONITOR.SERV_MOD_ACT_STAT_ENABLE (SERVICE_NAME => 'SERVICE1',MODULE_NAME => 'MODULE1‘);

DBMS_MONITOR.SERV_MOD_ACT_STAT_ENABLE (SERVICE_NAME => 'SERVICE1');

DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE (SERVICE_NAME => 'SERVICE1');

Statistics are reported in V$SERV_MOD_ACT_STATS

Page 45: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 45

DBMS_MONITOR Row Source Statistics In Oracle 11.2 and above PLAN_STAT parameter is supported in:

CLIENT_ID_TRACE_ENABLE DATABASE_TRACE_ENABLE SERV_MOD_ACT_TRACE_ENABLE SESSION_TRACE_ENABLE

PLAN_STAT specifies frequency of row source statistic dumps. Can be: NEVER FIRST_EXECUTION (default) ALL_EXECUTIONS

In Oracle 11.2 and above event 10046 level 16 also dumps row source

statistics for all executions To dump binds + waits + statistics use event 10046 level 28

ALTER SESSION SET EVENTS ‘10046 trace name context forever, level 28’’;

Page 46: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 46

DBMS_MONITOR Interpreting Trace Use TKPROF to sort and filter the trace file

Avoid re-explaining execution plans

Use TRCSESS to merge trace files Useful with shared server (MTS)

See Metalink Note: 39817.1 Interpreting Raw SQL_TRACE and

DBMS_SUPPORT.START_TRACE output Describes contents of trace file

See Optimizing Oracle Performance

Cary Millsap & Jeff Holt (O'Reilly)

Page 47: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 47

Oracle Tools Dumps

Page 48: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 48

ORADEBUG Overview Undocumented SQL*Plus feature Requires SQLDBA privilege

Allows session to connect to other processes and execute commands

A process must be specified for many ORADEBUG commands using one of:

ORADEBUG SETMYPID – connect to local process ORADEBUG SETORAPID – connect using Oracle process ID ORADEBUG SETOSPID – connect using OS pid

All commands are prefixed with ORADEBUG

To obtain a list of commands use ORADEBUG HELP

New functionality is frequently added Review in each new release

Page 49: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 49

ORADEBUG Dumps To list of available dumps can be obtained using ORADEBUG DUMPLIST Most dumps can be performed by

ORADEBUG ALTER SYSTEM ALTER SESSION

Some dumps can be very useful for specific investigations

Dumps are mostly undocumented

187 dumps listed in Oracle 11.2.0.3 Some dumps modify database Only use on

non-production databases when requested by Oracle Support

Page 50: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 50

Memory Dumps Trace Dumps Memory dumps can be taken using

ORADEBUG ALTER SYSTEM ALTER SESSION

For example to dump fixed SGA including contents

ALTER SESSION SET EVENTS 'immediate trace name global_area level 2‘;

To dump summary of SGA heap use:

ALTER SESSION SET EVENTS 'immediate trace name heapdump level 2‘;

To dump contents of a specific SGA subheap use:

ALTER SESSION SET EVENTS 'immediate trace name heapdump_addr level 2, addr <address>';

Page 51: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 51

File Dumps Redo Log Dumps To identify the current redo log use:

SELECT member FROM v$logfile WHERE group# = ( SELECT group# FROM v$log WHERE status = 'CURRENT' );

To dump the contents of a redo log use:

ALTER SYSTEM DUMP LOGFILE '<logfilename>';

Also works for archived log files

Page 52: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 52

File Dumps Redo Log Dumps Full syntax is:

ALTER SYSTEM DUMP LOGFILE 'FileName' SCN MIN MinimumSCN SCN MAX MaximumSCN TIME MIN MinimumTime TIME MAX MaximumTime LAYER Layer OPCODE Opcode DBA MIN FileNumber . BlockNumber DBA MAX FileNumber . BlockNumber RBA MIN LogFileSequenceNumber . BlockNumber RBA MAX LogFileSequenceNumber . BlockNumber;

See MOS Note 1031381.6 for further details

Page 53: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 53

File Dumps Block Dumps To dump a database block in Oracle 8.0 or above:

ALTER SYSTEM DUMP DATAFILE <absolute_file_number> BLOCK <block_number>;

ALTER SYSTEM DUMP DATAFILE <absolute_file_number> BLOCK MIN <minimum_block_number> BLOCK MAX <maximum_block_number>;

To dump a range of database blocks:

To dump a block from a datafile in a closed database:

ALTER SYSTEM DUMP DATAFILE '<file_name>' BLOCK <block_number>;

Page 54: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 54

Internals

Page 55: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals Overview Oracle dumps often include data. Common data types include

VARCHAR2 CHAR NUMBER DATE ROWID

Oracle dumps also include some common metadata structures including:

SCN DBA UBA RBA SQLID Command Types Unix Time

55

Page 56: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals Internal Data Type Each data type has an internal number

Internal data type numbers appear in many structures and dumps e.g. bind variables

Examples include:

56

Internal Type Description

1 VARCHAR2, NVARCHAR2

2 NUMBER

8 LONG

12 DATE

23 RAW

24 LONG RAW

58 ANYDATA

69 ROWID

96 CHAR, NCHAR

100 BINARY FLOAT

101 BINARY DOUBLE

111 XMLTYPE

Internal Type Description

112 CLOB, NCLOB

113 BLOB

114 BFILE

121 TYPE (User-defined)

122 TYPE (TABLE OF RECORD)

123 TYPE (VARRAY)

180 TIMESTAMP

181 TIMESTAMP WITH TZ

182 INTERVAL YEAR TO MONTH

183 INTERVAL DAY TO SECOND

208 UROWID

231 TIMESTAMP WITH LOCAL TZ

Page 57: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 57

Internals Hexadecimal Conversions To convert from hexadecimal to decimal use TO_NUMBER with the 'XXXX'

format mask e.g. SELECT TO_NUMBER (100,'XXX') FROM dual;

TO_NUMBER(100,'XXX') -------------------- 256

To convert from decimal to hexadecimal use TO_CHAR with the 'XXXX' format mask e.g.

SELECT TO_CHAR (256,'XXX') FROM dual;

TO_NUMBER(256,'XXX') -------------------- 100

Page 58: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals DUMP Function Column values can be dumped using the SQL DUMP function

Syntax is:

58

FUNCTION DUMP (expr[,return_format[,start_position[,length]]])

where RETURN_FORMAT is:

Format Description 8 Octal Notation

10 Decimal Notation

16 Hexadecimal Notation

17 Single Characters

For example:

SELECT DUMP (10000,16) FROM dual;

DUMP(10000,16) ----------------- Typ=2 Len=2: c3,2

Page 59: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals String Data Types VARCHAR2 (Internal Type 1)

Stores characters plus length byte(s) e.g. “Ferrari” in a VARCHAR2(20)

59

Len F e r r a r i

07 46 65 72 72 61 72 69

CHAR (InternalType 96) Pads data with space characters e.g. “Ferrari” in a CHAR(20)

Len F e r r a r i

07 46 65 72 72 61 72 69 20 20 20 20 20 20 20 20 20 20 20 20 20

Page 60: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals NUMBER Data Type NUMBER (Internal Type 2)

Oracle proprietary number format Platform independent representation Designed to ensure numbers collate Up to 38 bytes can be stored Numbers are stored as base-100 digits

Each digit is increased by 1 to ensure non-zero Numbers prefixed by

Sign bit Exponent

60

Number Value

Positive Infinity FF 65

Zero 80

Negative Infinity 01 01 66

Page 61: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals NUMBER Data Type Some examples:

61

Value Internal Representation

0 80

1 C1,02

2 C1,03

3 C1,04

8 C1,09

9 C1,0A

99 C1,64

100 C2,02

101 C2,02,02

102 C2,02,03

999 C2,02,64

1000 C2,0B

1001 C2,0B,2

Value Internal Representation

9999 C2,64,64

10000 C3,02

90000 C3,0A

100000 C3,0B

-1 3E,64,66

-2 3E,63,66

-3 3E,62,66

-4 3E,61,66

-99 3E,2,66

-100 3D,64,66

-101 3D,64,64,66

-102 3D,64,63,66

-103 3D,64,62,66

Page 62: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals DATE Data Type DATE (Internal Type 12)

Oracle can store dates ranging from Jan 1, 4712BC to Dec 31 4712AD Dates are always stored in 7 byte values Always prefixed by (redundant) length byte Format is:

62

Byte Value 0 Year / 100

1 Year % 100

2 Month

3 Day

4 Hour

5 Minute

6 Second

For example 26May2013 05:06:38 is

Len 0 1 2 3 4 5 6

07 DD 07 05 1A 05 06 26

Page 63: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 63

Internals ROWID Data Type ROWID (Internal Type 69)

Extended ROWIDs were introduced in Oracle 8.0

Use relative file number / block number / slot number

Absolute file number derived from relative file number and tablespace

number Can be accessed using DBMS_ROWID package

Useful functions include: ROWID_RELATIVE_FNO ROWID_BLOCK_NUMBER ROWID_ROW_NUMBER ROWID_TO_ABSOLUTE_FNO

Page 64: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 64

Internals ROWID Data Type For example:

SELECT ROWID, dbms_rowid.rowid_relative_fno (ROWID) AS "File Number", dbms_rowid.rowid_block_number (ROWID) AS "Block Number", dbms_rowid.rowid_row_number (ROWID) AS "Row Number" FROM gp.team;

ROWID File Number Block Number Row Number ------------------ ----------- ------------ ---------- AAAR1GAAGAAAAR4AAA 6 1144 0 AAAR1GAAGAAAAR4AAB 6 1144 1 AAAR1GAAGAAAAR4AAC 6 1144 2 AAAR1GAAGAAAAR4AAD 6 1144 3 ......

Page 65: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals Database Block Address Database Block Address (DBA)

Address of block in database Always 32-bit value

Even on 64 bit platform

SMALLFILE tablespaces (default) Upper 10 bits represent relative file number Lower 22 bits represent block number For example if DBA is 0x01000227

65

BIGFILE tablespaces All 32 bits represent block number Some dumps / tools may not report DBA correctly

File number = dba >> 22 = 4 Block number = dba & 0x3FFFFF = 551

Page 66: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals SQLID 13 character representation of a 64-bit unsigned integer

Uses a mapping of 32 characters to 0..31

Uses the characters 0-9 and a-z with the exception of e, i, l and o Used in Oracle 10.1 and above to identify SQL parent cursors

Also used to generate database replay file names

5-bit characters in SQL ID map to 8 bit characters in unsigned integer Right to left Top bit of left hand word is not used

Page 67: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals SQLID Conversion

0 1 2 3 4 5 6 7 00000 00001 00010 00011 00100 00101 00110 00111

8 9 a b c d f g 01000 01001 01010 01011 01100 01101 01110 01111

h j k m n p q r 10000 10001 10010 10011 10100 10101 10110 10111

s t u v w x y z 11000 11001 11010 11011 11100 11101 11110 11111

The following table shows the mapping between characters and byte values:

Page 68: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals SQLID Conversion Example

“6j02q68mxgzt4“ = 0xcdc4598000000001

6 j 0 2 q 6 8 m x g z t 4 00110 10001 00000 00010 10110 00110 01000 10011 11101 01111 11111 11001 00100

01101000 10000000 01010110 00110010 00100111 11010111 11111111 00100100

68 80 56 32 27 d7 ff 24

Page 69: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals SQLID Conversion The following C function converts a 64-bit unsigned integer into a 13-

character SQL ID

void ConvertLongToSQLID (unsigned long l,char *s) { char *map = "0123456789abcdfghjkmnpqrstuvwxyz"; int i;

for (i = 0;i < 13;i++) { unsigned long k = (l >> (60 - i * 5)) & 0x1F; s[i] = map[k]; } s[13] = 0; }

Page 70: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals SQLID Conversion The following C function converts a 13-character SQL ID into a 64-bit

unsigned integer

void ConvertSQLIDToLong (char *s,unsigned long *l) { char *map = "0123456789abcdfghjkmnpqrstuvwxyz"; unsigned long res = 0; int i,j;

for (i = 0;i < 13;i++) { for (j = 0;j < 32;j++) { if (s[i] == map[j]) break; } res = (res << 5) | j; } *l = res; }

Page 71: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals Command Types Command types are listed the SYS.AUDIT_ACTIONS table

Name Type ACTION NUMBER

NAME VARCHAR2(28)

Common actions include:

Action Name 1 CREATE TABLE

2 INSERT

3 SELECT

6 UPDATE

7 DELETE

9 CREATE INDEX

10 DROP INDEX

12 DROP TABLE

17 GRANT OBJECT

19 CREATE SYNONYM

Page 72: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Internals Unix Time Unix (POSIX) time – time in seconds since the Epoch

Not counting leap seconds

Epoch was midnight on 1st January 1970

To determine current Unix time on Linux:

#include <stdio.h> #include <sys/time.h>

main () { struct timeval tv; struct timezone tz;

gettimeofday (&tv,&tz);

printf ("Seconds = %ld (0x%lx)¥n",tv.tv_sec, tv.tv_sec); printf ("MicroSeconds = %ld (0x%lx)¥n",tv.tv_usec,tv.tv_usec);

}

Page 73: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 73

Operating System Tools

Page 74: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Operating System Tools strings and od On Unix and Linux useful operating system tools for investigations include:

strings – print ASCII strings in a binary file

default string length is 4 – may miss short strings use –n parameter to specify shorter minimum length e.g. –n 3

od – dumps blocks from a file in specified format

Formats include: -c – ASCII characters or backslash escapes -d – Unsigned decimal 2-byte units -o – Octal 2 byte units -x – Hexadecimal 2 byte units

74

Page 75: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 75

Operating System Tools tcpdump Allows packets to be captured and displayed

On busy systems packets can be stored in a file and examined

subsequently

Filters can be applied to Reduce number of packets captured Increase level of data captured

Promiscuous - network packets between other network interfaces can be

captured Based on libpcap

Similar to snoop on Solaris

Useful for investigating

RAC interconnect traffic Oracle Streams packets

Page 76: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 76

Custom Tools

Page 77: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Custom Tools If no suitable tools exist you can always develop your own….

C is the best language for most tools

Oracle is developed in C Structures align better in C Avoids unnecessary indirection

GNU compilers are usually available

Ensure development platform is:

Same architecture as target e.g. x86-64, SPARC, Itanium Same word size e.g. 32 or 64 bit

Ensure correct compiler switches are specified

Word size Word alignment

77

Page 78: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Custom Tools RedoAnalyzer

Analyses online redo log / archive redo log

Summarizes redo generation by: undo / redo object operation

Version specific

Usually requires porting for different platforms

LogMiner provides similar functionality LogMiner has high memory requirements RedoAnalyzer is less-intrusive RedoAnalyzer is faster

78

Page 79: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Custom Tools Block Dumper

Dumps database blocks Similar output format to ALTER SYSTEM DUMP command Can be adapted

For example non-intrusive analysis of chaining and migration

SGA Attach Attaches to shared memory Follows memory pointers from fixed SGA Dumps blocks of memory Dumps sessions, processes, parent and child cursors etc Dumps RAC structures Useful for research and for identifying bugs

79

Page 80: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Custom Tools TCP Dump

Based on libpmap Intercepts and dumps messages Useful for

Clusterware / RAC research Oracle Streams research

IO Performance

Reads random blocks from large files Measures I/O rates Fine grained Allows I/O rate to be calculated for

buffered I/O SAN cache SAN disk

80

Page 81: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke

Custom Tools ReplayAnalyzer

Extracts contents of database replay files (record and workload metadata) Writes output in pseudo-XML format Useful for understanding

database capture – what is captured and what is omitted database replay – what can be replayed

TrailAnalyzer

Dumps contents of Oracle GoldenGate trail Very similar to Oracle GoldenGate logdump utility

81

Page 82: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 82

Thank you for listening

[email protected]

Page 83: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 83

Backup

Page 84: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 84

Execution Plans AUTOTRACE Statistics reported are:

Statistic Name

recursive calls

db block gets

consistent gets

physical reads

redo size

bytes sent via SQL*Net to client

bytes received via SQL*Net from client

SQL*Net roundtrips to/from client

sorts(memory)

sorts(disk)

rows processed

Page 85: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 85

Execution Plans AUTOTRACE In Oracle 9.2 and below execution plan consists of four columns:

Column Name Description ID_PLUS_EXP ID of execution step

PARENT_ID_PLUS_EXP Parent ID of execution step

PLAN_PLUS_EXP Execution step

OBJECT_NODE_PLUS_EXP Database links or parallel query servers

Output can optionally be suppressed e.g.:

COLUMN PARENT_ID_PLUS_EXP NOPRINT

For parallel and remote operations additional information is displayed in another section:

Column Name Description OTHER_PLUS_EXP Shows the SQL query

OTHER_TAG_PLUS_EXP Shows reason for OTHER_PLUS_EXP

Page 86: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 86

Parameters Parameter Types Parameter types are reported numerically in

V$PARAMETER.TYPE X$KSPPI.KSPPITY

Values are:

Value Description 1 Boolean

2 String

3 Integer

4 File

6 Big Integer

Page 87: B13 Investigating oracle by Julian Dyke

juliandyke.com © 2013 Julian Dyke 87

Parameters Optimizer Parameters Optimizer parameters are grouped in FID_QKSCESYROW For example in Oracle 11.1

FID_QKSCESYROW COUNT(*)

QKSFM_ALL 101

QKSFM_CBO 71

QKSFM_CVM 1

QKSFM_EXECUTION 1

QKSFM_FBA 1

QKSFM_INDEX 1

QKSFM_JPPD 3

QKSFM_OR_EXPAND 1

QKSFM_PQ 6

QKSFM_STAR_TRANS 3

QKSFM_TRANSFORMATION 70