Can anyone share an example how I can create C function for PostgreSQL which takes array of two integers as input and returns array as output?
For simple integer I have:
#include "postgres.h"
#include <fmgr.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
int
add_one(int arg) {
arg++;
return arg;
}
And after compilation in PostgreSQL:
load '/usr/lib/postgresql/9.1/lib/add_one';
create or replace function add_one(integer)
returns integer as
'/usr/lib/postgresql/9.1/lib/add_one', 'add_one'
language c;
select add_one(1);
I need something like:
#include "postgres.h"
#include <fmgr.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
int
add_one(int[] arg) {
arg[1]++;
arg[2] = arg[2] + 2
return arg[];
}
And in PostgreSQL:
load '/usr/lib/postgresql/9.1/lib/add_one';
create or replace function add_one(integer[])
returns integer[] as
'/usr/lib/postgresql/9.1/lib/add_one', 'add_one'
language c;
select add_one(ARRAY[1::int,1::int]);
I have tried to modify some function from numeric.c, but without any success so far.