Large Objects   «Prev  Next»

Lesson 7 Temporary LOBs
ObjectiveUnderstand how and why to use temporary LOBs

How and why to use Temporary LOBs

Temporary LOBs (Large Objects) in Oracle are used to handle large data, such as text documents, images, or any other large content, within PL/SQL and SQL operations. Unlike permanent LOBs, temporary LOBs are session-specific and automatically cleaned up when the session ends, making them ideal for intermediate data storage and manipulation. Here’s an overview of why and how to use them. Why Use Temporary LOBs
  1. Efficient Memory Management: Temporary LOBs exist only for the duration of a session or transaction, which reduces the need to store large data permanently, thereby improving performance and memory management.
  2. Session-Specific: They are private to the session, so they provide a secure space for operations within a transaction without affecting other sessions.
  3. Ideal for Intermediate Data: They’re useful for holding temporary large data, such as processing intermediate stages of images, documents, or large text strings.
  4. Automatic Cleanup: Oracle automatically frees up temporary LOBs when they go out of scope or the session ends, which helps avoid memory leaks.

How to Use Temporary LOBs Using temporary LOBs in PL/SQL requires several steps, including creating, manipulating, and freeing the LOB. Here’s a basic workflow for handling temporary LOBs:
  1. Declare the LOB Variable: Use a BLOB or CLOB variable in PL/SQL to hold the temporary LOB data.
  2. Initialize the Temporary LOB:
    • Use the DBMS_LOB.CREATETEMPORARY procedure to create a temporary LOB instance. This sets up the LOB for use in the session.
    • DECLARE
          temp_clob CLOB;
      BEGIN
          DBMS_LOB.CREATETEMPORARY(temp_clob, TRUE); -- TRUE means it's a session-based LOB, FALSE would mean transaction-based
                      
  3. Write or Manipulate Data in the LOB:
    • Use DBMS_LOB.WRITE, DBMS_LOB.WRITEAPPEND, or SQL functions to insert or modify data in the temporary LOB.
    • DBMS_LOB.WRITE(temp_clob, LENGTH('Hello, World!'), 1, 'Hello, World!');
                      
  4. Perform Operations on the LOB:
    • You can use temporary LOBs to perform operations like concatenation, substring extraction, or pattern matching, depending on your needs.
  5. Free the Temporary LOB:
    • Once you’re done with the temporary LOB, use DBMS_LOB.FREETEMPORARY to release the memory explicitly. Although Oracle will automatically clean up the LOB at the end of the session, it’s a good practice to free it when you’re done to manage memory effectively.
    • DBMS_LOB.FREETEMPORARY(temp_clob);
      END;
                      
  6. Example Code:
    • Here’s a complete example that initializes a temporary CLOB, writes to it, reads from it, and then frees it:
    • DECLARE
          temp_clob CLOB;
          temp_data VARCHAR2(100);
      BEGIN
          -- Create a temporary CLOB
          DBMS_LOB.CREATETEMPORARY(temp_clob, TRUE);
          
          -- Write data to the temporary CLOB
          DBMS_LOB.WRITE(temp_clob, LENGTH('This is some temporary text data'), 1, 'This is some temporary text data');
          
          -- Read a portion of the CLOB into a VARCHAR2 variable
          DBMS_LOB.READ(temp_clob, 20, 1, temp_data);
          DBMS_OUTPUT.PUT_LINE('Data from CLOB: ' || temp_data);
          
          -- Free the temporary CLOB
          DBMS_LOB.FREETEMPORARY(temp_clob);
      END;
                      
Important Notes:
  • Storage: Temporary LOBs are stored in temporary tablespaces, so make sure there is enough space available in your database’s temporary tablespace.
  • Automatic Cleanup: Temporary LOBs are automatically cleaned up at the end of the session, but it’s best practice to free them explicitly when they’re no longer needed.

Using temporary LOBs effectively allows Oracle applications to manage large data objects efficiently without permanently consuming storage, making them ideal for scenarios requiring large data manipulations in PL/SQL.
A temporary LOB is a way to store a LOB in a temporary tablespace. These temporary LOBs, by default, last only for the duration of a session. You would typically use a temporary LOB when you are performing a number of operations on a LOB (such as morphing an image) and you want to keep a complete copy of the LOB temporarily.

How to use temporary LOB

You create and manipulate temporary LOBs with procedures from the DBMS_LOB package. These procedures are as follows:
Procedure Description
DBMS_LOBCREATETEMPORARYThis procedure creates a temporary LOB. You can specify whether you want a temporary LOB to last for the duration of the session or the duration of the particular call.
code>DBMS_LOB.FREETEMPORARY This procedure frees the space used by a temporary LOB and invalidates the LOB locator for the temporary LOB.
DBMS_LOB.ISTEMPORARY This procedure checks to see if a particular LOB is a temporary or permanent LOB.

When you create a temporary LOB, it is empty. You can use the DBMS_LOB.COPY() function to move the contents of a permanent LOB into a temporary LOB. The specific temporary LOB operations are illustrated in the following series of images.

Temporary LOBs

The CREATETEMPORARY procedure creates a LOB locator for a temporary LOB.
1) The CREATETEMPORARY procedure creates a LOB locator for a temporary LOB.
  1. Text Content:
    • Top Text:
      DBMS_LOB.CREATETEMPORARY(dest_loc, TRUE, DBMS_LOB.SESSION)
    • Text on Orange Background:
      Default temporary tablespace
    • Bottom Text:
      The CREATETEMPORARY procedure creates a LOB locator for a temporary LOB.
  2. Relevant Features:
    • The image appears to demonstrate the usage of the DBMS_LOB.CREATETEMPORARY procedure in Oracle, which is used to create a temporary LOB (Large Object) locator within a session.
    • A green rectangle (likely representing a temporary LOB segment or object) is shown inside an orange background labeled "Default temporary tablespace," indicating that the temporary LOB is stored in the default temporary tablespace.
    • An arrow pointing from the procedure call to the green rectangle visually links the action of creating a temporary LOB with its allocation in the tablespace.
    • The layout and color scheme help in understanding how the CREATETEMPORARY procedure associates a LOB with the default temporary tablespace in Oracle databases.

This visualization effectively illustrates how temporary LOBs are managed within the database's temporary tablespace by using the `DBMS_LOB.CREATETEMPORARY` procedure.


The FREETEMPORARY procedure frees the space in memory that had been occupied by the data in the temporary LOB.
2) The FREETEMPORARY procedure frees the space in memory that had been occupied by the data in the temporary LOB. The LOB locator becomes invalid.

The ISTEMPORARY procedure lets you know if a LOB locator points to a temporary LOB
3) The ISTEMPORARY procedure lets you know if a LOB locator points to a temporary LOB


You can also use most of the manipulation functions in the DBMS_LOB package on temporary LOBs.
Once a temporary LOB is created, you can use it as a value in a WHERE clause for UPDATE, INSERT, and DELETE statements, or as a location to SELECT INTO. If you select into a temporary LOB from a permanent LOB, the LOB locator for the permanent LOB overwrites the temporary LOB locator, rather than copying the value of the permanent LOB into the temporary LOB.
In the next lesson, you will learn about buffering options for LOBs.

SEMrush Software