Overview

Flashback features use undo and flashback logs to look at or restore past data without restoring a backup: Flashback Query (AS OF), Flashback Version Query, Flashback Table, Flashback Drop (recycle bin) and Flashback Database. They are fast ways to undo human error such as a bad UPDATE or DROP TABLE. They do not replace backups (oracle-rman-backup).

Core concepts

  • Flashback Query: SELECT ... AS OF TIMESTAMP | SCN ... reads old committed data from undo. Limited by UNDO_RETENTION (900 s here) and available undo (oracle-redo-undo); error ORA-01555 when the undo is gone.
  • Flashback Table: FLASHBACK TABLE t TO SCN n | TIMESTAMP ... rewinds a table in place; needs ALTER TABLE t ENABLE ROW MOVEMENT.
  • Flashback Drop: a dropped table goes to the recycle bin (USER_RECYCLEBIN) and returns with FLASHBACK TABLE t TO BEFORE DROP; DROP TABLE ... PURGE bypasses it.
  • Flashback Database: rewinds the whole database using flashback logs in the fast recovery area; the Flashback Database option showed TRUE in the test build.
  • Flashback Data Archive keeps history of selected tables for a long period (audit).
  • The practical reference time is an SCN; get it with DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER.

Worked example

Executed on the Free container:

CREATE TABLE acct2 (id NUMBER PRIMARY KEY, bal NUMBER);
INSERT INTO acct2 VALUES (1,100); INSERT INTO acct2 VALUES (2,200); COMMIT;
EXEC DBMS_SESSION.SLEEP(10)
SELECT DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER FROM dual;      -- 2295206
UPDATE acct2 SET bal = 0; COMMIT;                              -- the mistake
 
SELECT id, bal FROM acct2;                                     -- 1|0   2|0
SELECT id, bal FROM acct2 AS OF SCN 2295206;                   -- 1|100 2|200
ALTER TABLE acct2 ENABLE ROW MOVEMENT;
FLASHBACK TABLE acct2 TO SCN 2295206;                          -- bal restored

Recycle bin:

DROP TABLE acct;
SELECT original_name, type FROM user_recyclebin;     -- ACCT  TABLE
FLASHBACK TABLE acct TO BEFORE DROP;                 -- 2 rows back

A gotcha seen in testing: my first attempts at AS OF SCN failed with ORA-01466: unable to read data - table definition has changed even though only DML had followed. The SCN I had read from V$DATABASE.CURRENT_SCN was taken right after CREATE TABLE; reading DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER after a short pause worked. In general, flashback cannot go back past a DDL change of the table, so allow a clear gap after DDL.

Pitfalls and production notes

  • Flashback Query depends on undo retention; ALTER TABLE ... FLASHBACK ARCHIVE or longer retention are options for longer histories.
  • Flashback Table does not work across DDL changes and takes locks while it runs.
  • Recycle bin objects consume space until purged; PURGE RECYCLEBIN or space pressure removes them.
  • Always grant flashback privileges deliberately (FLASHBACK ANY TABLE is powerful).
  • Do not treat it as an audit trail; use unified auditing (oracle-security).
  • Equivalents: PostgreSQL has no built-in table flashback (use PITR, pg-backup-pitr); SQL Server offers temporal tables and PITR (sqlserver-backup-recovery).

Practice questions

  1. Why does Flashback Query fail after a day on a busy system?
  2. What privileges and settings does FLASHBACK TABLE need?
  3. How is Flashback Database different from Flashback Table?

References

  • Oracle Database Development Guide, Using Oracle Flashback Technology; Backup and Recovery User’s Guide

oracle-redo-undo · oracle-rman-backup · oracle-security