DirectFunctionCall not executing for a function in the same file

102 Views Asked by At

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.

2

There are 2 best solutions below

0
Abstract_Monk On BEST ANSWER

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>
}
0
Kihara On

Instead of calling the same piece of code directly from multiple functions in the same file, it is better to create a separate helper function and call that function from the other functions.This is a good practice for PostgreSQL extensions because it makes the code more modular and reusable. It also makes it easier to debug and maintain the code. Here is an example of how to do this:

static Datum write_helper(PG_FUNCTION_ARGS);

// Wrapper function for the write_helper function
Datum write(PG_FUNCTION_ARGS) {
    Datum result = write_helper(fcinfo);
    // Process the result if needed
    PG_RETURN_DATUM(result);
}

Datum some_function(PG_FUNCTION_ARGS) {
    // ...
    Datum result = write_helper(fcinfo);
    // Process the result if needed
    // ...
    PG_RETURN_DATUM(result);
}

// Define the static function
static Datum write_helper(PG_FUNCTION_ARGS) {
    // Extract and validate your function's parameters
    // ...
    // Call your logic here
    // ...
    // Return the result
    PG_RETURN_DATUM(result);
}