Category Archives: flashback

Oracle 12c – Flashback Transaction Query

Flashback Transaction Query is used to retrieve metadata and historical data for single and all transactions in a time interval from a static data dictionary view FLASHBACK_TRANSACTION_QUERY.

The FLASHBACK_TRANSACTION_QUERY has a column UNDO_SQL which is the opposite of DML which caused the change.  You can use the DBMS_FLASHBACK.TRANSACTION_BACKOUT procedure to roll back transaction and dependent transactions.   This procedure does not commit the rollback but temporarily holds the rollback until a manual commit is made.  To perform the flashback transaction query the database must be in archivelog mode and supplemental logging must be enabled with command:  ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;

 

For a user to use the flashback query the admin must grant following.

  1. Grant flashback to user.
  2. Grant select privileges on objects.
  3. To allow queries on all tables grant FLASHBACK ANY TABLE privilege.
  4. Grant SELECT ANY TRANSACTION privilege.
  5. Must have SELECT, UPDATE, DELETE, INSERT privilege on that table.
  6. Execute privilege on DBMS_FLASHBACK package.

 

This article details setting up flashback transactions for your database, granting user scott the appropriate privileges, and performing flashback.

 

  1. Logon to your SQLPLUS as sysdba.

 

[oracle@linux2 flash_recovery_area]$ sqlplus / as sysdba

 

SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 6 08:25:35 2017

 

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

 

Connected to:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 – 64bit Production

With the Partitioning, OLAP, Advanced Analytics, Real Application Testing

and Unified Auditing options

 

SQL>

 

  1. Verify that the database is in archivelog mode, if not switch to archive log mode.

 

SQL> archive log list

Database log mode              Archive Mode

Automatic archival             Enabled

Archive destination            USE_DB_RECOVERY_FILE_DEST

Oldest online log sequence     2708

Next log sequence to archive   2710

Current log sequence           2710

SQL>

 

  1. Issue the command ‘ ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;’

 

SQL> ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;

Database altered.

SQL>

 

  1. To allow queries on all tables grant FLASHBACK ANY TABLE privilege.

 

SQL> grant flashback any table to scott;

Grant succeeded.

SQL>

 

  1. Grant SELECT ANY TRANSACTION privilege.

 

SQL> grant select any transaction to scott;

Grant succeeded.

SQL>

 

  1. Execute privilege on DBMS_FLASHBACK package.

 

SQL> grant execute on dbms_flashback to scott;

Grant succeeded.

SQL>

 

  1. Logon as the user scott.

 

SQL> connect scott/password

Connected.

SQL>

 

  1. Create the test table fb_test.

 

create table fb_test(fname varchar2(30), amt number);

SQL> create table fb_test(fname varchar2(30), amt number);

Table created.

SQL>

 

  1. Insert single record into fb_test.

 

insert into scott.fb_test(fname,amt) values(‘Larry’,111);

commit;

 

 

SQL> insert into scott.fb_test(fname,amt) values(‘Larry’,111);

commit;

1 row created.

SQL>

Commit complete.

SQL>

 

  1. Update single record and commit;

 

update scott.fb_test set amt=222 where fname=’Larry’;

commit;

 

 

SQL> update scott.fb_test set amt=222 where fname=’Larry’;

commit;

1 row updated.

SQL>

Commit complete.

SQL>

 

 

  1. Select value from scott.fb_test.

 

SQL> select * from scott.fb_test;

FNAME                                 AMT

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

Larry                                 222

SQL>

 

  1. Query flashback_transaction_query for transactions against scott.fb_test.

 

SQL> select operation, undo_sql  from flashback_transaction_query

where table_name=’FB_TEST’;  2

OPERATION

——————————–

UNDO_SQL

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

INSERT

delete from “SCOTT”.”FB_TEST” where ROWID = ‘AAAXaKAAGAAALUdAAA’;

UPDATE

update “SCOTT”.”FB_TEST” set “AMT” = ‘111’ where ROWID = ‘AAAXaKAAGAAALUdAAA’;

SQL>

 

  1. You can now execute the UNDO_SQL column to rewind the update statement.

 

SQL> update “SCOTT”.”FB_TEST” set “AMT” = ‘111’ where ROWID = ‘AAAXaKAAGAAALUdAAA’;

1 row updated.

SQL> commit;

Commit complete.

SQL>

 

  1. For a more complex set of updates impacting muptiple objects, you would use the dbms_flashback.transaction_backout procedure.

 

  1. This completes flashback transaction query.

 

 

Larry Catt

OCP

Oracle 12c – Flashback Technology Overview

Flashback table – by default when a table is dropped oracle does not immediately remove the space, but renames it and places the table and associated objects in recycle bin.   This allows it to be recovered at a later time.

 

The recycle bin is a data dictionary table that contains info needed to recover dropped tables.  Original table stays in place and occupies same space until purged or database needs space for storage.

 

tablespaces are not placed in recycle bin and once dropped are gone.   All tables in that tablespace are removed from recycle bin if they have previously been dropped.

 

Recycle bin can be disabled with recyclebin initialization parameter.

 

FLASHBACK TABLE ….  TO BEFORE DROP;    statement is used to recover tables from recyclebin.  You must use the system generated recyclebin name.  You can rename the table with RENAME clause during recovery.  The USER_RECYCLEBIN view can be used to find the system generated recyclebin name.

example:

 

FLASHBACK TABLE test2_bak TO BEFORE DROP RENAME test2;

 

Larry Catt

OCP

Oracle 12c – New Feature in Flashback Data Archive

User-context tracking – allows you to determine which user has made a change to a table through the use of procedure SET_CONTEXT_LEVEL in the DBMS_FLASHBACK_ARCHIVE package.  The information can be accessed via the GET_SYS_CONTEXT procedure. When set to TYPICAL changes are recorded with userID, global userID, and hostname.

 

begin

DBMS_FLASHBACK_ARCHIVE.SET_CONTEXT_LEVEL(TYPICAL);

end;

/

 

Database hardening – You can now associate a subset of tables with a single application and enable flashback just those subset of tables.  You can perform the following on the subset of tables:

  1. Enable flashback on just subset.
  2. Lock DML operation on subset.

This enables you to protect and track sensitive set of tables.   This procedure REGISTER_APPLICATION in package DBMS_FLASHBACK_ARCHIVE is used for this hardening process.

 

Import and export of history – You can create a temporary history table for a table. This history can be extract showing exact changes to a table over period of time.

 

Larry Catt

OCP

Oracle 12c – Flashback Table

By default when a table is dropped oracle does not immediately remove the space, but renames it and places the table and associated objects in recycle bin which allows it to be recovered at a later time.  The recycle bin is a data dictionary table that contains info needed to recover dropped tables.  Original table stays in place and occupies same space until purged or database needs space for storage.  Tablespaces are not placed in recycle bin and once dropped are gone.   All tables in that tablespace are removed from recycle bin if they have previously been dropped.   Recycle bin can be disabled with recyclebin initialization parameter.   To recover a dropped table from the recyclebin the command ‘FLASHBACK TABLE ….  TO BEFORE DROP; ‘is used.  You must use the system generated recyclebin name.  You can rename the table with RENAME clause during recovery.  The USER_RECYCLEBIN view can be used to find the system generated recyclebin name.  This article demonstrates the use of flashback table.

 

  1. Logon to SQLPLUS as the user scott.

 

[oracle@linux2 ~]$ scott/password

SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 6 08:25:35 2016

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Connected to:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 – 64bit Production

With the Partitioning, OLAP, Advanced Analytics, Real Application Testing

and Unified Auditing options

SQL>

 

 

  1. Create table fb_test.

 

create table fb_test(fname varchar2(30), amt number);

SQL> create table fb_test(fname varchar2(30), amt number);

Table created.

SQL>

 

 

  1. Perform insert into test table fb_test and commit;

 

insert into scott.fb_test(fname,amt) values(‘Larry’,111);

commit;

SQL> insert into scott.fb_test(fname,amt) values(‘Larry’,111);

commit;

1 row created.

SQL>

Commit complete.

SQL>

 

 

  1. Select the values from test table fb_test.

 

SQL> select * from fb_test;

FNAME                                 AMT

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

Larry                                 111

SQL>

 

  1. Drop the test table fb_test.

 

SQL> drop table fb_test;

Table dropped.

SQL>

 

 

  1. Attempt a select from table fb_test and see the table does not exist.

 

SQL>  select * from fb_test;

 select * from fb_test

               *

ERROR at line 1:

ORA-00942: table or view does not exist

SQL>

 

 

 

  1. Flashback the dropped table.

 

SQL> flashback table fb_test to before drop;

Flashback complete.

SQL>

 

  1. Perform select operation on table fb_test.

 

SQL> select * from fb_test;

FNAME                                 AMT

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

Larry                                 111

SQL>

 

  1. This completes flashback to table.

 

 

Larry Catt

OCP

Oracle 12c – Enabling Flashback Database

In Oracle 12c Flashback Database must be configured which allow for tracking of changes for the database to revert to.   This article details the steps in enabling flashback database feature.

 

 

  1. Logon to server as the oracle software owner.

 

[root@orclc-db1 devadmin]# su – oracle

Last login: Mon Feb 19 18:31:43 UTC 2016 on pts/0

mylinux#

 

  1. Open or mount the oracle database.

 

mylinux# sqlplus / as sysdba

SQL*Plus: Release 12.1.0.2.0 Production on Tue Feb 20 12:41:43 2016

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup mount

ORACLE instance started.

Total System Global Area 5.1540E+10 bytes

Fixed Size                  2938792 bytes

Variable Size            3.4628E+10 bytes

Database Buffers         1.6777E+10 bytes

Redo Buffers              131276800 bytes

Database mounted.

SQL>

 

  1. Set the DB_FLASHBACK_RETENTION_TARGET initialization parameter to time period of recovery, default is 1440 minutes or 1 day.

SQL> alter system set DB_FLASHBACK_RETENTION_TARGET=2880 scope=both;

System altered.

SQL>

 

  1. Set the initialization parameter DB_RECOVERY_FILE_DEST_SIZE to a suitable size. NOTE:  This is simple a test system so we area setting the recovery size to 4GB,  a real system will require a larger size.

 

SQL> alter system set db_recovery_file_dest_size = 4g scope=both;

System altered.

 

  1. Set the initialization parameter DB_RECOVERY_FILE_DEST to a suitable large enough space one disk.

 

SQL> alter system set db_recovery_file_dest=’ /u04/oradata/ORCL/flash_recovery_area’ scope=both;

System altered.

SQL>

 

  1. Ensure that archivelog mode is enabled and enable if not.

 

SQL> archive log list

Database log mode              No Archive Mode

Automatic archival             Disabled

Archive destination            /opt/app/oracle/orcl_db/dbs/arch

Oldest online log sequence     1613

Current log sequence           1633

 

  1. Archiving log is not currently enabled, restart the database in mount mode and enable archiving.

 

SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup mount

ORACLE instance started.

 

Total System Global Area  549453824 bytes

Fixed Size                  2926616 bytes

Variable Size             268437480 bytes

Database Buffers          272629760 bytes

Redo Buffers                5459968 bytes

Database mounted.

SQL> alter database archivelog;

 

Database altered.

 

SQL>

 

  1. Enable flashback for database with ALTER DATABASE command, before opening database.

 

SQL> alter database flashback on;

Database altered.

SQL>

 

  1. Alter the database to open.

 

SQL> alter database open;

Database altered.

SQL>

 

  1. This completes enabling flashback for your Oracle RDBMS.

 

Larry Catt

OCP

 

Oracle 12c – Steps to perform a Flashback Database Operation

The flashback database feature allows you to reset the database to earlier point in time to resolve logical corruption.    It is faster than point in time recovery and does not require backup and restore operations.  Flashback database operations is available from RMAN or SQL with command: FLASHBACK DATABASE.   To enable flashback database the FAST RECOVERY AREA must be configured and FLASHBACK RETENTION TARGET set.   The DB_FLASHBACK_RETENTION_TARGET states how far in the past the database can be recovered to and Flashback must be setup in advance of need. (See article Enabling Flashback Database)  The database periodically copies blocks from every datafile into recovery area and maintains logs of changes to those blocks.  Additionally flashback logs, redo logs must be available for entire span of retention target.   FLASHBACK DATABASE window is the time that is covered by flashback logs.  FLASHBACK will delete oldest logs first if it runs low on space.

 

FLASHBACK has these limits:

  1. It can only undo changes to datafile made by Oracle Database, not media failure or deletion of file.
  2. It cannot undo shrink operations.
  3. If control file is recovered or recreated all flashback data is invalid.
  4. IF NOLOGGING is used, block corruption is likely if flashback is used.

 

Restore points can be used instead of time or SCN, Restore points are alias of scn or time.

Commands that support restore point are RECOVER DATABASE and FLASHBACK DATABASE in RMAN   and FLASHBACK TABLE in SQL.  Guaranteed restore points never age out of control file and must be explicitly dropped, it ensures that you can use flashback database to a single SCN even if flashback logging is not enabled.  When enabled it enforces retention of flashback logs all the way to guaranteed SCN.  This article will demonstrate database flashback in RMAN.

 

  1. Logon to server as the oracle software owner.

 

[larry@linux2 ~]$ su – oracle

Password:

Last login: Thu Jan  5 10:45:29 EST 2015 on pts/1

Enter database to use:

1 – ORCL

2 – CDB1

Option >

1

[oracle@linux2 ~]$

 

  1. Start RMAN and connect to your database.

 

[oracle@linux2 ~]$ rman

Recovery Manager: Release 12.1.0.2.0 – Production on Tue Jan 10 09:18:41 2015

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

RMAN> connect target /

connected to target database: ORCL (DBID=1457738340)

RMAN>

 

  1. Use timestamp_to_scn function to get SCN for 48 hours ago.

 

RMAN> select timestamp_to_scn(systimestamp-2) from dual;

TIMESTAMP_TO_SCN(SYSTIMESTAMP-2)

——————————–

                         4236799

RMAN>

 

  1. Shutdown the database with immediate option if open.

 

RMAN> shutdown immediate

using target database control file instead of recovery catalog

database closed

database dismounted

Oracle instance shut down

RMAN>

 

  1. Startup the database with mount.

 

RMAN> startup mount

connected to target database (not started)

Oracle instance started

database mounted

Total System Global Area     549453824 bytes

Fixed Size                     2926616 bytes

Variable Size                272631784 bytes

Database Buffers             268435456 bytes

Redo Buffers                   5459968 bytes

RMAN>

 

  1. Execute the FLASHBACK DATABASE command with SCN retrieved above.

 

flashback database to scn 4236799;

RMAN> flashback database to scn 4236799;

Starting flashback at 10-JAN-17

allocated channel: ORA_DISK_1

channel ORA_DISK_1: SID=22 device type=DISK

 

starting media recovery

 

archived log for thread 1 with sequence 2761 is already on disk as file /u04/oradata/ORCL/flash_recovery_area/ORCL/archivelog/2015_01_08/o1_mf_1_2761_d74og7tj_.arc

media recovery complete, elapsed time: 00:00:02

Finished flashback at 10-JAN-17

 

RMAN>

 

  1. Open the database with command ‘ALTER DATABASE OPEN READ ONLY;’

 

RMAN> alter database open read only;

Statement processed

RMAN>

 

  1. If not errors, shutdown the database with immediate option.

 

RMAN> shutdown immediate

database closed

database dismounted

Oracle instance shut down

RMAN>

 

  1. Startup the database with mount option.

 

RMAN> startup mount

connected to target database (not started)

Oracle instance started

database mounted

 

Total System Global Area     549453824 bytes

Fixed Size                     2926616 bytes

Variable Size                272631784 bytes

Database Buffers             268435456 bytes

Redo Buffers                   5459968 bytes

 

RMAN>

 

  1. Open the database with resetlogs option: ‘ALTER DATABASE OPEN RESETLOGS;’

 

RMAN> ALTER DATABASE OPEN RESETLOGS;

Statement processed

RMAN>

 

  1. You should take a full backup at the end of any recovery.
  2. This completes flashing back a database in RMAN.

 

Larry Catt

OCP

Perform Flashback for a CDB in Oracle 12c

Oracle 12c provides the ability to flashback the database to a previous state.  This procedures demonstrates the use of this process to flashback the database 15 minutes.   NOTE:  Four states must exist for flashback to work:

  1. Must be in archivelog mode.
  2. Database must have flashback on.
  3. Control files must contain flashback logs, thus not recreated.
  4. No tablespaces exist with flashback off set.

 

 

  1. Logon to your Oracle database server as the Oracle software owner and logon to SQLPLUS.

 

[root@linux2 etc]# su – oracle

Last login: Thu Jul  1 09:25:23 EST 2015 on pts/1

[oracle@linux2 ~]$

[oracle@linux2 ~]$ sqlplus / as sysdba

 

SQL*Plus: Release 12.1.0.2.0 Production on Thu Jul 1 13:47:01 2015

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Connected to:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 – 64bit Production

With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL>

 

  1. Determine SCN of database 15 min ago by using the timestamp_to_scn().

 

SQL> select timestamp_to_scn(sysdate-1/96) from dual;

TIMESTAMP_TO_SCN(SYSDATE-1/96)

——————————

                       5067892

SQL>

 

  1. Now using SCN extracted by the previous step, shutdown the database, startup in mount mode and execute the flashback command.

 

SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup mount

ORACLE instance started.

 

Total System Global Area  734003200 bytes

Fixed Size                  2928728 bytes

Variable Size             524292008 bytes

Database Buffers          201326592 bytes

Redo Buffers                5455872 bytes

Database mounted.

SQL> FLASHBACK DATABASE TO SCN 5067892;

 

Flashback complete.

 

SQL>

 

  1. Startup the database for normal use with resetlogs option and see that all transactions of the last 15 minutes are gone.

 

SQL> alter database open resetlogs;

Database altered.

SQL>

 

 

  1. NOTE: You should take a full backup of the database as soon after flashback as possible to ensure future recovery.

 

  1. This completes the use of Flashback in Oracle 12c.

 

 

Larry Catt

OCP

Oracle 12c – Flashback Version Query

Flashback Version Query is used to get metadata and historical data for a particular time interval.  This is done by VERSIONS BETWEEN clause with time interval being either timestamps or SCN using the following pseudo columns:

  • VERSIONS_START
  • VERSIONS_END
  • VERSIONS_XID
  • VERSIONS_OPERATIONS

 

Return columns are from START date and up until (not including) the END date.

 

EXAMPLE:

 

 

  1. Logon to your Oracle 12c database as user scott.

 

[oracle@linux2 flash_recovery_area]$ sqlplus scott/password

SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 6 07:33:45 2015

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Wed Jan 04 2015 07:45:52 -05:00

Connected to:

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 – 64bit Production

With the Partitioning, OLAP, Advanced Analytics, Real Application Testing

and Unified Auditing options

SQL>

 

  1. Create a table fb_scott.

 

SQL> create table fb_test(fname varchar2(30), amt number);

Table created.

 

  1. Select current system timestamp

 

select systimestamp from dual;

SQL> select systimestamp from dual;

SYSTIMESTAMP

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

06-JAN-15 07.44.13.923919 AM -05:00

SQL>

 

  1. Insert values into your new table fb_test and commit insert.

 

insert into scott.fb_test(fname,amt) values(‘Larry’,111);

commit;

SQL> insert into scott.fb_test(fname,amt) values(‘Larry’,111);

commit;

1 row created.

SQL>

Commit complete.

SQL>

 

  1. Now perform 3 update operations on table fb_test and commit between each update.

 

update scott.fb_test set amt=222 where fname=’Larry’;

commit;

update scott.fb_test set amt=333  where fname=’Larry’;

commit;

update scott.fb_test set amt=444 where fname=’Larry’;

commit;

 

SQL> update scott.fb_test set amt=222 where fname=’Larry’;

commit;

update scott.fb_test set amt=333  where fname=’Larry’;

commit;

update scott.fb_test set amt=444 where fname=’Larry’;

commit;

1 row updated.

SQL>

Commit complete.

SQL>

1 row updated.

SQL>

Commit complete.

SQL>

1 row updated.

SQL>

Commit complete.

SQL>

 

  1. Finally select current systimestamp from dual.

 

select systimestamp from dual;

SQL> select systimestamp from dual;

SYSTIMESTAMP

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

06-JAN-15 07.45.55.308034 AM -05:00

SQL>

 

 

  1. Select the past versions of your table fb_test using the versions between clause.

 

select versions_starttime, versions_endtime, versions_xid, versions_operation as op, amt

from scott.fb_test  versions between timestamp to_timestamp (’06-JAN-15 07:44:13′, ‘DD-MON-YY HH24:MI:SS’) and to_timestamp (’06-JAN-15 07:45:55′, ‘DD-MON-YY HH24:MI:SS’) where fname=’Larry’;

 

Results:

 

SQL> select versions_starttime, versions_endtime, versions_xid, versions_operation as op, amt

from scott.fb_test  versions between timestamp to_timestamp (’06-JAN-15 07:44:13′, ‘DD-MON-YY HH24:MI:SS’) and to_timestamp (’06-JAN-15 07:45:55′, ‘DD-MON-YY HH24:MI:SS’) where fname=’Larry’;

  2

VERSIONS_STARTTIME

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

VERSIONS_ENDTIME

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

VERSIONS_XID     O        AMT

—————- – ———-

06-JAN-15 07.45.30 AM

090009006F0B0000 U        444

06-JAN-15 07.45.30 AM

06-JAN-15 07.45.30 AM

02001500AF0B0000 U        333

VERSIONS_STARTTIME

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

VERSIONS_ENDTIME

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

VERSIONS_XID     O        AMT

—————- – ———-

06-JAN-15 07.45.27 AM

06-JAN-15 07.45.30 AM

04000A00680A0000 U        222

06-JAN-15 07.45.12 AM

06-JAN-15 07.45.27 AM

VERSIONS_STARTTIME

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

VERSIONS_ENDTIME

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

VERSIONS_XID     O        AMT

—————- – ———-

080021001F0C0000 I        111

SQL>

 

  1. This completes viewing previous versions of a table in Oracle 12c.

 

Larry Catt

OCP