Oracle: Get Current SCN with DBMS_FLASHBACK
How to Find the Current SCN in Oracle Database Using DBMS_FLASHBACK
What number marks "now" inside an Oracle database? The System Change Number (SCN) does. One call to DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER returns it from any session that has EXECUTE on the package.
What Is an SCN?
The System Change Number (SCN) is a logical, internal time stamp used by Oracle Database. SCNs occur in a monotonically increasing sequence, and every transaction has one. When a transaction commits, the database records an SCN for that commit. Oracle uses the SCN for:
- Read consistency — a statement is consistent to the SCN recorded when it began, so it sees only data committed as of that SCN
- Recovery — Oracle uses SCNs to mark the SCN before which all changes are known to be on disk, so recovery avoids applying unnecessary redo
- Flashback operations —
AS OF SCNreturns rows as they existed at the specified SCN
Several events can share the same SCN, which means they occurred at the same time in the database. Repeated observations of the SCN return equal or greater values.
The Query
1SELECT dbms_flashback.get_system_change_number FROM dual
2/
Code Breakdown
Components of the Query
| Component | Type | Description |
|---|---|---|
SELECT | SQL keyword | Begins a data retrieval statement |
DBMS_FLASHBACK | PL/SQL Package | Oracle-supplied package for flashback operations |
GET_SYSTEM_CHANGE_NUMBER | Package Function | Returns the current SCN as an Oracle NUMBER datatype |
FROM dual | Pseudo-table | Oracle's single-row utility table; used when no real table is needed |
/ | SQL*Plus command | Executes the preceding SQL block in SQL*Plus or SQL*Plus-compatible tools |
What the Function Returns
DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER returns a single NUMBER value representing the current SCN at the exact moment the function is called. A later call returns a value equal to or greater than an earlier one. The SCN never decreases.
Example output:
1GET_SYSTEM_CHANGE_NUMBER
2------------------------
3 2249659
Key Points
- Package required —
DBMS_FLASHBACKis a built-in Oracle-supplied package. It is documented in the Oracle9i Release 1 manual. - Privilege required — The calling user needs the
EXECUTEprivilege onDBMS_FLASHBACK. If the call fails, ask the DBA to grant it:GRANT EXECUTE ON DBMS_FLASHBACK TO your_user; - Never decreasing — Repeated observations of the SCN return equal or greater values.
- Changes between calls — Two calls made milliseconds apart may return different SCN values because other sessions are committing at the same time.
- Works in any Oracle session — The documentation requires only the
EXECUTEprivilege on the package, so you do not needSYSDBAjust to read the SCN (only to grant execute, if needed).
Alternative Ways to Get the Current SCN
There are several other methods to retrieve the current SCN. Use the one that fits your privilege level and use case:
1-- Method 2: Using V$DATABASE
2SELECT current_scn FROM v$database;
3
4-- Method 3: Using TIMESTAMP_TO_SCN (approximation based on sysdate)
5SELECT timestamp_to_scn(SYSDATE) FROM dual;
V$DATABASE.CURRENT_SCN is null if the database is not currently open. TIMESTAMP_TO_SCN returns the approximate SCN for a timestamp. The database remembers the SCN-to-timestamp association for a limited period (no less than 120 hours), and the function returns an error if the timestamp is too old.
Tip:
V$DATABASE.CURRENT_SCNandDBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBERmay return slightly different values if queried back to back, because the SCN advances between calls. Both are correct snapshots at their respective moments.
DBA Insights and Practical Uses
Capture the SCN Before a Risky Operation
Before running a bulk update, delete, or data load, capture the SCN. If something goes wrong, you can use Flashback Query to see what the data looked like before the change:
1-- Step 1: Capture the SCN before your change
2COL x NEW_VALUE saved_scn
3SELECT dbms_flashback.get_system_change_number x FROM dual;
4
5-- Step 2: Run your risky operation
6DELETE FROM orders WHERE status = 'CANCELLED';
7COMMIT;
8
9-- Step 3: If something went wrong, query data AS OF that SCN
10SELECT * FROM orders AS OF SCN &saved_scn WHERE status = 'CANCELLED';
Use the SCN in Shell Scripts
DBAs frequently capture the SCN in shell scripts to tag backup windows or audit log boundaries:
1CURRENT_SCN=$(sqlplus -S user/pass@db <<EOF
2SET HEADING OFF FEEDBACK OFF
3SELECT dbms_flashback.get_system_change_number FROM dual;
4EXIT;
5EOF
6)
7echo "Backup start SCN: $CURRENT_SCN"
Permissions Quick Reference
1-- Grant execute to a non-DBA user (run as SYSDBA or DBA)
2GRANT EXECUTE ON dbms_flashback TO your_schema_name;
3
4-- Verify the grant
5SELECT grantee, privilege, table_name
6FROM dba_tab_privs
7WHERE table_name = 'DBMS_FLASHBACK';
References
- Oracle Docs — DBMS_FLASHBACK Package Reference (Oracle 18c) — Official Oracle documentation for the
DBMS_FLASHBACKpackage, including theGET_SYSTEM_CHANGE_NUMBERfunction syntax and return type. - Oracle Docs — DBMS_FLASHBACK (Oracle 19c) — States the
EXECUTEprivilege requirement for the package. - Oracle Docs — Transactions and SCNs (Oracle 19c Concepts) — Defines the SCN as a logical, internal time stamp and describes its monotonic ordering.
- Oracle Docs — TIMESTAMP_TO_SCN (Oracle 19c) — Describes the approximate SCN returned for a timestamp and the retention limit.
- Oracle Docs — V$DATABASE (Oracle 19c) — Documents the
CURRENT_SCNcolumn. - Oracle Docs — DBMS_FLASHBACK (Oracle 9i) — The Oracle9i Release 1 manual page documenting
GET_SYSTEM_CHANGE_NUMBER, with its description and syntax. - Ask TOM — DBMS_FLASHBACK Usage — A community thread on using
dbms_flashback.get_system_change_numberto return the current system SCN. - Oracle Internals by Riyaj — SCN: What, Why, and How — Internals article on SCN generation, the SCN format, and how redo relates to SCNs.
- Prakash's DBA Blog — Finding the Current SCN of a Database — Practical DBA post comparing multiple methods to retrieve the current SCN in Oracle 10g and 11g.
- Database Journal — Oracle Flashback Query: TIMESTAMP or SCN? — Explains when to use SCN versus TIMESTAMP in flashback queries, with code examples showing both approaches.
- Alec Kaplan DBA Blog — Oracle SCN — Hands-on examples of retrieving SCN using multiple methods, including the
ORA-00904privilege error and its fix.
Posts in this series
- Oracle Flashback: Enable with Shutdown, Mount, ALTER
- DB_FLASHBACK_RETENTION_TARGET: Set Oracle Flashback Window
- Oracle Fast Recovery Area: Set db_recovery_file_dest
- Oracle: Get Current SCN with DBMS_FLASHBACK
- Oracle exp: Export Database with FLASHBACK_TIME
- Oracle Flashback Query: Copy Historical Data to a Table
- Oracle: Disable Flashback with DBMS_FLASHBACK.DISABLE
- Oracle DBMS_FLASHBACK.ENABLE_AT_TIME: Session Flashback
- Run a Consistent Export with expdp FLASHBACK_TIME
- Manage the Recycle Bin with DBA_RECYCLEBIN