Re: Defined C function gives nondeterministic results - Mailing list pgsql-general

From Tom Lane
Subject Re: Defined C function gives nondeterministic results
Date
Msg-id 10190.1015516017@sss.pgh.pa.us
Whole thread Raw
In response to Defined C function gives nondeterministic results  ("Patrick L. Nolan" <pln@razzle.Stanford.EDU>)
List pgsql-general
"Patrick L. Nolan" <pln@razzle.Stanford.EDU> writes:
> We just installed Postgresql 7.2 on Redhat Linux 7.1.  I'm trying to
> write my first function extension in C.  I think I'm following the
> rules, but it gives nonsense results.

> The extension is added by this statement:
> create or replace function distf (float4, float4, float4, float4) returns float4 as
> '/home/pln/pg/mylib.so', 'distf' language C;

> Here's the C code:
> #include "postgres.h"
> #include "fmgr.h"
> #define fac 57.2957795   /* pi/180  */
> PG_FUNCTION_INFO_V1(distf);
> Datum
> distf(PG_FUNCTION_ARGS) {
>   float4 ra1 = PG_GETARG_FLOAT4(0);   /* Extract the 4 arguments */
>   float4 dec1 = PG_GETARG_FLOAT4(1);
>   float4 ra2 = PG_GETARG_FLOAT4(2);
>   float4 dec2 = PG_GETARG_FLOAT4(3);
>   double dot;
>   float4 angle;
>   dot = cos((double)(ra1/fac))*cos((double)(ra2/fac))+
>     sin((double)(ra1/fac))*sin((double)(ra2/fac))*cos((double)((dec1-dec2)/fac));
>   angle = fac * acos(dot);
>   PG_RETURN_FLOAT4(angle);
> }

Hmm.  That looks perfectly correct to me, with just one caveat: I'd
recommend adding "with (isStrict)" to the CREATE command, since your
function is not prepared to cope with NULL inputs.  That doesn't
explain your junk results though.

You could try adding some printouts to the function to see what values
it thinks it's getting and computing; that would at least give you a
hint whether the problem is in acquiring the inputs, returning the
result, or in between.  (For temporary hacks like this, I'd just do
fprintf to stderr and then look in the postmaster stderr log to see the
results.)

> 1.  I thought there might be an error log file with some useful information.
> Couldn't find one.

What script are you using to start the postmaster?  Look to see what it
does with stderr.

            regards, tom lane

pgsql-general by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: "select myfunc(fields) from my table" inside plpgslq
Next
From: "Corey W. Gibbs"
Date:
Subject: Re: How do I pass the -i option during boot time?