Zerknij na to:
create or replace function f_palenisko_tr() RETURNS trigger AS
$body$BEGIN
if tg_op = 'INSERT' then
IF NEW.geom IS not NULL THEN
new.nr_dom = (select dom.id FROM dom WHERE ST_WITHIN(new.geom,dom.geom) order by dom.wiek desc limit 1);
END IF;
RETURN NEW;
elsif tg_op = 'UPDATE' then
new.nr_dom = (select dom.id FROM dom WHERE ST_WITHIN(new.geom,dom.geom) order by dom.wiek desc limit 1);
RETURN NEW;
END IF;
END;$body$
LANGUAGE plpgsql;
CREATE TRIGGER tr_palenisko BEFORE INSERT OR UPDATE ON palenisko FOR EACH ROW EXECUTE PROCEDURE f_palenisko_tr();
create or replace function f_dom_tr() RETURNS trigger AS
$body$ BEGIN
raise notice '%,%,%',tg_table_name,tg_when,tg_op;
UPDATE palenisko set nr_dom = null--d.nr_dom
from (select p.id,d.id as nr_dom,row_number() over (partition by p.id order by d.wiek desc) as lp
from palenisko p
left join dom d on ST_WITHIN(p.geom,d.geom)
) d
where d.id=palenisko.id and d.lp=1
and coalesce(palenisko.nr_dom,0) != coalesce(d.nr_dom,0);
return new;
END;$body$
LANGUAGE plpgsql;
CREATE TRIGGER tr_dom AFTER INSERT OR UPDATE OR DELETE ON dom FOR EACH STATEMENT EXECUTE PROCEDURE f_dom_tr();
PS
założyłem, że tabele wyglądają tak jak niżej
create table palenisko(id serial primary key ,nr_dom int ,geom geometry);
create table dom(id serial primary key, wiek varchar(10) ,geom geometry);
CREATE INDEX palenisko_dom_ix ON public.palenisko(nr_dom);