Using DBMS_FLASHBACK
, you can flash back to a version of the database at a specified wall-clock time or a specified system change number (SCN).
See Also:
For detailed information aboutDBMS_FLASHBACK
:
This chapter contains the following topics:
Overview
Security Model
Types
Exceptions
Operational Notes
Examples
DBMS_FLASHBACK
provides an interface for the user to view the database at a particular time in the past, with the additional capacity provided by transaction backout features that allow for selective removal of the effects of individual transactions. This should not be confused this with a flashback database which actually moves the database back in time.
When DBMS_FLASHBACK
is enabled, the user session uses the Flashback version of the database, and applications can execute against the Flashback version of the database.
You may want to use DBMS_FLASHBACK
for the following reasons:
Self-service repair: If you accidentally delete rows from a table, you can recover the deleted rows.
Packaged applications such as e-mail and voicemail: You can use Flashback to restore deleted e-mail by re-inserting the deleted message into the current message box.
Decision support system (DSS) and online analytical processing (OLAP) applications: You can perform data analysis or data modeling to track seasonal demand.
To use this package, a database administrator must grant EXECUTE
privileges for DBMS_FLASHBACK
.
Table 69-2 DBMS_FLASHBACK Error Messages
Error | Description |
---|---|
|
Time specified is too old |
|
Invalid system change number specified |
|
User cannot begin read-only or serializable transactions in Flashback mode |
|
User cannot enable Flashback within an uncommitted transaction |
|
User cannot enable Flashback within another Flashback session |
|
|
DBMS_FLASHBACK
is automatically turned off when the session ends, either by disconnection or by starting another connection.
PL/SQL cursors opened in Flashback mode return rows as of the flashback time or SCN. Different concurrent sessions (connections) in the database can perform Flashback to different wall-clock times or SCNs. DML and DDL operations and distributed operations are not allowed while a session is running in Flashback mode. You can use PL/SQL cursors opened before disabling Flashback to perform DML.
Under Automatic Undo Management (AUM) mode, you can use retention control to control how far back in time to go for the version of the database you need.If you need to perform a Flashback over a 24-hour period, the DBA should set the undo_retention
parameter to 24 hours. This way, the system retains enough undo information to regenerate the older versions of the data.
You can set the RETENTION
GUARANTEE
clause for the undo tablespace to ensure that unexpired undo is not discarded.UNDO_RETENTION
is not in itself a complete guarantee because, if the system is under space pressure, unexpired undo may be overwritten with freshly generated undo. In such cases, RETENTION
GUARANTEE
prevents this. For more information, see the Oracle Database Administrator's Guide
In a Flashback-enabled session, SYSDATE
is not affected; it continues to provide the current time.
DBMS_FLASHBACK
can be used within logon triggers to enable Flashback without changing the application code.
The following example illustrates how Flashback can be used when the deletion of a senior employee triggers the deletion of all the personnel reporting to him. Using the Flashback feature, you can recover and re-insert the missing employees.
DROP TABLE employee; DROP TABLE keep_scn; REM -- Keep_scn is a temporary table to store scns that we are interested in CREATE TABLE keep_scn (scn number); SET ECHO ON CREATE TABLE employee ( employee_no number(5) PRIMARY KEY, employee_name varchar2(20), employee_mgr number(5) CONSTRAINT mgr_fkey REFERENCES EMPLOYEE ON DELETE CASCADE, salary number, hiredate date ); REM -- Populate the company with employees INSERT INTO employee VALUES (1, 'John Doe', null, 1000000, '5-jul-81'); INSERT INTO employee VALUES (10, 'Joe Johnson', 1, 500000, '12-aug-84'); INSERT INTO employee VALUES (20, 'Susie Tiger', 10, 250000, '13-dec-90'); INSERT INTO employee VALUES (100, 'Scott Tiger', 20, 200000, '3-feb-86'); INSERT INTO employee VALUES (200, 'Charles Smith', 100, 150000, '22-mar-88'); INSERT INTO employee VALUES (210, 'Jane Johnson', 100, 100000, '11-apr-87'); INSERT INTO employee VALUES (220, 'Nancy Doe', 100, 100000, '18-sep-93'); INSERT INTO employee VALUES (300, 'Gary Smith', 210, 75000, '4-nov-96'); INSERT INTO employee VALUES (310, 'Bob Smith', 210, 65000, '3-may-95'); COMMIT; REM -- Show the entire org SELECT lpad(' ', 2*(level-1)) || employee_name Name FROM employee CONNECT BY PRIOR employee_no = employee_mgr START WITH employee_no = 1 ORDER BY LEVEL; REM -- Sleep for a short time (approximately 10 to 20 seconds) to avoid REM -- querying close to table creation EXECUTE DBMS_LOCK.SLEEP(10); REM -- Store this snapshot for later access through Flashback DECLARE I NUMBER; BEGIN I := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER; INSERT INTO keep_scn VALUES (I); COMMIT; END; / REM -- Scott decides to retire but the transaction is done incorrectly DELETE FROM EMPLOYEE WHERE employee_name = 'Scott Tiger'; COMMIT; REM -- notice that all of scott's employees are gone SELECT lpad(' ', 2*(level-1)) || employee_name Name FROM EMPLOYEE CONNECT BY PRIOR employee_no = employee_mgr START WITH employee_no = 1 ORDER BY LEVEL; REM -- Flashback to see Scott's organization DECLARE restore_scn number; BEGIN SELECT scn INTO restore_scn FROM keep_scn; DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER (restore_scn); END; / REM -- Show Scott's org. SELECT lpad(' ', 2*(level-1)) || employee_name Name FROM employee CONNECT BY PRIOR employee_no = employee_mgr START WITH employee_no = (SELECT employee_no FROM employee WHERE employee_name = 'Scott Tiger') ORDER BY LEVEL; REM -- Restore scott's organization. DECLARE scotts_emp NUMBER; scotts_mgr NUMBER; CURSOR c1 IS SELECT employee_no, employee_name, employee_mgr, salary, hiredate FROM employee CONNECT BY PRIOR employee_no = employee_mgr START WITH employee_no = (SELECT employee_no FROM employee WHERE employee_name = 'Scott Tiger'); c1_rec c1 % ROWTYPE; BEGIN SELECT employee_no, employee_mgr INTO scotts_emp, scotts_mgr FROM employee WHERE employee_name = 'Scott Tiger'; /* Open c1 in flashback mode */ OPEN c1; /* Disable Flashback */ DBMS_FLASHBACK.DISABLE; LOOP FETCH c1 INTO c1_rec; EXIT WHEN c1%NOTFOUND; /* Note that all the DML operations inside the loop are performed with Flashback disabled */ IF (c1_rec.employee_mgr = scotts_emp) then INSERT INTO employee VALUES (c1_rec.employee_no, c1_rec.employee_name, scotts_mgr, c1_rec.salary, c1_rec.hiredate); ELSE IF (c1_rec.employee_no != scotts_emp) THEN INSERT INTO employee VALUES (c1_rec.employee_no, c1_rec.employee_name, c1_rec.employee_mgr, c1_rec.salary, c1_rec.hiredate); END IF; END IF; END LOOP; END; / REM -- Show the restored organization. select lpad(' ', 2*(level-1)) || employee_name Name FROM employee CONNECT BY PRIOR employee_no = employee_mgr START WITH employee_no = 1 ORDER BY LEVEL;
Table 69-3 DBMS_FLASHBACK Package Subprograms
Subprogram | Description |
---|---|
Disables the Flashback mode for the entire session |
|
Enables Flashback for the entire session. Takes an SCN as an Oracle number and sets the session snapshot to the specified number. Inside the Flashback mode, all queries return data consistent as of the specified wall-clock time or SCN |
|
Enables Flashback for the entire session. The snapshot time is set to the SCN that most closely matches the time specified in |
|
Returns the current SCN as an Oracle number. You can use the SCN to store specific snapshots |
|
Provides the mechanism to backout a transaction |
This procedure takes an SCN as an input parameter and sets the session snapshot to the specified number. In the Flashback mode, all queries return data consistent as of the specified wall-clock time or SCN. It enables Flashback for the entire session.
This procedure enables Flashback for the entire session. The snapshot time is set to the SCN that most closely matches the time specified in query_time.It enables Flashback for the entire session.
Table 69-5 ENABLE_AT_TIME Procedure Parameters
Parameter | Description |
---|---|
|
This is an input parameter of type
|
This function returns the current SCN as an Oracle number datatype. You can obtain the current change number and store it for later use. This helps you retain specific snapshots.
This procedure provides a mechanism to backout a set of transactions. The user can call these procedures with either transaction names or transaction identifiers (XIDS
).
The procedure analyzes the transactional dependencies, perform DML's and generates an extensive report on the operation performed by the subprogram. This procedure does not commit the DML's performed as part of transaction backout. However it holds all the required locks on rows and tables in the right form, so that no other dependencies can enter the system. To make the changes permanent the user needs to explicitly commit the transaction.
A report is generated in the system tables DBA_FLASHBACK_TRANSACTION_STATE
and DBA_FLASHBACK_TRANSACTION_REPORT
.
DBMS_FLASHBACK.TRANSACTION_BACKOUT numtxns NUMBER, xids XID_ARRAY, options NUMBER default NOCASCADE, timeHint TIMESTAMP default MINTIME); DBMS_FLASHBACK.TRANSACTION_BACKOUT numtxns NUMBER, xids XID_ARRAY, options NUMBER default NOCASCADE, scnHint TIMESTAMP default 0 ); DBMS_FLASHBACK.TRANSACTION_BACKOUT numtxns NUMBER, txnnames TXNAME_ARRAY, options NUMBER default NOCASCADE, timehint TIMESTAMP MINTIME ); DBMS_FLASHBACK.TRANSACTION_BACKOUT numtxns NUMBER, txnNames TXNAME_ARRAY, options NUMBER default NOCASCADE, scnHint NUMBER 0);
Table 69-6 TRANSACTION_BACKOUT Procedure Parameters
Parameter | Description |
---|---|
|
Number of transactions passed as input |
|
List of transaction IDs in the form of an array |
|
List of transaction names in the form of an array |
|
Backout dependent transactions:
|
|
Time hint on the start of the transaction |
|
SCN hint on the start of the transaction |
Note:
For information about restrictions in usingTRANSACTION_BACKOUT
, see "Using Flashback Transaction" in the Oracle Database Advanced Application Developer's Guide.If transaction name is used, a time hint must be provided. The time hint should be a time before the start of all the given transactions to back out.
If the SCN hint is provided, it must be before the start of the earliest transaction in the specified input set, or this raises an error and terminate. If it is not provided and the transaction has committed within undo retention, the database system is able to determine the start time.