Powinieneś użyć funkcji construct_array
lub construct_md_array
#include "catalog/pg_type.h"
PG_FUNCTION_INFO_V1(array_four);
Datum
array_four(PG_FUNCTION_ARGS)
{
Datum elements[4];
ArrayType *array;
elements[0] = PG_GETARG_DATUM(0);
elements[1] = PG_GETARG_DATUM(1);
elements[2] = PG_GETARG_DATUM(2);
elements[3] = PG_GETARG_DATUM(3);
array = construct_array(elements, 4, INT8OID, 8, true, 'd');
PG_RETURN_POINTER(array);
}
drugi wariant:
Datum
array_four(PG_FUNCTION_ARGS)
{
Datum elements[4];
ArrayType *array;
int i;
for (i = 0; i < 4; i++)
elements[i] = Int64GetDatum(i);
array = construct_array(elements, 4, INT8OID, 8, true, 'd');
PG_RETURN_POINTER(array);
}
rejestracja:
CREATE FUNCTION array_four(int, int, int, int)
RETURNS int[]
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
test:
postgres=# select array_four(10,20,30,40);
┌───────────────┐
│ array_four │
╞═══════════════╡
│ {10,20,30,40} │
└───────────────┘
(1 row)