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 byUNDO_RETENTION(900 s here) and available undo (oracle-redo-undo); errorORA-01555when the undo is gone. - Flashback Table:
FLASHBACK TABLE t TO SCN n | TIMESTAMP ...rewinds a table in place; needsALTER TABLE t ENABLE ROW MOVEMENT. - Flashback Drop: a dropped table goes to the recycle bin (
USER_RECYCLEBIN) and returns withFLASHBACK TABLE t TO BEFORE DROP;DROP TABLE ... PURGEbypasses it. - Flashback Database: rewinds the whole database using flashback logs in the fast recovery area; the
Flashback Databaseoption showedTRUEin 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 restoredRecycle bin:
DROP TABLE acct;
SELECT original_name, type FROM user_recyclebin; -- ACCT TABLE
FLASHBACK TABLE acct TO BEFORE DROP; -- 2 rows backA 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 ARCHIVEor 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 RECYCLEBINor space pressure removes them. - Always grant flashback privileges deliberately (
FLASHBACK ANY TABLEis 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
- Why does Flashback Query fail after a day on a busy system?
- What privileges and settings does
FLASHBACK TABLEneed? - How is Flashback Database different from Flashback Table?
References
- Oracle Database Development Guide, Using Oracle Flashback Technology; Backup and Recovery User’s Guide