Обсуждение: Aggregate with external sfunc

Поиск
Список
Период
Сортировка

Aggregate with external sfunc

От
Tiberiu Stef
Дата:
Problem: New aggregate, with custom-made functions, crashes connection.
I want to add a new aggregate (based on C functions) and I'm stuck :

I created a C function:
CREATE OR REPLACE FUNCTION fintransfunc(double precision,double precision)
RETURNS double precision AS '$libdir/finops.so' LANGUAGE C;
fintransfunc is exactly the same as float8smaller (which is used in the
min(float) aggregate).
finops.so is the library containing the fintransfunc, which I built and
placed in /usr/lib/pgsql/.
I tested fintransfunc with select fintransfunc(2,4); and it worked fine.

Then I defined the aggregate:
create aggregate finmin (sfunc=fintransfunc, basetype = double precision
,stype= double precision );

When I tested it ( select finmin(row) from table ) it resets my database
connection:
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!#

However, if I replace in the aggregate creation the fintransfunc with
float8smaller, and then test again, the query goes through and produces
the desired result.

Are there any issues with using for aggregate definitions functions that
are
in custom-made libraries ?
From my tests, I guess the aggregate interpreter fails to find a function
that is not in the default postgres server library.

Can anyone help me out here ?

Thanks
T.Stef



Re: Aggregate with external sfunc

От
Tom Lane
Дата:
Tiberiu Stef <tstef@cs.purdue.edu> writes:
> I created a C function:
> CREATE OR REPLACE FUNCTION fintransfunc(double precision,double precision)
> RETURNS double precision AS '$libdir/finops.so' LANGUAGE C;

You probably want to mark it strict (WITH (isStrict)), unless it's
prepared to deal with NULL inputs.  I'm betting your table contains
some NULLs.

> However, if I replace in the aggregate creation the fintransfunc with
> float8smaller, and then test again, the query goes through and produces
> the desired result.

float8smaller is marked isStrict ...

> From my tests, I guess the aggregate interpreter fails to find a function
> that is not in the default postgres server library.

No, your routine is dumping core on a null pointer dereference, as you
should have been able to find out for yourself with a debugger.  If you
don't know how to use gdb or some other debugger, writing C functions is
not going to be much fun for you.

            regards, tom lane