PL/R User's Guide - R Procedural Language
Prev | Home |  | Next

# Chapter 8. Aggregate Functions

Aggregates in PostgreSQL are extensible via SQL commands. In general, to create a new aggregate, a state transition function and possibly a final function are specified. The final function is used in case the desired output of the aggregate is different from the data that needs to be kept in the running state value.

There is more than one way to create a new aggregate using PL/R. A simple aggregate can be defined using the predefined PostgreSQL C function, `plr_array_accum` (see Chapter 7) as a state transition function, and a PL/R function as a finalizer. For example:

    create or replace function r_median(_float8) returns float as '
      median(arg1)
    ' language 'plr';

    CREATE AGGREGATE median (
      sfunc = plr_array_accum,
      basetype = float8,
      stype = _float8,
      finalfunc = r_median
    );

    create table foo(f0 int, f1 text, f2 float8);
    insert into foo values(1,'cat1',1.21);
    insert into foo values(2,'cat1',1.24);
    insert into foo values(3,'cat1',1.18);
    insert into foo values(4,'cat1',1.26);
    insert into foo values(5,'cat1',1.15);
    insert into foo values(6,'cat2',1.15);
    insert into foo values(7,'cat2',1.26);
    insert into foo values(8,'cat2',1.32);
    insert into foo values(9,'cat2',1.30);

    select f1, median(f2) from foo group by f1 order by f1;
      f1  | median
    ------+--------
     cat1 |   1.21
     cat2 |   1.28
    (2 rows)

A more complex aggregate might be created by using a PL/R functions for both state transition and finalizer.

Prev | Home | Next
PostgreSQL Support Functions |  | Window Functions