Oracle Logminer Viewer and Flashback Transaction
Total Page:16
File Type:pdf, Size:1020Kb
DBA Tips Archive for Oracle Oracle LogMiner Viewer and Flashback Transaction by Jeff Hunter, Sr. Database 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.sql 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 <ENTER> to continue. Create table SCOTT.EMPLOYEES. Hit <ENTER> 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 <ENTER> to continue. Initialize new table with test data. Hit <ENTER> 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 <ENTER> to continue. Modify test data (TRANSACTION 1). Hit <ENTER> 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 <ENTER> to continue. Modify test data (TRANSACTION 2). Hit <ENTER> 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