Home PostgreSQL now() value don't change value in function
Reply: 1

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

user9174050
1#
user9174050 Published in 2018-02-14 09:25:22Z

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!

Vao Tsun
2#
Vao Tsun Reply to 2018-02-14 09:39:24Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314191 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO