Re: custom C function problem

From: "Dan \"Heron\" Myers" <heron(at)xnapid(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: custom C function problem
Date: 2008-05-03 17:58:41
Message-ID: 481CA7D1.6040101@xnapid.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom Lane wrote:
> What cases have you gotten to work correctly?
>
> My guess is that you're either messed up about V0 vs V1 calling
> convention (ie you forgot PG_FUNCTION_INFO_V1, or added it when you
> shouldn't have), or you've got some kind of problem with not detoasting
> toasted input values. There's not enough info here to venture more.
>
> regards, tom lane

This one works correctly:

PG_FUNCTION_INFO_V1(event_duration);

Datum
event_duration(PG_FUNCTION_ARGS)
{
int32 state = PG_GETARG_INT32(0);
int32 target = PG_GETARG_INT32(1);
int32 event = PG_GETARG_INT32(2);
Timestamp start = PG_GETARG_TIMESTAMP(3);
Timestamp end = PG_GETARG_TIMESTAMP(4);

//If this event is the correct type we need to add the event time
to the total event time (state)
if(target == event){
state += (end - start);
}

PG_RETURN_INT32(state);
}

I can use event_duration in this query without problems:

SELECT call_id, event_duration(4,event_type,start_time,end_time) AS
talking_duration FROM event GROUP BY call_id;

One case that fails is essentially copied from the V1 section in the
documentation:

PG_FUNCTION_INFO_V1(copytext);

Datum copytext(PG_FUNCTION_ARGS)
{
text* t = PG_GETARG_TEXT_P(0);
text* new_t = (text *) palloc(VARSIZE(t));
SET_VARSIZE(new_t, VARSIZE(t));

memcpy((void *) VARDATA(new_t), (void *) VARDATA(t),
VARSIZE(t) - VARHDRSZ);
PG_RETURN_TEXT_P(new_t);
}

Attempting to use copytext in a query results in Postgres crashing.
For example:

SELECT copytext(calling_party) FROM event;

crashes.

- Dan

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Steve Atkins 2008-05-03 18:17:00 Re: large query by offset and limt
Previous Message Tom Lane 2008-05-03 17:50:35 Re: custom C function problem