Database Reference
In-Depth Information
functions is quite blurred, and the programmer doing so is expected to be able to fig-
ure some things out independently.
The bright side is that the friendly folks at the PostgreSQL mailing lists are usually
happy to help you out if they see that your question is a serious one and that you
have made some effort to figure out the basic stuff yourself.
To see how arguments of array types are handled, you have to start digging around
on the internet and/or in the backend code. One place where you can find a sample
is the contrib/hstore/ module in the PostgreSQL source code. The contrib
modules are a great reference for examples of officially supported extension mod-
ules from PostgreSQL.
Though the code there does not do exactly what we need—it works on text[] and
not int[] —it is close enough to figure out what is needed, by supplying the basic
structure of array handling and sample usage of some utility macros and functions.
After some digging around in back-end code and doing some web searches, it is not
very hard to come up with a code for integer arrays.
So here is C code for a function which sums all non-null elements in its argument
array:
#include "utils/array.h " // array utility
functions and macros
#include "catalog/pg_type.h " // for INT4OID
PG_MODULE_MAGIC;
Datum add_int32_array(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(add_int32_array);
Datum
add_int32_array(PG_FUNCTION_ARGS)
{
ArrayType *input_array;
Search WWH ::




Custom Search