Oracle System Change Number (SCN):
✓ The System Change Number (SCN) is a critical component that acts as a logical timestamp,
playing a central role in maintaining data consistency, managing transactions, facilitating
recovery processes, and enabling features like flashback queries.
Purpose and importance:
Data Consistency:
✓ SCNs are fundamental to Oracle's ability to provide a consistent view of data, even with
concurrent transactions. Each query and transaction is associated with an SCN, ensuring that
a user sees only the data that was committed at or before that SCN.
Transactions:
✓ Every transaction in an Oracle database is assigned a unique SCN when it commits. This SCN
acts as a boundary for the transaction's changes.
Recovery:
✓ SCNs are crucial for instance and media recovery. They are used to mark redo log entries,
allowing Oracle to synchronize recovery processing and determine which changes need to
be applied or rolled back during recovery.
Flashback Queries:
✓ The SCN is used in flashback queries to allow users to view the state of the database at a
specific point in time (represented by an SCN or timestamp).
How SCNs work:
✓ SCNs are monotonically increasing sequences of numbers, generated and incremented
internally by Oracle.
✓ While they appear like a time-based counter, they are not directly tied to a physical clock
and can increment at varying rates depending on database activity.
✓ Oracle ensures that "readers do not wait for writers and writers don't need for readers" by
using SCNs to manage read consistency. When a query starts, it takes a snapshot of the
database at a specific SCN. Any data blocks read are either at that SCN or are reconstructed
using undo segments to reflect the state at the query's start SCN.
✓ SCNs are stored in various locations within the database, including redo logs, control files,
and data block headers, each serving a specific purpose related to data consistency and
recovery.
SCN structure and format:
✓ Historically, SCNs were 6-byte (48-bit) numbers, composed of two parts: a 16-bit "wrap"
component and a 32-bit "base" component.
✓ When the "base" component reached its maximum value, the "wrap" component
incremented, and the base reset to 0.
✓ Starting with Oracle 12c, the SCN format changed to an 8-byte number, providing a
significantly larger range and lifespan for SCNs.
SCN functions:
✓ SCN_TO_TIMESTAMP: This function converts a given SCN to its approximate timestamp,
which can be useful for human-readable representation or associating changes with specific
times.
✓ TIMESTAMP_TO_SCN: This function takes a timestamp and returns the approximate SCN
corresponding to that point in time.
Important Notes:
✓ The relationship between an SCN and its timestamp is maintained by Oracle for a limited
period, typically determined by undo retention and flashback archive settings, but not less
than 120 hours. Attempting to convert SCNs or timestamps older than this period can result
in errors.
✓ While SCNs are generally unique per committed transaction, under high concurrency, it is
possible for multiple transactions to share the same SCN
What happens when a transaction commits?
When a transaction commits,
✓ A system change number (SCN) is generated for the COMMIT.
✓ The internal transaction table for the associated undo tablespace records that the
transaction has committed. The corresponding unique SCN of the transaction is assigned and
recorded in the transaction table. See "Serializable Isolation Level".
✓ The log writer (LGWR) process writes remaining redo log entries in the redo log buffers to
the online redo log and writes the transaction SCN to the online redo log. This atomic event
constitutes the commit of the transaction.
✓ Oracle Database releases locks held on rows and tables.
✓ Users who were enqueued waiting on locks held by the uncommitted transaction are
allowed to proceed with their work.
✓ Oracle Database deletes savepoints.
✓ Oracle Database performs a commit cleanout.
✓ If modified blocks containing data from the committed transaction are still in the SGA, and if
no other session is modifying them, then the database removes lock-related transaction
information from the blocks. Ideally, the COMMIT cleans out the blocks so that a subsequent
SELECT does not have to perform this task.
Each type of SCN and their location and what they have been called and its purpose: