Flashback Query
Flashback Query
Flashback Query
CURRENT_SCN TO_CHAR(SYSTIMESTAM
----------- -------------------
722452 2004-03-29 13:34:12
COUNT(*)
----------
1
SELECT COUNT(*) FROM flashback_query_test AS OF TIMESTAMP
TO_TIMESTAMP('2004-03-29 13:34:12', 'YYYY-MM-DD
HH24:MI:SS');
COUNT(*)
----------
0
COUNT(*)
----------
0
CURRENT_SCN TO_CHAR(SYSTIMESTAM
----------- -------------------
725202 2004-03-29 14:59:08
CURRENT_SCN TO_CHAR(SYSTIMESTAM
----------- -------------------
725219 2004-03-29 14:59:36
VERSIONS_STARTSCN VERSIONS_STARTTIME
VERSIONS_ENDSCN VERSIONS_ENDTIME VERSIONS_XID
V DESCRIPTION
------------------ ------------------------ ------------------ ------------------------
---------------- - -----------
725212 29-MAR-04 02.59.16 PM
02001C0043030000 U THREE
725209 29-MAR-04 02.59.16 PM 725212 29-MAR-04
02.59.16 PM 0600030021000000 U TWO
725209 29-MAR-04 02.59.16 PM
ONE
SELECT versions_startscn, versions_starttime,
versions_endscn, versions_endtime,
versions_xid, versions_operation,
description
FROM flashback_version_query_test
VERSIONS BETWEEN SCN 725202 AND 725219
WHERE id = 1;
VERSIONS_STARTSCN VERSIONS_STARTTIME
VERSIONS_ENDSCN VERSIONS_ENDTIME VERSIONS_XID
V DESCRIPTION
------------------ ------------------------ ------------------ ------------------------
---------------- - -----------
725212 29-MAR-04 02.59.16 PM
02001C0043030000 U THREE
725209 29-MAR-04 02.59.16 PM 725212 29-MAR-04
02.59.16 PM 0600030021000000 U TWO
725209 29-MAR-04 02.59.16 PM
ONE
The available pseudocolumn meanings are:
VERSIONS_STARTSCN or VERSIONS_STARTTIME - Starting SCN
and TIMESTAMP when row took on this value. The value of NULL is
returned if the row was created before the lower bound SCN ot
TIMESTAMP.
VERSIONS_ENDSCN or VERSIONS_ENDTIME - Ending SCN and
TIMESTAMP when row last contained this value. The value of NULL is
returned if the value of the row is still current at the upper bound SCN ot
TIMESTAMP.
VERSIONS_XID - ID of the transaction that created the row in it's
current state.
VERSIONS_OPERATION - Operation performed by the transaction
((I)nsert, (U)pdate or (D)elete)
At this point, the DBA detects the application error and needs to diagnose
the problem. The DBA issues the following query to retrieve versions of
the rows in the emp table that correspond to empno 111. The query uses
Flashback Version Query pseudocolumns.
connect dba_name/password
SELECT versions_xid XID, versions_startscn START_SCN,
versions_endscn END_SCN, versions_operation OPERATION,
empname, salary FROM hr.emp
VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE
where empno = 111;
The results table reads chronologically, from bottom to top. The third row
corresponds to the version of the row in emp that was originally inserted
in the table when the table was created. The second row corresponds to
the row in emp that was deleted by the erroneous transaction. The first
row corresponds to the version of the row in emp that was reinserted with
a new employee name.
The DBA identifies transaction 000200030000002D as the erroneous
transaction and issues the following Flashback Transaction Query to audit
all changes made by this transaction:
SELECT xid, start_scn START, commit_scn COMMIT,
operation OP, logon_user USER,
undo_sql FROM flashback_transaction_query
WHERE xid = HEXTORAW('000200030000002D');
4 rows selected
The rightmost column (undo_sql) contains the SQL code that will undo
the corresponding change operation. The DBA can execute this code to
undo the changes made by that transaction. TheUSER column
(logon_user) shows the user responsible for the transaction.
A DBA might also be interested in knowing all changes made in a certain
time window. In our scenario, the DBA performs the following query to
view the details of all transactions that executed since the erroneous
transaction identified earlier (including the erroneous transaction itself):
SELECT xid, start_scn, commit_scn, operation, table_name, table_owner
FROM flashback_transaction_query
WHERE table_owner = 'HR' AND
start_timestamp >=
TO_TIMESTAMP ('2002-04-16 11:00:00','YYYY-MM-DD
HH:MI:SS');
6 rows selected
Flashback Table
CURRENT_SCN
-----------
715315
CURRENT_SCN
-----------
715340
COUNT(*)
----------
0
COUNT(*)
----------
1
Flashback of tables can also be performed using timestamps.
FLASHBACK TABLE flashback_table_test TO TIMESTAMP
TO_TIMESTAMP('2004-03-03 10:00:00', 'YYYY-MM-DD HH:MI:SS');
SHOW RECYCLEBIN
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE
DROP TIME
---------------- ------------------------------ ------------ -------------------
FLASHBACK_DROP_T BIN$TstgCMiwQA66fl5FFDTBgA==$0
TABLE 2004-03-29:11:09:07
EST
ID
----------
1
Tables in the recycle bin can be queried like any other table.
DROP TABLE flashback_drop_test;
SHOW RECYCLEBIN
ID
----------
1
If an object is dropped and recreated multiple times all dropped versions
will be kept in the recycle bin, subject to space. Where multiple versions
are present it's best to reference the tables via
the RECYCLEBIN_NAME. For any references to
the ORIGINAL_NAME it is assumed the most recent object is drop
version in the referenced question. During the flashback operation the
table can be renamed.
FLASHBACK TABLE flashback_drop_test TO BEFORE DROP
RENAME TO flashback_drop_test_old;
Several purge options exist.
PURGE TABLE tablename; -- Specific table.
PURGE INDEX indexname; -- Specific index.
PURGE TABLESPACE ts_name; -- All tables in a specific
tablespace.
PURGE TABLESPACE ts_name USER username; -- All tables in a
specific tablespace for a specific user.
PURGE RECYCLEBIN; -- The current users entire recycle
bin.
PURGE DBA_RECYCLEBIN; -- The whole recycle bin.
Several restrictions apply relating to the recycle bin.
Only available for non-system, locally managed tablespaces.
There is no fixed size for the recycle bin. The time an object remains in
the recycle bin can vary.
The objects in the recycle bin are restricted to query operations only (no
DDL or DML).
Flashback query operations must reference the recycle bin name.
Tables and all dependent objects are placed into, recovered and purged
from the recycle bin at the same time.
Tables with Fine Grained Access policies aer not protected by the recycle
bin.
Partitioned index-organized tables are not protected by the recycle bin.
The recycle bin does not preserve referential integrity.
This feature can be disabled/enabled at the session or system level.
-- Session level.
ALTER SESSION SET recyclebin = OFF;
ALTER SESSION SET recyclebin = ON;
-- System level.
ALTER SYSTEM SET recyclebin = OFF;
ALTER SYSTEM SET recyclebin = ON;
Viewing and Querying Objects in the Recycle Bin
You can view the contents of the recycle bin using the SQL*Plus
command SHOW RECYCLEBIN.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT
TYPE DROP TIME
---------------- --------------------------------- ------------ -------------------
EMPLOYEE_DEMO BIN$gk3lsj/3akk5hg3j2lkl5j3d==$0 TABLE
2003-06-11:17:08:54
-- Flashback 5 minutes.
CONN sys/password AS SYSDBA
SHUTDOWN IMMEDIATE
STARTUP MOUNT EXCLUSIVE
FLASHBACK DATABASE TO TIMESTAMP SYSDATE-(1/24/12);
ALTER DATABASE OPEN RESETLOGS;
SELECT *
FROM emp AS OF TIMESTAMP SCN_TO_TIMESTAMP(993240);
DECLARE
l_scn NUMBER;
l_timestamp TIMESTAMP;
BEGIN
l_scn := TIMESTAMP_TO_SCN(SYSTIMESTAMP - 1/24);
l_timestamp := SCN_TO_TIMESTAMP(l_scn);
END;
/