sql >> Base de Datos >  >> RDS >> PostgreSQL

Crear una matriz int8[] en la función C de PostgreSQL

Deberías usar la función construct_array o 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);
}

segunda variante:

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);
}

registro:

CREATE FUNCTION array_four(int, int, int, int)
RETURNS int[]
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;

prueba:

postgres=# select array_four(10,20,30,40);
┌───────────────┐
│  array_four   │
╞═══════════════╡
│ {10,20,30,40} │
└───────────────┘
(1 row)