user9174050
user9174050

Reputation: 11

PostgreSQL now() value don't change value in function

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;

the problem is: the value of now() don't change during execution of function

Example

...
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!

Upvotes: 1

Views: 1210

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51466

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() timestamp with time zone Current date and time (changes during statement execution);

or

statement_timestamp() timestamp with time zone Current date and time (start of current statement);

not the

now() timestamp with time zone Current date and time (start of current transaction);

formatting mine.

also check the https://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

Upvotes: 4

Related Questions