Kamran Agayev's Oracle Blog

Oracle Certified Master

Oracle Flashback Data Archive (Total Recall)

Posted by Kamran Agayev A. on July 21st, 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]

15 Responses to “Oracle Flashback Data Archive (Total Recall)”

  1. Ulfet Says:

    As usual, nice article, nice explanation.

  2. Fuad HAJIYEV Says:

    I have read the article and saw it understandable and easy to apply.
    Thank you teacher!

  3. ArtistePrasanna Says:

    Dear Mr.Kamran,

    Thanks for this post. I have been following your posts one by one. Its gainning more confidence in me and techinically more strong. I would like to purchase your book. Has your book on RMAN – “My Book – Expert secrets for using RMAN and Data Pump” been released. I want to procure it.
    I am a Oracle 9i Certified Associate – DBA. Now I am approaching OCP in 9i. Then I have planned to take OCPs in 10g & 11g. I trust that your book will be very useful to me in all aspects.

    Can you also give me collections of Questions & Answers on Interview point of view for Oracle DBA?

  4. Kamran Agayev A. Says:

    Dear Artiste

    My book should be available for the end of this year

    I’ll let you know

  5. ArtistePrasanna Says:

    Thanks. Looking forward for that book.
    Can you also give me collections of Questions & Answers on Interview point of view for Oracle DBA?

  6. Xenofon Says:

    Sorry for the many messages. The link can be found behind my name “Xenofon”. Here in plain text:

    http://forums.oracle.com/forums/thread.jspa?threadID=1556446&tstart=45

  7. Kamran Agayev A. Says:

    This is a bug. See the following metalink note:
    Bug 7028762: FLASHBACK DATA ARCHIVE COSUMES HIGH CPU

  8. Bora Yüret Says:

    Kamran,

    Aşağıdaki komuttaki fl_archive yerine fl_arch olması gerekiyor.

    Kolay gelsin,

    Bora Yüret

    SQL> alter table tbl_fl_archive flashback archive fl_archive;

  9. Kamran Agayev A. Says:

    Merhaba Bora, hos geldin bloguma :)

    Tesekkurler, deyisdim :)

    Yine bekleriz, kendine iyi bak

  10. Usman Says:

    hi!
    Kamran..
    i need to know that can we use multiple data archives having different retention period on same table.
    Like table A has 2 flashback data archives fda1 and fda2 ,fda1 has retention period of 3 months while fda2 has retention period of 4 months and they both correspond to same table A.is that possible?

  11. Flashback Data Archive « Casa do DBA Says:

    […] and Data Pump Enhacements. California USA. 2.1 ed. Setembro 2010. v.II, p.212-230 Disponível em http://kamranagayev.com/2010/07/21/oracle-flashback-data-archive-total-recall/. Acesso em: 19 out. […]

  12. sagar verma Says:

    Great Document

  13. keshav Says:

    very well explained sir

  14. affrayam Says:

    Dear Mr.Kamran,

    Thanks for this post. I have been following your posts one by one. As a learner i am gaining excellent knowledge.Few days back i have seen your blog regarding flashback archive concept, its very nice and it was in simple way to understand.Once again thanks for your posts.

  15. Kamran Agayev A. Says:

    Thank you for the feedback, Affrayam

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>