| Lesson 5 | Managing LOB size |
| Objective | Use LOB commands to adjust the storage size of LOBs in Oracle 23ai |
DBMS_LOB procedures that change the size (or apparent size)
of LOB values in-place.
When you create or alter a table, the LOB storage clause controls the physical characteristics of a LOB segment. These settings have a direct impact on space usage, redo/undo behavior, and the amount of data Oracle moves per I/O.
Oracle can store small LOB values inside the table row (inline) and move larger values to a dedicated LOB segment (out-of-line). The main knob is:
ENABLE STORAGE IN ROW — allow inline storage when the value is small enoughDISABLE STORAGE IN ROW — force out-of-line storageCREATE TABLE t_lobs (
id NUMBER PRIMARY KEY,
notes CLOB
)
LOB (notes) STORE AS SECUREFILE (
ENABLE STORAGE IN ROW
);
The important tuning idea: inline LOBs can reduce pointer chasing for small values, but they also make rows larger. Larger rows increase logical I/O for full scans and can increase buffer cache pressure.
CHUNK defines the allocation and I/O unit used by the LOB segment (the size of pieces Oracle reads/writes).
For write-heavy workloads, chunk size influences how efficiently Oracle can stream changes.
ALTER TABLE t_lobs
MODIFY LOB (notes)
(CHUNK 8192);
LOB read consistency can require older versions of LOB data. Historically that was managed with PCTVERSION.
Modern guidance is to use RETENTION (including RETENTION AUTO) so Oracle can make smarter decisions
based on undo and workload behavior.
ALTER TABLE t_lobs
MODIFY LOB (notes)
(RETENTION AUTO);
Practical takeaway: when LOB churn is high (frequent updates to LOB content), retention settings and undo pressure become part of “LOB size management,” because older versions consume space until they can be released.
LOB caching determines whether LOB data is kept in the database buffer cache. This matters when LOBs are read often and are small enough to benefit from caching, or when caching them would evict more valuable blocks.
ALTER TABLE t_lobs
MODIFY LOB (notes)
(NOCACHE);
In Oracle’s modern LOB implementation (SecureFiles), you can trade CPU for reduced storage via compression and deduplication, and you can protect data at rest with encryption. These can materially change LOB storage size.
ALTER TABLE t_lobs
MODIFY LOB (notes)
(STORE AS SECUREFILE (
DEDUPLICATE
COMPRESS HIGH
ENCRYPT
));
Size management point: compression and deduplication reduce disk usage, but can add CPU and may not benefit already-compressed payloads (e.g., JPEG images). Always validate on representative data.
A classic best practice is to isolate LOB segments into their own tablespace for capacity planning and I/O management. This doesn’t shrink a LOB by itself, but it makes growth predictable and reduces fragmentation side effects on the base table.
ALTER TABLE t_lobs
MODIFY LOB (notes)
(TABLESPACE lob_tbs);
DBMS_LOB package provides APIs to copy data between LOBs, append content, trim length, and erase regions.
These operations are the “hands-on” portion of LOB size management.
| Procedure | Why it matters for size | Key behavior |
DBMS_LOB.COPY |
Build or rebuild a LOB by copying only the needed portion | Supports offsets and byte counts; can overwrite a region in a destination LOB |
DBMS_LOB.APPEND |
Grow a LOB incrementally (log bodies, report assembly, document composition) | Concatenates at the end; does not replace existing content |
DBMS_LOB.TRIM |
Reduce the length of a LOB value (true shrink of the value) | Changes the LOB length to the new value (must be ≤ current length) |
DBMS_LOB.ERASE |
Clear bytes from a region without changing LOB length | Erases content but generally does not reduce length; space reclamation is different from TRIM |
The next sections align with the figures on this page so the narrative stays continuous: COPY, APPEND, TRIM, then ERASE and the “space is not reclaimed” nuance.
DBMS_LOB.COPY is often used during controlled migrations, normalization routines, or when you want to rebuild a LOB
while skipping unwanted regions. Because you specify offsets and the number of bytes, COPY is a precise tool.
DBMS_LOB.COPY(
dest_lob, -- destination locator
src_lob, -- source locator
amount, -- number of bytes/chars to copy
dest_offset, -- start position in destination
src_offset -- start position in source
);
DBMS_LOB.COPY, a specified byte range is copied from src_lob into dest_lob.
This supports partial copying (using offsets) so you can rebuild or reshape a LOB without copying everything.
DBMS_LOB.APPEND is a common pattern for assembling a large CLOB/BLOB in steps. Conceptually, this is “dest := dest || src”. From a size perspective, APPEND is a deliberate growth mechanism.
DBMS_LOB.APPEND(dest_lob, src_lob);
DBMS_LOB.APPEND, the content in src_lob is appended to the end of dest_lob.
Existing data in dest_lob remains intact; the LOB becomes larger by the length of the appended content.
DBMS_LOB.TRIM is the canonical “make it smaller” operation. If you need a hard maximum size, or you want to remove
trailing content permanently, TRIM changes the LOB length itself.
DBMS_LOB.TRIM(lob_locator, newlen);
DBMS_LOB.TRIM, the length of the LOB is reduced to the specified number of bytes.
This is a true length reduction (unlike ERASE).
DBMS_LOB.ERASE removes data from a region inside the LOB, but it does not behave like TRIM.
The key concept (and the reason this belongs in a “size management” lesson) is that ERASE can leave the LOB at the
same length while clearing content inside it.
DBMS_LOB.ERASE(lob_loc, amount, offset);
DBMS_LOB.ERASE, bytes are erased from within the LOB, but the LOB length is not reduced.
Use TRIM when you need to reduce length; use ERASE when you need to clear a region without changing length.
In real systems, “LOB size management” usually means combining DDL storage choices with a small set of safe, repeatable LOB manipulation patterns:
In the next lesson, you will learn how Oracle uses LOBs to store large arrays (for example, VARRAYs stored as LOBs), which inherits many of the same storage and sizing principles.