Troubles with array_ref - Mailing list pgsql-hackers

From Cristian Prieto
Subject Troubles with array_ref
Date
Msg-id 011701c5e4af$4d1f8c10$6500a8c0@gt.ClickDiario.local
Whole thread Raw
Responses Re: Troubles with array_ref
List pgsql-hackers
Hi, sorry for the question but I still having serious troubles with the
array_ref function. The function is not documented and I can't get a useful
example inside the contrib directory. The function is defined as:

Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
          int arraylen, int elmlen, bool elmbyval, char elmalign,
          bool *isNull);

I guess nSubscripts is the number of "dimensions" of the array and indx is
the index number of the element I want to get; arraylen I guess is the
length of the ArrayType structure and I also guess that if ArrayType is a
varlena element I could get it with VARSIZE() [if that is wrong somebody
could tell me how to get that info?]; elmlen I guess is the size of any of
the members of the array; elmbyval and elmalign are the "passed by val" and
align properties of each of the elements in the array; and of course isNull
is just to show if the array could have null values or not. [again, if any
of these asserts are false then please correct me and I will try to document
it as soon as possible].

Well, anyway, this is the Stored Function I've been workin on; it simply
take an array and an integer just to return this item from the array; The
array could have any kind of elements so I declare it as anyarray (the
parameter) and anyelement (the return value), please help me, I don't know
where to get info about it.

========= THIS IS THE FUNCTION ==========
PG_FUNCTION_INFO_V1(test);
Datum
test(PG_FUNCTION_ARGS)
{
    ArrayType *v = PG_GETARG_ARRAYTYPE_P(1);
    Datum      element;
    Oid        array_type = ARR_ELEMTYPE(PG_GETARG_ARRAYTYPE_P(1));
    int16      typlen;
    bool       typbyval;
    char       typalign;
    int           i = PG_GETARG_INT32(0);

    get_typlenbyvalalign(array_type, &typlen, &typbyval, &typalign);
    element = array_ref(v, 1, &i, VARSIZE(v), typlen, typbyval,
typalign, false);
    PG_RETURN_DATUM(element);
}

========= THIS IS THE DECLARATION IN SQL =====

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

======== AND THIS IS THE ERROR ===============
SELECT test(1, array[1,2,3]);
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: WARNING:
terminating connection because of crash of another server process
DETAIL:  The postmaster has commanded this server process to roll back the
current transaction and exit, because another server process exite
d abnormally and possibly corrupted shared memory.
HINT:  In a moment you should be able to reconnect to the database and
repeat your command.
Failed.

Thanks a lot for your help...


pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: lc_numeric and decimal delimiter
Next
From: Tom Lane
Date:
Subject: Re: Troubles with array_ref