PostgreSQL触发器间歇执行触发器函数

时间:2018-09-07 12:09:03

标签: sql postgresql

-- this is the trigger --
create trigger vrms_ave_and_irms_total_snr_monitor_trigger
    on public.snr_monitor
    for each row
    execute procedure 
    public.log_vrms_ave_and_irms_total_on_snr_monitor(); 



-- this is the stored procedure --
create or replace function 
    public.log_vrms_ave_and_irms_total_on_snr_monitor()
        return trigger as 
            $body$

            begin
                update snr_monitor as t1
                set 
                    vrms = round(cast(t2.v_average as numeric), 3),
                    irms = round(cast(t2.i_sum as numeric), 3)
                from (
                    select
                        device_id
                        rdts, 
                        avg(vrms) as v_average,
                        sum(irms) as i_sum
                    from snr_monitor
                    where phase < 3
                    group by device_id, rdts
                    order by device_id, rdts desc
                    limit 1
                ) as t2 
                where t1.device_id = t2.device_id
                and t1.rdts = t2.rdts
                and t1.phase = 3;

                return null;
            end;

            $body$

language plpgsql volatile
cost 100;
alter function public.log_vrms_ave_and_irms_total_on_snr_monitor()
owner to postgresadmin;

触发器正在监视应该执行更新过程的同一表。

每当相位等于0到2的行上发生插入时,应该更新相位等于3的行

触发器及其过程正在执行,但是有些触发器却错过了某些行的更新。

知道为什么会这样吗?

0 个答案:

没有答案