Postgres 存储过程为多次调用 now() 返回相同的纪元
Postgres stored procedure returning same epoch for multiple calls to now()
我写了一段代码来使用下面的存储过程获取过程调用的执行时间。但问题是输出显示 start_time
和 end_time
具有相同的纪元值,因此 execution_time 总是返回 0。你能帮我看看为什么我得到相同的纪元值吗调用 now() ?
CREATE OR REPLACE PROCEDURE public.calculate_execution_time()
LANGUAGE plpgsql
AS $procedure$
DECLARE
start_time float;
end_time float;
BEGIN
SELECT extract(epoch from now()) into start_time;
call public.another_stored_procedure();
SELECT extract(epoch from now()) into end_time;
raise notice 'Execution time = % - % = % seconds', end_time, start_time, (end_time - start_time);
END;
$procedure$
;
示例输出:
call public.calculate_execution_time()
Execution time = 1620213218.485702 - 1620213218.485702 = 0 seconds
具有now()
return 固定时间(事务开始时)被认为是一项功能而不是错误。它实际上对长运行存储过程很方便。
您需要 clock_timestamp()
:
BEGIN
SELECT extract(epoch from clock_timestamp) into start_time;
call public.another_stored_procedure;
SELECT extract(epoch from clock_timestamp) into end_time;
raise notice 'Execution time = % - % = % seconds', end_time, start_time, (end_time - start_time);
END;
我写了一段代码来使用下面的存储过程获取过程调用的执行时间。但问题是输出显示 start_time
和 end_time
具有相同的纪元值,因此 execution_time 总是返回 0。你能帮我看看为什么我得到相同的纪元值吗调用 now() ?
CREATE OR REPLACE PROCEDURE public.calculate_execution_time()
LANGUAGE plpgsql
AS $procedure$
DECLARE
start_time float;
end_time float;
BEGIN
SELECT extract(epoch from now()) into start_time;
call public.another_stored_procedure();
SELECT extract(epoch from now()) into end_time;
raise notice 'Execution time = % - % = % seconds', end_time, start_time, (end_time - start_time);
END;
$procedure$
;
示例输出:
call public.calculate_execution_time()
Execution time = 1620213218.485702 - 1620213218.485702 = 0 seconds
具有now()
return 固定时间(事务开始时)被认为是一项功能而不是错误。它实际上对长运行存储过程很方便。
您需要 clock_timestamp()
:
BEGIN
SELECT extract(epoch from clock_timestamp) into start_time;
call public.another_stored_procedure;
SELECT extract(epoch from clock_timestamp) into end_time;
raise notice 'Execution time = % - % = % seconds', end_time, start_time, (end_time - start_time);
END;