PostgreSQL now()的值不会更改函数中的值 [英] PostgreSQL now() value don't change value in function
问题描述
我需要测量时间间隔以测试功能的持续时间...
1)在功能开始时,我将now()的值分配给名为v_start的变量;
2)在函数结束之前,我将now()的值分配给名为v_end的变量;
I need measure timelaps for testing the duration of function... 1) at the begin of function I assign value of now() to a variable called v_start; 2) before end the function I assign value of now() to a variable called v_end;
问题是:now()的值不函数执行过程中的更改
the problem is: the value of now() don't change during execution of function
示例
...
begin
v_start := now(); ex. "2018-02-14 10:03:52.394263+01"
...
...
v_end := now(); -- this value is equal to v_start
select EXTRACT(EPOCH FROM now()-now())/3600 into v_timelaps;
-- v_timelaps is always 0;
end;
任何解决方案???谢谢!
Any solutions??? Thanx!
推荐答案
不应该。因为函数在事务中运行,并且函数运行时事务开始时间不变。
It is not supposed to. Because function runs in transaction and transaction start time does not change while function runs.
https://www.postgresql.org/docs/current/static/functions-datetime.html
clock_timestamp()
带有时区的时间戳
当前日期和时间
(语句执行期间的变化);
clock_timestamp()
timestamp with time zone
Current date and time (changes during statement execution);
或
statement_timestamp()
带有时区的时间戳
当前日期和
时间(开始于当前语句);
statement_timestamp()
timestamp with time zone
Current date and time (start of current statement);
不是
now()
带时区的时间戳记
当前日期和时间(
当前交易的开始);
now()
timestamp with time zone
Current date and time (start of current transaction);
格式化矿井。
还要检查 https://www.postgresql.org/docs/current/static/functions-datetime .html#FUNCTIONS-DATETIME-CURRENT
这篇关于PostgreSQL now()的值不会更改函数中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!