r/DB2 • u/DazzlingAd4254 • 20d ago
Unable to delete a UDF
There was this user-defined function called `notnull' in a module `util' which was created as:
alter module util publish FUNCTION NOTNULL(str varchar(4096)) SPECIF notnull RETURNS VARCHAR(4096);
alter module util add FUNCTION NOTNULL(str varchar(4096)) RETURNS VARCHAR(4096) DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN <a string from some string operation>.
For years it has been working as intended. Then, along came I to add blob support:
alter module util publish FUNCTION NOTNULL(data blob) specific notnull_blob RETURNS VARCHAR(4096);
Now, instead of doing:
alter module util add FUNCTION NOTNULL(data blob) specific notnull_blob RETURNS VARCHAR(4096) DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN <a string from some blob operation>
I mistakenly did on the dev. server (Db2 12.1.2 LUW):
alter module util add FUNCTION NOTNULL(data *varchar(4096)*) specific notnull_blob RETURNS VARCHAR(4096) DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN <a string from some blob operation>.
Not noticing any error, I repeated the above on the Testing server (Db2 12.1.3 LUW)!
Now, whether I call the function with a varchar parameter or blob parameter, I get <a string from some blob operation>:
select util.notnull('test') from sysibm.sysdummy1
=> the_expected_blob_operation_result
Therefore, I want to delete the function but deleting it fails:
alter module util DROP FUNCTION notnull;
=> SQL0901N The SQL statement or command failed because of a database system
error. (Reason "ObjectId 1144709497 for object type PSMROUTINE does not
exist.".) LINE NUMBER=1. SQLSTATE=58004
alter module TANOCAT.util DROP specific FUNCTION notnull_blob
=> SQL0901N The SQL statement or command failed because of a database system
error. (Reason "ObjectId 1144709497 for object type PSMROUTINE does not
exist.".) LINE NUMBER=1. SQLSTATE=58004
None of the `googled' solutions seem to work:
- Rebind the problematic package
user db2inst1 rebound all packages:
db2rbind theDbName -l db2rbind.log all
8 packages failed to rebind but none of them are from my schema; they are from another user's schema and from schema SYSIBMADM.
- Drop and Recreate the Affected Routine
Well, that is what is failing.
- Refresh the Catalog Statistics & Clear Plan Cache
user db2inst1 ran 'RUNSTATS ON TABLE SYSIBM.SYSROUTINES and detailed indexes all', then 'FLUSH OPTIMIZATION PROFILE CACHE' and finally 'FLUSH PACKAGE CACHE DYNAMIC'. Still did't help.
Any help would be greatly appreciated.
EDIT: included Db2 versions
3
u/anibjoshi 20d ago
Hi, I asked your question to Db2 Genius AI. Here's the response -
Root cause
Catalog corruption in module function metadata — you accidentally created a duplicate function signature (
notnull_blobspecific name pointing toVARCHAR(4096)parameter instead ofBLOB), creating inconsistent entries acrossSYSCAT.ROUTINES,SYSCAT.MODULEOBJECTS, and internal PSMROUTINE structures. When Db2 tries to drop the function, it finds mismatched object IDs between different catalog tables (orphan rows), triggering SQL0901N.Confidence
High — SQL0901N with "ObjectId does not exist for PSMROUTINE" is a classic orphan catalog row symptom; your accidental duplicate signature with the same specific name created the inconsistency.
Evidence
notnullfunction signatures now exist with conflicting definitions (both targeting VARCHAR(4096) parameters)Resolution
Option A: db2cat orphan row cleanup (Recommended — IBM-supported catalog repair)
This is the official IBM approach for cleaning orphaned catalog entries:
1. Back up your database first:
2. Detect orphan rows:
Look for output mentioning
SYSCAT.ROUTINES,SYSCAT.MODULEOBJECTS, or entries related toUTIL.NOTNULL.3. Contact IBM Support to obtain a service password (required for catalog cleanup operations). Mention:
db2cat -cl4. Clean up orphan rows:
When prompted
Do you wish to proceed committing the changes made by db2cleancat (y/n)?, typey.5. Verify the module state:
6. If the orphaned function entries are removed, recreate the BLOB overload correctly:
Option B: Drop and recreate the entire module (Nuclear option)
If
db2cat -cldoes not resolve the issue or you cannot wait for IBM Support:1. Export the working function definition:
2. Drop the entire module (this will likely fail, but try):
3. If DROP MODULE also fails with SQL0901N, you'll need IBM Support to force-remove the module entry from system catalogs using specialized tools (similar to the "How to force drop the table" procedure documented in SMAP — requires service password and specialized tooling not available to end users).
4. Once removed, recreate the module from scratch:
Prevention
Next step: Start with
db2cat -d <dbname> -chon both dev and test servers to see the extent of the catalog corruption, then open a case with IBM Support to obtain the service password for cleanup.1
u/Ginger-Dumpling 20d ago
I forget, are there alternative options when dropping? alter module drop SPECiFIC function xxx; alter module drop function(param signature);
You've confirmed in syscat.routines/routineparams to see what versions of the function exist?
If all else fails, can you drop and recreate the entire module?
0
u/Glittering_Pen_4462 20d ago
Probably one solution is to get the ddl drop statement from data studio? With the “”