de haan - flash me back, scotty
DESCRIPTION
Lex de Haan Lex de Haan Applied Maths, TU Delft Oracle employee 1990-2004 ISO SQL Standardization March 2004: Natural Join B.V. Flash Me Back, Scotty - 1 ©2005, Natural Join B.V. – Teacher, delivery manager, product manager seminars, curriculum developer/manager, ST development ([email protected]) [email protected] http://www.naturaljoin.nl 2TRANSCRIPT
©2005, Natural Join B.V. Flash Me Back, Scotty - 1
HotsosSymposiumDallas, March 2005
Oracle Database 10g:Flash Me Back, Scotty …
Lex de Haan([email protected])
2
Who Am I
Lex de [email protected] Maths, TU DelftOracle employee 1990-2004
– Teacher, delivery manager,product manager seminars,curriculum developer/manager,ST development
ISO SQL StandardizationMarch 2004: Natural Join B.V.http://www.naturaljoin.nl
©2005, Natural Join B.V. Flash Me Back, Scotty - 2
3
Topics
1. Flashback (Versions) Query– Flashback pseudocolumns– Guaranteed undo retention– SCN <=> TIMESTAMP conversion functions
2. Flashback transaction query3. Flashback table
4. Flashback drop– The recyclebin– Purging the recyclebin
5. Flashback database
4
Flashback (Versions) Query
Retrieve row versions back in time from undo segments
Purposes:– Table
auditing– Transaction
miningLevels:
– Statementlevel
– Sessionlevel
select ... from ...VERSIONS BETWEEN{SCN {n|MINVALUE} AND {m|MAXVALUE}|TIMESTAMP t1 AND t2 }where ...
select ... from ...AS OF {SCN n|TIMESTAMP t}where ...
dbms_flashback.ENABLE_AT_TIME(t);...dbms_flashback.DISABLE;
1.
©2005, Natural Join B.V. Flash Me Back, Scotty - 3
5
Flashback Versions Pseudocolumns
Operation that produced the versionVERSIONS_OPERATION
Transaction that created the version(useful for further investigations)VERSIONS_XID
Version validity range: Upper boundVERSIONS_ENDTIME
VERSIONS_ENDSCN
Version validity range: Lower boundVERSIONS_STARTTIME
VERSIONS_STARTSCN
DescriptionPseudocolumn
6
Guaranteed Undo Retention
SQL> alter tablespace undotbs12 RETENTION [NO]GUARANTEE;
SQL> select tablespace_name, RETENTION2 from dba_tablespaces;
TABLESPACE_NAME RETENTION--------------- ---------UNDOTBS1 GUARANTEE... ...
©2005, Natural Join B.V. Flash Me Back, Scotty - 4
7
New SQL Conversion Functions
Precision: about 3 secondsRetention: minimum 5 days (depends on undo retention)
SQL> select current_scn,2 SCN_TO_TIMESTAMP(current_scn)3 from v$database;
CURRENT_SCN SCN_TO_TIMESTAMP(CURRENT_SCN)----------- -------------------------------
4479369 14-DEC-04 10.27.31.000000000 PM
TIMESTAMP -> NUMBERTIMESTAMP_TO_SCN
NUMBER -> TIMESTAMPSCN_TO_TIMESTAMP
8
Flashback Transaction Query
View database changes at the transaction levelPerform transaction auditingRecover from user or application errorsDerived from undo segments; faster than LogMinerProtected by a system privilege
SQL> select operation, undo_sql, table_name2 from FLASHBACK_TRANSACTION_QUERY3 where xid = hextoraw('...');
SQL> select operation, undo_sql, table_name2 from FLASHBACK_TRANSACTION_QUERY3 where start_timestamp >= '...'4 and commit_timestamp <= '...';
2.
©2005, Natural Join B.V. Flash Me Back, Scotty - 5
9
Combining Flashback Versionsand Flashback Transaction
SQL> select operation, undo_sql
2 , table_name
3 from FLASHBACK_TRANSACTION_QUERY
4 where xid = hextoraw('xxxxxxxxxxxxxxxx');
SQL> select versions_xid
2 , salary
3 from hr.employees
4 versions between scn
5 minvalue and maxvalue
6 where last_name = 'De Haan';
10
Flashback Table
Recover tables to a specific point in time: fast, easy, in-placeExecuted as a single-statement (autocommit) transactionExclusive DML table locks neededTriggers temporarily disabled by default; indexes maintainedRecorded in the alert log fileTip: Record the current SCN first (from v$database)
SQL> FLASHBACK TABLE hr.employees [, ...]2 TO {SCN n|TIMESTAMP t}3 [{ENABLE|DISABLE} TRIGGERS]
SQL> alter table hr.employees2 ENABLE ROW MOVEMENT;
3.
©2005, Natural Join B.V. Flash Me Back, Scotty - 6
11
Flashback Table: Error Messages
SQL> flashback table hr.employees to scn ...;flashback table hr.employees
*ERROR at line 1:ORA-02091: transaction rolled backORA-02291: integrity constraint(HR.EMP_JOB_FK)
violated - parent key not found
SQL> flashback table hr.jobs to scn ...;flashback table hr.jobs
*ERROR at line 1:ORA-02091: transaction rolled backORA-02292: integrity constraint(HR.EMP_JOB_FK)
violated - child record found
12
Flashback Table: Error Messages
SQL> flashback table hr.jobs to scn ...;flashback table hr.jobs
*ERROR at line 1:ORA-01466: unable to read data -
table definition has changed
SQL> flashback table hr.employees to scn ...;flashback table hr.employees
*ERROR at line 1:ORA-08183: Flashback cannot be enabled in the
middle of a transaction
©2005, Natural Join B.V. Flash Me Back, Scotty - 7
13
Flashback Drop Feature
Tables (and any associated objects) are renamedand marked as dropped; space is not releasedFlashback drop does not depend on undoSeveral implicit purge policies(quota, space allocation algorithm)Recycle bin can be disabled with a hidden parameter
drop table employees;
FLASHBACK table employeesTO BEFORE DROP[RENAME TO ...];
4.
14
Flashback Drop Feature
Drop objects without storing them in the recycle bin:
Query the recycle bin:
SQL> drop table <table_name> PURGE;
SQL> select * from [USER_|DBA_]RECYCLEBIN;
SQL> show recyclebin [original_name]
©2005, Natural Join B.V. Flash Me Back, Scotty - 8
15
Querying the Recycle Bin
You can query tables in the recycle bin using object namesTables in the recycle bin are read only
– Flashback queries are possible– Queries may fail with “ORA-1410: invalid ROWID”
SQL> select ...2 from "BIN$VoMP0begR7qB4RxzSz5CzQ==$0"3 [AS OF ...]4 where ...
16
PURGE Command
SQL> purge {TABLE <table_name>|INDEX <index_name>};
SQL> purge TABLESPACE <ts_name>[USER <user_name>];
SQL> purge [USER_|DBA_]RECYCLEBIN;
©2005, Natural Join B.V. Flash Me Back, Scotty - 9
17
SQL Commands and Implicit Purging
The recycle bin is purged:– If you drop a tablespace or a user– If you reach your tablespace quota– If a tablespace needs more space (purge before extend)
SQL> drop TABLESPACE <ts_name>[including contents];
SQL> drop USER <user_name> [cascade]
18
Flashback Database
Flashback database is much faster than point-in-timerecovery
– “Database rewind” functionalityUseful in cases of logical data corruptionUseful in Data Guard configurations
– Real time apply: enable flashback on standby db– Avoid recreating physical standby db after open resetlogs on
primary– Convert old primary db into a new standby db after a failover
For flashback database:– The database must be in archivelog mode– The database must be mounted exclusive
5.
©2005, Natural Join B.V. Flash Me Back, Scotty - 10
19
Flashback Database
You cannot use flashback database in the following situations:– The control file has been restored or re-created– A tablespace has been dropped– A data file has been shrunk– You recovered through a RESETLOGS operation
After flashback database, you can open the database:– In read-only mode, to verify that the desired situation is reached– With a RESETLOGS operation, to allow for updates
20
Instance DatabaseFlash recovery area
Architectural Components
Flashback logs
Redo log files
LGWR
RVWRFlashbackbuffer
Redo logbuffer
©2005, Natural Join B.V. Flash Me Back, Scotty - 11
21
Defining a Flash Recovery Area
You define the flash recovery area by setting parameters:– DB_RECOVERY_FILE_DEST_SIZE (size, in bytes) – DB_RECOVERY_FILE_DEST (the location)– DB_FLASHBACK_RETENTION_TARGET (in mins; default 1 day)
Centralized area for recovery purposes
SQL> alter system set2 DB_RECOVERY_FILE_DEST_SIZE=64G scope=both;
SQL> alter system set2 DB_RECOVERY_FILE_DEST='/oradata/fr_area' scope=both;
SQL> select name, space_limit, space_used2 , space_reclaimable, number_of_files2 from V$RECOVERY_FILE_DEST;
22
Configuring Flashback Database
1. Configureflash recovery area
3. Enableflashback database
2. Setretentiontarget
SQL> alter database FLASHBACK {ON|OFF};
SQL> select flashback_on, current_scn2 from v$database;
©2005, Natural Join B.V. Flash Me Back, Scotty - 12
23
Monitoring Flashback Database
Monitor the flashback database retention target;adjust flash recovery area size when needed
Monitor the overhead of logging flashback data(view contains 24 hours of information, one row per hour)
SQL> select oldest_flashback_scn2 , retention_target3 from V$FLASHBACK_DATABASE_LOG;
SQL> select begin_time, end_time2 , flashback_data, redo_data3 from V$FLASHBACK_DATABASE_STAT;
24
Flashback Database: Examples
RMAN> flashback database 2> to TIME = to_date3> ('2004-08-11 16:15:14',4> 'YYYY-MM-DD HH24:MI:SS');
RMAN> flashback database to SCN=87654;
RMAN> flashback database 2> to SEQUENCE=42 THREAD=1;
SQL> flashback database 2 to TIMESTAMP (systimestamp-2/24);
SQL> flashback database to scn 76543;
©2005, Natural Join B.V. Flash Me Back, Scotty - 13
25
Errors During Flashback Database
SQL> flashback database to scn ...;ERROR at line 1: ORA-38729: Not enough flashback databaselog data to do FLASHBACK.
SQL> flashback database to scn ...;ERROR at line 1: ORA-38753: Cannot flashback data file 4;no flashback log data. ORA-01110: data file 4: '/oradata/10g/users01.dbf' ORA-38753: Cannot flashback data file 5;no flashback log data. ORA-01110: data file 5:'/oradata/10g/example01.dbf'
26
Excluding Tablespacesfrom Flashback Database
If you disable flashback for a tablespace:– You must take it offline before you can perform a flashback
database– After flashback database: drop it, or recover the offline data files
with traditional point-in-time recovery
SQL> alter tablespace <ts> FLASHBACK {ON|OFF}
SQL> select tablespace_name, FLASHBACK_ON2 from v$tablespace;
©2005, Natural Join B.V. Flash Me Back, Scotty - 14
27
Granting Flashback Privileges
To use flashback features, you need the following privileges:
For flashback database:– SYSDBA database connection
For flashback table or flashback versions query:– FLASHBACK ANY TABLE
– Appropriate object privilegesFor flashback transaction query:
– SELECT ANY TRANSACTION
28
By the Way …
Every Oracle table has a new pseudocolumn in 10g:
For maximum precision, for the price of six bytes per row:
SQL> select empno, ename, ORA_ROWSCN2 from scott.emp;
SQL> create table my_emp ROWDEPENDENCIES2 as select * from scott.emp;
SQL> select empno, ename2 , SCN_TO_TIMESTAMP(ORA_ROWSCN)3 from scott.emp;
©2005, Natural Join B.V. Flash Me Back, Scotty - 15
29
That’s All ...