CREATE OR REPLACE FUNCTION region_boundary_update() RETURNS trigger AS '
 DECLARE
  rid INT;
 BEGIN
  SELECT INTO rid id FROM region WHERE boundary IS NOT NULL AND id = new.id;
  IF FOUND THEN
    SELECT INTO rid regionid FROM region_updates WHERE regionid = new.id;
    IF NOT FOUND THEN
      INSERT INTO region_updates (regionid, updatetype) VALUES (new.id, ''boundary'');
    END IF;
  END IF;
  RETURN NULL;
 END
' LANGUAGE plpgsql;

CREATE TRIGGER region_boundary_inert_trigger AFTER INSERT ON region FOR EACH ROW EXECUTE PROCEDURE region_boundary_update();

CREATE TRIGGER region_boundary_update_trigger AFTER UPDATE OF boundary ON region FOR EACH ROW EXECUTE PROCEDURE region_boundary_update();

CREATE TRIGGER region_parent_update_trigger AFTER UPDATE OF parent_id ON region FOR EACH ROW EXECUTE PROCEDURE region_boundary_update();

