Database Reference
In-Depth Information
PG_RETURN_INT32(PG_GETARG_INT32(0) +
PG_GETARG_INT32(1));
But it is much more readable as written, and most likely a good optimizing C compiler
will compile both into equivalently fast code.
Most compilers will issue a warning message as: warning: no previous pro-
totype for 'add_ab' for the preceding code, so it is a good idea to also put a
prototype for the function in the file:
Datum add_ab(PG_FUNCTION_ARGS);
The
usual
place
to
put
it
is
just
before
the
code
line
PG_FUNCTION_INFO_V1(add_ab);
While the prototype is not strictly required, it enables much cleaner compiles with no
warnings.
Version 0 call conventions
There is an even simpler way to write PostgreSQL functions in C, called the Version
0 Calling Conventions . The preceding a + b function can be written as the follow-
ing code:
int add_ab(int arg_a, int arg_b)
{
return arg_a + arg_b;
}
Version 0 is shorter for very simple functions, but it is severely limited for most other
usages—you can't do even some basic things such as checking if a pass by value
argument is null , return a set of values, or write aggregate functions. Also, Version
0 does not automatically take care of hiding most differences of pass by value and
pass by reference types which Version 1 does. Therefore, it is better to just write all
your functions using Version 1 Calling Conventions and ignore the fact that Version
0 even exists.
Search WWH ::




Custom Search