You are on page 1of 24

Using Flashback Technology II

Copyright © 2009, Oracle. All rights reserved.


> Total Recall
Flashback Drop
Objectives

After completing this lesson, you should be able to:


• Describe and use Oracle Total Recall
– Creating and enabling a Flashback Data Archive (FDA)
– Managing FDAs
– Viewing metadata
• Describe and use flashback recycle bins
– Restore dropped tables from the recycle bin
– Manage space usage in the recycle bin
– Query the recycle bin

11 - 2 Copyright © 2009, Oracle. All rights reserved.


Oracle Total Recall Overview

Automated tracking of historical database changes:


• Enable at the table level with your specified retention
period.
• All subsequent changes are transparently stored and
tamper proof.
• Records older than retention period are automatically
removed.
• Use Flashback technologies to retrieve history.
SELECT … AS OF TIMESTAMP…
SELECT … VERSIONS BETWEEN TIMESTAMP and TIMESTAMP…

Use Cases for Flashback request:


updates Base history Flashback queries Audit, compliance, historical
Table Data reports, ILM
Archive

11 - 3 Copyright © 2009, Oracle. All rights reserved.


Setup Process

1. Create a new tablespace to hold the FDA.


2. With the FLASHBACK ARCHIVE ADMINISTER system
privilege: Create a Flashback Data Archive, assign it to the
tablespace, and specify its retention period.
CREATE FLASHBACK ARCHIVE fda1
TABLESPACE fda_tbs1 QUOTA 10M RETENTION 1 YEAR;

3. With the FLASHBACK ARCHIVE object privilege: Alter the


base tables to enable archiving and assign it to a flashback
archive.
ALTER TABLE HR.EMPLOYEES FLASHBACK ARCHIVE fda1;

Flashback
Data
Base table Archive

11 - 4 Copyright © 2009, Oracle. All rights reserved.


How Total Recall Works

History data:
• Row captured asynchronously by background
processes at self-tuned intervals (default: 5 min)
• Stored compressed and partitioned
DML

• Automatically purged per


Original data in retention policy
buffer cache undo Undo
data

fbda slaves (as needed)


DML

fbda background
help ...
process
history data
• Partitions automatically created
based on time and volume
• Unrelated partitions skipped by
queries
EMPLOYEES FDA1

11 - 5 Copyright © 2009, Oracle. All rights reserved.


Oracle Total Recall Scenario

Using Flashback Data Archive to access historical data:

-- create the Flashback Data Archive


CREATE FLASHBACK ARCHIVE DEFAULT fla1 1
TABLESPACE tbs1 QUOTA 10G RETENTION 5 YEAR;

-- Specify the default Flashback Data Archive


ALTER FLASHBACK ARCHIVE fla1 SET DEFAULT; 2

-- Enable Flashback Data Archive


ALTER TABLE inventory FLASHBACK ARCHIVE; 3
ALTER TABLE stock_data FLASHBACK ARCHIVE;

SELECT product_number, product_name, count FROM inventory AS OF


TIMESTAMP TO_TIMESTAMP ('2007-01-01 00:00:00', 'YYYY-MM-DD
HH24:MI:SS');

11 - 6 Copyright © 2009, Oracle. All rights reserved.


Oracle Total Recall Scenario

Optionally, adding space:


ALTER FLASHBACK ARCHIVE fla1
ADD TABLESPACE tbs3 QUOTA 5G;
4

Optionally, changing retention time:


ALTER FLASHBACK ARCHIVE fla1 MODIFY RETENTION 2 YEAR; 5
Optionally, purging data:
ALTER FLASHBACK ARCHIVE fla1 PURGE BEFORE TIMESTAMP
6
(SYSTIMESTAMP - INTERVAL '1' day);

Optionally, dropping a Flashback Data Archive:


DROP FLASHBACK ARCHIVE fla1; 7

11 - 7 Copyright © 2009, Oracle. All rights reserved.


Transparent Schema Evolution

• DDL support for:


– Add, drop, rename, and modify column
– Drop and truncate partition
– Rename and truncate table
Flashback Version Query
1 2 3
1
Add column
Drop Column

time

• Flashback queries work across DDL changes.


• All other DDL is not automatically supported (see next
slide).

11 - 8 Copyright © 2009, Oracle. All rights reserved.


Full Schema Evolution

Disassociate or associate procedures in the


DBMS_FLASHBACK_ARCHIVE package:
• Disable Total Recall on specified tables and allow more
complex DDL (upgrades, split tables, and so on).
• Enforce schema integrity during association. (Base table
and history table must be the same schema.)
Note: This function should be used with care and with the
understanding that the archive can no longer be guaranteed to
be immutable because the history could have been altered
during the time of disassociation.
1 2
Base
disassociate Alter schema Table
History
Table associate Alter schema History
Table
4 3

11 - 9 Copyright © 2009, Oracle. All rights reserved.


Restrictions

• You cannot enable Total Recall for base tables with Hybrid
Columnar compression.
• If disassociate is used, immutability of history is no longer
guaranteed (but you could always purge history previously
anyway with the right privilege).
• There is no transportability of history tables.

11 - 10 Copyright © 2009, Oracle. All rights reserved.


Guidelines

• Use SCN for precise queries.


or
• Use Flashback technology for your convenience.
• Flashback uses current system settings.
• Ensure database consistency with a COMMIT or ROLLBACK
operation before querying past data.
• You cannot retrieve past data from a dynamic performance
(V$) view. They contain current data.
• However, you can perform queries on past data in static
data dictionary views, such as *_TABLES.

11 - 11 Copyright © 2009, Oracle. All rights reserved.


Viewing Flashback Data Archives

Viewing the results:


View Name (DBA/USER) Description

*_FLASHBACK_ARCHIVE Displays information about Flashback


Data Archives

*_FLASHBACK_ARCHIVE_TS Displays tablespaces of Flashback Data


Archives

*_FLASHBACK_ARCHIVE_TABLES Displays information about tables that are


enabled for flashback archiving

11 - 12 Copyright © 2009, Oracle. All rights reserved.


Quiz

You cannot drop, but you can truncate, a table that is tracked
by Oracle Total Recall.
1. True
2. False

11 - 13 Copyright © 2009, Oracle. All rights reserved.


Quiz

Select all correct statements about Oracle Total Recall:


1. Oracle Total Recall is enabled by default.
2. A Flashback Data Archive provides the ability to track and
store all transactional changes to a “tracked” table over its
lifetime.
3. Dropping a column in a table enabled for Flashback Data
Archive causes an error.
4. Flashback processing always uses the settings that were
in effect at the time of being queried.
5. Flashback uses the current session settings, such as
national language and character set.

11 - 14 Copyright © 2009, Oracle. All rights reserved.


Total Recall
Flashback Drop > Flashback Drop

and the Recycle Bin


RECYCLEBIN=ON

DROP TABLE employees; FLASHBACK TABLE


employees
TO BEFORE DROP;
By mistake

11 - 15 Copyright © 2009, Oracle. All rights reserved.


Recycle Bin

BIN$zbjrBdpw==$0 EMPLOYEES
BIN$zbjra9wy==$0 EMPLOYEES_PK
Recycle
bin
4

DBA_FREE_SPACE

EMPLOYEES BIN$zbjrBdpw==$0
3
EMPLOYEES_PK BIN$zbjra9wy==$0
Objects are:
– Renamed
– Not moved
1
2 DROP TABLE employees;

11 - 16 Copyright © 2009, Oracle. All rights reserved.


Restoring Tables from the Recycle Bin

• Restore dropped tables and dependent objects.


• If multiple recycle bin entries have the same original name:
– Use unique, system-generated names to restore a particular
version
– When using original names, the restored table is last in, first
out (LIFO)
• Rename the original name if that name is currently used.

FLASHBACK TABLE <table_name> TO BEFORE DROP


[RENAME TO <new_name>];

11 - 18 Copyright © 2009, Oracle. All rights reserved.


Recycle Bin: Automatic Space Reclamation

Recycle bin

BIN$zbjrBdpw==$0
BIN$zbjra9wy==$0 BIN$zbjrBdpw==$0

BIN$zbjra9wy==$0

DBA_FREE_SPACE - RECYCLEBIN 1

Autoextend 3

11 - 19 Copyright © 2009, Oracle. All rights reserved.


Recycle Bin: Manual Space Reclamation

PURGE {TABLE <table_name>|INDEX <index_name>}

PURGE TABLESPACE <ts_name> [USER <user_name>]

PURGE [USER_|DBA_]RECYCLEBIN

11 - 20 Copyright © 2009, Oracle. All rights reserved.


Bypassing the Recycle Bin

DROP TABLE <table_name> [PURGE] ;

DROP TABLESPACE <ts_name>


[INCLUDING CONTENTS] ;

DROP USER <user_name> [CASCADE] ;

Security considerations for the recycle bin:


ALTER SYSTEM SET RECYCLEBIN=OFF SCOPE=SPFILE;

11 - 21 Copyright © 2009, Oracle. All rights reserved.


Querying the Recycle Bin

SELECT owner, original_name, object_name,


type, ts_name, droptime, related, space
FROM dba_recyclebin
WHERE can_undrop = 'YES';

SQL> SELECT original_name, object_name, ts_name, droptime


FROM user_recyclebin WHERE can_undrop = 'YES';

ORIGINAL_NAME OBJECT_NAME TS_NAM DROPTIME


------------- ----------------------- ------ -------------------
EMPLOYEES2 BIN$NE4Rk64w...gbpQ==$0 USERS 2007-07-02:15:45:13

SQL> SHOW RECYCLEBIN

11 - 22 Copyright © 2009, Oracle. All rights reserved.


Quiz

When you flash back a dropped table, the recovered indexes,


triggers, and constraints keep their recycle bin names.
1. True
2. False

11 - 23 Copyright © 2009, Oracle. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Set up and use Total Recall
• Restore dropped tables from the recycle bin
• Query the recycle bin

11 - 24 Copyright © 2009, Oracle. All rights reserved.


Practice 11 Overview:
Using Flashback Technology
This practice covers the following topics:
• Using Total Recall
• Recycle bin activities (optional)

11 - 25 Copyright © 2009, Oracle. All rights reserved.

You might also like