Oracle DBMS_FLASHBACK Enable At Time: Set Your Session Back in Time
Oracle DBMS_FLASHBACK Enable At Time: Session Flashback Guide
Oracle's DBMS_FLASHBACK.ENABLE_AT_TIME is a powerful procedure that lets you travel back in time within your database session β letting you query data exactly as it existed at a specific past moment, without needing a full database restore.
What This Code Does
This one-line call puts your entire Oracle session into "flashback mode," pointed at a specific past date and time. All SELECT queries you run after this call will return data as it was at that moment β not the current data. This is ideal for investigating accidental deletes, data corruption, or auditing historical states.
The Code
1exec dbms_flashback.enable_at_time(to_date('2006-AUG-24 12:00:00', 'YYYY-MON-DD HH24:MI:SS'));
Breakdown of the Code
Full Statement
1exec dbms_flashback.enable_at_time(
2 to_date('2006-AUG-24 12:00:00', 'YYYY-MON-DD HH24:MI:SS')
3);
Part-by-Part Explanation
| Part | What It Means |
|---|---|
exec | SQL*Plus shortcut for EXECUTE β runs a single PL/SQL statement |
dbms_flashback | Built-in Oracle package for session-level flashback operations |
.enable_at_time(...) | Procedure that turns on flashback mode for the whole session, anchored to a specific time |
to_date(...) | Converts a text string into an Oracle DATE value |
'2006-AUG-24 12:00:00' | The target past date and time you want to flash back to |
'YYYY-MON-DD HH24:MI:SS' | The format mask that tells Oracle how to read the date string |
Once this runs, Oracle internally maps the time you gave to the closest System Change Number (SCN) in the undo tablespace. Every query in your session then reads data consistent with that SCN.
Prerequisites
Before running this code, two conditions must be true.
1. undo_management Must Be AUTO
Automatic Undo Management (AUM) must be enabled. This means Oracle manages undo (rollback) data automatically in a dedicated undo tablespace. You can verify this with:
1show parameter undo_management;
The value must return AUTO. If it returns MANUAL, flashback queries using DBMS_FLASHBACK will not work.
2. Check undo_retention First
The undo_retention parameter (in seconds) tells Oracle the minimum time to keep committed undo data before it can be overwritten. Run this to check your setting:
1show parameter undo_retention;
The default is 900 seconds (15 minutes). If you want to flash back hours or days, this value must be large enough β or you will get a "Snapshot Too Old" error. To retain undo for 24 hours, set it to 86400:
1alter system set undo_retention = 86400;
**Note:** If your undo tablespace uses
AUTOEXTEND, Oracle automatically tunes undo retention to just exceed the longest-running query. This may still be too short for flashback. You may need to setundo_retentionmanually or use a fixed-size undo tablespace.
Key Restriction: Cannot Run as SYS
You cannot run DBMS_FLASHBACK procedures as the SYS user. Oracle will return:
1ORA-08185: Flashback not supported for user SYS
Use SYSTEM or any other DBA-privileged user instead. The SYS user can still use AS OF clause flashback queries, but not the DBMS_FLASHBACK package.
How to Use It: A Full Working Example
1-- Step 1: Connect as SYSTEM (not SYS)
2-- Step 2: Enable flashback to a past time
3exec dbms_flashback.enable_at_time(
4 to_date('2006-AUG-24 12:00:00', 'YYYY-MON-DD HH24:MI:SS')
5);
6
7-- Step 3: Run your historical queries
8SELECT * FROM my_table WHERE id = 1001;
9
10-- Step 4: Always disable flashback when done
11exec dbms_flashback.disable;
Always call DBMS_FLASHBACK.DISABLE after your session work is complete. While in flashback mode, you cannot perform DML (INSERT, UPDATE, DELETE).
Key Insights
- SCN mapping: Oracle does not store data by clock time. Internally, it maps your
to_datevalue to the nearest SCN. If undo for that SCN no longer exists, you get a "Snapshot Too Old" error. - Session scope only: Flashback mode applies only to your current session. Other users and sessions see current data normally.
- Compare past vs. present: A powerful pattern is to enable flashback, store past results in a temp table, disable flashback, then join the temp table with current data to see what changed.
- No DML in flashback mode: While
DBMS_FLASHBACKis enabled, your session is read-only. You must callDBMS_FLASHBACK.DISABLEbefore writing data. - Automatic undo tuning risk: In databases with
AUTOEXTENDundo tablespaces, Oracle tunes retention for active queries, not for flashback. For reliable flashback over hours or days, explicitly setundo_retention. - Alternative β AS OF clause: For simpler one-off queries, you can use
SELECT \* FROM table AS OF TIMESTAMP ...without needing theDBMS_FLASHBACKpackage, and this even works for theSYSuser.
Quick Reference: DBMS_FLASHBACK Procedures
| Procedure | Purpose |
|---|---|
ENABLE_AT_TIME(timestamp) | Flash session back to a specific wall-clock time |
ENABLE_AT_SYSTEM_CHANGE_NUMBER(scn) | Flash session back to a specific SCN |
GET_SYSTEM_CHANGE_NUMBER | Returns the current SCN β useful for saving a snapshot point |
DISABLE | Ends flashback mode and returns session to current data |
References
- Oracle 18c DBMS_FLASHBACK Package Documentation β Official Oracle reference for all
DBMS_FLASHBACKprocedures includingENABLE_AT_TIMEsyntax and parameters - Using Oracle Flashback Technology (Oracle 23ai) β Oracle developer guide covering how to use
DBMS_FLASHBACKin PL/SQL, including enabling, querying, disabling, and comparing past vs. present data - How to Use DBMS_FLASHBACK β Alex Lima Blog β Practical walkthrough with examples showing the SYS restriction, SCN variables, and real flashback session usage
- Flashback Query β ORACLE-BASE β Tim Hall's concise guide to enabling and disabling flashback queries using
DBMS_FLASHBACK - Automatic Undo Management β ORACLE-BASE β Explains
undo_management=AUTO, undo tablespaces, and how AUM supports flashback query - Oracle 19c Automatic Tuning of Undo Retention β Example β Practical examples of checking
undo_retention, tablespace AUTOEXTEND behavior, and its effect on flashback reliability - Automatically Managing Undo β Oracle Documentation β Official Oracle explanation of AUM, undo tablespace space management, and the importance of
UNDO_RETENTIONfor flashback features - Managing Undo β Oracle Database 26 Admin Guide β Current Oracle admin guide chapter on setting up and managing undo, including switching to
AUTOundo management - FLASHBACK FOR SYS USER β Oracle Forums β Community confirmation that SYS cannot use
DBMS_FLASHBACKpackage calls, but can useAS OFclause queries - Use DBMS_FLASHBACK to Set Your Session Back in Time β Minimalistic Oracle Blog β Short practical post on using
enable_at_timeas a convenient session time-travel method