Human error (e.g., executing a rogue UPDATE or DELETE without a WHERE clause, dropping an active table) accounts for a large fraction of production database emergencies. Traditional recovery requires restoring terabytes of backups, taking hours or days.
Oracle Flashback Technology leverages historical Undo Segments and Flashback Logs to rewind tables, queries, and entire databases within seconds.
Flashback Query (Time-Travel SQL)
Using the AS OF clause, any user can execute queries against historical snapshots of a table:
-- Query data as it existed exactly 45 minutes ago
SELECT * FROM account_balances
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '45' MINUTE)
WHERE account_id = 90210;
-- Query data at a specific System Change Number (SCN)
SELECT * FROM orders
AS OF SCN 14592038
WHERE order_id = 5500;
Flashback Version Query: Row Audit History
Inspect every revision made to a specific row across a time window, including transaction IDs:
SELECT
versions_startscn,
versions_endscn,
versions_operation, -- 'I' for Insert, 'U' for Update, 'D' for Delete
account_id,
balance
FROM account_balances
VERSIONS BETWEEN TIMESTAMP
SYSTIMESTAMP - INTERVAL '4' HOUR AND SYSTIMESTAMP
WHERE account_id = 90210
ORDER BY versions_startscn;
Flashback Table: In-Place Point-in-Time Recovery
If an erroneous script corrupted a table, restore it in place without affecting any other database objects:
-- Step 1: Row movement must be enabled for Flashback Table
ALTER TABLE corporate_accounts ENABLE ROW MOVEMENT;
-- Step 2: Flash back table to state before the rogue script executed
FLASHBACK TABLE corporate_accounts
TO TIMESTAMP TO_TIMESTAMP('2024-04-15 14:30:00', 'YYYY-MM-DD HH24:MI:SS');
Flashback Drop & The Recyclebin
Dropping a table in Oracle does not physically erase data blocks immediately. Oracle renames the table to a system-generated identifier (BIN$...) in the Recyclebin:
-- Inspect dropped tables currently in the recyclebin
SELECT object_name, original_name, droptime FROM user_recyclebin;
-- Restore dropped table instantly with all indexes and triggers!
FLASHBACK TABLE employees TO BEFORE DROP;
-- Restore and rename to avoid conflicts:
FLASHBACK TABLE employees TO BEFORE DROP RENAME TO employees_restored;
-- Completely bypass the recyclebin and physically purge data immediately
DROP TABLE temp_scratch_pad PURGE;
Exercises
- Write a script that uses
AS OF TIMESTAMPto recover accidentally deleted rows into an active table usingINSERT INTO ... SELECT .... - Inspect the
USER_RECYCLEBINview and restore a dropped table. - Explain the relationship between the
UNDO_RETENTIONinitialization parameter and Flashback Query availability.