我想在触发器中计算时间码块
I want to calculate time code block in trigger
我在 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 毫秒。 :)
问题出在 current_timestamp
。整个交易都是一样的。
使用clock_timestamp
代替
它将反映时钟时间。
我在 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 毫秒。 :)
问题出在 current_timestamp
。整个交易都是一样的。
使用clock_timestamp
代替
它将反映时钟时间。