r/DB2 • • 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:

  1. 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.

  1. Drop and Recreate the Affected Routine

Well, that is what is failing.

  1. 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

2 Upvotes

6 comments sorted by

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_blob specific name pointing to VARCHAR(4096) parameter instead of BLOB), creating inconsistent entries across SYSCAT.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

  • ALTER MODULE DROP fails with "ObjectId for object type PSMROUTINE does not exist"
  • Two notnull function signatures now exist with conflicting definitions (both targeting VARCHAR(4096) parameters)
  • Function routing is broken — calling with VARCHAR or BLOB both return blob operation result
  • RUNSTATS, FLUSH, and db2rbind did not resolve the issue (expected — this is catalog structure corruption, not stale statistics)

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:

1 line

db2 backup db <dbname> to /backup/path compress

2. Detect orphan rows:

1 line

db2cat -d <dbname> -ch

Look for output mentioning SYSCAT.ROUTINES, SYSCAT.MODULEOBJECTS, or entries related to UTIL.NOTNULL.

3. Contact IBM Support to obtain a service password (required for catalog cleanup operations). Mention:

  • SQL0901N with "ObjectId does not exist for PSMROUTINE"
  • Module function catalog corruption
  • Need service password for db2cat -cl

4. Clean up orphan rows:

2 lines

export DB2SVCPW=<service_password_from_IBM>
db2cat -d <dbname> -cl

When prompted Do you wish to proceed committing the changes made by db2cleancat (y/n)?, type y.

5. Verify the module state:

1 line

CALL SYSPROC.ADMIN_CMD('DESCRIBE MODULE UTIL');

6. If the orphaned function entries are removed, recreate the BLOB overload correctly:

5 lines

ALTER MODULE util PUBLISH FUNCTION NOTNULL(data BLOB) SPECIFIC notnull_blob RETURNS VARCHAR(4096);
ALTER MODULE util ADD FUNCTION NOTNULL(data BLOB) SPECIFIC notnull_blob 
  RETURNS VARCHAR(4096) 
  DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL 
  RETURN <your blob operation>;

Option B: Drop and recreate the entire module (Nuclear option)

If db2cat -cl does not resolve the issue or you cannot wait for IBM Support:

1. Export the working function definition:

1 line

db2look -d <dbname> -e -z UTIL -tw UTIL -o util_module_backup.sql

2. Drop the entire module (this will likely fail, but try):

1 line

DROP MODULE UTIL;

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:

15 lines

CREATE MODULE UTIL;

-- Recreate VARCHAR overload
ALTER MODULE util PUBLISH FUNCTION NOTNULL(str VARCHAR(4096)) SPECIFIC notnull RETURNS VARCHAR(4096);
ALTER MODULE util ADD FUNCTION NOTNULL(str VARCHAR(4096)) SPECIFIC notnull
  RETURNS VARCHAR(4096)
  DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
  RETURN <your string operation>;

-- Recreate BLOB overload (correctly this time)
ALTER MODULE util PUBLISH FUNCTION NOTNULL(data BLOB) SPECIFIC notnull_blob RETURNS VARCHAR(4096);
ALTER MODULE util ADD FUNCTION NOTNULL(data BLOB) SPECIFIC notnull_blob
  RETURNS VARCHAR(4096)
  DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
  RETURN <your blob operation>; Show more

Prevention

  1. Always verify function signatures immediately after creation:
4 lines

SELECT ROUTINENAME, SPECIFICNAME, PARM_COUNT, PARMNAME, TYPENAME
   FROM SYSCAT.ROUTINEPARMS
   WHERE ROUTINESCHEMA = 'UTIL' AND ROUTINENAME = 'NOTNULL'
   ORDER BY SPECIFICNAME, ORDINAL;
  1. Use DESCRIBE to validate module state after ALTER MODULE ADD:
1 line

CALL SYSPROC.ADMIN_CMD('DESCRIBE MODULE UTIL');
  1. Test overloaded functions with explicit type casting after creation:
2 lines

SELECT util.notnull(CAST('test' AS VARCHAR(4096))) FROM SYSIBM.SYSDUMMY1;
   SELECT util.notnull(CAST(BLOB('test') AS BLOB)) FROM SYSIBM.SYSDUMMY1;

Next step: Start with db2cat -d <dbname> -ch on 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/anibjoshi 20d ago

Please DM me if you need more information. I'm the PM for Db2.

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?

1

u/DazzlingAd4254 20d ago

You've confirmed in syscat.routines/routineparams to see what versions of the function exist?

Yes, there is only 1 routine in syscat.routines that has routinename='NOTNULL'. IBM Data Studio also shows only 1 function with that name.

If all else fails, can you drop and recreate the entire module?

Yes, I tried but re-creating failed 'because one or more dependencies exist on the target object. Target object type: "FUNCTION".'

1

u/DazzlingAd4254 20d ago

Unable to drop the module. Same error:

$ db2 drop module util

SQL0901N  The SQL statement or command failed because of a database system  
error. (Reason "ObjectId 694200937 for object type PSMROUTINE does not  
exist.".)  LINE NUMBER=1.  SQLSTATE=58004

0

u/Glittering_Pen_4462 20d ago

Probably one solution is to get the ddl drop statement from data studio? With the “”