I have the following two tables: create table a ( id serial primary key, ); create table b ( id serial primary key, a_id int4 references a (id), amount decimal(16, 2) ); and would like a constraint to guarantee that "sum(b.amount) = 0 group by b.a_id".