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