You are on page 1of 10

FlashBack Architecture There are a number of flashback levels row level table level database level flashback query,

flashback versions query, flashback transaction query flashback table, flashback drop flashback database

-1-

Oracle 10g has several error-correction techiques that use undo data, however they are only available if you use automatic undo management (AUM),

Flashback query - retrieves data from a past point in time Flashback versions query - shows you different versions of data rows, plus start and end times of a particular transaction that created that row Flashback transaction query - lets you retrieve historical data for a given transaction and the SQL code to undo the transaction. Flashback table - recovers a table to its state at a past point in time, without having to perform a point in time recovery.

There are two other flashback technologies that do not use the undo data, they use flashback logs and recycled bin instead.

flashback database - restore the hole database back to a point in time. flashback drop - allows you to reverse the effects of a drop table statement, without resorting to a pointin-time recovery

DBMS_FLASHBACK, flashback table query, flashback transaction query, flashback version query and select .. as of .. statements all use the undo segments. Flashback database.doc uses the flashback logs and flashback drop.doc uses the recycled bin. When using flashback, if any operations violate a constraint the flashback operation will be rolled back, you can disable constraints but its probably not a good idea. If you have a table using a foreign key it is a good idea to flashback both tables. Flashback technology requires you to lock the whole table if it cannot it will fail immediately. RMAN can only do flashback database and no other flashback technology. Flashback Query Using flashback query involves using a select statement with an AS OF clause. you can select data from a past point in time. If you get a ORA-08180 it means that the data is no longer available in the undo segments. Privilege Flashback query (time) grant flashback on table_test to pvalle; grant flashback on any table to pvalle; select * from employees as of timestamp to_timestamp('2007-12-03 08:00:00', 'YYYY-MM-DD HH:MI:SS') where last_name = valle;

FlashBack Architecture

-2-

select employee_id, name from hr.employee as of timestamp (systimestamp - interval '15' minute); select * from employees as of scn = 4542; Flashback query (SCN) Note: using a scn will put you with 3 secs if you need to be dead accurate use timestamps instead. execute dbms_flashback.enable_at_time(to_timestamp('2007-12-03 08:00:00', 'YYYYMM-DD HH:MI:SS')); execute dbms_flashback.disable; insert into employees select * from employees as of timestamp to_timestamp('2007-12-03 08:00:00', 'YYYY-MM-DD HH:MI:SS') where last_name = valle;

Take the whole session back in time

Reinserting

insert into employees select * from employees as of scn = 4542; List the flashback entries select * from flashback_transaction_query; Obtain a time from an select scn_to_timestamp(1408640) as ts from dual; SCN Obtain SCN from time select timestamp_to_scn('2007-12-02 08:00:00') as scn from dual; point select begin_time, end_time, tuned_undoretention from v$undostat; What is in the undo tablespace Note: the time is in seconds Flashback Version Query Flashback version query provides you will all the versions of a row between two points in time or SCN, this is useful if you want to audit a table finding out what happened to a row. However here are some points to remember:

You can only retrieve committed rows They query will retrieve all deleted rows as well as current rows The query will retrieve any rows thata were deleted and reinserted later on Query result is table format and contains a row for each version of a row during the time or SCN interval you specify

The imitations of flashback version query are:


You can only query actual tables not views you cannot apply the versions clause across DDL operations The query will ignore physical row changes for example during a segment shrink operation You cannot use against external or temporary tables.

The most useful columns to obtain are below:

FlashBack Architecture

-3-

VERSIONS_STARTTIME - start timestamp of version VERSIONS_STARTSCN - start SCN of version VERSIONS_ENDTIME - end timestamp of version VERSIONS_ENDSCN - end SCN of version VERSIONS_XID - transaction ID of version VERSIONS_OPERATION - DML operation of version select versions_xid, as xid, versions_startscn as start_scn, versions_endscn as end_scn, versions_operation as operation, empname from employees versions between timestamp to_timestamp('2007-12-02 08:00:00', 'YYYY-MM-DD HH:MI:SS') and to_timestamp('2007-12-03 08:00:00', 'YYYY-MM-DD HH:MI:SS') order by versions_startscn; select versions_xid as xid, versions_startscn as start_scn, versions_endscn as end_scn, versions_operation as operation, empname from employees versions between scn = 140986 and scn = 141000 where emp_id = 863;

Flashback version query (time)

Flashback version query (SCN)

Obtain a time from an select scn_to_timestamp(1408640) as ts from dual; SCN Obtain SCN from time select timestamp_to_scn('2007-12-02 08:00:00') as scn from dual; point Flashback Transaction Query Identifies which transaction or transactions were responsible for a certain change in a table's data during a specified time period. Basically it queries the flashback_transaction_query view. It provides the SQL code that will undo the change, flashback transaction query can use an index path to retrieve data instead of reading the entire redo log file. Flashback transaction considerations:

Turn on minimal supplemental logging if your operations involve chained rows and special storage structures, such as clustered tables When querying IOT, an update is shown as a delete/insert operation. If the query involves a dropped table or a dropped user, it returns object numbers and user ID's instead of the object names and usernames.

Consider setting the retention guarantee option for the undo tablespace, this will ensure that the unexpired data in the undo segments is preseved. Flashback transaction query will contain the following columns

start_scn and start_timestamp - identify when a certain was created commit_scn and commit_timestamp - tell you when a certain row was committed

FlashBack Architecture

-4-

xid_row_id and undo_change# - identify the row, transaction and change numbers operation - tells you what sort of operation occurred insert, delete or update. logon_user, table_name and table_owner - username, table nam and schema name undo_sql - the exact SQL code to undo the change

If you have chained rows or use clustered tables then oracle recommends that you should turn on supplemental logging on at the database level. Privilege grant select any transaction to pvalle; Supplemental logging alter database add supplemental log data; Display undo segments select operation, undo_sql, table_name from flashback_transaction_query; select operation, undo_sql, table_name from flashback_transaction_query where start_timestamp >= to_timestamp ('2007-12-04 05:00:00', 'YYYY-MM-DD HH:MI:SS') and commit_timestamp <= to_timestamp ('2007-12-04 08:00:00', 'YYYY-MM-DD Flashback transaction HH:MI:SS') query and table_owner='VALLEP'; Note: this will give you the SQL to reserver the change that was applied to the database,just cut and paste to use. Obtain a time from an SCN Obtain SCN from time point select scn_to_timestamp(1408640) as ts from dual; select timestamp_to_scn('2007-12-02 08:00:00') as scn from dual;

You use flashback version query to obtain the xid and then use the xid in the flashback transaction query statement flashback version select versions_xid, as xid, versions_startscn as start_scn, versions_endscn as end_scn, and flashback versions_operation as operation, transaction query empname from employees versions between timestamp scn minvalue and maxvalue as of scn 7920 where emp_id = 222; XID start_scn end_scn operation empname salary --------------------------------------------------------------------------------0003002F00038BA9 2266 I Paul 20000 Now use the XID in the flashback transaction query to obtain the SQL to undo the change. select xid, start_scn, commit_scn, operation, logon_user, undo_sql from flashback_transaction_query where xid=hextoraw('0003002F00038BA9');

FlashBack Architecture

-5-

XID start_scn commit_scn operation user undo_sql -------------------------------------------------------------------------------------------------------0003002F00038BA9 195243 195244 delete vallep insert into HR.EMP ("EMPNO", "EMPNAME", "SALARY") values ('222', 'Paul', '20000'); Flashback Table There are two distinct table related flashback table features in oracle, flashback table which relies on undo segments and flashback drop which lies on the recycle bin not the undo segments. Flashback table lets you recover a table to a previous point in time, you don't have to take the tablespace offline during a recovery, however oracle acquires exclusive DML locks on the table or tables that you are recovering, but the table continues to be online. When using flashback table oracle does not preserve the ROWIDS when it restores the rows in the changed data blocks of the tables, since it uses DML operations to perform its work, you must have enabled row movement in the tables that you are going to flashback, only flashback table requires you to enable row movement. If the data is not in the undo segments then you cannot recover the table by using flashback table, however you can use other means to recover the table. Restriction on flashback table recovery

You cannot use flashback table on SYS objects You cannot flashback a table that has had preceding DDL operations on the table like table structure changes, dropping columns, etc The flashback must entirely exceed or it will fail, if flashing back multiple tables all tables must be flashed back or none. Any constraint violations will abort the flashback operation You cannot flashback a table that has had any shrink or storage changes to the table (pctfree, initrans and maxtrans) grant flashback on table_test to pvalle; grant flashback on any table to pvalle; alter table test enable row movement; select table_name, row_movement from user_tables; flashback table test to scn 4587309871; flashback table employees to timestamp to_timestamp ('2007-12-04 05:00:00', 'YYYY-MM-DD HH:MI:SS')

Privilege Enable row movement Display row movement Flashback table (SCN) Flashback table (time)

FlashBack Architecture

-6-

flashback table employees to timestamp (systimestamp - interval '15' minute); flashback table employess to timestamp (sysdate - 1); flashback table employees, depts to timestamp (sysdate - 1); flashback table employees to timestamp (sysdate -1) enable triggers; Enable triggers Note: Oracle disables triggers by default when falshing back a table Flashback Drop Flashback drop lets you reinstate previously dropped tables exactly as it was before the drop, below is a table of what is kept where when a table is dropped:

Recycle bin: tables and indexes Data dictionary: unique keys, primary key, not-null constraints, triggers and grants Not recovered: foreign key constraints

If two tables exist in the recycle bin with the same name the newest one will be restored unless you state which one you want to restore. If you restore a table it is removed from the recyclebin. Recover Recover and rename Note: use if table name already exists Recover same name table flashback table BIN$hfkjdshfkhs to before drop; select object_name, original_name, type from user_recyclebin; select object_name, original_name, type from dba_recyclebin; List recycle bin select * from recyclebin; (shows only current user info) show recyclebin; drop table <table_name> purge; drop table completely Note: table will not be in recycle bin purge table <table_name> drop table from r/bin Note: table will be gone from recycle bin truncate table truncate table Note: table will not be in the recycle bin drop user test cascade drop user Note: will not store anything in the recycle bin purge recyclebins purge recyclebin; (purge user recyclebin) purge dba_recyclebin;(purges all recyclebins) flashback table <table_name> to before drop flashback table <table_name> to before drop rename to <new name>;

FlashBack Architecture

-7-

List dropped table contents Naming Convention

purge tablespace test user test03; (purge test03 from tablespace test) select * from BIN$NwM/FEjuSUORrgxHUPR3WA==$0; Note: the double quotes BIN$globalUID$version

Space pressure on a tablespace will cause it to purge the recycle bins of the users within that tablespace, it is based on a FIFO order. When a tablespace has the auto extend feature turned on it will clear down the recyclebin first, then auto extend. Limitations on flashback drop:

Recycle bin is only available to non-system, locally managed tablespaces. There is no guaranteed timeframe for how long an object will be stored in the recycle bin DML and DDL cannot be used on objects in the recycle bin Must use the recycle bin name to query the table All dependents objects are retrieved when you perform a flashback drop. Virtual private database (VPD) and FGA policies defined on tables are not protected for security reasons Partitioned index-organised tables are not protected by the recycle bin. Referential constraints are not protected by the recycle bin. They must be re-created after table has been rebuilt.

Flashback Database The database can be taken back in time change by change reversing all work done sequentially. The database must be opened with resetlogs as if an incomplete recovery has happened. This is ideal if you have a database corruption (wrong transaction, etc) and require the database to be rewound before the corruption occurred. If you have media or a physical problem a normal recovery is required. Flashback database is not enabled by default, when enabled flashback dataabse a process (RVWR recovery Writer) copies modified blocks to the flashback buffer. This buffer is then flushed to disk (flashback logs). Remember the flashback logging is not a log of changes but a log of the complete block images. Not every changed block is logged as this would be too much for the database to cope with, so only as many blocks are copied such that performance is not impacted. Flashback database will construct a version of the datafiles that is just before the time you want. The datafiles probably will be in a inconsistent state as different blocks will be at different SCNs, to complete the flashback process, Oracle then uses the redo logs to recover all the blocks to the exact time requested thus synchronising all the datafiles to the same SCN. Archiving mode must be enabled to use flashback database. An important note to remember is that Flashback can never reserve a change only to redo them. The advantage in using flashback database is speed and convenience with which you can take the database back in time. You can use rman, sql and Enterprise manager to flashback a database. If the flash recovery area does not have enough room the database will continue to function but flashback operations may fail. It is not possible to flashback one tablespace, you must flashback the whole database. If performance is being affected by flashback data collection turn some tablespace flashbacking off.

FlashBack Architecture

-8-

You cannot undo a resized datafile to a smaller size. When using backup recovery area and backup recovery files controlfiles , redo logs, permanent files and flashback logs will not be backed up. select log_mode from v$database; (must use archivelog mode) alter system set db_recovery_file_dest=c:/flash_recovery_area; alter system set dba_recovery_file_dest_size=8G; alter system set db_flashback_retention_target=4320; (3 days time in minutes) shutdown immediate; startup mount; alter database flashback on; (RVWR process will start) alter database open; alter session set nls_date_format = dd-mon-yyyy hh24:mi:ss; select flashback_on from v$database; (check its enabled) select retention_target, estimated_flashback_size, flashback_size from v$flashback_database_log; select oldest_flashback_scn, oldest_flashback_time from v$flashback_database_log; select end_time, flashback_data, db_data, redo_data from v$flashback_database_stat; (flashback growth) select * from v$sgastat where name like flashback%; startup mount; flashback database to timestamp to_timestamp(15-02-07 10:00:00, dd-mm-yy hh24:mi:ss); alter database open read only; (check schema ok start db, if not continue, optional) shutdown abort; startup mount; flashback database to timestamp to_timestamp(15-02-07 10:02:00, dd-mm-yy hh24:mi:ss); alter database open read only; (check schema ok start db, if not continue, optional) When happy alter database open resetlogs; flashback database to time = to_date(15-02-07 10:00:00, dd-mm-yy hh24:mi:ss); flashback database to scn = 2765665; flashback database to sequence=2123 thread=1; Tablespace Flashback Configuration alter tablespace <tablespace_name> flashback on; (must be in mount mode) alter tablespace <tablespace_name> flashback off; (this can be done in open or mount mode) select name, flashback_on from v$tablespace; select * from v$flash_recovery_area_usage;

Enable

Monitoring

Flashback Buffer

Flashback database example

Flashback using RMAN

Turn flashback on (tablespace) Turn flashback off (tablespace) Display tablespaces with flashback Flashback space usage

FlashBack Architecture

-9-

Note: if one or more tablespaces are not generating flashback data, then before carrying out a flashback operation the files making up the tablespace must be taken offline. Offline files are ignored by recover and flashback. Remember that you must make these files to the same point as the flashback otherwise the database will not open. Flashback Recovery Area The alert log and DBA_OUTSTANDING_ALERTS will hold status information regarding the flash recovery area. You can use the commands backup copy or backup for flash recovery area. Controlfiles and redo logs are permanently stored in the flash recovery area. select * from v$recovery_file_dest; Monitoring Note: this details space_used, space_limit, space_reclaimable, # of files rman> backup recovery area; Note: includes backup sets, datafile and archive, controlfile excludes: flashback log, current Backing up flashback controlfile, online redo logs area rman> backup recovery files; Note: includes all files whether or not they are in the flash recovery area Using Restore Points There is a new enhancement to recovery techniques by allowing you to create restore points, a restore point is alias for an SCN, which elimates the need to research and record SCN's or timestamps which you need for flashback database and flashback table operations, all you need to do is refer to the restore point when recovering. The restore point does not guarantee that the data will remain in the flashback logs necessary for a flashback database operation to succeed under all circumstances. By creating a restore point that will guarantee that all data will be preserved you can be assured that the restore will be sucessful, remember that you will need enough space in the flash recovery area to hold the necessary data. A guaranteed restore point does not depend on flashback logs, thus you can create a guaranteed restore point with flashback logging turned off, they use a logging mechanism that's similar to flashback logs but it's separate from them, thus if you are using guaranteed restore points it better to turn off flashback logging otherwise you may end up filling up the flash recovery area. One note is that you can only restore back to the restore point, you cannot restore back to a point in time using restore points, you must then use the backups and archived logs to do a point in time recovery. Not Guaranteed create restore point large_update; drop restore point large_update; Guaranteed create restore point test_guarantee guarantee flashback database; drop restore point test_guarantee;

create remove create guaranteed restore point remove guaranteed restore

FlashBack Architecture

- 10 -

point Displaying restore points Flashback databse running Note: you should get a restore point only reply if using restore points Other Operations select name, scn, storage_size, time, guarantee_flashback_database from v$restore_point; select flashback_on from v$database;

You might also like