Skip to content

BLOB and CLOB types

View as Markdown

BLOB (binary large object) and CLOB (character large object) store large values such as documents and images. CoraDB keeps LOB data outside the database volumes, in files on the database server's file system, and stores only a reference to the file, the LOB locator, in the row.

Type Holds SQL value form Maximum size
BLOB Binary data Bit string The largest file the file system allows
CLOB Character data Character string The largest file the file system allows
  • LOB files are named {table_name}_{unique_name}, where unique_name is generated by the server, so the LOB files of many tables can share one directory.
  • The files are created, replaced and deleted by CoraDB together with the rows that reference them. Do not change them with other tools; CoraDB cannot keep them consistent with the database if you do.
  • Converting a LOB value to a bit or character string (and the reverse) is limited to 1 GB.

BLOB and CLOB columns are created with CREATE TABLE or added with ALTER TABLE, like any other column. The following table stores a manual and an image for products of the e-commerce sample database:

CREATE TABLE product_doc (
product_id INT PRIMARY KEY,
manual CLOB,
image BLOB
);

LOB columns have these restrictions:

Not allowed on a LOB column Error
An index, or a PRIMARY KEY, FOREIGN KEY or UNIQUE constraint Cannot define index on domain "clob".
NOT NULL Set NOT NULL on blob typed column is not allowed.
A DEFAULT other than NULL, or SHARED Set DEFAULT or SHARED on blob typed column is not allowed.
Use as an element of a collection type

A string or bit string assigned to a LOB column is converted automatically. The functions CHAR_TO_CLOB and BIT_TO_BLOB make the conversion explicit.

INSERT INTO product_doc VALUES (1618, CHAR_TO_CLOB('Gobi HeatTec Tee by Diadora. Breathable mesh.'), BIT_TO_BLOB(X'89504E47'));
INSERT INTO product_doc VALUES (1619, CHAR_TO_CLOB('Hero Hoodie. Fleece lining, kangaroo pocket.'), NULL);
INSERT INTO product_doc VALUES (1620, 'Plain string into CLOB', X'FFD8');
-- copy a LOB value from another row
INSERT INTO product_doc (product_id, manual) SELECT 1621, manual FROM product_doc WHERE product_id = 1619;
UPDATE product_doc SET manual = NULL WHERE product_id = 1621;
UPDATE product_doc SET image = (SELECT image FROM product_doc WHERE product_id = 1618) WHERE product_id = 1619;
DELETE FROM product_doc WHERE product_id = 1620;

What happens to the files:

  • INSERT of a non-NULL value creates a new file and stores its locator in the row. Copying a LOB value from another row creates a copy of the file.
  • UPDATE to a non-NULL value deletes the old file, if any, and creates a new one. UPDATE to NULL deletes the old file and stores NULL.
  • DELETE of a row deletes its LOB files, also when the row is deleted by a cascading foreign key.
  • DROP TABLE and ALTER TABLE … DROP COLUMN remove only the locators; the LOB files themselves are not deleted.

To load a file that already exists on the server, use CLOB_FROM_FILE or BLOB_FROM_FILE.

Convert a LOB value to a string or bit string with CLOB_TO_CHAR, BLOB_TO_BIT or CAST to work with its contents. CLOB_LENGTH and BLOB_LENGTH return the size without reading the data.

SELECT product_id,
CLOB_TO_CHAR(manual) AS manual_text,
CLOB_LENGTH(manual) AS chars,
BLOB_LENGTH(image) AS bytes,
BLOB_TO_BIT(image) AS bits,
image IS NULL AS no_image
FROM product_doc
ORDER BY product_id;
product_id manual_text chars bytes bits no_image
1618 Gobi HeatTec Tee by Diadora. Breathable mesh. 45 4 0x89504e47 0
1619 Hero Hoodie. Fleece lining, kangaroo pocket. 44 NULL NULL 1
1620 Plain string into CLOB 22 2 0xffd8 0
1621 Hero Hoodie. Fleece lining, kangaroo pocket. 44 NULL NULL 1

(Values right after the four INSERT statements above; bit strings as drivers return them, in hexadecimal.)

  • Drivers return the data of a LOB column. corasql shows the locator (a file: path) instead; use CLOB_TO_CHAR to see the text.
  • LIKE works on a CLOB column directly. Other string functions need CLOB_TO_CHAR first.
  • A LOB column cannot be used in ORDER BY or GROUP BY (product_doc.manual can not be an ORDER BY column), nor with comparison operators, IN or NOT IN ('=' operator is not defined on types clob and char). Convert it first.
  • IS NULL checks whether the column has a locator. A NULL LOB value has no file.
  • If a LOB file is removed outside CoraDB, the locator points to nothing and the LOB functions return NULL for it.
SELECT product_id FROM product_doc WHERE manual LIKE '%Hoodie%'; -- 1619, 1621
SELECT product_id, SUBSTRING(CLOB_TO_CHAR(manual), 1, 8) AS head
FROM product_doc
ORDER BY CLOB_TO_CHAR(manual);

CAST converts between LOB types and bit or character strings:

CAST (bit_or_char_value AS { BLOB | CLOB })
CAST (clob_value AS { CHAR | VARCHAR })
CAST (blob_value AS { BIT | BIT VARYING })

The LOB functions are described in LOB functions:

Function Purpose
CLOB_TO_CHAR CLOB to character string
BLOB_TO_BIT BLOB to bit string
CHAR_TO_CLOB Character string to CLOB
BIT_TO_BLOB Bit string to BLOB
CHAR_TO_BLOB Character string to BLOB (its bytes)
CLOB_FROM_FILE CLOB from a file on the server
BLOB_FROM_FILE BLOB from a file on the server
CLOB_LENGTH Length of a CLOB in characters
BLOB_LENGTH Length of a BLOB in bytes

LOB files are stored on the local file system of the database server, in the LOB base path of the database:

  • By default, the lob directory under the directory of the database volumes.
  • Another directory if you give one with --lob-base-path (-B) when you create the database; CoraDB creates the directory if needed. See createdb.
# LOB files in <volume directory>/lob
coradb createdb image_db en_US
# LOB files in /data/lob
coradb createdb --lob-base-path="file:/data/lob" image_db en_US

coradb spacedb shows the LOB base path in its last line (LOB space description file:/data/lob).

To move new LOB files to another directory, change the lob-base-path column of the database in databases.txt and restart the database server. LOB files already stored in the old directory remain readable.

  • Backup and restore cover the locators but not the LOB files. Back up the LOB directory separately, at the same point in time as the database.
  • copydb copies LOB files only when asked to (--copy-lob-path, or -B for a new path); otherwise update databases.txt for the copy yourself.
  • If the LOB base path registered in databases.txt is removed, the server and the utilities cannot work with the database.

Changes to LOB values are transactional: COMMIT and ROLLBACK keep each locator and its file consistent.

-- with autocommit off
INSERT INTO product_doc VALUES (2001, CHAR_TO_CLOB('Version 1'), NULL);
COMMIT;
UPDATE product_doc SET manual = CHAR_TO_CLOB('Version 2') WHERE product_id = 2001;
ROLLBACK;
SELECT CLOB_TO_CHAR(manual) FROM product_doc WHERE product_id = 2001; -- 'Version 1'

Keep in mind that the files themselves are outside the log:

  • If the server stops in the middle of a transaction that changed a LOB value, recovery rolls back the locator, which can then point to a file that no longer holds the old data. Reading it raises an error.
  • A driver reads LOB data when a row is fetched, not when the query runs. If another transaction updates the LOB value in between, the fetch can fail because the locator no longer matches its file.
  • After a point-in-time restore, locators and LOB files can be out of step for the same reason.