|Oracle9i Database Concepts
Release 1 (9.0.1)
Part Number A88856-02
This chapter describes the nature of and relationships among the logical storage structures in the Oracle server. It includes:
Oracle allocates logical database space for all data in a database. The units of database space allocation are data blocks, extents, and segments. Figure 3-1 shows the relationships among these data structures:
At the finest level of granularity, Oracle stores data in data blocks (also called logical blocks, Oracle blocks, or pages). One data block corresponds to a specific number of bytes of physical database space on disk.
The next level of logical database space is an extent. An extent is a specific number of contiguous data blocks allocated for storing a specific type of information.
The level of logical database storage above an extent is called a segment. A segment is a set of extents, each of which has been allocated for a specific data structure and all of which are stored in the same tablespace. For example, each table's data is stored in its own data segment, while each index's data is stored in its own index segment. If the table or index is partitioned, each partition is stored in its own segment.
Oracle allocates space for segments in units of one extent. When the existing extents of a segment are full, Oracle allocates another extent for that segment. Because extents are allocated as needed, the extents of a segment may or may not be contiguous on disk.
A segment and all its extents are stored in one tablespace. Within a tablespace, a segment can include extents from more than one file; that is, the segment can span datafiles. However, each extent can contain data from only one datafile.
Although you can allocate additional extents, the blocks themselves are allocated separately. If you allocate an extent to a specific instance, the blocks are immediately allocated to the free list. However, if the extent is not allocated to a specific instance, then the blocks themselves are allocated only when the high water mark moves. The high water mark is the boundary between used and unused space in a segment.
In Oracle9i, Release 1 (9.0.1), free space can be managed automatically. See "Free Space Management".
Oracle manages the storage space in the datafiles of a database in units called data blocks. A data block is the smallest unit of I/O used by a database. In contrast, at the physical, operating system level, all data is stored in bytes. Each operating system has a block size. Oracle requests data in multiples of Oracle data blocks, not operating system blocks.
The standard block size is specified by the initialization parameter
The Oracle data block format is similar regardless of whether the data block contains table, index, or clustered data. Figure 3-2 illustrates the format of a data block.
The header contains general block information, such as the block address and the type of segment (for example, data, index, or rollback).
This portion of the data block contains information about the table having rows in this block.
This portion of the data block contains information about the actual rows in the block (including addresses for each row piece in the row data area).
Once the space has been allocated in the row directory of a data block's overhead, this space is not reclaimed when the row is deleted. Therefore, a block that is currently empty but had up to 50 rows at one time continues to' have 100 bytes allocated in the header for the row directory. Oracle reuses this space only when new rows are inserted in the block.
The data block header, table directory, and row directory are referred to collectively as overhead. Some block overhead is fixed in size; the total block overhead size is variable. On average, the fixed and variable portions of data block overhead total 84 to 107 bytes.
This portion of the data block contains table or index data. Rows can span blocks.
Free space is allocated for insertion of new rows and for updates to rows that require additional space (for example, when a trailing null is updated to a nonnull value). Whether issued insertions actually occur in a given data block is a function of current free space in that data block and the value of the space management parameter
In data blocks allocated for the data segment of a table or cluster, or for the index segment of an index, free space can also hold transaction entries. A transaction entry is required in a block for each
INSERT, UPDATE, DELETE, and
SELECT...FOR UPDATE statement accessing one or more rows in the block. The space required for transaction entries is operating system dependent; however, transaction entries in most operating systems require approximately 23 bytes.
Free space can be managed automatically or manually.
With Oracle9i, Release 1 (9.0.1), free space can be managed automatically inside database segments. The in-segment free/used space is tracked using bitmaps, as opposed to free lists. Use of automatic segment-space management offers the following benefits:
You specify automatic segment-space management when you create a locally managed tablespace. The specification then applies to all segments subsequently created in this tablespace.
Two types of statements can increase the free space of one or more data blocks:
DELETE statements, and
UPDATE statements that update existing values to smaller values. The released space from these types of statements is available for subsequent
INSERT statements under the following conditions:
INSERTstatement is in the same transaction and subsequent to the statement that frees space, the
INSERTstatement can use the space made available.
INSERTstatement is in a separate transaction from the statement that frees space (perhaps being executed by another user), the
INSERTstatement can use the space made available only after the other transaction commits and only if the space is needed.
Released space may or may not be contiguous with the main area of free space in a data block. Oracle coalesces the free space of a data block only when (1) an
UPDATE statement attempts to use a block that contains enough free space to contain a new row piece, and (2) the free space is fragmented so the row piece cannot be inserted in a contiguous section of the block. Oracle does this compression only in such situations, because otherwise the performance of a database system decreases due to the continuous compression of the free space in data blocks.
In two circumstances, the data for a row in a table may be too large to fit into a single data block. In the first case, the row is too large to fit into one data block when it is first inserted. In this case, Oracle stores the data for the row in a chain of data blocks (one or more) reserved for that segment. Row chaining most often occurs with large rows, such as rows that contain a column of datatype
LONG RAW. Row chaining in these cases is unavoidable.
However, in the second case, a row that originally fit into one data block is updated so that the overall row length increases, and the block's free space is already completely filled. In this case, Oracle migrates the data for the entire row to a new data block, assuming the entire row can fit in a new block. Oracle preserves the original row piece of a migrated row to point to the new block containing the migrated row. The rowid of a migrated row does not change.
When a row is chained or migrated, I/O performance associated with this row decreases because Oracle must scan more than one data block to retrieve the information for the row.
For manually managed tablespaces, two space management parameters,
PCTUSED, enable you to control the use of free space for inserts of and updates to the rows in all the data blocks of a particular segment. Specify these parameters when you create or alter a table or cluster (which has its own data segment). You can also specify the storage parameter
PCTFREE when creating or altering an index (which has its own index segment).
See "LOB Datatypes" for more information.
PCTFREE parameter sets the minimum percentage of a data block to be reserved as free space for possible updates to rows that already exist in that block. For example, assume that you specify the following parameter within a
CREATE TABLE statement:
This states that 20% of each data block in this table's data segment will be kept free and available for possible updates to the existing rows already within each block. New rows can be added to the row data area, and corresponding information can be added to the variable portions of the overhead area, until the row data and overhead total 80% of the total block size. Figure 3-3 illustrates
PCTUSED parameter sets the minimum percentage of a block that can be used for row data plus overhead before new rows are added to the block. After a data block is filled to the limit determined by
PCTFREE, Oracle considers the block unavailable for the insertion of new rows until the percentage of that block falls below the parameter
PCTUSED. Until this value is achieved, Oracle uses the free space of the data block only for updates to rows already contained in the data block. For example, assume that you specify the following parameter in a
CREATE TABLE statement:
In this case, a data block used for this table's data segment is considered unavailable for the insertion of any new rows until the amount of used space in the block falls to 39% or less (assuming that the block's used space has previously reached
PCTFREE). Figure 3-4 illustrates this.
PCTUSED work together to optimize the use of space in the data blocks of the extents within a data segment. Figure 3-5 illustrates the interaction of these two parameters.
In a newly allocated data block, the space available for inserts is the block size minus the sum of the block overhead and free space (
PCTFREE). Updates to existing data can use any available space in the block. Therefore, updates can reduce the available space of a block to less than
For each data and index segment, Oracle maintains one or more free lists--lists of data blocks that have been allocated for that segment's extents and have free space greater than
PCTFREE. These blocks are available for inserts. When you issue an
INSERT statement, Oracle checks a free list of the table for the first available data block and uses it if possible. If the free space in that block is not large enough to accommodate the
INSERT statement, and the block is at least
After you issue a
UPDATE statement, Oracle processes the statement and checks to see if the space being used in the block is now less than
PCTUSED. If it is, the block goes to the beginning of the transaction free list, and it is the first of the available blocks to be used in that transaction. When the transaction commits, free space in the block becomes available for other transactions.
An extent is a logical unit of database storage space allocation made up of a number of contiguous data blocks. One or more extents in turn make up a segment. When the existing space in a segment is completely used, Oracle allocates a new extent for the segment.
When you create a table, Oracle allocates to the table's data segment an initial extent of a specified number of data blocks. Although no rows have been inserted yet, the Oracle data blocks that correspond to the initial extent are reserved for that table's rows.
If the data blocks of a segment's initial extent become full and more space is required to hold new data, Oracle automatically allocates an incremental extent for that segment. An incremental extent is a subsequent extent of the same or greater size than the previously allocated extent in that segment.
For maintenance purposes, the header block of each segment contains a directory of the extents in that segment.
Rollback segments always have at least two extents.
Storage parameters expressed in terms of extents define every segment. Storage parameters apply to all types of segments. They control how Oracle allocates free database space for a given segment. For example, you can determine how much space is initially reserved for a table's data segment or you can limit the number of extents the table can allocate by specifying the storage parameters of a table in the
STORAGE clause of the
CREATE TABLE statement. If you do not specify a table's storage parameters, it uses the default storage parameters of the tablespace.
Tablespaces can manage their extents either locally or through the data dictionary. Some storage parameters apply only to extents in dictionary-managed tablespaces, and other storage parameters apply to all extents.
A tablespace that manages its extents locally can have either uniform extent sizes or variable extent sizes that are determined automatically by the system. When you create the tablespace, the
AUTOALLOCATE (system-managed) clause specifies the type of allocation.
The storage parameters
NEXT, PCTINCREASE, MINEXTENTS, MAXEXTENTS, and
DEFAULT STORAGE are not valid for extents that are managed locally.
A tablespace that uses the data dictionary to manage its extents has incremental extent sizes, which are determined by the storage parameters
INITIAL, NEXT, and
PCTINCREASE. When you create a schema object in the tablespace, its first extent is allocated with the
INITIAL size. When additional space is needed, the
PCTINCREASE parameters determine the sizes of new extents. You can modify the values of
PCTINCREASE after creating a schema object.
Oracle uses different algorithms to allocate extents, depending on whether they are locally managed or dictionary managed.
In locally managed tablespaces, Oracle looks for free space to allocate to a new extent by first determining a candidate datafile in the tablespace and then searching the datafile's bitmap for the required number of adjacent free blocks. If that datafile does not have enough adjacent free space, Oracle looks in another datafile.
In dictionary-managed tablespaces, Oracle controls the allocation of incremental extents for a given segment as follows:
In the current example, if Oracle does not find a set of exactly 20 contiguous data blocks, Oracle searches for a set of contiguous data blocks greater than 20. If the first set it finds contains 25 or more blocks, it breaks the blocks up and allocates 20 of them to the new extent and leaves the remaining 5 or more blocks as free space. Otherwise, it allocates all of the blocks (between 21 and 24) to the new extent.
The blocks of a newly allocated extent, although they were free, may not be empty of old data. Usually, Oracle formats the blocks of a newly allocated extent when it starts using the extent, but only as needed (starting with the blocks on the segment free list). In a few cases, however, such as when a database administrator forces allocation of an incremental extent with the
ALLOCATE EXTENT clause of an
ALTER TABLE or ALTER CLUSTER statement, Oracle formats the extent's blocks when it allocates the extent.
In general, the extents of a segment do not return to the tablespace until you drop the schema object whose data is stored in the segment (using a
DROP TABLE or
DROP CLUSTER statement). Exceptions to this include the following:
DELETE ANYprivilege, can truncate the table or cluster with a
When extents are freed, Oracle modifies the bitmap in the datafile (for locally managed tablespaces) or updates the data dictionary (for dictionary-managed tablespaces) to reflect the regained extents as available space. Any data in the blocks of freed extents becomes inaccessible, and Oracle clears the data when the blocks are subsequently reused for other extents.
As long as a nonclustered table exists or until you truncate the table, any data block allocated to its data segment remains allocated for the table. Oracle inserts new rows into a block if there is enough room. Even if you delete all rows of a table, Oracle does not reclaim the data blocks for use by other objects in the tablespace.
After you drop a nonclustered table, this space can be reclaimed when other extents require free space. Oracle reclaims all the extents of the table's data and index segments for the tablespaces that they were in and makes the extents available for other schema objects in the same tablespace.
In dictionary-managed tablespaces, when a segment requires an extent larger than the available extents, Oracle identifies and combines contiguous reclaimed extents to form a larger one. This is called coalescing extents.
Coalescing extents is not necessary in locally managed tablespaces, because all contiguous free space is available for allocation to a new extent regardless of whether it was reclaimed from one or more extents.
Clustered tables store their information in the data segment created for the cluster. Therefore, if you drop one table in a cluster, the data segment remains for the other tables in the cluster, and no extents are deallocated. You can also truncate clusters (except for hash clusters) to free extents.
Oracle deallocates the extents of materialized views and materialized view logs in the same manner as for tables and clusters.
"Materialized Views" for a description of materialized views and their logs
All extents allocated to an index segment remain allocated as long as the index exists. When you drop the index or associated table or cluster, Oracle reclaims the extents for other uses within the tablespace.
Oracle periodically checks the rollback segments of the database to see if they have grown larger than their optimal size. If a rollback segment is larger than is optimal (that is, it has too many extents), Oracle automatically deallocates one or more extents from the rollback segment.
When Oracle completes the execution of a statement requiring a temporary segment, Oracle automatically drops the temporary segment and returns the extents allocated for that segment to the associated tablespace. A single sort allocates its own temporary segment in the temporary tablespace of the user issuing the statement and then returns the extents to the tablespace.
Multiple sorts, however, can use sort segments in a temporary tablespace designated exclusively for sorts. These sort segments are allocated only once for the instance, and they are not returned after the sort but remain available for other multiple sorts.
A temporary segment in a temporary table contains data for multiple statements of a single transaction or session. Oracle drops the temporary segment at the end of the transaction or session, returning the extents allocated for that segment to the associated tablespace.
A segment is a set of extents that contains 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.
Oracle databases use four types of segments, which are described in the following sections:
A single data segment in an Oracle database holds all of the data for one of the following:
Oracle creates this data segment when you create the table or cluster with the
The storage parameters for a table or cluster determine how its data segment's extents are allocated. You can set these storage parameters directly with the appropriate
ALTER statement. These storage parameters affect the efficiency of data retrieval and storage for the data segment associated with the object.
Every nonpartitioned index in an Oracle database has a single index segment to hold all of its data. For a partitioned index, every partition has a single index segment to hold its data.
Oracle creates the index segment for an index or an index partition when you issue the
CREATE INDEX statement. In this statement, you can specify storage parameters for the extents of the index segment and a 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.
When processing queries, Oracle often requires temporary workspace for intermediate stages of SQL statement parsing and execution. Oracle automatically allocates this disk space called a temporary segment. Typically, Oracle requires a temporary segment as a work area for sorting. Oracle does not create a segment if the sorting operation can be done in memory or if Oracle finds some other way to perform the operation using indexes.
The following statements could require the use of a temporary segment:
SELECT ... ORDER BY
SELECT DISTINCT ...
SELECT ... GROUP BY
SELECT ... UNION
SELECT ... INTERSECT
SELECT ... MINUS
Some unindexed joins and correlated subqueries can also require use of a temporary segment. For example, if a query contains a
DISTINCT clause, a
Oracle9i Database Reference for information on
Oracle can also allocate temporary segments for temporary tables and indexes created on temporary tables. Temporary tables hold data that exists only for the duration of a transaction or session.
Oracle allocates temporary segments differently for queries and temporary tables.
Oracle allocates temporary segments as needed during a user session, in the temporary tablespace of the user issuing the statement. Specify this tablespace with a
CREATE USER or an
ALTER USER statement using the
TEMPORARY TABLESPACE clause. If no temporary tablespace has been defined for the user, the default temporary tablespace is the
SYSTEM tablespace. The default storage characteristics of the containing tablespace determine those of the extents of the temporary segment.
Oracle drops temporary segments when the statement completes.
Because allocation and deallocation of temporary segments occur frequently, it is reasonable to create a special tablespace for temporary segments. By doing so, you can distribute I/O across disk devices, and you can avoid fragmentation of the
SYSTEM and other tablespaces that otherwise hold temporary segments.
Entries for changes to temporary segments used for sort operations are not stored in the redo log, except for space management operations on the temporary segment.
Chapter 24, "Controlling Database Access" for more information about assigning a user's temporary segment tablespace
Oracle allocates segments for a temporary table when the first
INSERT into that table is issued. (This can be an insert operation internally issued by
CREATE TABLE AS SELECT.) The first
INSERT into a temporary table allocates the segments for the table and its indexes, creates the root page for the indexes, and allocates any
Segments for a temporary table are allocated in the temporary tablespace of the user who created the temporary table.
Oracle drops segments for a transaction-specific temporary table at the end of the transaction and drops segments for a session-specific temporary table at the end of the session. If other transactions or sessions share the use of that temporary table, the segments containing their data remain in the table.
In Oracle8i, undo space management was performed using rollback segments, as described in "Introduction to Rollback Segments". This method is now called manual undo management mode.
Oracle9i, Release 1 (9.0.1), provides automatic undo management, a new way of managing undo space in Oracle databases. You need to perform fewer explicit actions to configure the system to use undo space efficiently and effectively. Automatic undo management is undo-tablespace based. You allocate space in the form of a few undo tablespaces, instead of allocating many rollback segments in different sizes. With automatic undo management, you have no reason to create, drop, or alter transaction tables or undo segments. You just need to create an undo tablespace. Undo management becomes much simpler.
Oracle9i Database Administrator's Guide for information about creating an undo tablespace
Automatic undo management gives you a way to explicitly control undo retention. Through the use of a system parameter (
UNDO_RETENTION), you can specify the amount of committed undo information to retain in the database. You specify the parameter as clock time (for example, 30 seconds). With retention control, you can configure your system to enable long queries to run successfully.
You can use the performance view
V$UNDOSTAT to monitor and configure your database system to achieve efficient use of undo space.
V$UNDOSTAT shows various undo/transaction statistics, such as the amount of undo space consumed in the instance.
Undo mode provides a more flexible way to migrate from manual undo management to automatic undo management. Starting with Oracle9i, Release 1 (9.0.1), a database system can run in either manual undo management mode or automatic undo management mode. In manual undo management mode, undo space is managed through rollback segments, the way it was in Oracle8i. Manual undo management mode is supported under any compatibility level. Use it when you need to run Oracle9i to take advantage of some new features, but are not yet not ready to convert to automatic undo management mode.
In automatic undo management mode, undo space is managed in undo tablespaces. To use automatic undo management mode, the DBA needs only to create an undo tablespace for each instance and set the
UNDO_MANAGEMENT initialization parameter to
AUTO. Automatic undo management mode is supported under compatibility levels of Oracle9i, Release 1 (9.0.1) or higher. Although manual undo management mode is supported, you are strongly encouraged to run in automatic undo management mode.
Oracle9i Database Administrator's Guide for descriptions of the syntax and the semantics of the DDL statements.
In automatic undo management mode, the system controls exclusively the assignment of transactions to undo segments, and controls space allocation for undo segments. An ill-behaved transaction can potentially use up much of the undo space, thus paralyzing the entire system. In manual undo management mode, you can control such possibilities by limiting the size of rollback segments with small
MAXEXTENTS values. However, you then have to explicitly assign long running transactions to larger rollback segments, using the
SET TRANSACTION USE ROLLBACK SEGMENT statement. This approach has proven to be cumbersome.
A new Resource Manager directive,
UNDO_POOL, is a more explicit way to control runaway transactions. The
UNDO_POOL directive enables DBAs to group users into consumer groups, with each group assigned a maximum undo space limit. Whenever the total undo space consumed by a group exceeds the limit, its users are not allowed to make any further updates, until undo space is freed up by other members (after their transactions commit or abort).
The default value of
UNLIMITED, where users are allowed to consume as much undo space as the undo tablespace has. DBAs have the option of limiting a particular user by using the
Long-running queries sometimes fail because undo information required for consistent read operations is no longer available. This happens because committed undo blocks are overwritten by active transactions.
Automatic undo management provides a way to explicitly control when undo space can be reused--how long undo information will be retained. A DBA can specify a retention period by using the parameter,
UNDO_RETENTION. For example, if
UNDO_RETENTION is set to 30 minutes, all committed undo information in the system will be retained for at least 30 minutes. This ensures that all queries running for thirty minutes or less will not get the OER (snapshot too old) error, under normal circumstances.
You can either set
UNDO_RETENTION at startup or change it dynamically with the
ALTER SYSTEM statement. The following example sets retention to 20 minutes:
If you do not set the
UNDO_RETENTION parameter, Oracle uses a small default value that should be adequate for most OLTP systems, where queries are not usually not very long.
In general, it is a good idea not to set retention to a value very close to what the undo tablespace can support, because that may result in excessive movement of space between undo segments. A 20% buffer of undo space is recommended.
You can monitor transaction and undo usage information with
V$ROLLSTAT. For automatic undo management, the information in V$ROLLSTAT reflects the behaviors of the automatic undo management undo segments.
Oracle9i, Release 1 (9.0.1), adds a new view,
V$UNDOSTAT. This view displays a histogram of statistical data to show how well the system is working. You can see statistics such as undo consumption rate, transaction concurrency, and lengths of queries executed in the instance. Using this view, you can better estimate the amount of undo space required for the current workload. This view is available in both the automatic undo management and manual undo management mode.
Oracle9i Database Administrator's Guide for more details about using
Each database contains one or more rollback segments. A rollback segment records the old values of data that were changed by each transaction (whether or not committed). Rollback segments are used to provide read consistency, to roll back transactions, and to recover the database.
Information in a rollback segment consists of several rollback entries. Among other information, a rollback entry includes block information (the file number 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 be found easily if necessary for transaction rollback.
Neither database users nor administrators can access or read rollback segments. Only Oracle can write to or read them. (They are owned by the user
Rollback entries change data blocks in the rollback segment, and Oracle records all changes to data blocks, including rollback entries, in the redo log. This second recording of the rollback information is very important for active transactions (not yet committed or rolled back) at the time of a 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. Once the recovery is complete, Oracle performs the actual rollbacks of transactions that had been neither committed nor rolled back at the time of the system crash.
For each rollback segment, Oracle maintains a transaction table--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 for each transaction. For every transaction, Oracle links each new change to the previous change. If you must roll back the transaction, Oracle applies the changes in a 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.
Each time a user's transaction begins, the transaction is assigned to a rollback segment in one of two ways:
SET TRANSACTION READ ONLYstatement.
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) can require a new extent to be allocated for the rollback segment.
Each rollback segment can handle a fixed 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.
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.
One transaction writes sequentially to a single rollback segment. Each transaction writes to only one extent of the rollback segment at any given time. Many active transactions can write concurrently to a single rollback segment--even the same extent of a rollback segment. However, each data block in a rollback segment's extent can contain information for only a single transaction.
When a transaction runs out of space in the current extent and needs to continue writing, Oracle finds an available extent of the same rollback segment in one of two ways:
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 information from an active transaction, 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-6 illustrates two transactions, T1 and T2, which begin writing in the third extent (E3) and continue writing to the fourth extent (E4) of 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-7, 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 the next extent. This figure shows the cyclical nature of extent use in rollback segments.
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 data from active transaction, 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
Figure 3-8 shows a new extent 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.
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.
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 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 if the extents immediately following the extent just filled are inactive, then Oracle deallocates consecutive nonactive extents from the rollback segment until the total size of the rollback segment is equal to or 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. For example:
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, create at least one additional rollback segment in the
When an Oracle 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:
SYSTEMsegment. If the instance is one of several instances accessing the database in an Oracle9i Real Application Clusters environment, it acquires the
SYSTEMrollback segment and at least one other rollback segment. If it cannot, Oracle returns an error, and the instance cannot open the database.
CEIL is a SQL function that returns the smallest integer greater than or equal to the numeric input. In the previous example, if
TRANSACTIONS equal 155 and
TRANSACTIONS_PER_ROLLBACK_SEGMENT equal 10, then the instance tries to acquire at least 16 rollback segments. (However, an instance can open the database even if the instance cannot acquire the number of rollback segments given by the division in the previous example.)
SYSTEMrollback segment, the instance next tries to acquire all private rollback segments specified by the instance's
ROLLBACK_SEGMENTSparameter. If one instance in Oracle9i Real Application Clusters opens a database and attempts to acquire a private rollback segment already claimed by another instance, the second instance trying to acquire the rollback segment receives an error during startup. An error is also returned if an instance attempts to acquire a private rollback segment that does not exist.
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.
A database used by Oracle9i Real Application Clusters can have both public and no private segments. Use of private segments is recommended.
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:
Has not been acquired (brought online) by any instance.
Has been acquired (brought online) by an instance and can contain data from active transactions.
Contains data from uncommitted transactions that cannot be rolled back (because the data files involved are inaccessible) or is corrupted.
Contains data from an in-doubt transaction (that is, an unresolved distributed transaction).
Has been dropped (The space once allocated to this rollback segment will later be used when a new rollback segment is created.)
The data dictionary table
DBA_ROLLBACK_SEGS lists the state of each rollback segment, along with other rollback information.
Figure 3-9 shows how a rollback segment moves from one state to another.
PARTLY AVAILABLE and
NEEDS RECOVERY states are very similar. A rollback segment in either state usually contains data from an unresolved transaction.
PARTLY AVAILABLErollback segment is being used by an in-doubt distributed transaction that cannot be resolved because of a network failure. A
NEEDS RECOVERYrollback segment is being used by a transaction (local or distributed) that cannot be resolved because of a local media failure, such as a missing or corrupted datafile, or is itself corrupted.
PARTLY AVAILABLErollback segment online. In contrast, you must take a
NEEDS RECOVERYrollback segment
OFFLINEbefore it can be brought online. (If you recover the database and thereby resolve the transaction, Oracle automatically changes the state of the
NEEDS RECOVERYrollback segment to
NEEDS RECOVERYrollback segment. (This lets the DBA drop corrupted segments.) A
PARTLY AVAILABLEsegment cannot be dropped. You must first resolve the in-doubt transaction, either automatically by the RECO process or manually.
If you bring a
PARTLY AVAILABLE rollback segment online (by a statement 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.
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 may need to acquire new extents for the active transactions, and therefore grow. To prevent the rollback segment from growing, a database administrator can create a new rollback segment for transactions to use until the in-doubt transaction is resolved, rather than bring the
PARTLY AVAILABLE segment online.
When a tablespace goes offline so that transactions cannot be rolled back immediately, Oracle writes to a deferred rollback segment. The deferred rollback segment contains the rollback entries that could not be applied to the tablespace, so that 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
The high water mark is the boundary between used and unused space in a segment. As requests for new free blocks that cannot be satisfied by existing free lists are received, the block to which the high water mark points becomes a used block, and the high water mark is advanced to the next block. In other words, the segment space to the left of the high water mark is used, and the space to the right of it is unused.
Figure 3-10 shows a segment consisting of three extents containing 10K, 20K, and 30K of space, respectively. The high water mark is in the middle of the second extent. Thus, the segment contains 20K of used space to the left of the high water mark, and 40K of unused space to the right of the high water mark.