Oracle7 Server Concepts
Segments
A segment is a set of extents that contain all the data for a specific logical storage structure within a tablespace. For example, for each table, Oracle allocates one or more extents to form that table's data segment, and, for each index, Oracle allocates one or more extents to form its index segment.
There are four types of segments used in Oracle databases:
The following sections discuss each type of segment.
Data Segments
Every non-clustered table (including snapshots and snapshot logs) in an Oracle database has a single data segment to hold all of its data. Oracle creates this data segment when you create the object with the CREATE TABLE/SNAPSHOT/SNAPSHOT LOG command.
Every cluster in an Oracle database uses a single data segment to hold the data for all of its tables. Oracle creates the data segment for the cluster when you issue the CREATE CLUSTER command.
The storage parameters for a table, snapshot, snapshot log, or cluster control the way that its data segment's extents are allocated. You can set these storage parameters directly with the CREATE TABLE/ SNAPSHOT/ SNAPSHOT LOG/CLUSTER or ALTER TABLE/ SNAPSHOT/ SNAPSHOT LOG/CLUSTER commands; these affect the efficiency of data retrieval and storage for the data segment associated with the object. For more information on the various CREATE and ALTER commands, see the Oracle7 Server SQL Reference.
Index Segments
Every index in an Oracle database has a single index segment to hold all of its data. Oracle creates the index segment for the index when you issue the CREATE INDEX command. This command allows you to specify the storage parameters for the extents of the index segment and the tablespace in which to create the index segment. (The segments of a table and an index associated with it do not have to occupy the same tablespace.) Setting the storage parameters directly affects the efficiency of data retrieval and storage.
Rollback Segments
Each database contains one or more rollback segments. A rollback segment is a portion of the database that records the actions of transactions if the transaction should be rolled back (undone). Rollback segments are used to provide read consistency, to rollback transactions, and to recover the database.
For specific information about how rollback segments function in these situations, see the appropriate sections of this book:
Contents of a Rollback Segment
Information in a rollback segment consists of several rollback entries. Among other information, a rollback entry includes block information (the filename and block ID corresponding to the data that was changed) and the data as it existed before an operation in a transaction. Oracle links rollback entries for the same transaction, so the entries can easily be found if necessary for transaction rollback.
Database users or administrators cannot access or read rollback segments; only Oracle can write to or read them. (They are owned by the user SYS, no matter which user creates them.)
Because rollback entries change data blocks, Oracle also records changes to them in the redo log. This second recording of the rollback information is very important for active transactions not yet committed at the time of the system crash. If a system crash occurs, Oracle automatically restores the rollback segment information, including the rollback entries for active transactions, as part of instance or media recovery. Oracle performs rollbacks of transactions that had not been committed or rolled back at the time of the failure after recovery is complete.
When Rollback Information Is Required
Oracle maintains a transaction table for each rollback segment contained in a database. Each table is a list of all transactions that use the associated rollback segment and the rollback entries for each change performed by these transactions. Oracle uses the rollback entries in a rollback segment to perform a transaction rollback and to create read-consistent results for queries.
Rollback segments record the data prior to change on a per transaction basis. For every transaction, Oracle links each new change to the previous change. If you must roll back the transaction, Oracle applies the changes in the chain to the data blocks in an order that restores the data to its previous state.
Similarly, when Oracle needs to provide a read-consistent set of results for a query, it can use information in rollback segments to create a set of data consistent with respect to a single point in time.
All types of rollbacks use the same procedures:
- statement level rollback (due to statement or deadlock execution error)
- rollback of a transaction due to user request
- rollback of a transaction due to abnormal process termination
- rollback of all outstanding transactions when an instance terminates abnormally
- rollback of incomplete transactions during recovery
Transactions and Rollback Segments
Each time a user's transaction begins, Oracle assigns the transaction to a rollback segment:
- Oracle can assign a transaction automatically to the next available rollback segment. The transaction assignment occurs when you issue the first DML or DDL statement in the transaction. Oracle never assigns read-only transactions (transactions that contain only queries) to a rollback segment, regardless of whether the transaction begins with a SET TRANSACTION READ ONLY statement.
For the duration of a transaction, the associated user process writes rollback information only to the assigned rollback segment.
When you commit a transaction, Oracle releases the rollback information, but does not immediately destroy it. The information remains in the rollback segment to create read-consistent views of pertinent data for queries that started before the transaction committed. To guarantee that rollback data is available for as long as possible for such views, Oracle writes the extents of rollback segments sequentially. When the last extent of the rollback segment becomes full, Oracle continues writing rollback data by wrapping around to the first extent in the segment. A long-running transaction (idle or active) may require a new extent to be allocated for the rollback segment. See Figure 3 - 7, Figure 3 - 8, and Figure 3 - 9 for more information about how transactions use the extents of a rollback segment.
Each rollback segment can handle a certain number of transactions from one instance. Unless you explicitly assign transactions to particular rollback segments, Oracle distributes active transactions across available rollback segments so that all rollback segments are assigned approximately the same number of active transactions. Distribution does not depend on the size of the available rollback segments. Therefore, in environments where all transactions generate the same amount of rollback information, all rollback segments can be the same size.
Additional Information: The number of transactions that a rollback segment can handle is an operating system-specific function of the data block size. See your Oracle operating system-specific documentation for more information.
How Extents Are Used and Allocated for Rollback Segments
When you create a rollback segment, you can specify storage parameters to control the allocation of extents for that segment. Each rollback segment must have at least two extents allocated.
A transaction writes sequentially to a single rollback segment. Each transaction writes to only one extent of the rollback segment at any given time. Furthermore, many active transactions (transactions in progress, not committed or rolled back) can write concurrently to a single rollback segment, even the same extent of a rollback segment; however, each block in a rollback segment's extent can contain information for a single transaction only.
When a transaction runs out of space in the current extent and needs to continue writing, Oracle must find an available extent of the same rollback segment in which to write. Oracle has two options:
- It can reuse an extent already allocated to the rollback segment.
- It can acquire (and allocate) a new extent for the rollback segment.
The first transaction that needs to acquire more rollback space checks the next extent of the rollback segment. If the next extent of the rollback segment does not contain active undo information, Oracle makes it the current extent, and all transactions that need more space from then on can write rollback information to the new current extent. Figure 3 - 7 illustrates two transactions, T1 and T2, which continue writing from the third extent to the fourth extent of a rollback segment.
![](image015.gif)
Figure 3 - 7. Use of Allocated Extents in a Rollback Segment
As the transactions continue writing and fill the current extent, Oracle checks the next extent already allocated for the rollback segment to determine if it is available. In Figure 3 - 8, when E4 is completely full, T1 and T2 continue any further writing to the next extent allocated for the rollback segment that is available; in this figure, E1 is this extent. This figure shows the cyclical nature of extent use in rollback segments.
![](image016.gif)
Figure 3 - 8. Cyclical Use of the Allocated Extents in a Rollback Segment
To continue writing rollback information for a transaction, Oracle always tries to reuse the next extent in the ring first. However, if the next extent contains active data, then Oracle must allocate a new extent. Oracle can allocate new extents for a rollback segment until the number of extents reaches the value set for the rollback segment's storage parameter MAXEXTENTS.
Figure 3 - 9 shows when a new extent must be allocated for a rollback segment. The uncommitted transactions are long running (either idle, active, or persistent in-doubt distributed transactions). At this time, they are writing to the fourth extent, E4, in the rollback segment. However, when E4 is completely full, the transactions cannot continue further writing to the next extent in sequence, E1, because it contains active rollback entries. Therefore, Oracle allocates a new extent, E5, for this rollback segment, and the transactions continue writing to this new extent.
![](image017.gif)
Figure 3 - 9. Allocation of a New Extent for a Rollback Segment
How Extents Are Deallocated from a Rollback Segment
When you create or alter a rollback segment, you can use the storage parameter OPTIMAL, which applies only to rollback segments, to specify the optimal size of the rollback segment in bytes. If a transaction needs to continue writing rollback information from one extent to another extent in the rollback segment, Oracle compares the current size of the rollback segment to the segment's optimal size. If the rollback segment is larger than its optimal size and the extents immediately following the extent just filled are inactive, Oracle deallocates consecutive non-active extents from the rollback segment until the total size of the rollback segment is equal to or as close to but not less than its optimal size. Oracle always frees the oldest inactive extents, as these are the least likely to be used by consistent reads. A rollback segment's OPTIMAL setting cannot be less than the combined space allocated for the minimum number of extents for the segment:
(INITIAL + NEXT + NEXT + ... up to MINEXTENTS) bytes
When you drop a rollback segment, Oracle returns all extents of the rollback segment to its tablespace. The returned extents are then available to other segments in the tablespace.
Instances and Types of Rollback Segments
When an instance opens a database, it must acquire one or more rollback segments so that the instance can handle rollback information produced by subsequent transactions. An instance can acquire both private and public rollback segments. A private rollback segment is acquired explicitly by an instance when the instance opens a database. Public rollback segments form a pool of rollback segments that any instance requiring a rollback segment can use.
Any number of private and public rollback segments can exist in a database. As an instance opens a database, the instance attempts to acquire one or more rollback segments according to the following rules:
CEIL(TRANSACTIONS/TRANSACTIONS_PER_ROLLBACK_SEGMENT)
CEIL is a SQL function that returns the smallest integer greater than or equal to the numeric input. In the example above, if TRANSACTIONS equal 155 and TRANSACTIONS_PER_ROLLBACK_SEGMENT equal 10, then the instance will try to acquire at least 16 rollback segments.
- If the instance already has acquired enough private rollback segments in Step 2, no further action is required. However, if an instance requires more rollback segments, the instance attempts to acquire public rollback segments. (An instance can open the database even if the instance cannot acquire the number of rollback segments given by the division above.)
Note: The TRANSACTIONS_PER_ROLLBACK_SEGMENT parameter does not limit the number of transactions that can use a rollback segment. Rather, it determines the number of rollback segments an instance attempts to acquire when opening a database.
Once an instance claims a public rollback segment, no other instance can use that segment until either the rollback segment is taken offline or the instance that claimed the rollback segment is shut down.
Note: A database used by the Oracle Parallel Server optionally can have only public and no private segments, as long as the number of segments in the database is high enough to ensure that each instance that opens the database can acquire at least two rollback segments, one of which is the SYSTEM rollback segment (see the following section). However, when using the Oracle Parallel Server, you may want to use private rollback segments. See Oracle7 Parallel Server Concepts & Administration for more information about rollback segment use in an Oracle Parallel Server.
The Rollback Segment SYSTEM
Oracle creates an initial rollback segment called SYSTEM whenever a database is created. This segment is in the SYSTEM tablespace and uses that tablespace's default storage parameters. You cannot drop the SYSTEM rollback segment. An instance always acquires the SYSTEM rollback segment in addition to any other rollback segments it needs.
If there are multiple rollback segments, Oracle tries to use the SYSTEM rollback segment only for special system transactions and distributes user transactions among other rollback segments; if there are too many transactions for the non-SYSTEM rollback segments, Oracle uses the SYSTEM segment as necessary. In general, after database creation, you should create at least one additional rollback segment in the SYSTEM tablespace.
Rollback Segment States
A rollback segment is always in one of several states, depending on whether it is offline, acquired by an instance, involved in an unresolved transaction, in need of recovery, or dropped. The state of the rollback segment determines whether it can be used in transactions, as well as which administrative procedures a DBA can perform on it.
The rollback segment states are the following:
Figure 3 - 10 shows how a rollback segment moves from one state to another.
![](image018.gif)
Figure 3 - 10. Rollback Segment States and State Transitions
PARTLY AVAILABLE and NEEDS RECOVERY Rollback Segments The PARTLY AVAILABLE and NEEDS RECOVERY states are very similar: a rollback segment in either state usually contains data from an unresolved transaction. The differences between the two states are the following:
If you bring a PARTLY AVAILABLE rollback segment online (by a command or during instance startup), Oracle can use it for new transactions. However, the in-doubt transaction still holds some of its transaction table entries, so the number of new transactions that can use the rollback segment is limited. (See "When Rollback Information Is Required"
for information on the transaction table.)
Also, until you resolve the in-doubt transaction, the transaction continues to hold the extents it acquired in the rollback segment, preventing other transactions from using them. Thus, the rollback segment might need to acquire new extents for the active transactions, and therefore grow. To prevent the rollback segment from growing, a database administrator might prefer to create a new rollback segment for transactions to use until the in-doubt transaction is resolved, rather than bring the PARTLY AVAILABLE segment online.
Viewing the State of a Rollback Segment The data dictionary table DBA_ROLLBACK_SEGS lists the status (state) of each rollback segment, along with other rollback segment information.
Deferred Rollback Segments
When a tablespace goes offline such that transactions cannot be rolled back immediately, Oracle writes a deferred rollback segment. The deferred rollback segment contains the rollback entries that could not be applied to the tablespace, so they can be applied when the tablespace comes back online. These segments disappear as soon as the tablespace is brought back online and recovered. Oracle automatically creates deferred rollback segments in the SYSTEM tablespace.