Database Reference
In-Depth Information
After that, we will describe some important things to be aware of when designing and
writing code that runs inside the server—such as memory management, executing
queries, and retrieving results.
As the topic of writing C-language PostgreSQL functions can be quite large and
our space for this topic is limited, we will occasionally skip some of the details and
refer you to the PostgreSQL manual for extra information, explanations, and spe-
cifications. We are also limiting this section to reference PostgreSQL 9.2. While most
things will work perfectly fine across versions, there are references to paths that will
be specific to a version.
Simplest C function - return (a + b)
Let's start with a simple function, which takes two integer arguments and returns the
sum of these. We first present the source code and then will move on to show you
how to compile it, load it into PostgreSQL, and then use it as any native function.
add_func.c
A C source file implementing add(int, int) returns int function looks like
the following code snippet:
#include "postgres.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(add_ab);
Datum
add_ab(PG_FUNCTION_ARGS)
{
int32 arg_a = PG_GETARG_INT32(0);
int32 arg_b = PG_GETARG_INT32(1);
PG_RETURN_INT32(arg_a + arg_b);
}
Search WWH ::




Custom Search