Re: Parameters in user-defined aggregate final functions - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Parameters in user-defined aggregate final functions
Date
Msg-id 24133.1515712297@sss.pgh.pa.us
Whole thread Raw
In response to Parameters in user-defined aggregate final functions  (Esteban Zimanyi <ezimanyi@ulb.ac.be>)
Responses Re: Parameters in user-defined aggregate final functions
List pgsql-hackers
Esteban Zimanyi <ezimanyi@ulb.ac.be> writes:
> How to tell PostgreSQL that my final function also needs a parameter? I am
> working on PostgreSQL 10.1. I know that according to the documentation
> direct parameters are only allowed for ordered-set aggregates, but I would
> also need a direct parameter for "normal" aggregates.

So define it as an ordered-set aggregate, and just ignore the question
of whether you need to sort the input (which is something that we leave
to the aggregate function to do anyway).  The syntax would be a little
weird/non-orthogonal, but you can blame the SQL committee for that.

regression=# create function trans(int, int) returns int language sql
regression-# as 'select $1+$2' strict;
CREATE FUNCTION
regression=# create function final(int, float8) returns float8 language sql
regression-# as 'select $1*$2' strict;
CREATE FUNCTION
regression=# create aggregate myosa(float8 order by int) (
regression(# sfunc = trans, stype = int, finalfunc = final);
CREATE AGGREGATE
regression=# select sum(ten), myosa(0.5) within group (order by ten) from tenk1;
  sum  | myosa
-------+-------
 45000 | 22500
(1 row)


            regards, tom lane


pgsql-hackers by date:

Previous
From: David Fetter
Date:
Subject: Re: Parameters in user-defined aggregate final functions
Next
From: Vaishnavi Prabakaran
Date:
Subject: Re: [HACKERS] PATCH: Batch/pipelining support for libpq