Flashback Oracle10gR1 10.1.0.2.0:
In Oracle 10g, the flashback functionality has been greatly extended.
Flashback Database:
Flashback Database is faster than traditional point-in-time recovery. Traditional recovery uses redo log files and backups. Flashback Database is implemented using a new type of log file called Flashback Database logs. The Oracle database server periodically logs before images of data blocks in the Flashback Database logs. The data block images are used to quickly back out changes to the database during Flashback Database.
Flashback Database reduces the time required to recover the database to a previous point. The time to restore a database is proportional to the number of changes that need to be backed out, not the size of the database.
RVWR Background Process:
When Flashback Database is enabled, a new RVWR background process is started. This process is similar to the LGWR (log writer) process. The new process writes Flashback Database data to the Flashback Database logs.
Figure 12.1 RVWR Background process and Flashback Database Logs Figure 12.1 RVWR Background process and Flashback.
Using Flashback Drop and Managing the Recycle Bin:
A user can view his objects in the recycle bin using the following statement:
SELECT * FROM RECYCLEBIN;
When you drop a tablespace including its contents, the objects in the tablespace are not placed in the recycle bin and the database purges any entries in the recycle bin for objects located in the tablespace. The database also purges any recycle bin entries for objects in a tablespace when you drop the tablespace, not including contents, and the tablespace is otherwise empty. Likewise:When you drop a user, any objects belonging to the user are not placed in the recycle bin and any objects in the recycle bin are purged.When you drop a cluster, its member tables are not placed in the recycle bin and any former member tables in the recycle bin are purged.When you drop a type, any dependent objects such as subtypes are not placed in the recycle bin and any former dependent objects in the recycle bin are purged.
When a dropped table is moved to the recycle bin, the table and its associated objects are given system-generated names.
The renaming convention is as follows:BIN$unique_id$version
where:unique_id is a 26-character globally unique identifier for this object, which makes the recycle bin name unique across all databasesversion is a version number assigned by the database
You can query objects that are in the recycle bin, just as you can query other objects. However, you must specify the name of the object as it is identified in the recycle bin. For example:
Oracle Flashback Table: Returning Individual Tables to Past States:
SQL> create table hr ( name varchar2(20));
Table created.
SQL> drop table hr;
Table dropped.
SQL> select * from recyclebin;
OBJECT_NAME ORIGINAL_NAME
OPERATION
------------------------------ --------------------------------
---------
TYPE TS_NAME CREATETIME
------------------------- ------------------------------ -------------------
DROPTIME DROPSCN PARTITION_NAME CAN CAN
------------------- ---------- -------------------------------- --- ---
RELATED BASE_OBJECT PURGE_OBJECT SPACE
---------- ----------- ------------ ----------
BIN$74ANPU11QhWw15ah+JM2gQ==$0 HR
DROPTABLE USERS 2006-11-16:17:56:252006-11-16:17:56:34
445402 YES YES
50424 50424 50424 8
SQL> show recyclebin
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE
DROP TIME
---------------- ------------------------------ ------------ -------------------
HR BIN$74ANPU11QhWw15ah+JM2gQ==$0 TABLE
2006-11-16:17:56:34
SQL> select * from "BIN$74ANPU11QhWw15ah+JM2gQ==$0";
no rows selected
Restoring Tables from the Recycle Bin:
SQL> flashback table hr to before drop rename to scott;
Flashback complete.
You can directly drop table using "purge" cluase.
if use "purge" clause then object not goes in recyclebin. it will delete parmanently.
SQL> drop table scott purge;
Table dropped.
You can also "purge" recyclebin
SQL> show recyclebin;ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME---------------- ------------------------------ ------------ -------------------
SCOTT BIN$QFb9kDMkRq2RpWJjjW9+ug==$0 TABLE 2006-11-16:18:14:26
SQL> purge recyclebin;
Recyclebin purged.
SQL> show recyclebin;
****************************************
Flashback Query using Row Movement:
Row movement must be enabled on the table for which you are issuing the FLASHBACK TABLE statement.
You can enable row movement with the following SQL statement
SQL> conn hr/hrConnected.
SQL> create table big_table as select * from all_objects;
Table created.
SQL> alter table big_table enable row movement;
Table altered.
SQL> alter session set nls_date_format = 'hh24:mi:ss';
Session altered.
SQL> select sysdate from dual;
SYSDATE
--------
18:43:49
SQL> select count(*) from big_table;
COUNT(*)
----------
46335
SQL> delete from big_table where rownum <= 445; 445 rows deleted.
SQL> commit;
Commit complete.
SQL> flashback table big_table to TIMESTAMP 2 to_timestamp ( '2006-11-16 18:44:00','YYYY-MM-DD HH24:MI:SS');
Flashback complete.
SQL> select count(*) from big_table;
COUNT(*)
----------
46335
Flashback Query:
you must choose an undo retention interval that is long enough to enable users to construct a snapshot of the database for the oldest version of the database that they are interested in.
SQL> conn sys as sysdba
Enter password:
Connected.
SQL> alter system set undo_retention = 3600;
System altered.
SQL> conn hr/hr
Connected.
SQL> alter session set nls_date_format = 'hh24:mi:ss';
Session altered.
SQL> select sysdate from dual;
SYSDATE
--------
19:04:49
SQL> create table hrtest ( no number, name varchar2(20));
Table created.
SQL> begin
2 insert into hrtest values ( 1, 'aaa');
3 insert into hrtest values ( 2, 'bbb');
4 insert into hrtest values ( 3, 'ccc');
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> select sysdate from dual;
SYSDATE
--------
19:06:23
SQL> delete from hrtest where name = 'ccc';
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from hrtest;
NO NAME
---------- --------------------
1 aaa
2 bbb
SQL> edWrote file afiedt.buf
1 select * from hrtest AS OF TIMESTAMP 2 to_timestamp ('2006-11-16 19:06:23','YYYY-MM-DD HH24:MI:SS')
3* where name = 'ccc'
4 /
NO NAME
---------- --------------------
3 ccc
SQL> insert into hrtest 2 ( select * from hrtest AS OF TIMESTAMP 3 to_timestamp ('2006-11-16 19:06:23','YYYY-MM-DD HH24:MI:SS')
4 where name = 'ccc');
1 row created.
SQL> select * from hrtest;
NO NAME
---------- --------------------
1 aaa
2 bbb
3 ccc
x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x
SQL> create table SCN_TEST (no number, name varchar2(20));
Table created.
SQL> insert into scn_test values (1,'a');
1 row created.
SQL> insert into scn_test values (2,'b');
1 row created.
SQL> insert into scn_test values (3,'c');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from scn_test;
NO NAME
---------- --------------------
1 a
2 b
3 c
SQL> show parameter undo_retention
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_retention integer 22719
SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
15893109
SQL> delete from scn_test where no = 2;
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from scn_test AS OF SCN 15893109 where no = 2;
NO NAME
---------- --------------------
2 b
SQL> insert into scn_test ( select * from scn_test AS OF SCN 15893109
2 where no = 2);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from scn_test;
NO NAME
---------- --------------------
1 a
3 c
2 b
************************
Using Flashback Version Query:
SQL> select * from hrtest;
NO NAME
---------- --------------------
1 aaa
2 bbb
3 ccc
SQL> begin
2 insert into hrtest values ( 4, 'ddd');
3 insert into hrtest values ( 5, 'eee');
4 commit;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select sysdate from dual;
SYSDATE
--------
19:35:26
SQL> select sysdate from dual;
SYSDATE
--------
19:38:57
SQL> delete from hrtest where rownum <= 3;
3 rows deleted.
SQL> commit;
Commit complete.
SQL> begin
2 update hrtest set no = 0;
3 commit;
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select sysdate from dual;
SYSDATE
--------
19:48:42
SQL> select * from hrtest;
NO NAME
---------- --------------------
0 ddd
0 eee
SQL> ed
Wrote file afiedt.buf
1 SELECT versions_startscn, versions_starttime,
2 versions_endscn, versions_endtime,
3 versions_xid, versions_operation,
4 hrtest.*
5 FROM hrtest
6 VERSIONS BETWEEN TIMESTAMP
7 TO_TIMESTAMP('2006-11-16 19:35:00', 'YYYY-MM-DD HH24:MI:SS')
8 AND TO_TIMESTAMP('2006-11-16 19:48:00', 'YYYY-MM-DD HH24:MI:SS')
9* WHERE name in ('aaa','bbb','ccc');
SQL> column versions_starttime format a25
SQL> column versions_endtime format a25
SQL> /
VERSIONS_STARTSCN VERSIONS_STARTTIME
VERSIONS_ENDSCN
----------------- -------------------------
---------------
VERSIONS_ENDTIME VERSIONS_XID V NO NAME
------------------------- ---------------- - ---------- -------------------- 449718 16-NOV-06 07.39.10 PM
04000E0028000000 D 3 ccc
449718 16-NOV-06 07.39.10 PM
04000E0028000000 D 2 bbb
449718 16-NOV-06 07.39.10 PM
04000E0028000000 D 1 aaa
44971816-NOV-06 07.39.10 PM
1 aaa
44971816-NOV-06 07.39.10 PM
2 bbb
44971816-NOV-06 07.39.10 PM
3 ccc
6 rows selected.
The available pseudocolumn meanings are:
* VERSIONS_STARTSCN or VERSIONS_STARTTIME - Starting SCN and TIMESTAMP when row took on this value. The value of NULL is returned if the row was created before the lower bound SCN ot TIMESTAMP.
* VERSIONS_ENDSCN or VERSIONS_ENDTIME - Ending SCN and TIMESTAMP when row last contained this value. The value of NULL is returned if the value of the row is still current at the upper bound SCN ot TIMESTAMP.
* VERSIONS_XID - ID of the transaction that created the row in it's current state.
* VERSIONS_OPERATION - Operation performed by the transaction ((I)nsert, (U)pdate or (D)elete)
SELECT commit_timestamp , logon_user FROM FLASHBACK_TRANSACTION_QUERY
WHERE xid IN
(SELECT versions_xid FROM employee VERSIONS BETWEEN
TIMESTAMP TO_TIMESTAMP('2003-04-04 14:00:00', 'YYYY-MM-DD HH:MI:SS')
and TO_TIMESTAMP('2003-04-04 17:00:00', 'YYYY-MM-DD HH:MI:SS')
WHERE name = 'JOHN');
Flashback Transaction Query:
Flashback transaction query can be used to get extra information about the transactions listed by flashback version queries. The VERSIONS_XID column values from a flashback version query can be used to query the FLASHBACK_TRANSACTION_QUERY view like:
SELECT xid, operation, start_scn,commit_scn, logon_user, undo_sql
FROM flashback_transaction_query
WHERE xid = HEXTORAW('0600030021000000');
XID OPERATION START_SCN COMMIT_SCN
---------------- -------------------------------- ---------- ----------
LOGON_USER
------------------------------
UNDO_SQL
----------------------------------------------------------------------------------------------------
0600030021000000 UPDATE 725208 725209
SCOTT
update "SCOTT"."FLASHBACK_VERSION_QUERY_TEST" set "DESCRIPTION" = 'ONE' where ROWID = 'AAAMP9AAEAAAA
AYAAA';
0600030021000000 BEGIN 725208 725209
SCOTT
XID OPERATION START_SCN COMMIT_SCN
---------------- -------------------------------- ---------- ----------
LOGON_USER
------------------------------
UNDO_SQL
----------------------------------------------------------------------------------------------------
2 rows selected.
Flashback Database:
Flashback Database is introduce with 10gr1, It is alternative for PITR (Point In Time Recovery).
It is superb feature, we can also called "rewind button" for database. becuase through this option we can rewind database to any point of time.
1. Database must run in archivelog mode
2. connect with SYS user and shutdown database with normal
SQL> conn / as sysdba
Connected.
SQL> shutdown
Database closed.
Database dismounted.
ORACLE instance shut down.
3. startup database with MOUNT mode
SQL> startup mount
4. Enable FLASHBACK DATABASE
SQL> alter database flashback on;
Database altered.
4. Open the database with normal operation
SQL> alter database open;
Database altered.
5. Check flashback database option enable for the database
SQL> select flashback_on from v$database;
FLA
---
YES
NOTE: FLASHBACK LOGS created in Flash recovery area
Up to this stage we only ENABLE the flashback database, now we need to configure one more parameter for How far can we bring the database back in time?
for that we need to configure below parameter
db_flashback_retention_target
NOTE: Parameter is dynamic, and default value is 1440 minutes means 24 hrs, parameter value is set in MINUTES
We can set FLASHBACK database option for particular tablespace also.
SQL> alter tablespace TEST1 flashback on;
SQL> alter tablespace TEST1 flashback off;
Estimating flashback database storage reqirement
We can get this information through below query after enable flashback database option.
SQL> SELECT ESTIMATED_FLASHBACK_SIZE FROM V$FLASHBACK_DATABASE_LOG;
How Flashback Database is important for DBA? lets check with below example
Nive (DBA) working in ABC bank as an DBA, suddenly one application user called and told he was by mistake DROP wrong production table with PURGE option.
Database version is 10gr1, so we can easily recovered dropped table through FLASHBACK TABLE but the problem is here user dropped table with PURGE option so we can't recovered dropped table without perform Point In Time Recover option.
Nive is happy becuase FLASHBACK DATABASE option is ENABLE.
So Miss Nive done the following steps
1. shutdown the database
2. flashback database before table was dropped
3. open database with read only
4. export dropped table
5. shutdown and startup database with mount mode
6. perform recover database
7. open database with normal operation without resetlogs
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
542151
SQL> drop table SCOTT.EMP purge;
Table dropped.
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
542182
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 171966464 bytes
Fixed Size 787988 bytes
Variable Size 144964076 bytes
Database Buffers 25165824 bytes
Redo Buffers 1048576 bytes
Database mounted.
SQL> flashback database to scn 542151;
Flashback complete.
SQL> alter database open read only;
Database altered.
SQL> desc scott.emp
Name Null? Type
----------------------------------------- -------- ---------------------------
[output cut]
SQL> host exp system/oracle file=c:\emp.dmp tables=SCOTT.EMP
Export: Release 10.1.0.2.0 - Production on
Sun Jul 20 17:42:03 2008
Copyright (c) 1982, 2004, Oracle.
All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Prodution
With the Partitioning, OLAP and Data Mining options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
Current user changed to SCOTT. .
exporting table EMP 14 rows exported
Export terminated successfully without warnings.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 171966464 bytes
Fixed Size 787988 bytes
Variable Size 144964076 bytes
Database Buffers 25165824 bytes
Redo Buffers 1048576 bytes
Database mounted.
SQL> recover database
Media recovery complete.
SQL> alter database open;
Database altered.
SQL> desc scott.emp
ERROR:ORA-04043: object scott.emp does not exist
SQL> host imp system/oracle file=c:\emp.dmp fromuser=scott touser=scott
Import: Release 10.1.0.2.0 - Production on
Sun Jul 20 17:43:10 2008
Copyright (c) 1982, 2004, Oracle.
All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Prodution
With the Partitioning, OLAP and Data Mining options
Export file created by EXPORT:V10.01.00 via conventional pathimport done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set.
importing SCOTT's objects into SCOTT. .
importing table "EMP" 14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
SQL> desc scott.emp
Name Null? Type ----------------------------------------- -------- ---------------------------
[output cut]
Finally Nive able to recover dropped table :)
We can flashback database through SCN or TIMESTAMP
How to get back dropped Tablespace using Flashback Database:
It needs to remember that if you drop your tablespace with including datafiles option then it is not possible to get your tablespace back with flashback database. While dropping if you use DROP TABLESPACE INCLUDING CONTENTS option then it is possible to get back tablespace.
Procedures with and Example:
---------------------------------
1)Create a Tablespace and Table inside it.
SQL> create tablespace test_restore datafile '/oradata2/test_restore01.dbf' size 5m;
Tablespace created.
SQL> create table test tablespace test_restore as select level a1 from dual connect by level <99;
Table created.
2)Note the SCN and Drop the Tablespace with including contents option.
---------------------------------------------------------------------------
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
938686
SQL> drop tablespace test_restore including contents;
Tablespace dropped.
3)Mount the database.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 167772160 bytes
Fixed Size 2019288 bytes
Variable Size 104857640 bytes
Database Buffers 54525952 bytes
Redo Buffers 6369280 bytes
Database mounted.
4)Perform FlashBack.
SQL> flashback database to scn 938686;
flashback database to scn 938686
*
ERROR at line 1:
ORA-38795: warning: FLASHBACK succeeded but OPEN RESETLOGS would get error below
ORA-01245: offline file 5 will be lost if RESETLOGS is done
ORA-01111: name for data file 5 is unknown - rename to correct file
ORA-01110: data file 5:
'/oracle/app/oracle/product/10.2.0/db_1/dbs/UNNAMED00005'
5)The datafile Become Unnamed. So rename it with original data file location.
SQL> alter database rename file '/oracle/app/oracle/product/10.2.0/db_1/dbs/UNNAMED00005' to
2 '/oradata2/test_restore01.dbf';
Database altered.
6)Now perforem Flashback and Open the database with read only mode.
SQL> flashback database to scn 938686;
Flashback complete.
SQL> alter database open read only;
Database altered.
SQL> select count(*) from test;
COUNT(*)
----------
98
sql> alter database open resetlogs;
FLASHBACK USING RMAN:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Robert>cd\
C:\>net start oracleservicedba
The OracleServiceDBA service is starting............
The OracleServiceDBA service was started successfully.
C:\>set oracle_sid=dba
C:\>sqlplus "/as sysdba"
SQL*Plus: Release 10.2.0.1.0 - Production on Sat Aug 1 22:46:03 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
SQL> conn robo/robo
Connected.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
TEST TABLE
SQL> select * from test;
NUM
----------
1
2
3
SQL> set time on;
22:46:58 SQL> select * from test;
NUM
----------
1
2
3
22:46:59 SQL> drop table test purge;
Table dropped.
22:47:10 SQL> ho rman target sys/sys
Recovery Manager: Release 10.2.0.1.0 - Production on Sat Aug 1 22:48:50 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: DBA (DBID=1152934289)
RMAN> shutdown immediate;
using target database control file instead of recovery catalog
database closed
database dismounted
Oracle instance shut down
RMAN> startup mount
connected to target database (not started)
Oracle instance started
database mounted
Total System Global Area 289406976 bytes
Fixed Size 1248576 bytes
Variable Size 75498176 bytes
Database Buffers 205520896 bytes
Redo Buffers 7139328 bytes
RMAN> flashback database to time "to_timestamp('01-AUG-2009 22:46:58','DD-MON-YYYY HH24:MI:SS')";
Starting flashback at 01-AUG-09
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK
starting media recovery
media recovery complete, elapsed time: 00:00:07
Finished flashback at 01-AUG-09
RMAN> open resetlogs database;
-------->
RMAN> FLASHBACK DATABASE TO SCN 46963;
RMAN> FLASHBACK DATABASE TO SEQUENCE 5304;
RMAN> FLASHBACK DATABASE TO TIME (SYSDATE-1/24); ----> 1 Hour before.
RMAN> FLASHBACK DATABASE TO TIME (SYSDATE-10/(24*60)); ----> 10 minutes minus from sysdate and if it is +10 then it add 10 minutes.
If you want 10 seconds ago, use SYSDATE - (10 * (1 / 24 / 60 / 60)), because (1 / 24 / 60 / 60) is the number of days in a second.
RMAN> FLASHBACK DATABASE TO TIME timestamp('2002-11-05 14:00:00');