Re: Internal function call from C-language function

From: Zoltan Boszormenyi <zboszor(at)dunaweb(dot)hu>
To: pgsql-general(at)postgresql(dot)org
Cc: kleptog(at)svana(dot)org
Subject: Re: Internal function call from C-language function
Date: 2006-12-07 11:55:47
Message-ID: 45780143.3000401@dunaweb.hu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Martijn van Oosterhout írta:
> On Thu, Dec 07, 2006 at 09:48:25AM +0100, Zoltan Boszormenyi wrote:
>
>> Hi,
>>
>> I need to call date_part() from a C function.
>> How to do that?
>>
>
> Look in fmgr.h for the functions {Oid,Direct,}FunctionCall* which
> provide various ways to call other functions.
>
> There's also FunctionCallInvoke() which is more efficient if you're
> going to call it lots of times.
>
> Have a nice day,
>

thanks, I found the DirectFunctionCall family,
that wasn't the problem.

The real trick was that inside a C function,
I have to use timestamp_part(), as date_part()
doesn't even exists. The header catalog/pg_proc.h
proves it. date_part() is an SQL wrapper around
real C functions: timestamp[tz]_part(), time[tz]_part()
and interval_part().

However, I have another problem. I have this in the code:

HeapTupleHeader t;
Datum timest;
bool isnull;

t = PG_GETARG_HEAPTUPLEHEADER(0);
timest = DatumGetTimestamp(GetAttributeByName(t, "ts_today",
&isnull));
elog(NOTICE, "DatumGetTimestamp() OK, value is %s", isnull ?
"NULL" : "NOT NULL");

if (isnull)
PG_RETURN_BOOL(false);

yeardatum = CStringGetDatum("year");
elog(NOTICE, "CStringGetDatum() 1 OK");
returndatum = DirectFunctionCall2(timestamp_part, yeardatum,
timest);
elog(NOTICE, "date_part() 1 OK");
year = DatumGetFloat8(returndatum);
elog(NOTICE, "conversion 1 OK");

...

But I get this:

NOTICE: PG_GETARG OK
NOTICE: DatumGetTimestamp() OK, value is NOT NULL
NOTICE: CStringGetDatum() 1 OK
ERROR: invalid memory alloc request size 1951613700

So DirectFunctionCall2() fails. How can I fix it?

Best regards,
Zoltán

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2006-12-07 12:10:56 Re: Internal function call from C-language function
Previous Message Hannes Dorbath 2006-12-07 11:42:52 Tsearch2 / PG 8.2 Which stemmer files?