Kamran Agayev's Oracle Blog

Oracle Certified Master

Archive for July 21st, 2010

Oracle Flashback Data Archive (Total Recall)

Posted by Kamran Agayev A. on 21st July 2010

Starting from Oracle 11g version, it’s possible to keep every transaction made to the table and keep it as long as you want. Before 11g, in order to get before image of any row, either we were getting it from archived redo log files (if they are kept) using Log Miner, or were writing a trigger to save the data in another log table. But now, using Flashback Data Archive feature, we don’t need to use Log Miner or trigger to track changes made to the table. The new background process, FBDA (Flashback Data Archive) tracks all changes made to the table and stores it in a file in a compressed and partitioned format. However, you can’t use this feature with clustered, temporary, nested, remote or external tables and LONG or nested columns

It tracks all transactional changes made to specific tables for the specific time interval. To use Flashback Data Archive feature the user needs to have the FLASHBACK ARCHIVE ADMINISTER system privilege. Moreover, the FLASHBACK ARCHIVE object privilege should be granted to the user to enable historical data tracking. In the following scenario we show you the configuration and usage of this feature in detailed examples

–          Create a new user and grant him the required privileges:

[sourcecode]

SQL> create user usr identified by usr;

User created.

SQL> grant connect, resource, flashback archive administer to usr;

Grant succeeded.

SQL>  [/sourcecode]

–          Create a new separate tablespace for data archive

[sourcecode]

SQL> CREATE TABLESPACE tbs_arch DATAFILE ‘c:\flashback_archive.dbf’ size 10m;

Tablespace created.

SQL> [/sourcecode]

–          Create flashback archive on this tablespace using CREATE FLASHBACK ARCHIVE command as follows:

[sourcecode] SQL> create flashback archive fl_archive

  2  tablespace tbs_arch retention 1 year;

Flashback archive created.

SQL> [/sourcecode]

With above command we’ve created a Flashback Archive named FL_ARCHIVE which resides in the tablespace TBS_ARCH and holds information for 1 year. It means that we can use any Flashback Query which contains 1 year historical information regarding the table that assigned to this flashback archive

–          Now, create a table, insert one row and assign it to this flashback archive:

[sourcecode] SQL> create table tbl_fl_archive (id number, name varchar2(20));

Table created.

SQL> insert into tbl_fl_archive values(1,’Flashback Archive’);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from tbl_fl_archive;

        ID NAME

———- ——————–

         1 Flashback Archive

SQL> alter table tbl_fl_archive flashback archive fl_archive;

Table altered.

SQL> [/sourcecode]

The historical change on the table TBL_FL_ARCHIVE is now will be written to the flashback archive named FL_ARCHIVE.

–          To test it, delete all rows and use Flashback Query on that table. Remember, it will not look for the UNDO data, it will look to the flashback archive file for the changes

[sourcecode] SQL> select to_char(sysdate,’ddmmyyyy hh24:mi:ss’) ddate from dual;

DDATE

—————–

13022010 12:46:49

SQL> delete from tbl_fl_archive;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select * from tbl_fl_archive;

no rows selected

SQL> select * from tbl_fl_archive as of timestamp to_timestamp(‘13022010 12:46:49′,’ddmmyyyy hh24:mi:ss’);

        ID NAME

———- ——————–

         1 Flashback Archive

SQL>  [/sourcecode]

In order to show and proof that it doesn’t look to the UNDO tablespace for the historical information on the rows for the specific time, create new undo tablespace and make it default by dropping the old one. Then use Flashback Versions on that table:

[sourcecode] SQL> conn / as sysdba

Connected.

SQL> show parameter undo_tablespace;

NAME                                 TYPE        VALUE

——————— ———–    ———   ————

undo_tablespace                      string      UNDOTBS1

SQL> select a.name from v$datafile a, v$tablespace b where a.ts#=b.ts# and b.name=’UNDOTBS1′;

NAME

————————————————

C:\APP\ADMINISTRATOR\ORADATA\DB2\UNDOTBS01.DBF

SQL> create undo tablespace undotbs2 datafile ‘C:\APP\ADMINISTRATOR\ORADATA\DB2\UNDOTBS02.dbf’ size 10m;

Tablespace created.

SQL> alter system set undo_tablespace=’UNDOTBS2′;

System altered.

SQL> startup force

ORACLE instance started.

Total System Global Area  431038464 bytes

Fixed Size                  1333676 bytes

Variable Size             251659860 bytes

Database Buffers          171966464 bytes

Redo Buffers                6078464 bytes

Database mounted.

Database opened.

SQL> show parameter undo_tablespace;

NAME                                 TYPE        VALUE

—————————              ———– ————

undo_tablespace                      string      UNDOTBS2 [/sourcecode]

As you see, we’re currently using the different UNDO tablespace that hasn’t any information about before images of data blocks of the TBL_FL_ARCHIVE. Now, let’s use Flashback Query against to that table:

[sourcecode] SQL> conn us1/us1

Connected.

SQL> select * from tbl_fl_archive as of timestamp to_timestamp(‘13022010 12:45:30′,’ddmmyyyy hh24:mi:ss’);

        ID NAME

———- ——————–

         1 Flashback Archive

SQL>  [/sourcecode]

This query gets the data from Flashback Data Archive

Modify the Flashback Data Archive

 

Use ALTER FLASHBACK ARCHIVE command to change the retention time, purge data or add or remove the tablespace. For this you need to have FLASHBACK ARCHIVE ADMINISTER privilege granted.  

–          To change the retention time, use:

[sourcecode] SQL> alter flashback archive fl_archive modify retention 6 month;

Flashback archive altered.

SQL> [/sourcecode]

–          To change tablespace quota of the tablespace that is used by a flashback data archive, use:

[sourcecode] SQL> alter flashback archive fl_archive add tablespace tbs_arch quota 50m;

Flashback archive altered.

SQL> [/sourcecode]

–          To add another tablespace for flashback data archive, use:

[sourcecode] SQL> create tablespace tbs_arch2 datafile ‘c:\flashback_archive2.dbf’ size 10m;

Tablespace created.

SQL> alter flashback archive fl_archive add tablespace tbs_arch2 quota 10m;

Flashback archive altered.

SQL> [/sourcecode]

–          To remove the tablespace from use by flashback data archive, use:

[sourcecode] SQL> alter flashback archive fl_archive remove tablespace tbs_arch2;

Flashback archive altered.

SQL> [/sourcecode]

–          To purge the data that’s in Flashback Data Archive, use PURGE BEFORE SCN (TIMESTAMP) or PURGE ALL as follows:

[sourcecode] SQL> ALTER FLASHBACK ARCHIVE fl_archive PURGE ALL;

Flashback archive altered.

SQL> ALTER FLASHBACK ARCHIVE fl_archive purge before timestamp to_timestamp(‘13022010 12:49:30′,’ddmmyyyy hh24:mi:ss’);

Flashback archive altered.

SQL> ALTER FLASHBACK ARCHIVE fl_archive purge before scn 988827;

Flashback archive altered.

SQL> [/sourcecode]

Dropping Flashback Data Archive

 

To drop flashback data archive use:

[sourcecode] SQL> drop flashback archive fl_archive;

Flashback archive dropped.

SQL> [/sourcecode]

Using default Flashback Data Archive for the system

 

As default, Oracle doesn’t use any flashback data archive. To set default flashback data archive, use the following command by connecting as SYS user:

[sourcecode] SQL> conn / as sysdba

Connected.

SQL> alter flashback archive fl_arc set default;

Flashback archive altered.

SQL> [/sourcecode]

By setting default flashback data archive, you will not need to specify it manually on each table you want to keep changes

To disable flashback archive for a table, use:

 

[sourcecode] SQL> alter table tbl_fl_archive no flashback archive;

Table altered.

SQL> [/sourcecode]

Query Flashback Data Archive

 

There’re mainly three views that are used to query the information regarding Flashback Data Archive.

The first view is DBA_FLASHBACK_ARCHIVE which gives information about Flashback Data Archive files

[sourcecode] SQL> select * from dba_flashback_archive;

FLASHBACK_ARCHIVE_NAME FLASHBACK_ARCHIVE# RETENTION_IN_DAYS CREATE_TIME

LAST_PURGE_TIME     

—————      ————–             ————  ———-

FL_ARCH                       2                 365           13-FEB-10

08.05.14.000000000 PM                    13-FEB-10 08.05.14.000000000 PM  [/sourcecode]

The second view is DBA_FLASHBACK_ARCHIVE_TS which gives information about tablespace that contains flashback data archives:

[sourcecode] SQL> select * from dba_flashback_archive_ts;

FLASHBACK_ARCHIVE_NAME       FLASHBACK_ARCHIVE#  TABLESPACE_NAME QUOTA_IN_MB

——————     —————–   ————-    ———-

FL_ARCH                        2                TBS_ARCH        FL_ARC [/sourcecode]

The third view is FLASHBACK_ARCHIVE_TABLES which displays the information of the tables that uses Flashback Data Archive:

[sourcecode] SQL> select * from dba_flashback_archive_tables;

TABLE_NAME    OWNER_NAME    FLASHBACK_ARCHIVE_NAME   ARCHIVE_TABLE_NAME

———-    ———-    ———————-   ——————

TBL_FL_ARCHIVE  US1        FL_ARC               SYS_FBA_HIST_69845 [/sourcecode]

As it was told above, Flashback Data Archie technology stores data in Compressed and Partitioned format. It should be checked easily. Get the name of the main table that’s used to store the data and query USER_TAB_PARTITIONS view as follows:

[sourcecode] SQL> select table_name,tablespace_name from user_tables;

TABLE_NAME                     TABLESPACE_NAME

—————————— ——————————

SYS_FBA_HIST_69845

SYS_FBA_TCRV_69845             TBS_ARCH

SYS_FBA_DDL_COLMAP_69845       TBS_ARCH

TBL_FL_ARCHIVE                 USERS

SQL> select table_name, partition_name, compression, compress_for from user_tab_partitions where table_name=’SYS_FBA_HIST_69845′;

TABLE_NAME                     PARTITION_NAME                 COMPRESS

—————-           ————–                 ———

SYS_FBA_HIST_69845             HIGH_PART                      ENABLED 

SQL> [/sourcecode]

Posted in RMAN Backup and Recovery | 15 Comments »