Re: C function woes (more info)

From: Marko Kreen <marko(at)l-t(dot)ee>
To: Chris Hayner <hayner80(at)astro(dot)ocis(dot)temple(dot)edu>
Cc: PostgreSQL-General <pgsql-general(at)postgresql(dot)org>
Subject: Re: C function woes (more info)
Date: 2001-02-20 23:06:31
Message-ID: 20010221010631.A32477@l-t.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message mg 2001-02-20 23:09:56 Error: 'cache lookup failed' w/trigger
Previous Message Roderick A. Anderson 2001-02-20 23:02:33 Re: Re: Printing PostgreSQL reports