Вы находитесь на странице: 1из 21

DBA Tips Archive for Oracle

Oracle LogMiner Viewer and Flashback Transaction by Jeff Hunter, Sr. atabase !d"inistrator

Contents

#ntroduction $e%uire"ents Flashback Transaction &'a"(le )sing Oracle LogMiner Viewer !bout the !uthor

Introduction
LogMiner is a built*in database tool introduced in Oracle+i that (ro,ides a relational interface for users to %uery redo records found in online and archi,ed redo log files. LogMiner is "ost often used for auditing (ur(oses, data analysis, or reco,ering data fro" a user error. The LogMiner utility can be accessed through S-L state"ents .co""and*line/ or through the Oracle LogMiner Viewer gra(hical user interface .0)#/. 1rior to Oracle atabase 22g, Oracle LogMiner Viewer was a se(arate Ja,a client a((lication that (ro,ided a 0)# to the the LogMiner utility. Starting with Oracle atabase 22g, Oracle LogMiner Viewer has been incor(orated into the Oracle &nter(rise Manager web*based interface and also integrated with the new Flashback Transaction feature, "aking it si"(le to reco,er transactions that "ay ha,e been "odified by "istake. 3ew to Oracle atabase 22g, Flashback Transaction allows the changes "ade by a transaction to be undone, o(tionally including changes "ade by de(endent transactions, while the database re"ains online. This tutorial (ro,ides an e'a"(le*based a((roach on how to use Oracle LogMiner Viewer and Flashback Transaction. The e'a"(le will be based on Oracle atabase 22g $elease 4 .22.4.5.6.5/ and Oracle &nter(rise Manager atabase 7ontrol running on the Linu' (latfor". 7onsult the following two guides for an o,er,iew of the LogMiner tool and a tutorial on how to use the LogMiner co""and*line interface.

)nderstanding LogMiner LogMiner by &'a"(le

Require ents
The following are re%uire"ents for the source database that LogMiner will "ine.

Oracle LogMiner Viewer and !lash"ac# Transaction


The source8"ining database "ust be Oracle atabase 22g or higher with Oracle &nter(rise Manager atabase 7ontrol or 0rid Manager configured.

Archivelog Mode
!rchi,elog "ode "ust 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 dismo !ted. ORACLE i!sta!ce s" t do#!. SQL> startup mount ORACLE i!sta!ce sta$ted. %otal S&stem Global A$ea '()*)'+**( b&tes ,i-ed Si.e ///+012 b&tes Va$iable Si.e '/0'+2012+ b&tes Database 3 44e$s *)/*/')*( b&tes Redo 3 44e$s 5''++2+ b&tes Database mo !ted. SQL> alter database archivelog; Database alte$ed. SQL> alter database open; Database alte$ed.

SQL> select log_mode from v$database; LOG_MODE -----------ARCHIVELOG

$ecurit%
! new role na"ed LOGMNR_ADMIN will be created and assigned the a((ro(riate (ri,ileges for LogMiner analysis. This role will be assigned to a new user na"ed MINER that will be used to (erfor" the LogMiner e'a"(les (resented in this guide.
SQL> create role logmnr_admin; Role c$eated. SQL> grant create session to logmnr_admin; G$a!t s cceeded. SQL> grant select on v_$logmnr_contents to logmnr_admin; G$a!t s cceeded. SQL> grant select on v_$logmnr_dictionary to logmnr_admin; G$a!t s cceeded. SQL> grant select on v_$logmnr_parameters to logmnr_admin; G$a!t s cceeded. SQL> grant select on v_$logmnr_logs to logmnr_admin; G$a!t s cceeded. SQL> grant select on v_$archived_log to logmnr_admin; G$a!t s cceeded. SQL> grant , , , , execute_catalog_role select any dictionary select any transaction select any table create tablespace

, drop tablespace to logmnr_admin; G$a!t s cceeded. SQL> create user miner identified by miner; 6se$ c$eated. SQL> grant logmnr_admin to miner; G$a!t s cceeded. -- Needed 4o$ ,las"bac7 %$a!sactio!. SQL> grant execute on dbms_flashback to miner; G$a!t s cceeded. -- Needed 4o$ ,las"bac7 %$a!sactio!. SQL> grant select any transaction to miner; G$a!t s cceeded. -- Needed 4o$ ,las"bac7 %$a!sactio!. SQL> grant create any table to miner; G$a!t s cceeded. -- Needed 4o$ ,las"bac7 %$a!sactio!. SQL> grant flashback any table to miner; G$a!t s cceeded.

$upple ental Logging


LogMiner is a redo*based a((lication and as such, re%uires at least "ini"al su((le"ental logging be enabled on the source database. Oracle does not enable any su((le"ental logging by default. !dditionally, su((le"ental logging for (ri"ary key colu"ns "ust be enabled for Flashback Transaction to work. !fter ,erifying su((le"ental logging is enabled, force a log switch in order for the new su((le"ental log configuration to begin archi,ing the additional colu"n data to the redo logs.

SQL> alter database add supplemental log data; Database alte$ed. SQL> alter database add supplemental log data (primary key) columns; Database alte$ed. SQL> select supplemental_log_data_min, supplemental_log_data_pk from v$database; S688LEMEN%AL_LOG_DA%A_MIN S688LEMEN%AL_LOG_DA%A_89 -------------------------- ------------------------:ES :ES SQL> alter system switch logfile; S&stem alte$ed.

$e"e"ber that su((le"ental logging "ust be enabled on the source database "efore generating redo log files that will be analy9ed by LogMiner.

!lash"ac# Transaction &'a ple (sing Oracle LogMiner Viewer


This section de"onstrates how to use Oracle LogMiner Viewer to ca(ture L and ML acti,ity and how to use Flashback Transaction to backout a bad transaction .including de(endent transactions/.

Initiate &'a ple Data


#n order to test the LogMiner and Flashback Transaction functionality, create a test en,iron"ent in the source database. )se the following scri(t to create a test table and (erfor" a few transactions against it that will be analy9ed by LogMiner. oracle:log"iner:,iewer:scri(t2.s%l
C !!"C# scott$tiger Co!!ected. %"# &'("%)*" +,,, %"# -)!"%)*" ./0 C -12! 3Current 1ser3 4 52'# '.6 C -12! 3Current #ime3 4 52'# '.+ C -12! 3Current %C!3 4 52'# +++++++++++

C C C C

-12! -12! -12! -12!

first_name last_name email phone_number

4 4 4 4

52'# 52'# 52'# 52'#

'., '.. '/. '.6

77 (et current user, date$time, and %C!8 %"-"C# user '% 3Current 1ser3 , # _C9'5(systimestamp, :22$;;$<<<< 99/=>2)>%%:) '% 3Current #ime3 , dbms_flashback8get_system_change_number '% 3Current %C!3 45 2 dual; C $$e!t 6se$ C $$e!t %ime C $$e!t SCN ------------- ------------------- -----------SCO%% '1;');/1'/ '(<1'<'1 '1///022* 77 Create new table and initiali?e with test data8 &'1%" Create table %C ##8"2&- <""%8 9it @"!#"5A to continue8 C$eate table SCO%%.EM8LO:EES. Hit =EN%ER> to co!ti! e. C5"'#" #'B-" scott8employees ( employee_id !12B"5(C) , first_name D'5C9'5/(/,) , last_name D'5C9'5/(/0) ! # !1-, email D'5C9'5/(/0) ! # !1-, phone_number D'5C9'5/(/,) , C !%#5')!# employees_pk. &5)2'5< E"< (employee_id) ); %able c$eated. &'1%" )nitiali?e new table with test data8 9it @"!#"5A to continue8 I!itiali.e !e# table #it" test data. Hit =EN%ER> to co!ti! e. )!%"5# )!# scott8employees D'-1"% (.,,., :Fulia:, :;esai:, :GdesaiHnodomain8com:, :/,+70007./.=:); ' $o# c$eated. )!%"5# )!# scott8employees D'-1"% (.,,/, :4ranklin:, :Dick:, :fvickHnodomain8com:, :/,+70007./.+:); ' $o# c$eated. )!%"5# )!# scott8employees D'-1"% (.,,6, :Beth:, :Ialton:, :bwaltonHnodomain8com:, :/,+70007././:); ' $o# c$eated.

)!%"5# )!# scott8employees D'-1"% (.,,=, :&at:, :'llen:, :pallenHnodomain8com:, :/,+70007./.6:); ' $o# c$eated. )!%"5# )!# scott8employees D'-1"% (.,,0, :%tacy:, : lsen:, :solsenHnodomain8com:, :/,+70007.//=:); ' $o# c$eated. C 22)#; Commit com>lete. %"-"C# employee_id , first_name , last_name , email , phone_number 45 2 scott8employees 5;"5 B< employee_id; EM8LO:EE_ID ----------'11' '11/ '11* '112 '11) 77 77 77 77 77 ,IRS%_NAME ---------? lia ,$a!7li! 3et" 8at Stac& LAS%_NAME ----------Desai Vic7 Calto! Alle! Olse! EMAIL --------------------@desaiA!odomai!.com 4Bic7A!odomai!.com b#alto!A!odomai!.com >alle!A!odomai!.com solse!A!odomai!.com 8HONE_N6M3ER ------------/10-)))-'/'2 /10-)))-'/'0 /10-)))-'/'/ /10-)))-'/'* /10-)))-'//2

#he following transaction (#5'!%'C#) ! .) will be backed out using racle -og2iner Diewer and 4lashback #ransaction8 4lashback #ransaction will detect a dependent transaction (#5'!%'C#) ! /) and prompt the user with a list of options on how to continue8

77 J#5'!%'C#) ! .K &'1%" 2odify test data (#5'!%'C#) ! .)8 9it @"!#"5A to continue8 Modi4& test data D%RANSAC%ION 'E. Hit =EN%ER> to co!ti! e. 1&;'#" scott8employees %"# phone_numberL:/,+70007++++: I9"5" employee_idL.,,0; ' $o# >dated.

;"-"#" 45 2 scott8employees I9"5" employee_idL.,,.; ' $o# deleted.

C 22)#; Commit com>lete. 77 J#5'!%'C#) ! /K &'1%" 2odify test data (#5'!%'C#) ! /)8 9it @"!#"5A to continue8 Modi4& test data D%RANSAC%ION /E. Hit =EN%ER> to co!ti! e. ;"-"#" 45 2 scott8employees I9"5" employee_idL.,,0; ' $o# deleted. C 22)#; Commit com>lete. %"-"C# employee_id , first_name , last_name , email , phone_number 45 2 scott8employees 5;"5 B< employee_id; EM8LO:EE_ID ----------'11/ '11* '112 ,IRS%_NAME ---------,$a!7li! 3et" 8at LAS%_NAME ----------Vic7 Calto! Alle! EMAIL --------------------4Bic7A!odomai!.com b#alto!A!odomai!.com >alle!A!odomai!.com 8HONE_N6M3ER ------------/10-)))-'/'0 /10-)))-'/'/ /10-)))-'/'*

77 (et current user, date$time, and %C!8 %"-"C# user '% 3Current 1ser3 , # _C9'5(systimestamp, :22$;;$<<<< 99/=>2)>%%:) '% 3Current #ime3 , dbms_flashback8get_system_change_number '% 3Current %C!3 45 2 dual; C $$e!t 6se$ C $$e!t %ime C $$e!t SCN ------------- ------------------- -----------SCO%% '1;');/1'/ '(<1*<*' '1///0020

3otice how the abo,e scri(t contains a nu"ber of S-L;1lus 8A6SE co""ands. # inserted the 8A6SE co""ands as a "ethod to (ro,ide an inter,al of ti"e between the different S-L o(erations. <ithout the 8A6SE co""ands, this scri(t would co"(lete in under 2 second. For e'a"(le, if the scri(t was started at =2582484524 2+>?@>@4= and ended at =2582484524 2+>?@>@4=, the L state"ent .CREA%E %A3LE/ would occur within the sa"e

2 second as the ML state"ents that we intend to backout using Flashback Transaction. #f this occurs, Oracle LogMiner Viewer will throw the following e'ce(tion when atte"(ting to (erfor" a Flashback Transaction belie,ing that the transactions being backed out occurred before the CREA%E %A3LE L state"ent> &rror

Flashback transaction error. 2. 7ause * The Flashback Transaction Aackout (rocess encountered an interesting obBect which had its last L o(eration done on it after the "ining start ti"e. !n interesting obBect is one that has been "odified by either the s(ecified transactions or any of their de(endents. 4. !ction * S(ecify transactions that ha,e co""itted after the last L done on all the obBects they touched. 6. S-L&'ce(tion * Ba,a.s%l.S-L&'ce(tion> O$!*@@@5@> L done on an interesting obBect after "ining start S73 O$!*5C@24> at =SDS. AMS:FL!SHA!7E=, line 6F O$!*5C@24> at =SDS. AMS:FL!SHA!7E=, line F5 O$!*5C@24> at line 4

Bac#out !ailed Transaction using Oracle LogMiner Viewer


Oracle LogMiner Viewer and Flashback Transaction will be used to backout the 68DA%E G .68DA%E...CHERE em>lo&ee_idF'11)/ and DELE%E G .DELE%E...CHERE em>lo&ee_idF'11'/ state"ents fro" the scri(t shown abo,e. These two state"ents are (art of TRA)$ACTIO) *. Aefore backing out the target transaction, Flashback Transaction will detect that there is a de(endent transaction .DELE%E...CHERE em>lo&ee_idF'11)/ and (ro"(t the user with a list of o(tions on how to continue. Since the transaction we intend to backout .TRA)$ACTIO) */ de(ends on the second transaction .TRA)$ACTIO) +/, we cannot backout the first transaction without first backing out the second transaction. To access Oracle LogMiner Viewer, launch Oracle &nter(rise Manager, select the Availa"ilit% tab and click the View and Manage Transactions link in the Manage section.

!igure *> Oracle LogMiner Viewer Fro" the LogMiner screen, filter the transactions that LogMiner will return by selecting the Ti"e $ange or S73 $ange that is likely to (ick u( the recorded transactions for LogMiner to analy9e. )se the o(tional ,uer% !ilter (ara"eters to further filter the nu"ber of transaction records for LogMiner to analy9e. For this e'a"(le, include the Ta"le )a e and DB (ser to s(ecify which table and database user to which the transactions belong. )se the Advanced ,uer% section to enter any additional filter infor"ation which allows you to edit the actual CHERE clause. 7lick the Info Icon under !d,anced -uery for a descri(tion of colu"ns in VGLOGMNR_CON%EN%S that can be included in the CHERE clause. #n this e'a"(le, # will lea,e the !d,anced -uery section set to its default ,alue click the -Continue. button.

!igure +> &nter Search 7riteria <ait while the contents of the online and archi,ed redo files are searched by Oracle LogMiner Viewer for transaction records "atching the search criteria you entered.

!igure /> Searching Online and !rchi,ed $edo Log Files The resulting (age shows the transaction records that "atch the search criteria you entered. The left"ost colu"n shows the transaction identifier .H# /, which is used by Oracle to uni%uely identify a transaction. The Transaction $u ar% colu"n shows how "any 68DA%E . >d/, INSER% .i!s/, and DELE%E .del/ state"ents were (erfor"ed for each transaction record. 7lick on the Transaction # associated with the 68DA%E and DELE%E state"ents being rolled back in this e'a"(le .TRA)$ACTIO) */. #n this case it is Transaction # 1511'1111A*A1111 and listed as =$COTT0&M1LO2&&$ 3* upd4 * del5=.

!igure 6> LogMiner $esults This dis(lays the Transaction Details (age which shows the 68DA%E and DELE%E state"ents. 7lick the -!lash"ac# Transaction. button.

!igure 7> Transaction etails 7onfir" the Flashback Transaction o(eration re%uest by clicking the -2es. button.

!igure 8> 7onfir" Flashback Transaction O(eration The Flashback Transaction o(eration detected the de(endent transaction .DELE%E...CHERE em>lo&ee_idF'11)/ fro" TRA)$ACTIO) +. Dou need to decide how the Flashback Transaction o(eration should handle any de(endent transactions. The Flashback Transaction o(eration is currently using the NONCON,LIC%_ONL: o(tion. 7lick the -Change Recover% Option. button to change the reco,ery o(tion settings.

!igure 9> e(endent Transactions etected Select the Cascade o(tion to backout the target transaction and all de(endent transactions.

!igure :> Aackout !ll e(endent Transactions .7ascade/ This brings you back to the $how Dependencies screen showing the Cascade o(tion selected. 7lick on the target transaction and any de(endent transactions to ,erify each state"ent will be e'ecuted G He-ecF&esI. !fter ,erifying the target and de(endent transactions, click the -)e't. button.

!igure ;> 7onfir" 7ascade O(tion Selected The Review screen (ro,ides you with the ability to show the )ndo S-L scri(t that will be used to backout the target .and de(endency/ transactions. #n addition, you can (ro,ide a S-L state"ent that will allow you to ,iew the effect of the changes (rior to co""itting the Flashback Transaction. 7lick the -!inish. button to co""it the Flashback Transaction o(eration.

!igure *<> $e,iew Flashback Transaction etails !fter the Flashback Transaction o(eration is co"(lete, click the -O=. button on the Results screen.

!igure **> Flashback Transaction $esults Screen 7licking -O=. on the $esults screen will return you to the Transaction Details screen. 7licking -O=. again will return you to the LogMiner Results screen. Finally, click the -Done. button to return the to( le,el of the Availa"ilit% tab screen. Verify the Flashback Transaction successfully backed out the target .and any de(endent/ transactions.
%"-"C# employee_id , first_name , last_name , email , phone_number 45 2 scott8employees 5;"5 B< employee_id; EM8LO:EE_ID ,IRS%_NAME LAS%_NAME EMAIL 8HONE_N6M3ER

----------'11' '11/ '11* '112 '11)

---------? lia ,$a!7li! 3et" 8at Stac&

----------Desai Vic7 Calto! Alle! Olse!

--------------------@desaiA!odomai!.com 4Bic7A!odomai!.com b#alto!A!odomai!.com >alle!A!odomai!.com solse!A!odomai!.com

------------/10-)))-'/'2 /10-)))-'/'0 /10-)))-'/'/ /10-)))-'/'* /10-)))-'//2

A"out the Author


Jeffrey Hunter is an Oracle 7ertified 1rofessional, Ja,a e,elo("ent 7ertified 1rofessional, !uthor, and an Oracle !7&. Jeff currently works as a Senior atabase !d"inistrator for The A! Ione, #nc. located in 1ittsburgh, 1ennsyl,ania. His work includes ad,anced (erfor"ance tuning, Ja,a and 1L8S-L (rogra""ing, de,elo(ing high a,ailability solutions, ca(acity (lanning, database security, and (hysical 8 logical database design in a )3#H, Linu', and <indows ser,er en,iron"ent. JeffJs other interests include "athe"atical encry(tion theory, (rogra""ing language (rocessors .co"(ilers and inter(reters/ in Ja,a and 7, L !1, writing web*based database ad"inistration tools, and of course Linu'. He has been a Sr. atabase !d"inistrator and Software &ngineer for o,er 2+ years and "aintains his own website site at> htt(>88www.i e,elo("ent.info. Jeff graduated fro" Stanislaus State )ni,ersity in Turlock, 7alifornia, with a AachelorJs degree in 7o"(uter Science.

Cop%right 3c5 *;;:>+<*/ ?effre% M0 @unter0 All rights reserved0


!ll articles, scri(ts and "aterial located at the #nternet address of htt(>88www.ide,elo("ent.info is the co(yright of Jeffrey M. Hunter and is (rotected under co(yright laws of the )nited States. This docu"ent "ay not be hosted on any other site without "y e'(ress, (rior, written (er"ission. !((lication to host any of the "aterial elsewhere can be "ade by contacting "e at BhunterKide,elo("ent.info. # ha,e "ade e,ery effort and taken great care in "aking sure that the "aterial included on "y web site is technically accurate, but # disclai" any and all res(onsibility for any loss, da"age or destruction of data or any other (ro(erty which "ay arise fro" relying on it. # will in no case be liable for any "onetary da"ages arising fro" such loss, da"age or destruction. Last "odified on Monday, 2@*Oct*4524 2F>2+>@2 & T 1age 7ount> 6C@4

Вам также может понравиться