DBA Tips Archive for Oracle

Oracle LogMiner Viewer and Flashback Transaction

by Jeff Hunter, Sr. Administrator Contents

• Introduction • Requirements

• Flashback Transaction Example Using Oracle LogMiner Viewer

• About the Author Introduction

LogMiner is a built-in database tool introduced in Oracle8i that provides a relational interface for users to query redo records found in online and archived redo log files. LogMiner is most often used for auditing purposes, data analysis, or recovering data from a user error.

The LogMiner utility can be accessed through SQL statements (command-line) or through the Oracle LogMiner Viewer graphical user interface (GUI). Prior to Oracle Database 11g, Oracle LogMiner Viewer was a separate Java client application that provided a GUI to the the LogMiner utility. Starting with Oracle Database 11g, Oracle LogMiner Viewer has been incorporated into the Oracle Enterprise Manager web-based interface and also integrated with the new Flashback Transaction feature, making it simple to recover transactions that may have been modified by mistake. New to Oracle Database 11g, Flashback Transaction allows the changes made by a transaction to be undone, optionally including changes made by dependent transactions, while the database remains online.

This tutorial provides an example-based approach on how to use Oracle LogMiner Viewer and Flashback Transaction. The example will be based on Oracle Database 11g Release 2 (11.2.0.3.0) and Oracle Enterprise Manager Database Control running on the Linux platform.

Consult the following two guides for an overview of the LogMiner tool and a tutorial on how to use the LogMiner command-line interface. • Understanding LogMiner • LogMiner by Example Requirements

The following are requirements for the source database that LogMiner will mine. Oracle LogMiner Viewer and Flashback Transaction

The source/mining database must be Oracle Database 11g or higher with Oracle Enterprise Manager Database Control or Grid Manager configured. Archivelog Mode

Archivelog mode must be enabled in order to generate usable redo log files.

SQL> select log_mode from v$database;

LOG_MODE ------NOARCHIVELOG

SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down.

SQL> startup mount ORACLE instance started.

Total System Global Area 1653518336 bytes Fixed Size 2228904 bytes Variable Size 1291849048 bytes Database Buffers 352321536 bytes Redo Buffers 7118848 bytes Database mounted.

SQL> alter database archivelog;

Database altered.

SQL> alter database open;

Database altered. SQL> select log_mode from v$database;

LOG_MODE ------ARCHIVELOG Security

A new role named LOGMNR_ADMIN will be created and assigned the appropriate privileges for LogMiner analysis. This role will be assigned to a new user named MINER that will be used to perform the LogMiner examples presented in this guide.

SQL> create role logmnr_admin;

Role created.

SQL> grant create session to logmnr_admin;

Grant succeeded.

SQL> grant select on v_$logmnr_contents to logmnr_admin;

Grant succeeded.

SQL> grant select on v_$logmnr_dictionary to logmnr_admin;

Grant succeeded.

SQL> grant select on v_$logmnr_parameters to logmnr_admin;

Grant succeeded.

SQL> grant select on v_$logmnr_logs to logmnr_admin;

Grant succeeded.

SQL> grant select on v_$archived_log to logmnr_admin;

Grant succeeded.

SQL> grant execute_catalog_role , select any dictionary , select any transaction , select any table , create tablespace , drop tablespace to logmnr_admin;

Grant succeeded.

SQL> create user miner identified by miner;

User created.

SQL> grant logmnr_admin to miner;

Grant succeeded.

-- Needed for Flashback Transaction.

SQL> grant execute on dbms_flashback to miner;

Grant succeeded.

-- Needed for Flashback Transaction.

SQL> grant select any transaction to miner;

Grant succeeded.

-- Needed for Flashback Transaction.

SQL> grant create any table to miner;

Grant succeeded.

-- Needed for Flashback Transaction.

SQL> grant flashback any table to miner;

Grant succeeded. Supplemental Logging

LogMiner is a redo-based application and as such, requires at least minimal supplemental logging be enabled on the source database. Oracle does not enable any supplemental logging by default. Additionally, supplemental logging for primary key columns must be enabled for Flashback Transaction to work.

After verifying supplemental logging is enabled, force a log switch in order for the new supplemental log configuration to begin archiving the additional column data to the redo logs. SQL> alter database add supplemental log data;

Database altered.

SQL> alter database add supplemental log data (primary key) columns;

Database altered.

SQL> select supplemental_log_data_min, supplemental_log_data_pk from v$database;

SUPPLEMENTAL_LOG_DATA_MIN SUPPLEMENTAL_LOG_DATA_PK ------YES YES

SQL> alter system switch logfile;

System altered.

Remember that supplemental logging must be enabled on the source database before generating redo log files that will be analyzed by LogMiner. Flashback Transaction Example Using Oracle LogMiner Viewer

This section demonstrates how to use Oracle LogMiner Viewer to capture DDL and DML activity and how to use Flashback Transaction to backout a bad transaction (including dependent transactions).

Initiate Example Data

In order to test the LogMiner and Flashback Transaction functionality, create a test environment in the source database. Use the following script to create a test table and perform a few transactions against it that will be analyzed by LogMiner.

oracle_logminer_viewer_script1.

CONNECT scott/tiger Connected.

SET PAGESIZE 9000 SET LINESIZE 125 COLUMN "Current User" FORMAT A13 COLUMN "Current Time" FORMAT A19 COLUMN "Current SCN" FORMAT 99999999999 COLUMN first_name FORMAT A10 COLUMN last_name FORMAT A11 COLUMN email FORMAT A21 COLUMN phone_number FORMAT A13

-- Get current user, date/time, and SCN.

SELECT user AS "Current User" , TO_CHAR(systimestamp, 'MM/DD/YYYY HH24:MI:SS') AS "Current Time" , dbms_flashback.get_system_change_number AS "Current SCN" FROM dual;

Current User Current Time Current SCN ------SCOTT 10/15/2012 16:01:10 102229443

-- Create new table and initialize with test data.

PAUSE Create table SCOTT.EMPLOYEES. Hit to continue.

Create table SCOTT.EMPLOYEES. Hit to continue.

CREATE TABLE scott.employees ( employee_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) NOT NULL , email VARCHAR2(25) NOT NULL , phone_number VARCHAR2(20) , CONSTRAINT employees_pk1 PRIMARY KEY (employee_id) );

Table created.

PAUSE Initialize new table with test data. Hit to continue.

Initialize new table with test data. Hit to continue.

INSERT INTO scott.employees VALUES (1001, 'Julia', 'Desai', '[email protected]', '209-555-1214');

1 row created.

INSERT INTO scott.employees VALUES (1002, 'Franklin', 'Vick', '[email protected]', '209-555-1219');

1 row created.

INSERT INTO scott.employees VALUES (1003, 'Beth', 'Walton', '[email protected]', '209-555-1212');

1 row created. INSERT INTO scott.employees VALUES (1004, 'Pat', 'Allen', '[email protected]', '209-555-1213');

1 row created.

INSERT INTO scott.employees VALUES (1005, 'Stacy', 'Olsen', '[email protected]', '209-555-1224');

1 row created.

COMMIT;

Commit complete.

SELECT employee_id , first_name , last_name , email , phone_number FROM scott.employees ORDER BY employee_id;

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER ------1001 Julia Desai [email protected] 209-555-1214 1002 Franklin Vick [email protected] 209-555-1219 1003 Beth Walton [email protected] 209-555-1212 1004 Pat Allen [email protected] 209-555-1213 1005 Stacy Olsen [email protected] 209-555-1224

-- The following transaction (TRANSACTION 1) will be backed out -- using Oracle LogMiner Viewer and Flashback Transaction. -- Flashback Transaction will detect a dependent transaction -- (TRANSACTION 2) and prompt the user with a list of options on -- how to continue.

-- [TRANSACTION 1]

PAUSE Modify test data (TRANSACTION 1). Hit to continue.

Modify test data (TRANSACTION 1). Hit to continue.

UPDATE scott.employees SET phone_number='209-555-9999' WHERE employee_id=1005;

1 row updated.

DELETE FROM scott.employees WHERE employee_id=1001;

1 row deleted. COMMIT;

Commit complete.

-- [TRANSACTION 2]

PAUSE Modify test data (TRANSACTION 2). Hit to continue.

Modify test data (TRANSACTION 2). Hit to continue.

DELETE FROM scott.employees WHERE employee_id=1005;

1 row deleted.

COMMIT;

Commit complete.

SELECT employee_id , first_name , last_name , email , phone_number FROM scott.employees ORDER BY employee_id;

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER ------1002 Franklin Vick [email protected] 209-555-1219 1003 Beth Walton [email protected] 209-555-1212 1004 Pat Allen [email protected] 209-555-1213

-- Get current user, date/time, and SCN.

SELECT user AS "Current User" , TO_CHAR(systimestamp, 'MM/DD/YYYY HH24:MI:SS') AS "Current Time" , dbms_flashback.get_system_change_number AS "Current SCN" FROM dual;

Current User Current Time Current SCN ------SCOTT 10/15/2012 16:03:31 102229949 Notice how the above script contains a number of SQL*Plus PAUSE commands. I inserted the PAUSE commands as a method to provide an interval of time between the different SQL operations. Without the PAUSE commands, this script would complete in under 1 second. For example, if the script was started at "10/12/2012 18:45:52" and ended at "10/12/2012 18:45:52", the DDL statement (CREATE TABLE) would occur within the same 1 second as the DML statements that we intend to backout using Flashback Transaction. If this occurs, Oracle LogMiner Viewer will throw the following exception when attempting to perform a Flashback Transaction believing that the transactions being backed out occurred before the CREATE TABLE DDL statement: Error

Flashback transaction error. 1. Cause - The Flashback Transaction Backout process encountered an interesting object which had its last DDL operation done on it after the mining start time. An interesting object is one that has been modified by either the specified transactions or any of their dependents. 2. Action - Specify transactions that have committed after the last DDL done on all the objects they touched. 3. SQLException - java.sql.SQLException: ORA-55505: DDL done on an interesting object after mining start SCN ORA-06512: at "SYS.DBMS_FLASHBACK", line 37 ORA-06512: at "SYS.DBMS_FLASHBACK", line 70 ORA-06512: at line 2

Backout Failed Transaction using Oracle LogMiner Viewer

Oracle LogMiner Viewer and Flashback Transaction will be used to backout the UPDATE — (UPDATE...WHERE employee_id=1005) and DELETE — (DELETE...WHERE employee_id=1001) statements from the script shown above. These two statements are part of TRANSACTION 1.

Before backing out the target transaction, Flashback Transaction will detect that there is a dependent transaction (DELETE...WHERE employee_id=1005) and prompt the user with a list of options on how to continue. Since the transaction we intend to backout (TRANSACTION 1) depends on the second transaction (TRANSACTION 2), we cannot backout the first transaction without first backing out the second transaction.

To access Oracle LogMiner Viewer, launch Oracle Enterprise Manager, select the Availability tab and click the View and Manage Transactions link in the Manage section. Figure 1: Oracle LogMiner Viewer

From the LogMiner screen, filter the transactions that LogMiner will return by selecting the Time Range or SCN Range that is likely to pick up the recorded transactions for LogMiner to analyze.

Use the optional Query Filter parameters to further filter the number of transaction records for LogMiner to analyze. For this example, include the Table Name and DB User to specify which table and database user to which the transactions belong.

Use the Advanced Query section to enter any additional filter information which allows you to edit the actual WHERE clause. Click the Info Icon under Advanced Query for a description of columns in V$LOGMNR_CONTENTS that can be included in the WHERE clause. In this example, I will leave the Advanced Query section set to its default value click the [Continue] button. Figure 2: Enter Search Criteria

Wait while the contents of the online and archived redo files are searched by Oracle LogMiner Viewer for transaction records matching the search criteria you entered. Figure 3: Searching Online and Archived Redo Log Files

The resulting page shows the transaction records that match the search criteria you entered. The leftmost column shows the transaction identifier (XID), which is used by Oracle to uniquely identify a transaction. The Transaction Summary column shows how many UPDATE (upd), INSERT (ins), and DELETE (del) statements were performed for each transaction record.

Click on the Transaction ID associated with the UPDATE and DELETE statements being rolled back in this example (TRANSACTION 1). In this case it is Transaction ID 070010000A3A0000 and listed as "SCOTT.EMPLOYEES (1 upd, 1 del)". Figure 4: LogMiner Results

This displays the Transaction Details page which shows the UPDATE and DELETE statements. Click the [Flashback Transaction] button. Figure 5: Transaction Details

Confirm the Flashback Transaction operation request by clicking the [Yes] button. Figure 6: Confirm Flashback Transaction Operation

The Flashback Transaction operation detected the dependent transaction (DELETE...WHERE employee_id=1005) from TRANSACTION 2. You need to decide how the Flashback Transaction operation should handle any dependent transactions.

The Flashback Transaction operation is currently using the NONCONFLICT_ONLY option. Click the [Change Recovery Option] button to change the recovery option settings. Figure 7: Dependent Transactions Detected

Select the Cascade option to backout the target transaction and all dependent transactions. Figure 8: Backout All Dependent Transactions (Cascade)

This brings you back to the Show Dependencies screen showing the Cascade option selected. Click on the target transaction and any dependent transactions to verify each statement will be executed — [exec=yes].

After verifying the target and dependent transactions, click the [Next] button. Figure 9: Confirm Cascade Option Selected

The Review screen provides you with the ability to show the Undo SQL script that will be used to backout the target (and dependency) transactions. In addition, you can provide a SQL statement that will allow you to view the effect of the changes prior to committing the Flashback Transaction.

Click the [Finish] button to commit the Flashback Transaction operation. Figure 10: Review Flashback Transaction Details

After the Flashback Transaction operation is complete, click the [OK] button on the Results screen. Figure 11: Flashback Transaction Results Screen

Clicking [OK] on the Results screen will return you to the Transaction Details screen.

Clicking [OK] again will return you to the LogMiner Results screen.

Finally, click the [Done] button to return the top level of the Availability tab screen.

Verify the Flashback Transaction successfully backed out the target (and any dependent) transactions.

SELECT employee_id , first_name , last_name , email , phone_number FROM scott.employees ORDER BY employee_id;

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER ------1001 Julia Desai [email protected] 209-555-1214 1002 Franklin Vick [email protected] 209-555-1219 1003 Beth Walton [email protected] 209-555-1212 1004 Pat Allen [email protected] 209-555-1213 1005 Stacy Olsen [email protected] 209-555-1224 About the Author

Jeffrey Hunter is an Oracle Certified Professional, Java Development Certified Professional, Author, and an Oracle ACE. Jeff currently works as a Senior for The DBA Zone, Inc. located in Pittsburgh, Pennsylvania. His work includes advanced performance tuning, Java and PL/SQL programming, developing high availability solutions, capacity planning, database security, and physical / logical database design in a UNIX, Linux, and Windows server environment. Jeff's other interests include mathematical encryption theory, programming language processors (compilers and interpreters) in Java and C, LDAP, writing web-based database administration tools, and of course Linux. He has been a Sr. Database Administrator and Software Engineer for over 18 years and maintains his own website site at: http://www.iDevelopment.info. Jeff graduated from Stanislaus State University in Turlock, California, with a Bachelor's degree in Computer Science.

Copyright (c) 1998-2013 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at [email protected].

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on Monday, 15-Oct-2012 17:18:51 EDT Page Count: 3652