Thread: C function woes (more info)

C function woes (more info)

From
Chris Hayner
Date:
additional information whic may be useful for those smarter than i:
thanks for any possible help, Chris

how it was made:
gcc -fPIC -c test.c
gcc -shared test.o
*** --I have also tried using ld -shared (GNU ld)
a.out test.so

from psql:

test=# select  hello();
 hello
-------
 sesu
(1 row)

test=# select  hello();
 hello
-------

(1 row)

test=# select  hello();


^CCancel request sent
^CCancel request sent
^CCancel request sent --at this point, the whole back end freezes

-----------------------------------------------------------------
hello all:

I am attempting to create a function which will return data from a
C-function. This is what i have, and it so far has been successful. Any
help anyone can give me would be greatly appreciated.

gcc Compiler, PgSQL 7.1 beta 3
thanks,
chris


-----------------------------------------------------------------------
#include <string.h>
#include "/usr/local/pgsql/include/postgres.h"

text *
hello()
 {
        char data[] = "hello world";
        int32 new_text_size = VARHDRSZ + sizeof(data);
        text *new_text = (text *) palloc(new_text_size);

        strcpy(VARDATA(new_text), data);
        return new_text;
 }




Re: C function woes (more info)

From
Marko Kreen
Date:
On Tue, Feb 20, 2001 at 04:56:53PM -0500, Chris Hayner wrote:
> I am attempting to create a function which will return data from a
> C-function. This is what i have, and it so far has been successful. Any
> help anyone can give me would be greatly appreciated.

> -----------------------------------------------------------------------
> #include <string.h>
> #include "/usr/local/pgsql/include/postgres.h"
>
> text *
> hello()
>  {
>         char data[] = "hello world";
>         int32 new_text_size = VARHDRSZ + sizeof(data);
>         text *new_text = (text *) palloc(new_text_size);

    VARATT_SIZEP(new_text) = new_text_size;

>         strcpy(VARDATA(new_text), data);

You include the '\0' too, so you see in psql 'right' result coz it
uses libc for printing.  Actually the size is random.

>         return new_text;
>  }

For real 7.1 fmgr interface you should do:

PG_FUNCTION_INFO_V1(hello);
Datum hello(PG_FUNCTION_ARGS)
{
    [...]

    PG_RETURN_TEXT_P(new_text);
}



--
marko