简体   繁体   中英

Postgresql does not update last updated row by trigger

I created a trigger. When I update a row the trigger before doesn't work. Later, when I update a row second time the trigger is working. Why doesn't work before?

CREATE TRIGGER trg_update_em_z_module_mod_codes
  before UPDATE
ON emlakmuayene.em_z_module
FOR EACH ROW
  EXECUTE PROCEDURE emlakmuayene.update_em_z_module_mod_codes();

CREATE OR REPLACE FUNCTION emlakmuayene.update_em_z_module_mod_codes()
  RETURNS trigger AS
$BODY$
    BEGIN   
        if (TG_OP = 'UPDATE') then 
        if new.mod_codes_ids is not null  then      
        new.mod_codes=
        (
        SELECT translate(array_Agg(pages)::Text,'{}','') as mod_codes 
        FROM emlakmuayene.em_z_module  t1 
        join emlakmuayene.em_z_order_pages_view  t2 
        on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_Seq_X=new.id_seq_x  group by t1.id_Seq_X );          
        end if ;
        end if;
    RETURN new;
    END;    
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

After added my trigger IF pg_trigger_depth() <> 1 THEN RETURN NEW; and change my update sql update emlakmuayene.em_z_module t1 set mod_codes = t2.mod_codes ,reg=1 from ( SELECT t1.id_Seq_x ,translate(array_Agg(pages)::Text,'{}','') as mod_codes FROM emlakmuayene.em_z_module t1 join emlakmuayene.em_z_order_pages_view t2 on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_seq_x=new.id_seq_x group by t1.id_Seq_X ) t2 where t1.id_seq_x=T2.id_seq_x; is working

then my trigger CREATE OR REPLACE FUNCTION emlakmuayene.update_em_z_module_mod_codes() RETURNS trigger AS $BODY$ BEGIN
IF pg_trigger_depth() <> 1 THEN RETURN NEW; END IF; update emlakmuayene.em_z_module t1 set mod_codes = t2.mod_codes ,reg=1 from ( SELECT t1.id_Seq_x ,translate(array_Agg(pages)::Text,'{}','') as mod_codes FROM emlakmuayene.em_z_module t1 join emlakmuayene.em_z_order_pages_view t2 on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_seq_x=new.id_seq_x group by t1.id_Seq_X ) t2 where t1.id_seq_x=T2.id_seq_x; return new; END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION emlakmuayene.update_em_z_module_mod_codes() OWNER TO tuvimer_ortak
CREATE OR REPLACE FUNCTION emlakmuayene.update_em_z_module_mod_codes() RETURNS trigger AS $BODY$ BEGIN
IF pg_trigger_depth() <> 1 THEN RETURN NEW; END IF; update emlakmuayene.em_z_module t1 set mod_codes = t2.mod_codes ,reg=1 from ( SELECT t1.id_Seq_x ,translate(array_Agg(pages)::Text,'{}','') as mod_codes FROM emlakmuayene.em_z_module t1 join emlakmuayene.em_z_order_pages_view t2 on t2.id_seq_x = any ( ('{'||t1.mod_codes_ids||'}')::bigint[]) where t1.id_seq_x=new.id_seq_x group by t1.id_Seq_X ) t2 where t1.id_seq_x=T2.id_seq_x; return new; END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION emlakmuayene.update_em_z_module_mod_codes() OWNER TO tuvimer_ortak

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM