--Problem is: i have more companies and i want to make id_shutter "autoincrement" from 1 for each company (id_company and id_shutter are composite PK)
CREATE FUNCTION insert_shutter() RETURNS TRIGGER AS $insert_shutter$
BEGIN
IF exists(select 1 from shutter where id_company=new.id_company) then
SELECT MAX(id_shutter) INTO new.id_shutter FROM shutter where id_company=new.id_company;
new.id_shutter:=id_shutter+1;
ELSE
new.id_shutter=1;
end if;
RETURN NEW;
END;
$insert_shutter$ LANGUAGE plpgsql;
CREATE TRIGGER insert_shutter
BEFORE INSERT ON shutter
FOR EACH ROW
EXECUTE PROCEDURE insert_shutter();