Home > Articles > Data > Oracle

A Close Look at Oracle8i Data Block Internals

  • Print
  • + Share This
Oracle expert Dan Hotka explores some internals of the Oracle RDBMS, primarily physical storage of data. This article concentrates on data blocks: how they're created, how space is really managed inside a tablespace, and some other geeky internal things of interest in the physical storage of data.

Reproduced with permission from Pinnacle Publishing. This article appeared in the Sept 1999 issue of Oracle Professional. All rights reserved.

A table segment consists of one segment header and one or more extents, and these extents are made up of one or more contiguous data blocks. The first block of each Oracle segment is the segment header. A segment is made up of at least two blocks: the segment header and the initial extent. CREATE TABLE X (C NUMBER) Storage (INITIAL 1) creates a segment with two blocks.

The block size is defined at CREATE DATABASE time and can't be changed easily. The default block size for most systems is 2KB. Oracle8 supports block sizes from 2KB to 64KB. Always choose a block size that's an even multiple of the operating system's filesystem block size. If you make the Oracle block size smaller than the operating system block size, extra data will always be read with each read operation. If the Oracle block size is not an even multiple of the operating system block size, there will be wasted space in the operating system's filesystem.

There's a distinct tradeoff between block size and performance. Larger blocks are useful for data warehouse and decision-support type systems. More data is read into memory with a single read operation and more index leaf pointers can be stored in a single read operation. If an application has lots of updates and deletes (DML), smaller blocks are useful. There will be less contention for rows because there will be fewer rows per block.

You can query the X$KVIS view to see the actual values of db_block_size.

Figure 1 illustrates the Oracle8 data block format. All Oracle blocks have similar attributes.

Figure 1

Block layout.

All blocks contain a header area consisting of a block type, a block format, relative database address (DBA), SCN, SCN sequence number, a check value, and a tail:

  • Block format refers to whether this is an Oracle7 or an Oracle8 formatted block.

  • Sequence number refers to the order of the blocks within the same SCN.

  • The check value is an optional INIT.ORA setting (DB_CHECK_SUM) that provides integrity checking at the block level.

  • The tail is used as a block consistency check to ensure that the beginning of the block and the end of the block are consistent. This number consists of the SCN + block type + SCN sequence number.

The following list shows some important block types:

Type

Description

1

Undo segment header

2

Undo segment block

5

Data segment header

7

Temporary table

11

Data file header

Several methods are used to visualize or dump the data blocks. In UNIX, use the dd command:

dd bs=2k if=/ora8/data/users01.dbf skip=200 count = 5 | od -x > pg

In the VMS world, use the DUMP command:

DUMP /BLOCKS = (START:<os block #>, END:,os block #>) 
/OUT = blockdmp.out users01.dbf

The NT world uses HEDIT. A formatted dump can be attained through Oracle via SVRMGR:

alter system dump datafile 7 block 201

Since this discussion is on physical storage, this article discusses the blocks related to physical storage, such as segment headers and data blocks. A segment header is always the first block of an object (see Figure 2). The segment header has six major areas: extent control header, map header, extent map, and three types of free lists (free lists are described in the next section).

  • The extent control header contains the number of extents allocated to this segment, the number of blocks allocated to the segment, DBA of the last block of the extent map (0 if no UNLIMITED EXTENTS is used), number of extent map blocks, high water mark (HWM) extent size, HWM extent number, and HWM block number within the extent. The high water mark (HWM) is the pointer to the DBA of the last block to receive inserts. Blocks below this mark are maintained on free lists. Blocks in extents above this mark have been allocated to the segment but haven't been used yet. Blocks above the HWM are available for SQL*Loader direct-load operations, parallel DML, and for inserts after the free list has been exhausted and the HWM has been moved.

  • The map header simply contains DBA information of the next block containing extent map information.

  • The extent map contains the beginning data block DBA and the number of blocks associated with the extent. The size of the extent map is directly related to the block size being utilized.

    Oracle7.3 introduced a concept known as unlimited extents. Instead of having the block size constrained by a limited number of extents (such as 121 for a 2KB block), Oracle allows for objects to grow and dynamically extend indefinitely. This is accomplished by chaining additional blocks to store additional extent-map information.

Figure 2

Space utilization in a segment.

There are three types of free lists that store information about available space within a segment:

  • Master free lists (MFL) contain newly allocated blocks up to the HWM and blocks freed by a committed transaction.

  • Process free lists (PFL) are user definable (storage parameter FREELISTS N) and are used to reduce contention on the master free list. This list spreads the requests over several blocks.

  • The transaction free list (TFL) holds blocks freed by a particular transaction to see whether that same transaction can use the space later. After the commit, this space is then put on the MFL. The MFL contains a flag that says whether the free list is used or not (1 or 0). The segment header also contains the DBA of the first block on the free list chain and the DBA of the last block on the free list chain. The blocks on the free list chain (FLC) contain a flag indicating that this block is linked to the free list chain and the DBA of the next block on the chain. This information is stored in the cache layer of the data block header.

Blocks below the HWM that meets the PCTFREE parameters are managed on the MFL (master free list).

Figure 3 shows how Oracle looks for free space to store a newly inserted row. Oracle searches the uncommitted TFL first. If it finds space, it uses that space; otherwise, it searches the PFL. If it finds space, it uses that space. Oracle then searches the MFL for free space. If it finds free space, it moves the space to the PFL and uses it. Oracle then searches the committed TFLs; if it finds space there, it moves the space to the MFL and begins the whole search process again. If this entire process is unsuccessful, Oracle checks whether the HWM can be advanced. If so, it adds this new space to the MFL and searches again. Otherwise, Oracle adds another extent to the object, advances the HWM, and searches the MFL yet again for space. If max-extents is hit, the max extents exceeded error is returned; otherwise, the no more space in tablespace error is returned.

Figure 3

Finding free space.

An extent is a contiguous set of blocks as defined by the storage parameter. If the extent size is indicated in bytes, Oracle rounds up the extent size to the nearest multiple of five blocks or to the nearest multiple of the MINIMUM EXTENT storage parameter value (if defined). Oracle searches the dictionary cache first, looking for an exact extent size match. If not found, it searches the fet$ for an exact or larger extent size match. If no exact match is found, Oracle repeats the two searches, looking for a larger area. If found, Oracle uses the required extent size from the larger area; if no matches are found, it coalesces the tablespace and repeats this whole process again. If no space is found, Oracle rounds down the extent size and tries the whole process again. If on the third loop of this process no space is found, Oracle either extends the tablespace (if this tablespace option is set) or returns an error. Figure 4 shows this process.

Figure 4

Dynamic extent allocation.

Adjacent unused extents are coalesced to create larger extents that may satisfy space searches more quickly. Free space can be coalesced via three methods: on demand (as needed during a search, as explained above), manually alter tablespace ... coalesce, or by SMON. SMON performs this action for the tablespaces during a space transaction and only if PCTINCREASE is greater than 0 at the tablespace level.

Data blocks also contain a cache layer, a transaction layer, and a data layer (see Figure 5). This discussion is limited to data blocks, but suffice to say that the contents of the data layer depend on the type of flag as to what kind of Oracle block this is.

Figure 5

Data block layout.

The data block contains a header, a table directory, a row directory, row data, and hopefully some free space. The block header contains the same static block information as all other blocks, discussed previously in this article.

The cache layer contains information about the number of interested transaction list (ITL) slots, a flag for data or index block, and free list information such as free list flag and next block on the free list (that is, the free list chain). The ITL is used to mark the row locked until the transaction completes. The ITL contains the transaction identifier. The ITL defaults to 1 and is maintained by storage parameters INITRANS and MAXTRANS. The number of ITLs controls how many concurrent DML operations can happen to a particular block.

The transaction layer contains the actual ITL number (or numbers as determined by INITRANS and MAXTRANS), the undo address information, a status flag, the number of rows being affected by this transaction, and free space credits. Free space credits holds any space that was freed by an update or delete until the commit actually occurs.

NOTE

ITLs do take space that would otherwise be available for rows.

The table directory tracks the tables that have rows in this data block; that is, tables involved in a cluster.

Transaction free lists (TFLs) are stored in the block as a separate structure and are not controlled by any external parameters. There are a minimum of 16 per segment, created implicitly by Oracle. Each TFL is associated with only one transaction. If the limit of TFLs is reached, the transaction waits for a TFL to free. This activity can be monitored with V$WAIT and V$TRANSACTIONS.

The row directory contains information about the rows stored in the data block.

Figure 6 illustrates the row data format. Notice that the available space in a block is between the row data and the block header information. As rows are inserted or updated, the row data part grows from the end of the block toward the front. The block header grows down from the top based on the number of ITLs, rows added, and so on. This free space is managed by a combination of PCTUSED and PCTFREE storage clause parameters. PCTUSED determines when a block is no longer available for inserted rows. PCTFREE determines when a block is again available to have inserted rows.

TIP

The combination of PCTFREE and PCTUSED should be 75.

NOTE

PCTFREE always defaults to at least 10%, even if set lower than 10%.

Figure 6

Row layout.

Row data is added from the bottom of the free space in a block toward the top. The static part of the row format contains a row flag, a lock byte (ITL entry locking this row), number of columns in this row, and a cluster key indicator. Then there's a column length (1 byte if length is < 254, otherwise 3 bytes) and the actual column data. Trailing null value fields are not stored. When reading a dump, the next row would be indicated by the presence of another row flag.

The following table shows some values for row flags:

Value

Description

H

Head of row piece

K

Cluster key

D

Deleted row

F

First data piece

L

Last data piece

P

First column continued from previous location

Row chaining (see Figure 7) occurs when a row is either inserted or updated and is physically longer than the available block size. Row migration (see Figure 8) occurs when an updated row no longer fits into the existing block. In my experience, Oracle migrates and/or chains rows based on a variety of conditions.

Figure 7

Chained row layout.

Figure 8

Migrated row layout.

The truncate command is the quickest way to delete all the rows from a table. This method of clearing out a table has no recovery. It's fast because it simply sets HWM, MFL, and TFLs to null. If the REUSE option isn't specified (default behavior), all the extents except for MINEXTENTS are then released to the tablespace. Full table scans scan through all the blocks until reaching the HWM. If objects are loaded with many rows, most of the rows are deleted; the table should be reorganized to move the HWM where it belongs, rather than at the end of many empty or almost-empty blocks.

Direct-path loading is a very fast way of loading data into an Oracle database. It completely circumvents the SGA, read-consistent features, and so on. Direct-path loading happens above the HWM. This method of loading data begins by putting a shared lock on the object being loaded. A temporary segment is then created, based on the extent size of the object being loaded. The tablespace must have enough room to accommodate an extent size of the initial and next extent of the original table. This temporary segment is populated with the rows, the extents are added to the original table, and the HWM is moved to the end of these new extents.

SQL*Loader and some parallel operations utilize direct-path loading.

Temporary segments are needed when Oracle is unable to sort in memory. Create index or select statements that have an order by, distinct, group by, or union clause cause a sort operation. The INIT.ORA parameter, sort_area_size, defines the amount of memory dedicated for sort operations. When this area is exhausted, Oracle utilizes temporary segments to do the sort operation. There are two types of temporary storage: permanent tablespace (the tablespace as we know it) and temporary tablespaces. These are easily created by adding the TEMPORARY clause in the CREATE TABLESPACE command. Tablespaces can be exchanged with the ALTER TABLESPACE command. The basic difference in the two is that SMON cleans up unused temporary segments in a permanent tablespace where the segments are left alone after a sort operation in a temporary tablespace. Also, only temporary segments can exist in a temporary tablespace. This allows the space requirements of sort operations to grow and be maintained throughout the life of the instance.

Monitor this sort area via V$SORT_SEGMENT and V$SORT_USAGE.

NOTE

Use a consistent value for initial and next extent size with PCTINCREASE = 0. Make the extent size a multiple of sort_area_size. Add one db_block size to allow for the segment header in the initial extent.

About the Author

Dan Hotka is a director of database field operations for Quest Software. He has more than 22 years in the computer industry and more than 17 years of experience with Oracle products. He is an acknowledged Oracle expert, with Oracle experience dating back to the Oracle V4.0 days. He has just completed Oracle8i from Scratch (Que, 2000, ISBN 0789723697) and coauthored the popular books Oracle Unleashed (SAMS, 1996, 067230872X), Oracle8 Server Unleashed (SAMS, 1998, ISBN 0672312077), Oracle Development Unleashed, Third Edition (SAMS, 2000, ISBN 0672315750), and Special Edition Using Oracle8/8i (Que, 2000, ISBN 0789719754). Dan is frequently published in Oracle Professional, the monthly trade journal by Pinnacle Publications, and regularly speaks at Oracle conferences and usergroups around the world. Dan can be reached at dhotka@earthlink.net or dhotka@quest.com.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.