关于postgresql:now()默认值都显示相同的时间戳

now() default values are all showing same timestamp

我用列创建了表(类型:timestamp with timezone)并将其默认值设置为now()(current_timestamp())。

我在单个函数中的单独语句中运行一系列插入,我注意到所有时间戳都等于(ms),是否为整个函数调用或事务缓存和共享了函数值?


这是预期和记录的行为:

从手册:

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.

如果您希望每次运行语句时都有更改,则需要使用statement_timestamp()或甚至clock_timestamp()(再次参阅手册中的说明)


now()current_timestampSTABLE函数,在事务开始时返回时间戳。

考虑PostgreSQL提供的其他选项之一,特别是statement_timestamp()
每个文件:

statement_timestamp() returns the start time of the current statement (more specifically, the time of receipt of the latest command message from the client)