| This page is user contributed documentation. See the bottom of the page for information about the author. | |
PostgreSQL 7.3 now supports a much more flexible system for writing set returning functions (SRFs) that when combined with some of the new function permission options allow a greater flexibility in setting up schemas. I assume in this that you already have some experience with writing functions in SQL and PL/pgSQL for PostgreSQL. We're going to work with a very simple set of tables and data and functions written in SQL and PL/pgSQL.
create table department(id int primary key, name text);
create table employee(id int primary key, name text, salary int,
departmentid int references department);
insert into department values (1, 'Management');
insert into department values (2, 'IT');
insert into employee values (1, 'John Smith', 30000, 1);
insert into employee values (2, 'Jane Doe', 50000, 1);
insert into employee values (3, 'Jack Jackson', 60000, 2);
SRFs can return either a rowtype as defined by an existing table or a generic record type. First let's look at a simple SQL function that returns an existing table's rowtype.
create function GetEmployees() returns setof employee
as 'select * from employee;' language 'sql';
This very simple function simply returns all the rows from employee. Let's break down this function. The return type of the function is setof employee, meaning it is going to return a rowset of employee rows. The body of the function is a very simple SQL statement to generate the output rows.
An SRF can be used in place of a table or subselect in the FROM clause of a query. For example, to use this function to get all the information on employees with an id greater than 2 you could write:
select * from GetEmployees() where id > 2;
This is great, but what if you wanted to return something more complicated, for example, a list of departments and the total salary of all employees in that department. If you want to return an existing record type, you need to make a dummy type to hold the output type, for example:
create type holder as (departmentid int, totalsalary
int8);
Here we are defining a new type named holder which is a composite type of an integer named departmentid and a bigint named totalsalary. We can then define functions that return sets of this type. For this function we'll write a version in SQL and then a version in PL/pgSQL:
create function SqlDepartmentSalaries() returns setof
holder as
'
select departmentid, sum(salary) as totalsalary from
GetEmployees() group by departmentid
'
language 'sql';
create or replace function
PLpgSQLDepartmentSalaries() returns setof holder as
'
declare
r holder%rowtype;
begin
for r in select departmentid, sum(salary) as totalsalary from
GetEmployees() group by departmentid loop
return next r;
end loop;
return;
end
'
language 'plpgsql';
The SQL is very similar to the GetEmployee() function above. It returns a rowset of rows defined by the type holder (int, int8). The rows that it returns are defined by the group by query in its body.
The PL/pgSQL function is a little more complicated, but let's go through it. The function starts off by declaring a variable r to be of the rowtype holder. This variable will be used to store the rows coming from the query in the main body of the function. The main body does a loop over the group by query stated setting r to each row in sequence. The body of the loop is the new return form, 'return next' which means that an output row is queued into the return set of the function. This does not cause the function to return. Currently, SRF returning PL/pgSQL functions must generate the entire result set before returning although if the set becomes large it will be written to disk. This limitation may be removed in a future version.
These functions are used in the same fashion as the first function,
select * from PLpgSQLDepartmentSalaries();
A PL/pgSQL function can also do additional operations on the records or only queue up only some records. For example, if you wanted to get an idea of operating expenses for departments where the overhead is 75% for departments whose total salaries were greater than 70,000 and 50% otherwise and only wanted to return department ids for departments whose salaries plus overhead was greater than 100,000 you might write something like:
create or replace function ExpensiveDepartments()
returns setof int as
'
declare
r holder%rowtype;
begin
for r in select departmentid, sum(salary) as totalsalary
from GetEmployees() group by departmentid loop
if (r.totalsalary > 70000) then
r.totalsalary := CAST(r.totalsalary * 1.75 as int8);
else
r.totalsalary := CAST(r.totalsalary * 1.5 as int8);
end if;
if (r.totalsalary > 100000) then
return next r.departmentid;
end if;
end loop;
return;
end
'
language 'plpgsql';
Let's look at the differences between this and PLpgSQLDepartmentSales(). This function returns a set of integers (department ids) rather than a set of a composite type because we only need to return the id for the expensive departments. The major changes to the workings of the function are inside the loop, so let's look more closely.
if (r.totalsalary > 70000) then
r.totalsalary := CAST(r.totalsalary * 1.75 as int8);
else
r.totalsalary := CAST(r.totalsalary * 1.5 as int8);
end if;
Here we're figuring out the total salary plus the overhead and updating the record appropriately. Next, we want to determine if the totalsalary is now greater than 100,000 and if so return it's identifier, so
if (r.totalsalary > 100000) then
return next r.departmentid;
end if;
Note that for the return next we are not returning the record r, but instead are returning just the departmentid because this function returns a set of integers. If we instead had wanted to return a holder to include the salary + overhead value, we could have defined the function to return setof holder and used return next r; here.
So far, the composite type returning functions only work if you're certain that you're returning a type that is made up of the same types as the one the function is declared to return. If you make a mistake, you'll get an error at creation time for SQL functions and at execute time for PL/pgSQL functions. But what happens if you only know what the details of the composite type the function needs to return at execution time? In that case, you can return a setof record. This tells PostgreSQL that you want to the function to return an composite type but that you're going to tell it what types to expect later.
Let's make a function that returns all the rows of a table whose name you pass in as a parameter.
create or replace function GetRows(text) returns setof
record as
'
declare
r record;
begin
for r in EXECUTE ''select * from '' || $1 loop
return next r;
end loop;
return;
end
'
language 'plpgsql';
Calling this function is a little more complicated than calling the SRFs above. We need to give the system an idea of what types we expect this function to return as part of the query. PostgreSQL treats these functions somewhat similarly to table subselects and uses a similar syntax for providing this information as one would use to give aliases to subselect columns.
select * from GetRows('Department') as dept(deptid int,
deptname text);
Here we've passed in Department as the argument which means that we expect to get rows in the general form of Department records which is an integer followed by a text string, so we tell PostgreSQL that the alias for the result should be called dept and that it is made up of an integer named deptid and a text named deptname.
Finally, we're going to make PL/pgSQL functions that synthesize rows completely from scratch. Let's do something very simple, a function that returns the numbers from 1 to an argument and those numbers doubled. The first version uses a pre-defined type as its return type and internal type.
create type numtype as (num int, doublenum int);
create or replace function GetNum(int) returns setof numtype as
'
declare
r numtype%rowtype;
i int;
begin
for i in 1 .. $1 loop
r.num := i;
r.doublenum := i*2;
return next r;
end loop;
return;
end
'
language 'plpgsql';
It's pretty simple. The function makes a variable of the rowtype numtype and for each number from 1 to the passed argument assigns num and doublenum and then does return next r; in order to enqueue a row of output. We can do the same thing using a record type so that we do not need an outside type, however it is much more complicated and involves a bogus select.
create function InsertEmployee(int,text,int,int) returns
??? as '
insert into employee values ($1, $2, $3, $4);
' language 'SQL'
create function InsertEmployee(int,text,int,int) return int
as '
insert into employee values ($1, $2, $3, $4);
//How do i capture any errors???
//if no errors return success [0] else error code or something
' language 'SQL'
The following simplified example shows what I'm talking
about
(I know this could be done with sub-selects, but in more
complicated
cases it must be done interatively):
create type returntype as
(
a int,
b int,
c_type1 varchar,
c_type2 varchar,
d_type1 varchar
d_type2 varchar
);
create function tst_func() returns setof returntype as
'
declare
r returntype%rowtype;
rLoopA RECORD;
rLoopB RECORD;
begin
for rLoopA IN
select a, b from foo where argle
loop
r.a := rLoopA.a;
r.b := rLoopA.b;
for rLoopB IN
select distinct on (foo, bar) foo, bar, data from sometable where
bargle
-- this select may return a row for for every column of returntype
-- if a row/column is not returned, it should show null in the
result set
loop
if foo = type1 and bar = c then
r.c_type1 := rLoopB;
else
r.c_type1 := NULL; -- note the explicit set to null
end if;
if foo = type2 and bar = c then
r.c_type2 := rLoopB;
else
r.c_type2 := NULL;
end if;
if foo = type1 and bar = d then
r.d_type1 := rLoopB;
else
r.d_type1 := NULL;
end if;
if foo = type2 and bar = d then
r.d_type2 := rLoopB;
else
r.d_type2 := NULL;
end if;
end loop;
return next r;
end loop;
end;
' language 'plpgsql';
SELECT GetEmployees();
I get a list of obvious numbers. That might be ok.
But If I give a SELECT * from GetEmployees();
then I get a ---> ERROR: parser: parse
error at or near "(".
Does someone know what is wrong with the example? I
am using postgreSQL version 7.2.2
create or replace function
get_current_rec(numeric) returns setof rec as
'
declare
r rec%rowtype;
begin
for r in select a, b, c, d as total from table where key = $1 loop
if r.replaced_by IS NULL THEN
return next r;
ELSE
raise notice ''trying to fetch record for %'',r.replaced_by;
select into r * from get_current_rec(r.replaced_by);
return next r;
end if;
end loop;
return;
end
'
language 'plpgsql';
For the longest time I was stuck on getting 0
records back. Turns out selecting into r and calling next fixed
that. I've tested this with 4 levels of recursion so far and its
worked, so I believe it is correct.
create type foo as (blah int, blum
int);
create type bar as (words text, when timestamp);
create type things as (foo, bar);
create function something() returns setof things as '
statements;
'