Re: reading array[text] in C extension function

From: Rémi Cura <remi(dot)cura(at)gmail(dot)com>
To: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: reading array[text] in C extension function
Date: 2014-01-16 13:19:47
Message-ID: CAJvUf_vy1wRpO8t9j3NxA0pZO-m6X04PGohV0UU-9369FwEyjA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'll auto-answer ;-)

Based on the function "btoptions" from postgres source, which takes aas a
first arg a text[] :

ArrayType *array;
Datum *dimdatums;
int ndim;
array = DatumGetArrayTypeP(dimensions);
Assert(ARR_ELEMTYPE(array) == TEXTOID);
pcinfo("after assert \n");
deconstruct_array(array, TEXTOID, -1, false, 'i',
&dimdatums, NULL, &ndim);

//construct the array to hold the result :
char ** final_dimension_array = (char **) pcalloc(ndim * sizeof(char * ) );
for (i = 0; i < ndim; i++)
{
text *dimensiontext = DatumGetTextP(dimdatums[i]);
char *text_str = VARDATA(dimensiontext);
int text_len = VARSIZE(dimensiontext) - VARHDRSZ;
char *s;
char *p;
s = TextDatumGetCString(dimdatums[i]);
final_dimension_array[i] = s;
}
//pcinfo("end of the text retrieval\n");

Cheers,

Rémi-C

2014/1/15 Rémi Cura <remi(dot)cura(at)gmail(dot)com>

> Hey,
> I'm trying to use an array of text as input in a C function in a custom
> extension.
>
> the prototype of the sql function is :
> CREATE OR REPLACE FUNCTION pc_subset( dimensions TEXT[])
>
>
> it is called like :
> pc_subset( ARRAY['X'::text,'Y'::text])
>
> and the C function trying to read the text array (converting it to cstring
> ) :
>
> text ** vals;
>
> char ** cstring_array;
> nelems = ARR_DIMS(arrptr)[0];
> vals = (text**) ARR_DATA_PTR(arrptr);
> cstring_array = (char **) pcalloc(nelems * sizeof(char * ) );
> for (i3=0;i3<nelems;i3++)
> {
> cstring_array[i3] = text_to_cstring( vals[i3]);
> elog(INFO, "elem %d of dim_array : %s\n",i3,cstring_array[i3]);
> }
>
> I crashes postgres because of a segfault.
>
>
> Any help appreciated =)
>
> Cheers,
> Rémi-C
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Felix.徐 2014-01-16 14:11:52 Breakpoints are not triggered in analyze.c (debugging Postgresql in Visual studio)
Previous Message Alban Hertroys 2014-01-16 11:34:09 Re: PostgreSQL with ZFS on Linux