drop table c;
drop table c1;
drop table c2;
drop function tr_c1_summary ();

create table c (id int,primary key (id));
insert into c values (1);
insert into c values (2);

create table c1 ( id int references c(id), value int );
insert into c1 values (1,1);
insert into c1 values (1,2);
insert into c1 values (1,3);
insert into c1 values (2,4);
insert into c1 values (2,6);
insert into c1 values (2,7);

create table c2 ( id int, value_sum int);

create function tr_c1_summary () RETURNS OPAQUE AS '
    BEGIN
	/* update game_rating_summary */
	delete from c2 where id = NEW.id;

	insert into c2 (id, value_sum)
	select id, sum(value) from c1 
	where id = NEW.id group by id;

	RETURN NEW;
    END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER tr_c1_update AFTER UPDATE OR INSERT ON c1 
FOR EACH ROW EXECUTE PROCEDURE tr_c1_summary();

update c1 set value = value;
select * from c2;
select distinct id from c;
select distinct id from c1;
select distinct id from c2;
