Re: Defined C function gives nondeterministic results

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Patrick L(dot) Nolan" <pln(at)razzle(dot)Stanford(dot)EDU>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Defined C function gives nondeterministic results
Date: 2002-03-07 15:46:57
Message-ID: 10190.1015516017@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Patrick L. Nolan" <pln(at)razzle(dot)Stanford(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Corey W. Gibbs 2002-03-07 15:47:54 Re: How do I pass the -i option during boot time?
Previous Message Stephan Szabo 2002-03-07 15:38:20 Re: Migration problem - serial fields