Re: custom C function problem - Mailing list pgsql-general

From Dan \"Heron\" Myers
Subject Re: custom C function problem
Date
Msg-id 481CA7D1.6040101@xnapid.com
Whole thread Raw
In response to Re: custom C function problem  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: custom C function problem  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: custom C function problem
Next
From: jdietrch@fastmail.fm
Date:
Subject: Problem revoking a user's 'create' privilege on schema public