Tuesday, August 14, 2007

Using Flashback Versions Query

Using Flashback Versions Query

===============================

 

(1) Execute update statments to generate changes

 

scott@TOY10G> update accounts set balance=2000 where account_no=1;

 

1 row updated.

 

scott@TOY10G> commit;

 

Commit complete.

 

scott@TOY10G> update accounts set balance=4000 where account_no=1;

 

1 row updated.

 

scott@TOY10G> commit;

 

Commit complete.

 

scott@TOY10G> update accounts set balance=9000 where account_no=1;

 

1 row updated.

 

scott@TOY10G> commit;

 

Commit complete.

 

scott@TOY10G>

 

 

(2) Query using versions between

 

scott@TOY10G> select balance from accounts

  2  versions between

  3  scn minvalue and maxvalue

  4  where account_no=1;

 

   BALANCE

----------

      6000

      4000

      2000

77195.4526

 

 

(3) Using timestamp 

 

select balance from accounts

versions between timestamp

to_timestamp('14-AUG-2007 19:15:01','DD-MON-YYYY HH24:MI:SS') and

to_timestamp('14-AUG-2007 21:29:01','DD-MON-YYYY HH24:MI:SS')

where account_no=1;

 

 

(4) pseudocolumns

 

scott@TOY10G> select to_char(versions_starttime,'DD-MON-YYYY HH24:MI') "START DATE",

to_char (versions_endtime, 'DD-MON-YYYY HH24:MI') "END DATE",

versions_xid,

versions_operation,

balance

from accounts

versions between scn

minvalue and maxvalue

where account_no=1;  2    3    4    5    6    7    8    9 

 

START DATE        END DATE          VERSIONS_XID     V    BALANCE

----------------- ----------------- ---------------- - ----------

14-AUG-2007 21:27                   000500110000039F U       9000

14-AUG-2007 21:27 14-AUG-2007 21:27 0007001B0000116F U       4000

14-AUG-2007 21:27 14-AUG-2007 21:27 0001002100000396 U       2000

14-AUG-2007 20:56 14-AUG-2007 21:27 0007001E0000116C U       6000

14-AUG-2007 20:56 14-AUG-2007 20:56 0002001C00000390 U       4000

14-AUG-2007 20:56 14-AUG-2007 20:56 0007000E0000116C U       2000

                  14-AUG-2007 20:56                    77195.4526

 

Note:

 

There are several new pseudocolumns that help you work with the Flashback Versions Query:

_ VERSIONS_STARTTIME

_ VERSIONS_STARTSCN

_ VERSIONS_ENDTIME

_ VERSIONS_ENDSCN

_ VERSIONS_XID

_ VERSIONS_OPERATION

 

Recycle Bin and Flashback Drop

This test shows the commands to flashback drop a table

Env: Oracle 10gR2

(1) Table structure

scott@TOY10G> desc t1

Name Null? Type

----------------------------------------------------- -------- ------------------------------------

OWNER NOT NULL VARCHAR2(30)

OBJECT_NAME NOT NULL VARCHAR2(30)

SUBOBJECT_NAME VARCHAR2(30)

OBJECT_ID NOT NULL NUMBER

DATA_OBJECT_ID NUMBER

OBJECT_TYPE VARCHAR2(19)

CREATED NOT NULL DATE

LAST_DDL_TIME NOT NULL DATE

TIMESTAMP VARCHAR2(19)

STATUS VARCHAR2(7)

TEMPORARY VARCHAR2(1)

GENERATED VARCHAR2(1)

SECONDARY VARCHAR2(1)

(2) Drop the table

scott@TOY10G> drop table t1;

Table dropped.

(3) Object in the recyclebin

scott@TOY10G> show recyclebin;

ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME

---------------- ------------------------------ ------------ -------------------

T1 BIN$N69b2mR+VDHgRAADupTRjg==$0 TABLE 2007-08-14:15:05:11

(4) We can query recycle bin name

scott@TOY10G> ;

1 select object_name from "BIN$N69b2mR+VDHgRAADupTRjg==$0"

2* where rownum < 10

scott@TOY10G> /

OBJECT_NAME

------------------------------

CON$

I_COL2

I_USER#

C_TS#

I_OBJ#

I_CON2

IND$

BOOTSTRAP$

UET$

9 rows selected.

(5) Flash back drop and rename the table

scott@TOY10G> flashback table "BIN$N69b2mR+VDHgRAADupTRjg==$0" to before drop rename to t3;

Flashback complete.

(6) New table contents

scott@TOY10G> select object_name from t3

where rownum < 10 2

3 /

OBJECT_NAME

------------------------------

CON$

I_COL2

I_USER#

C_TS#

I_OBJ#

I_CON2

IND$

BOOTSTRAP$

UET$

9 rows selected.

(7) Recyclebin is clean now

scott@TOY10G> show recyclebin;

scott@TOY10G>

OCP 10g ch5 - Understanding the Flashback Database

Review Questions

1. What type of recovery is the Flashback Database best suited for?
(Choose all that apply.)
A. User error
B. Physical corruption
C. Logical corruption
D. Media failure
--
Ans. A, C.
A Flashback Database recovery is best suited for a user error such as a
truncated table or a logical corruption like an incomplete batch
transaction affecting many tables. Media recovery situations cannot be
performed with the Flashback Database recovery method.


2. Flashback Database recovery can recover from which of the following
failure
scenarios?
A. Loss of control file
B. Dropping a tablespace through RESETLOGS
C. A user error that resized datafiles to a smaller size
D. A large truncated table or group of tables
--
Ans: D.
A Flashback Database recovery can recover a large truncated table or
group of tables.

3. What new background process is responsible for writing before block
images and recovering from the Flashback Database log?
A. RWVR
B. RVWR
C. RWRV
D. RVRW
---
Ans B.
The RVWR process is responsible for writing the "before" image
information tothe Flashback Database log. The Flashback Database log is
read to perform theFlashback Database recovery.

4. What are the benefits of the flash recovery area in the recovery and
backupprocess?
A. Recovery efficiency is improved because all files are stored on tape
media for fast access.
B. Recovery efficiency is improved because the files are stored in
multiple
locations.
C. Recovery efficiency is improved because the files are stored in one
location on tape.
D. Recovery efficiency is improved because the files are stored in one
location on disk.
---
Ans D.
The flash recovery area is a centralized storage area for backups on
disk. This
allows for a more efficient recovery process because the required files
are in one
location and are stored on disk instead of tape.

5. Where is information about the status of the flash recovery area
displayed?
(Choose all that apply.)
A. Alert log
B. Background trace files
C. V$_OUSTANDING_ALERTS
D. DBA_OUTSTANDING_ALERTS
---
Ans: A, D.
The Alert log reports space usage and other information about the flash
recovery area. DBA_OUTSTANDING_ALERTS also show the information state of
the
flash recovery area.

6. How is the size of the flash recovery area determined? (Choose all
that apply.)
A. The size is automatically allocated at 2 gigabytes.
B. Using the ALTER SYSTEM command to dynamically set the size.
C. With the initialization parameter DB_RECOVERY_FILE_DEST_SIZE.
D. Using the ALTER TABLESPACE command.
---
Ans: B, C.
The flash recovery area can be determined by either setting the
initialization parameter DB_RECOVERY_FILE_DEST_SIZE or using the ALTER
SYSTEM command.


7. What type of backup commands can the flash recovery area be used for?
(Choose all that apply.)
A. BACKUP COPY
B. BACKUP IMAGE
C. BACKUP
D. BACKUPSET
---
Ans: A, C.
The flash recovery area supports both the BACKUP and BACKUP COPY
commands, which perform backup sets and image copies.

8. The flash recovery area space utilization and stored files can be
identified by what method?
A. DBA_OUTSTANDING_ALERTS
B. V$OUTSTANDING_ALERTS
C. V$RECOVERY_FILE_DEST
D. DBA_RECOVERY_FILE_DEST
---
Ans: C.
The dynamic view V$RECOVERY_FILE_DEST displays the space utilization and
the
amount of files that make up the flash recovery area.


9. What parameter determines the length of time that the Flashback
Database will store "before" images that can be used in the recovery
process?
A. DB_FLASHBACK_RETENTION_POLICY
B. DB_FLASHBACK_RETENTION_TIME
C. DB_FLASHBACK_RETENTION_STORE
D. DB_FLASHBACK_RETENTION_TARGET
--
Ans: D.
The DB_FLASHBACK_RETENTION_TARGET parameter determines how much data is
available to recover.

10. How is the DB_FLASHBACK_RETENTION_TARGET parameter measured?
A. By SCN
B. By redo log sequences
C. By time in minutes
D. By redo log sequence and threads
---
Ans: C.
DB_FLASHBACK_RETENTION_TARGET is a parameter that is measured in
minutes.
This value determines how many minutes the Flashback Database should
write data before this data gets overwritten.

11. To enable the Flashback Database, what must be done to the database?
(Choose all that apply.)
A. It must be mounted.
B. It must be opened with RESETLOGS.
C. The flash recovery area must be created.
D. The database must be in ARCHIVELOG mode.
---
Ans: A, C, D.
To enable the Flashback Database, the flash recovery area must be
created.
The database must be mounted but not opened to turn on the Flashback
Database.
The database must also be in ARCHIVELOG mode.

12. When using the Flashback Database in a recovery situation, what
information would be useful to know? (Choose all that apply.)
A. Information about the smallest SCN number that is stored in the
Flashback Database log
B. Information about the earliest timestamp that is stored in the
Flashback
Database log
C. Information about the greatest SCN number that is stored in the
Flashback Database log
D. Information about the latest timestamp that is stored in the
Flashback
Database log
---
Ans: A, B.
The earliest timestamp and smallest SCN will tell you how far back you
can recover the database. These values can be queried by the
V$FLASHBACK_DATABASE_LOG dynamic view.

13. How can you determine if the Flashback Database is turned on?
A. Query the DBA_FLASHBACK_DATABASE view.
B. Query the V$DATABASE dynamic view.
C. Check the initialization parameters.
D. Check the alert log.
---
Ans: B.
The V$DATABASE dynamic view has a new column called FLASHBACK_ON, which
contains a value of YES or NO.

14. Which of the following views can determine how much space you may
need in the future in the Flashback Database logs?
A. V$DATABASE
B. V$FLASHBACK_DATABASE_STAT
C. V$FLASHBCK_DATABASE_LOG
D. DBA_FLASHBACK_LOG
----
Ans: B.
The V$FLASHBACK_DATABASE_STAT dynamic view shows the daily growth and
utilization of the Flashback Database log. You can match daily
activities to the daily utilization of the Flashback Database log.


15. What is the default size of a redo log file created in the flash
recovery area?
A. 100MB
B. 150MB
C. 10MB
D. 50MB
---
Ans: A.
The default size of a redo log file created in the flash recovery area
is 100MB.

16. Which initialization parameter will not create archive logs to the
flash recovery area?
A. ARCHIVE_LOG_DEST
B. ARCHIVE_DUPLEX_DEST
C. ARCHIVE_LOG_DEST_n
D. LOG_ARCHIVE_DEST_n
---
Ans: D.
The LOG_ARCHIVE_DEST_n is the only initialization parameter that will
create or write archive logs to the flash recovery area.

17. Which database files are permanently stored in the flash recovery
area?
Choose all that apply.)
A. Datafiles
B. RMAN files
C. Control files
D. Current online redo logs
E. Archive logs
----
Ans: C, D.
Control files and redo logs area considered permanent files in the flash
recovery area. These files are not made obsolete and deleted, even when
backed up to tape.

18. Which files will not be backed up in the flash recovery area when
you're using the BACKUP RECOVERY AREA and BACKUP RECOVERY FILES
commands?
(Choose all that apply.)
A. Control files
B. Redo logs
C. Datafiles
D. Permanent files
E. Flashback logs
---
Ans: A, B, D, E.
The flash recovery area will not back up redo log files, control files,
or
flashback logs with the BACKUP RECOVERY AREA and BACKUP RECOVERY FILES
commands. Permanent files are considered to be current online redo logs
and control
files.

19. What is responsible for applying the "before" images to the database
during a Flashback Database recovery?
A. LGWR
B. SMON
C. DBWR
D. RWVR
--
Ans: D.
The RWVR process is responsible for writing the "before" images to the
Flashback Database log. This process is also responsible for applying
these to the database during a recovery.

20. What administrative database activity cannot be undone with the
Flashback Database recovery?
A. Dropped table
B. Dropped user
C. Resized datafiles to smaller size
D. Dropped tablespace
---
Ans: C.
Resizing of a tablespace or datafiles to a smaller size cannot be undone
with Flashback Database recovery.

Flash Recovery Practice

Flash Recovery Practice
=========================
Env: Oracle 10gR2

1. Display init.ora parameter

sys@TOY10G> show parameter db_recover

NAME TYPE VALUE
------------------------------------ -----------
------------------------------
db_recovery_file_dest string
/ora01/orabkup/GENQ/.temp_toy1

0g/ora01/orarcv/TOY10G
db_recovery_file_dest_size big integer 2G

2. Connect to target database

dfsdb1:TOY10G: /ora01/orabkup/GENQ/.temp_toy10g/oracle/admin/TOY10G >
rman

Recovery Manager: Release 10.2.0.3.0 - Production on Tue Aug 14
08:54:08 2007

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

RMAN> connect target

connected to target database: TOY10G (DBID=3330944552)

RMAN>

3. Check default backup location is same as db_recovery_file_dest
RMAN> show all;

RMAN configuration parameters are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F';
# default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; #
default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; #
default
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT
'/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/%rec_area_%s_%p.ba
k';
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO
'/oracle/product/10.2.0/db_1/dbs/snapcf_TOY10G.f'; # default


4. Create a dummy table t1 from all_objects

scott@TOY10G> create table t1 as select * from all_objects;

Table created.

scott@TOY10G> select count(*) from t1;

COUNT(*)
----------
9531


5. Perform a backup of target database

RMAN> backup as copy database;
Starting backup at 14-AUG-2007 09:25:12
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=30 devtype=DISK
channel ORA_DISK_1: starting datafile copy
input datafile fno=00001
name=/ora01/orabkup/GENQ/.temp_toy10g/ora01/oradata/TOY10G/system01.dbf
output
filename=/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/%rec_area_
33_1.bak tag=TAG20070814T092512 recid=21 stamp=630581119
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile fno=00003
name=/ora01/orabkup/GENQ/.temp_toy10g/ora01/oradata/TOY10G/sysaux01.dbf
output
filename=/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/%rec_area_
34_1.bak tag=TAG20070814T092512 recid=22 stamp=630581126
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile fno=00004
name=/ora01/orabkup/GENQ/.temp_toy10g/ora01/oradata/TOY10G/users01.dbf
output
filename=/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/%rec_area_
35_1.bak tag=TAG20070814T092512 recid=23 stamp=630581127
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile copy
input datafile fno=00005
name=/ora01/orabkup/GENQ/.temp_toy10g/ora01/oradata/TOY10G/indx01.dbf
output
filename=/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/%rec_area_
36_1.bak tag=TAG20070814T092512 recid=24 stamp=630581128
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile copy
input datafile fno=00002
name=/ora01/orabkup/GENQ/.temp_toy10g/ora01/oradata/TOY10G/undotbs01.dbf
output
filename=/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/%rec_area_
37_1.bak tag=TAG20070814T092512 recid=25 stamp=630581129
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 14-AUG-2007 09:25:30

Starting Control File and SPFILE Autobackup at 14-AUG-2007 09:25:30
piece
handle=/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G/TOY10G/autob
ackup/2007_08_14/o1_mf_s_630581130_3d3gttsp_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 14-AUG-2007 09:25:32

6. Query the V$RECOVERY_FILE_DEST to determine if the SPACE_USED column
(verify we have backups)

sys@TOY10G> select * from v$recovery_file_dest;

NAME

------------------------------------------------------------------------
----------------------------
SPACE_LIMIT SPACE_USED SPACE_RECLAIMABLE NUMBER_OF_FILES
----------- ---------- ----------------- ---------------
/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G
2147483648 44187648 37781504 7

sys@TOY10G> ho ls -lh
/ora01/orabkup/GENQ/.temp_toy10g/ora01/orarcv/TOY10G
total 2215666
-rw-r----- 1 oracle dba 500M Aug 14 09:25
%rec_area_33_1.bak
-rw-r----- 1 oracle dba 500M Aug 14 09:25
%rec_area_34_1.bak
-rw-r----- 1 oracle dba 46M Aug 14 09:25
%rec_area_35_1.bak
-rw-r----- 1 oracle dba 25M Aug 14 09:25
%rec_area_36_1.bak
-rw-r----- 1 oracle dba 10M Aug 14 09:25
%rec_area_37_1.bak
drwxr-x--- 3 oracle dba 512 Jun 12 11:04 TOY10G

7. Enable flash back feature

sys@TOY10G> select flashback_on from v$database;

FLASHBACK_ON
------------------
NO

7.1 Start the database in MOUNT mode:
7.2 Set the DB_FLASHBACK_RETENTION_TARGET parameter to the desired value
(in min).
sys@TOY10G> show parameter db_flash

NAME TYPE VALUE
------------------------------------ -----------
------------------------------
db_flashback_retention_target integer 1440

sys@TOY10G> alter system set db_flashback_retention_target=1445;

7.3. Enable the flashback capability:
sys@TOY10G> alter database flashback on;

7.4 Now the database can be opened for normal use:

sys@TOY10G> select flashback_on from v$database;

FLASHBACK_ON
------------------
YES

8. Drop table t1 in SCOTT schema

scott@TOY10G> drop table t1;

Table dropped.

9. performing a Flashback Database recovery to a SCN
9.1 Query the V$FLASHBACK_DATABASE_LOG view to retrieve the
OLDEST_FLASHBACK_SCN:
Login as SYS

sys@TOY10G> select oldest_flashback_scn, oldest_flashback_time from
v$flashback_database_log;

OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TIM
-------------------- --------------------
4235495 14-AUG-2007 09:45:02

9.2 shut down and start the database in MOUNT mode

9.3 issue the Flashback Database recovery from RMAN:
RMAN> connect target

connected to target database: TOY10G (DBID=3330944552, not open)

RMAN> flashback database to scn=4235495;

Starting flashback at 14-AUG-2007 10:19:48
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=45 devtype=DISK


starting media recovery
media recovery complete, elapsed time: 00:00:01

Finished flashback at 14-AUG-2007 10:19:52

9.4 Open the database with the RESETLOGS option, because you recovered
to a time prior to the current database:
sys@TOY10G> alter database open resetlogs;

Database altered.

10. Check table t1 now:

scott@TOY10G> select count(*) from t1;

COUNT(*)
----------
9531