当脚本工作时,Postgres now()时间戳不会更改 [英] Postgres now() timestamp doesn't change, when script works

查看:158
本文介绍了当脚本工作时,Postgres now()时间戳不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP脚本将运行几秒钟,并且我需要随时从Postgres数据库获取实时时间戳。但是,当我从Postgres获取current_timestamp时,它总是返回相同的值。

My PHP script will work for several seconds, and I need to get real timestamp from Postgres database anytime. But when I fetch current_timestamp from Postgres, it always return same value.

这是我的示例脚本(使用DBAL):

Here is my example script (using DBAL):

    echo "DB:" . $dbal->fetchColumn("select current_timestamp") . PHP_EOL;
    echo "PHP: " . date("Y-m-d H:i:s") . PHP_EOL;
    sleep(3);

    echo "DB:" . $dbal->fetchColumn("select current_timestamp") . PHP_EOL;
    echo "PHP: " . date("Y-m-d H:i:s") . PHP_EOL;
    sleep(3);

    echo "DB:" . $dbal->fetchColumn("select current_timestamp") . PHP_EOL;
    echo "PHP: " . date("Y-m-d H:i:s") . PHP_EOL;

这是输出:

DB:2014-05-07 11:59:50.118+04
PHP: 2014-05-07 11:59:50

DB:2014-05-07 11:59:50.118+04
PHP: 2014-05-07 11:59:53

DB:2014-05-07 11:59:50.118+04
PHP: 2014-05-07 11:59:56

Postgres安装在本地计算机上。为什么总是返回同一时间?
如何获取实时信息?

Postgres installed on local machine. Why it always returns same time? How to get real time?

我使用了now()函数,结果是一样的。
Postgres版本:9.3.4 x64。 PHP版本:5.5.11

I used now() function, there was same result. Postgres version: 9.3.4 x64. PHP version: 5.5.11

推荐答案

来自TFM ,突出显示我的:


9.9.4 。当前日期/时间



PostgreSQL提供了许多函数,这些函数返回与
相关的值与当前日期和时间。这些SQL标准函数所有
返回值均基于当前交易的开始时间

CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_TIME(precision)
CURRENT_TIMESTAMP(precision)
LOCALTIME
LOCALTIMESTAMP
LOCALTIME(precision)
LOCALTIMESTAMP(precision)

...

由于这些函数返回当前
事务的开始时间,因此它们的值在事务期间不会更改。该
被认为是一项功能:目的是允许单个事务
具有一致的当前时间概念,以便同一笔交易中的多个
修改可以同时承担

Since these functions return the start time of the current transaction, their values do not change during the transaction. This is considered a feature: the intent is to allow a single transaction to have a consistent notion of the "current" time, so that multiple modifications within the same transaction bear the same time stamp.

PostgreSQL还提供了返回
current语句的开始时间以及
瞬间的实际当前时间的函数。函数被调用。非SQL标准时间
函数的完整列表为:

PostgreSQL also provides functions that return the start time of the current statement, as well as the actual current time at the instant the function is called. The complete list of non-SQL-standard time functions is:

transaction_timestamp()
statement_timestamp()
clock_timestamp()
timeofday()
now()

transaction_timestamp()等效于 CURRENT_TIMESTAMP ,但命名为
以清楚地反映返回的内容。 statement_timestamp()
返回当前语句的开始时间(更具体地说,
是从客户端收到最新命令消息的时间)。
statement_timestamp() transaction_timestamp()在以下命令的第一个命令中返回相同的
值交易,但在后续命令中可能会不同
clock_timestamp()返回实际的
当前时间
,因此,即使在单个SQL
命令中,其值也会更改。 timeofday()是PostgreSQL的历史功能。像
clock_timestamp()一样,它返回实际的当前时间,但是以
格式的文本字符串而不是带有时区值的时间戳来返回。
now()是传统的PostgreSQL,等效于 transaction_timestamp()

transaction_timestamp() is equivalent to CURRENT_TIMESTAMP, but is named to clearly reflect what it returns. statement_timestamp() returns the start time of the current statement (more specifically, the time of receipt of the latest command message from the client). statement_timestamp() and transaction_timestamp() return the same value during the first command of a transaction, but might differ during subsequent commands. clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command. 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. now() is a traditional PostgreSQL equivalent to transaction_timestamp().

这篇关于当脚本工作时,Postgres now()时间戳不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆