Search code examples
cpostgresqlapache-age

DirectFunctionCall not executing for a function in the same file


In Apache AGE, when I call the DirectFunctionCall() function. It doesn't get executed. I have tried putting logging statements using elog() function in the write() function. But, it's of no use. The code sample is given below -

PG_FUNCTION_INFO_V1(some_function);

PG_FUNCTION_INFO_V1(write);

Datum
some_function(PG_FUNCTION_ARGS) {
    .
    .
    .
    DirectFunctionCall4(write, dest_lob, 2 * size, dest_offset, cstring_to_text(buffer));
}

The write function mentioned here is defined below in the same file. I have checked the parameters, as well as the return type. Everything is matching correctly but still the statement is not executed.


Solution

  • Based on your code snippet, changing the order of the function info definitions should work. The dependent function should be defined after the base function.

    PG_FUNCTION_INFO_V1(write);
    PG_FUNCTION_INFO_V1(some_function);
    

    But there is a better way to achieve the same functionality. Generally, if you want to call the same piece of code for many functions, it is advisable to make it a separate module( in this case a separate function)

    The best way to deal with this case in Postgres extensions in the c code - is to not use direct function calls within the same file.

    Instead, create a static helper function (write_helper) and make the PG function write a wrapper function that calls this function.

    PG_FUNCTION_INFO_V1(write);
    PG_FUNCTION_INFO_V1(some_function);
    
    static <return_type> write_helper(<parameters>);
    
    // wrapper function for the write_helper function
    Datum
    write(PG_FUNCTION_ARGS){
       <return_value type> result;
       result = write_helper(<parameters>);
       .
       .
       .
    
    }
    
    Datum
    some_function(PG_FUNCTION_ARGS) {
        .
        .
        .
        write_helper(<parameters>);
        .
        .
        .
    }
    
    // define the static function
    static 
    <return_type> write_helper(<parameters>){
         returns <return value>
    }