A reader's question: a table on a remote database stores plain text in a BLOB column (a CLOB would have been the better choice). Loading it into a staging area over a database link took 218 minutes. Converting the BLOB to VARCHAR2 pieces in a view on the source cut it to 2 minutes. But putting the pieces back together into one CLOB on the target failed with:
ORA-22992: cannot use LOB locators selected from remote tables
Here is why it happens, and the approach I recommend for Oracle 19c and 21c.
The setup
The view on the source database returns two VARCHAR2 pieces for normal rows, and a CLOB only for the large ones:
CREATE OR REPLACE VIEW owner.table_v AS
SELECT id,
date_col,
UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(blob_col, 2000, 1)) AS clob_pt1,
UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(blob_col, 2000, 2001)) AS clob_pt2,
CASE WHEN DBMS_LOB.GETLENGTH(blob_col) > 4000
THEN TO_CLOB(blob_col, NLS_CHARSET_ID('AL32UTF8'))
END AS clob_col
FROM owner.source_table;
Why 2000? DBMS_LOB.SUBSTR on a BLOB returns a RAW, and a RAW in SQL is limited to 2000 bytes (with the default MAX_STRING_SIZE=STANDARD), so two pieces cover the first 4000 bytes.
On the target, both of these work on their own:
-- Short rows: concatenate the two VARCHAR2 pieces INSERT INTO sa.testtable (id, date_col, clob_col) SELECT id, date_col, clob_pt1 || clob_pt2 FROM owner.table_v@db_lnk; -- Long rows: copy the remote CLOB straight in INSERT INTO sa.testtable (id, date_col, clob_col) SELECT id, date_col, clob_col FROM owner.table_v@db_lnk;
But this fails with ORA-22992:
INSERT INTO sa.testtable (id, date_col, clob_col)
SELECT id, date_col,
CASE WHEN clob_col IS NOT NULL
THEN clob_col
ELSE clob_pt1 || clob_pt2
END
FROM owner.table_v@db_lnk;
Why it fails
The CASE statement is not the real problem; the remote LOB is. Oracle can copy a remote LOB column as it is into a local LOB column. That is a supported distributed operation. But once the remote LOB is part of an expression evaluated on the local database, such as a CASE, a concatenation or a function call, Oracle needs the remote LOB's locator locally, and that is what ORA-22992 refuses.
- Plain copy of a remote LOB column: works.
- Remote LOB inside a local expression: can fail with ORA-22992.
- Expression evaluated fully on the remote side (for example inside the view): usually works.
Oracle 12.2 and later relaxed many remote LOB restrictions, but mixing a remote LOB with locally built values in one expression can still fail, depending on the plan. Test on your exact versions.
Also check the logic: the overflow CLOB should win when it is present, so the CASE must return clob_col when it is NOT NULL. Swapping the branches is an easy mistake.
Recommended fix: two inserts
Both halves already work separately, so keep them separate:
-- 1. Rows that fit in the two pieces (no remote LOB involved) INSERT /*+ APPEND */ INTO sa.testtable (id, date_col, clob_col) SELECT id, date_col, TO_CLOB(clob_pt1 || clob_pt2) FROM owner.table_v@db_lnk WHERE clob_col IS NULL; COMMIT; -- 2. Large rows: plain copy of the remote CLOB INSERT /*+ APPEND */ INTO sa.testtable (id, date_col, clob_col) SELECT id, date_col, clob_col FROM owner.table_v@db_lnk WHERE clob_col IS NOT NULL; COMMIT;
The COMMIT after each insert is needed: after a direct-path (APPEND) insert, the same session cannot touch that table again until it commits (ORA-12838).
Most rows go through the fast first insert; only the few large rows pay the LOB cost in the second.
A UNION ALL of the two queries in one INSERT looks neater, but Oracle may still combine both branches into one expression and raise ORA-22992 again. The two separate statements are the safer choice.
Watch the BLOB-to-text conversion
- Use the right conversion. From 12.2,
TO_CLOB(blob_col, csid)converts a BLOB to a CLOB using the character set you name, such asNLS_CHARSET_ID('AL32UTF8'). On older versions, useDBMS_LOB.CONVERTTOCLOBin PL/SQL. Find out which character set the application really used to write the BLOBs. - Multibyte characters can be split.
UTL_RAW.CAST_TO_VARCHAR2works on bytes. With UTF-8 text, a character can be cut in half at byte 2000, which gives a corrupted character in both pieces. If the text is not plain ASCII, check rows near the boundary, or use the CLOB path for all of them. - Measure length in bytes. Use
DBMS_LOB.GETLENGTH(blob_col), which returns bytes for a BLOB, to decide which rows need the CLOB path.
Better long-term options
- Convert on the source. If you control the source database, convert the BLOB to a CLOB there once, in a staging table or a column, with the correct character set. The target then does a plain CLOB copy, which works over a database link.
- Extended data types. With
MAX_STRING_SIZE=EXTENDED(12c and later), VARCHAR2 and RAW in SQL can be up to 32767 bytes, so one piece can carry much more text without a LOB. Changing this parameter cannot be undone and requires runningutl32k.sqlin upgrade mode, so plan it carefully and test it first. - Fix the data type. If the column only ever holds text, ask the application owner to change it to a CLOB. That removes the conversion problems at the root.
Why the view made it 100 times faster
Fetching LOBs over a database link is slow because each LOB value can need extra network round trips. VARCHAR2 values travel in normal row batches. Turning most LOB values into VARCHAR2 on the source side, so only the few large rows are fetched as LOBs, is what took the load from 218 minutes to 2. A SQL trace of both versions would show the difference in round trips.
More DBA scripts are in my GitHub repository: github.com/sbajwa55/dba-scripts. For help with Oracle, cloud migrations or AWS, visit Bajwa Consulting Services.

No comments:
Post a Comment