Re: Defining input function for new datatype - Mailing list pgsql-general

From Tom Lane
Subject Re: Defining input function for new datatype
Date
Msg-id 9759.1303397240@sss.pgh.pa.us
Whole thread Raw
In response to Defining input function for new datatype  (Nick Raj <nickrajjain@gmail.com>)
List pgsql-general
Nick Raj <nickrajjain@gmail.com> writes:
> 1     Datum mpoint_in(PG_FUNCTION_ARGS)
> 2     {
> 3
> 4        mpoint *result;
> 5        char *pnt=(char *)malloc (sizeof (20));
> 6        char *ts=(char *)malloc (sizeof (20));

(1) You should *not* use malloc here.  There is seldom any reason to use
malloc directly at all in functions coded for Postgres.  Use palloc,
or expect memory leaks.

(2) sizeof(20) almost certainly doesn't mean what you want.  It's most
likely 4 ...

> 11      result->p = point_in(PointerGetDatum(pnt));                //
> point_in (input function for point that assigns x, y into point)

You need to use DirectFunctionCallN when trying to call a function that
obeys the PG_FUNCTION_ARGS convention, as point_in does.  And the result
is a Datum, which means you're going to need to apply a DatumGetWhatever
macro to get a bare Point or Timestamp from these functions.

Look around in the PG sources for examples.

            regards, tom lane

pgsql-general by date:

Previous
From: "Chris Greenhill"
Date:
Subject: Re: Trouble loading Perl modules from postgresql.conf
Next
From: Greg Smith
Date:
Subject: Re: SSDs with Postgresql?