Re: Internal function call from C-language function

Поиск
Список
Период
Сортировка
От Zoltan Boszormenyi
Тема Re: Internal function call from C-language function
Дата
Msg-id 45780143.3000401@dunaweb.hu
обсуждение исходный текст
Ответ на Re: Internal function call from C-language function  (Martijn van Oosterhout <kleptog@svana.org>)
Ответы Re: Internal function call from C-language function  (Martijn van Oosterhout <kleptog@svana.org>)
Список 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


В списке pgsql-general по дате отправления:

Предыдущее
От: Hannes Dorbath
Дата:
Сообщение: Tsearch2 / PG 8.2 Which stemmer files?
Следующее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: Internal function call from C-language function