views:

41

answers:

3

As far as I understand now() returns the same time during the whole PostgreSQL transaction? But how to get real time?

Also, I am interested if there any configuration parameter to limit duration of transaction, so that after this period expiration transaction would immediately fail or somehow else prohibit following queries?

A: 

You can use CURRENT_TIMESTAMP.

Pablo Santa Cruz
Does clock_timestamp also still work?
Tobiasopdenbrouw
this is the same as *now()*. It will also return the same value during the whole transaction.
Yes, *clock_timestamp()* also works and also gives a real time.
A: 
Timeofday()

May work for you.

Tobiasopdenbrouw
Yes, it works. Thanks.
A quirk you have to have in mind: "timeofday() is a historical PostgreSQL function. Like clock_timestamp(), it returns the actual current time, but as a formatted text string rather than a timestamp with time zone value."
Milen A. Radev
A: 

To limit the time of a statement (not a transaction) you can use statement_timeout. now() will increment on each execution if not within a transaction block. Thus:

postgres=# select now();
              now              
-------------------------------
 2010-08-11 13:44:36.207614-07
(1 row)

postgres=# select now();
              now              
-------------------------------
 2010-08-11 13:44:36.688054-07
(1 row)

postgres=# select now();
              now              
-------------------------------
 2010-08-11 13:44:40.407623-07
(1 row)

postgres=# begin;
BEGIN
postgres=# select now();
              now              
-------------------------------
 2010-08-11 13:44:43.417611-07
(1 row)

postgres=# select now();
              now              
-------------------------------
 2010-08-11 13:44:43.417611-07
(1 row)

postgres=# 
Joshua D. Drake