Thread: Help with Array Function in C language...

Help with Array Function in C language...

From
"Cristian Prieto"
Date:
Hello, I'm doing a very simple C language function in PostgreSQL but I can't
figure out why this is not working, the documentation about the PostgreSQL
internals is not so good about arrays and I couldn't find a suitable example
of the use of some kind of array functions inside the pgsql source tree.

I'm trying to get the N item from any array passed as argument, this could
be the SQL declaration of my function:

CREATE OR REPLACE FUNCTION test_array(integer, anyarray) RETURNS anyelement
AS 'test.so' LANGUAGE 'C';

And the function could look like this:

PG_FUNCTION_INFO_V1(test_array);
Datum
test_array(PG_FUNCTION_ARGS)
{
    ArrayType *v = PG_GETARG_ARRAYTYPE_P(1);
    Datum      element;
    Oid        array_type = get_array_type(v);
    int        typlen;
    bool       typbyval;
    char       typalign;

    get_typlenbyvalalign(array_type, &typlen, &typbyval, &typalign);
    element = array_ref(v, 1, PG_GETARG_INT32(0), ARR_SIZE(v), typlen,
typbyval, typalign, false);

    PG_RETURN_DATUM(element);
}

The function compiles without error, but when I try something like that:
SELECT test_array(3, array[1,2,3,4,5,6]);

It returns to me an error like this:
ERROR:  cache lookup failed for type 0

What is wrong here?


Re: [HACKERS] Help with Array Function in C language...

From
Tom Lane
Date:
"Cristian Prieto" <cristian@clickdiario.com> writes:
> Datum
> test_array(PG_FUNCTION_ARGS)
> {
>     ArrayType *v = PG_GETARG_ARRAYTYPE_P(1);
>     Datum      element;
>     Oid        array_type = get_array_type(v);

I think you want get_element_type, instead.  And you definitely ought to
be checking for a failure result (zero).

            regards, tom lane

Re: [HACKERS] Help with Array Function in C language...

From
"Cristian Prieto"
Date:
Thanks a lot, but I still getting an error message like this:
ERROR: cache lookup failed for type 0

What is wrong?

-----Original Message-----
From: pgsql-hackers-owner@postgresql.org
[mailto:pgsql-hackers-owner@postgresql.org] On Behalf Of Tom Lane
Sent: Lunes, 07 de Noviembre de 2005 05:17 p.m.
To: Cristian Prieto
Cc: pgsql-general@postgresql.org; pgsql-hackers@postgresql.org
Subject: Re: [HACKERS] Help with Array Function in C language...

"Cristian Prieto" <cristian@clickdiario.com> writes:
> Datum
> test_array(PG_FUNCTION_ARGS)
> {
>     ArrayType *v = PG_GETARG_ARRAYTYPE_P(1);
>     Datum      element;
>     Oid        array_type = get_array_type(v);

I think you want get_element_type, instead.  And you definitely ought to
be checking for a failure result (zero).

            regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to majordomo@postgresql.org so that your
       message can get through to the mailing list cleanly