我想在触发器中计算时间码块
Posted
技术标签:
【中文标题】我想在触发器中计算时间码块【英文标题】:I want to calculate time code block in trigger 【发布时间】:2019-07-18 14:04:29 【问题描述】:我在 Postgresql 中创建了一个触发器。此触发器执行一些更新语句。我想计算做这些语句的时间有多少。一个例子我有两个更新语句,我不知道这些语句的执行时间。第一个更新执行时间是什么,第二个更新执行时间是什么?
create or replace
function public.insert_sth() returns trigger language plpgsql as $function$
declare time1 timestamp without time zone;
declare time2 timestamp without time zone;
declare timediff bigint;
begin
time1:=current_timestamp;--now();
RAISE NOTICE 'Calling update healthy time1 %', time1;
if exists(
select 1
from
basetable
where
id= NEW.id) then update
basetable
set
previous_id= this_id
where
id= NEW.id;
update
basetable
set
this_id= NEW.id
where
id= NEW.id;
update
basetable
set
healthy_id= (
select id
from
sometable
where
connectionresult = 0
or connectionresult = 2
and sometable.id= NEW.id
order by
sometable.actiontime desc
limit 1)
where
id= new.id;
time2:=current_timestamp;--now();
timediff:= (EXTRACT('epoch' from time2) - EXTRACT('epoch' from time1)) * 1000;
RAISE NOTICE 'Calling update healthy check %', timediff;
RAISE NOTICE 'Calling update healthy time2 %', time2;
return new;
else insert
into
bastable(id,
this_id)
values(NEW.id,
NEW.id);
return new;
end if;
end;
$function$ ;
但输出是:
00000:调用更新健康时间1 2019-07-18 17:00:08.085
00000:调用更新健康检查0
00000:调用更新健康时间2 2019-07-18 17:00:08.085
这是不可能的。更新执行不应为 0 ms。 :)
【问题讨论】:
【参考方案1】:问题在于current_timestamp
。整个交易都是一样的。
改用 clock_timestamp
它将反映时钟时间。
DIfference between CURRENT_TIMESTAMP AND CLOCK_TIMESTAMP()
【讨论】:
@a_horse_with_no_name : 我不敢相信 session 是从哪里来的 :-)以上是关于我想在触发器中计算时间码块的主要内容,如果未能解决你的问题,请参考以下文章
如何在发送推送通知期间关闭计算机时处理 Quartz 触发器