Managing Tablespaces   «Prev  Next»
Lesson 6 Coalescing free space
ObjectiveCoalesce adjoining free extents.

Coalescing free space

As an Oracle Database Administrator (DBA), managing space efficiently is crucial for optimal database performance and storage utilization. Coalescing free space and adjoining free extents in an Oracle database are essential tasks in this regard. Here is how you can approach these tasks:

Coalescing Free Space

  1. Analyze Tablespace Usage:
    • Begin by analyzing the tablespaces for fragmentation. Use Oracle's data dictionary views such as `DBA_FREE_SPACE` to identify tablespaces with significant amounts of free space that can be coalesced.
  2. Use the `ALTER TABLESPACE` Command:
    • Oracle provides an `ALTER TABLESPACE` command with a `COALESCE` option. This command can be used to merge contiguous free extents into larger free extents within the same tablespace.
    • Syntax: `ALTER TABLESPACE tablespace_name COALESCE;`
    • This operation is beneficial in situations where you have many small free extents that can be combined into fewer larger ones, thus optimizing space usage.
  3. Consideration for Locally Managed Tablespaces:
    • If the tablespaces are locally managed (which is often the recommended approach), Oracle automatically manages free space and coalescing operations. Therefore, manual coalescing may not be necessary.

Coalescing Adjoining Free Extents

  1. Identify Segments with Fragmented Extents:
    • Use Oracle's data dictionary views such as `DBA_SEGMENTS` and `DBA_EXTENTS` to identify segments with fragmented extents. These segments may benefit from coalescing.
  2. Reorganizing Tables and Indexes:
    • You can reorganize tables and indexes to coalesce adjoining free extents. Use the `ALTER TABLE` or `ALTER INDEX` command with the `REBUILD` or `MOVE` clause.
    • Syntax for tables: `ALTER TABLE table_name MOVE TABLESPACE tablespace_name;`
    • Syntax for indexes: `ALTER INDEX index_name REBUILD TABLESPACE tablespace_name;`
    • These commands move the segments to new extents, thereby coalescing the free space.
  3. Online Segment Shrink:
    • For tables that support row movement, use the `ALTER TABLE` command with the `SHRINK SPACE` clause to reclaim space and coalesce extents without locking the table.
    • Syntax: `ALTER TABLE table_name ENABLE ROW MOVEMENT; ALTER TABLE table_name SHRINK SPACE;`
    • This method is beneficial for tables with significant free space due to deletes or updates.
  4. Use Automatic Segment Space Management (ASSM):
    • ASSM automates the management of free space within segments, reducing the need for manual coalescing. If not already using ASSM, consider migrating to tablespaces with ASSM enabled.

Best Practices and Considerations

  • Backup Before Reorganization: Always perform a backup before reorganizing large tables or indexes.
  • Monitor Performance: After coalescing, monitor the database performance to ensure that the operations have positively impacted the system.
  • Schedule During Low Activity Periods: Plan such maintenance activities during periods of low database activity to minimize impact on users.

In conclusion, coalescing free space and adjoining free extents in an Oracle database involves a combination of using built-in database commands and strategies for segment reorganization. By carefully analyzing the database and employing these techniques, you can optimize space usage and improve database performance.

Create Oracle Tablespace

When you first create a tablespace, all the free space within that tablespace is in one large, contiguous chunk. Over time, as objects get created and dropped in a tablespace, the free space can become fragmented into small pieces that are not large enough to be useful. The following Slide Show illustrates the potential problem:
When you first create a tablespace, all of the space is one contiguous chunk
1) When you first create a tablespace, all of the space is one contiguous chunk

As objects are created and dropped over time, free space will become fragmented into small chunks
2) As objects are created and dropped over time, free space will become fragmented into small chunks

In this example, even though free space appears adequate, a large extent cannot be allocated because no free extent is large enough
3) In this example, even though free space appears adequate, a large extent cannot be allocated because no free extent is large enough

Coalescing the free space caused adjacent free extents to be joined together. Now, there is a 
free extent large enough to allocated space for our new extent.
4) Coalescing the free space caused adjacent free extents to be joined together. Now, there is a free extent large enough to allocated space for our new extent.


Coalescing free space within Tablespace

Coalescing free space within a tablespace will happen automatically if the tablespace's PCTINCREASE setting is greater than 0. In that case, the SMON process will periodically coalesce free space for you. Otherwise, you have to initiate the process manually, using the command below: ALTER TABLESPACE tablespace_name COALESCE; Even if PCTINCREASE is not zero, you may still need to coalesce free space manually if you need it done immediately. If you are creating an object with a large initial extent and you want to verify that there is enough space for that extent, you can query the dba_free_space view. The following query displays the largest extent available in the USERS tablespace:

SELECT MAX(bytes)
FROM dba_free_space
WHERE tablespace_name='USERS';

If the largest free extent is smaller than what you need, you can try coalescing the free space. If the largest free extent is still not large enough, then you must add space to the tablespace. In the next lesson, you will learn how to drop a tablespace.

Coalescing Freespace - Quiz

Click the Quiz link below to test your knowledge of allocating file space.
Coalescing Freespace - Quiz