В дополнение к ответу @ rorycl, здесь есть рабочий тестовый сценарий под PostgreSQL 8.4.8:
DROP TYPE IF EXISTS matview_location_slots_info;
CREATE TYPE matview_location_slots_info AS
(
node varchar,
dater date,
people integer[]
);
DROP TABLE IF EXISTS locationnodes;
CREATE TABLE locationnodes
(
rs_node varchar,
rs_date date,
rs_people_c integer[]
);
INSERT INTO locationnodes VALUES
('aaa', '2011-01-01', '{}'),
('bbb', '2011-01-02', '{}'),
('ccc', '2011-01-03', '{731}'),
('ddd', '2011-01-04', '{32}'),
('eee', '2011-01-05', '{31}'),
('fff', '2011-01-06', '{}'),
('ggg', '2011-01-07', '{62}'),
('hhh', '2011-01-08', '{540, 72, 69, 53, 37, 42, 201, 51, 58}'),
('iii', '2011-01-09', '{64}');
Функция PL / pgSQL:
CREATE OR REPLACE FUNCTION fn_matview_location_slots (week_start date)
RETURNS setof matview_location_slots_info AS $$
DECLARE
resulter matview_location_slots_info%ROWTYPE;
BEGIN
FOR resulter IN
SELECT
rs_node AS node,
rs_date AS dater,
rs_people_c AS people
FROM
locationnodes
WHERE
rs_date >= week_start
LOOP
RETURN NEXT resulter;
END LOOP;
END; $$ LANGUAGE plpgsql;
Результат:
SELECT fn_matview_location_slots('2011-01-01');
fn_matview_location_slots
---------------------------------------------------
(aaa,2011-01-01,{})
(bbb,2011-01-02,{})
(ccc,2011-01-03,{731})
(ddd,2011-01-04,{32})
(eee,2011-01-05,{31})
(fff,2011-01-06,{})
(ggg,2011-01-07,{62})
(hhh,2011-01-08,"{540,72,69,53,37,42,201,51,58}")
(iii,2011-01-09,{64})
(9 rows)