Working with Large Objects: Mastering DBMS_LOB
VARCHAR2 stops at 32KB. When you need to store gigabytes of raw JSON, XML, or binary image data, you must turn to LOBs. Learn how to manipulate them efficiently using the DBMS_LOB package.
The 32KB Limit
In PL/SQL, a VARCHAR2 variable maxes out at 32,767 bytes (and in a SQL table column, it maxes out at 4,000 bytes unless extended string sizes are enabled).
If you are integrating with an external API that returns a 50MB JSON payload, or you need to store high-res product images for an e-commerce platform, VARCHAR2 fails.
You must use LOBs (Large Objects), which can store up to 4 Terabytes (or more, depending on block size) in a single row.
LOB Locators vs Values
When you query a VARCHAR2, Oracle returns the actual string value. When you query a CLOB or BLOB, Oracle does not return the 4 Terabyte value (which would instantly crash your application's memory).
Instead, Oracle returns a LOB Locator. A locator is essentially a pointer to the physical location of the LOB data on disk. You use this locator, in conjunction with the DBMS_LOB package, to read or write the data in manageable chunks.
Reading and Writing with DBMS_LOB
Because LOBs are so massive, you generally process them iteratively.
Creating and Writing to a Temporary CLOB
If you need to build a massive string in memory (like generating a giant XML file), you create a Temporary LOB.
Reading from a CLOB
To read a CLOB, you read it in chunks (e.g., 8000 bytes at a time) using DBMS_LOB.READ.
Searching Inside a CLOB
You cannot easily use standard LIKE operators on massive CLOBs efficiently. Instead, use DBMS_LOB.INSTR to find the exact character position of a substring.
Common Gotchas
Important Gotchas
- !
If you call
DBMS_LOB.CREATETEMPORARYin a loop and forget to callDBMS_LOB.FREETEMPORARY, you will exhaust the database's PGA memory and crash the Oracle instance. Always clean up temporary LOBs, ideally in theEXCEPTIONblock as well. - !
You can use the
||operator to concatenate aVARCHAR2to aCLOB. However, it creates an implicit temporary LOB under the hood every single time it executes. In a loop, this is incredibly slow. Always useDBMS_LOB.WRITEAPPENDfor loops.
Key Takeaways
Key Takeaways
- Use CLOBs for text > 32KB and BLOBs for raw binary data.
- Oracle variables hold LOB Locators (pointers), not the full LOB value.
- Use
DBMS_LOB.WRITEAPPENDandDBMS_LOB.READto process data in manageable chunks. - Always free temporary LOBs to prevent catastrophic memory leaks.


