Thread: Troubles with array_ref

Troubles with array_ref

From
"Cristian Prieto"
Date:
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...


Re: Troubles with array_ref

From
Tom Lane
Date:
"Cristian Prieto" <cristian@clickdiario.com> writes:
> 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.

You could save yourself a lot of time if you enabled warnings from your
C compiler (eg, -Wall for gcc) and then paid some attention to them.
The last parameter to array_ref is a bool *, not a bool, and I have no
doubt that the backend is crashing while trying to dereference "false".

(Another problem is that the fourth parameter should be -1 not VARSIZE.)

            regards, tom lane

Re: [GENERAL] Troubles with array_ref

From
"Cristian Prieto"
Date:
Thanks a lot man!!! You saved my life :P

-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of Tom Lane
Sent: Martes, 08 de Noviembre de 2005 04:13 p.m.
To: Cristian Prieto
Cc: pgsql-general@postgresql.org; pgsql-hackers@postgresql.org
Subject: Re: [GENERAL] [HACKERS] Troubles with array_ref

"Cristian Prieto" <cristian@clickdiario.com> writes:
> 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.

You could save yourself a lot of time if you enabled warnings from your
C compiler (eg, -Wall for gcc) and then paid some attention to them.
The last parameter to array_ref is a bool *, not a bool, and I have no
doubt that the backend is crashing while trying to dereference "false".

(Another problem is that the fourth parameter should be -1 not VARSIZE.)

            regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match