Filter Null Blobs

Checking for NOT NULL Values in a BLOB Column

In this post, I will share a simple solution that we implemented to improve query performance and error handling. Recently, we reviewed and updated all the packages in our database that contained the “WHEN OTHERS THEN NULL” blocks, replacing them with more robust error-handling code. The original “WHEN OTHERS THEN NULL” blocks were causing many errors to be silently discarded, making it difficult to diagnose underlying issues. As Thomas Kyte famously said, “A WHEN OTHERS is almost always a bug unless it is immediately followed by a RAISE.” During the process of modifying these packages, we started uncovering numerous errors that had previously been ignored.

One of the errors we identified was :

This error occurred in a PL/SQL function, where NULL values were being passed as input when they shouldn’t have been. I will provide a simple test case to simulate this scenario, using the HR schema as an example.

For illustration, let’s assume we have a table that stores employees’ favorite football teams, which are stored as encrypted data using DBMS_CRYPTO in a BLOB column. The table is structured such that the column containing this encrypted data is nullable, and some records may contain NULL values.

We will search the table for rows that contain the input keyword and determine whether the data exists or not. Since the column is nullable, some rows may have NULL values, which could lead to issues if not handled correctly.

Create a sample test table:

Now, I will create a basic encryption and decryption function. To use DBMS_CRYPTO, explicit privileges are required.

Now, I will insert some data and prepare for the test.

Now, we have two records in the ENCRYPTED_DATA_TABLE. One record has a NULL value in the SECRET_DATA column, while the other contains an encrypted BLOB. The actual topic of this blog post begins here.

The goal is to search for decrypted data and return results for people who support the Besiktas football team. Since the decryption function no longer discards error messages, the query does not return an error. However, an error log message is recorded in the CAUGHT_ERRORS table, allowing us to be aware of the error.

What I initially tried was simply adding a NOT NULL condition, hoping that this would prevent the errors, since NULL blob rows will be filtered but it did not work.

Although only non-null records were queried, the DECRYPT function was executed for every row.

The simple solution I found was to add a DBMS_LOB.GET_LENGTH() > 0 condition to filter out the NULL BLOB columns. This not only resolved the issue of filtering non-null records but also improved query performance. If the length is greater than 0, it indicates that the BLOB is not NULL.

Hope it helps.


Discover More from Osman DİNÇ


Comments

Leave your comment