How to get a real time within PostgreSQL transaction?

↘锁芯ラ 提交于 2019-12-18 14:00:17

问题


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?


回答1:


Timeofday()

May work for you.




回答2:


Use clock_timestamp().

now() is a traditional PostgreSQL equivalent to transaction_timestamp(), which is equivalent to CURRENT_TIMESTAMP. These functions return the start time of the current transaction. Their values do not change during the transaction.

statement_timestamp() returns the time of receipt of the latest command message from the client.

clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command.

For more information see the documentation.




回答3:


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=# 


来源:https://stackoverflow.com/questions/3363376/how-to-get-a-real-time-within-postgresql-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!