Db2

 View Only
  • 1.  How to perform logging in DB2 external function UDF in C/C++

    Posted Fri December 20, 2024 10:43 AM

    Dear experts, i am developing a UDF in C/C++.

    I am currently using community for development:

    output from db2level
    DB21085I  This instance or install (instance name, where applicable: 
    "db2inst1") uses "64" bits and DB2 code release "SQL11058" with level 
    identifier "0609010F".
    Informational tokens are "DB2 v11.5.8.0", "s2209201700", "DYN2209201700AMD64", 
    and Fix Pack "0".
    Product is installed at "/opt/ibm/db2/V11.5".

    I have a few questions related to logging.

    1.) Where can i locate the log file related to the UDF loading errors, etc

    2.) What APIs can be used to log errors in the UDF?

    Any help will be greatly appreciated. :)



    ------------------------------
    Jeremy C
    ------------------------------


  • 2.  RE: How to perform logging in DB2 external function UDF in C/C++

    Posted Fri December 20, 2024 02:45 PM

    The db2diag tool on the server can show you records from the db2 diagnostic log (db2diag.log). You may find there UDF-related error messages as well.

    You may use it in the follow mode, for example:

    db2diag -f

    If you want to print your own messages to this file, you should use the corresponding API function: db2AdminMsgWrite



    ------------------------------
    Mark Barinstein
    ------------------------------



  • 3.  RE: How to perform logging in DB2 external function UDF in C/C++

    Posted Sun December 22, 2024 05:38 AM

    Thanks Mark. Greatly appreciate it.

    I have a external library context which needs to initialise when the UDF loads, the library context is required for the UDFs.

    I'm think of using a global static variable for the initialisation. Is there any best practices that is recommended?

    What's the lifecycle of the library so file after being loaded?



    ------------------------------
    Jeremy C
    ------------------------------



  • 4.  RE: How to perform logging in DB2 external function UDF in C/C++

    Posted Mon December 23, 2024 03:24 AM
    Edited by Mark Barinstein Mon December 23, 2024 03:32 AM

    Jeremy,

    Don't use any global or static variables. Use an entity called "scratchpad" to save the UDF's state from one invocation to the next.
    Scratchpads for external functions and methods

    Note that you can't preserve any state between different calls of UDF in your SQL statement. The term "invocation" is about internal calls made for your single function reference in a higher SQL statement.

    You may declare and code your function to accept a number of internal calls as described in the following articles.
    Scalar UDF: External scalar function and method processing model
    Table UDF: External table function processing model

    There are the examples (if you installed them) of these functions coding, which reside in the `~${DB2INSTANCE}/sqllib/samples/cli` directory on your server as well.

    udfsrv.c

    That is you:

    • initialize your external library during the FIRST call of your function and save necessary info to the scratchapad
    • use this scratchpad during the next calls
    • free allocated in the FIRST call resources (if any) in the FINAL call



    ------------------------------
    Mark Barinstein
    ------------------------------



  • 5.  RE: How to perform logging in DB2 external function UDF in C/C++

    Posted Mon December 23, 2024 11:18 AM

    Thanks Mark!
    I got the initial UDF version working now.

    Merry Xmas and a happy new year to you!

    Best wishes to you and your family.



    ------------------------------
    Jeremy C
    ------------------------------