Postgres:在查询的更多地方使用 NOW() 是否保证始终相同?

Postgres: is NOW() used in more places of the query guaranteed to be always the same?

我正在编写一个查询,其中您在 table 中有票。 first_use_on 列表示第一次使用的时间戳。 first_use_on 默认为 NULL,然后仅在第一次使用后更新。我的查询工作正常,但现在我需要在查询之外知道运行的查询是否触发 first_use_on,所以我考虑在 RETURNING 中添加 first_use_on = NOW() AS is_first_usage。我可以 100% 确定返回中比较的 NOW() 始终与 UPDATE 部分中使用的相同吗?在某些情况下它们会有所不同吗?

UPDATE
    l_codes
SET first_use_on = (
    CASE WHEN first_use_on IS NULL THEN 
        NOW()
    ELSE 
        first_use_on 
    END )
WHERE
    l_code = 'C9TCH' AND id_mostra = 1
RETURNING 
    first_use_on,
    first_use_on = NOW() AS is_first_usage,
    NOW() > DATE_TRUNC('day', first_use_on + INTERVAL '1 DAY') AS expired,
    DATE_TRUNC('day', first_use_on + INTERVAL '1 DAY') AS expiration_on;

是的。如 now():

的文档中所指定

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.

整个语句的事务开始是不变的。

Postgres: is NOW() used in more places of the query guaranteed to be always the same?

是的。它 returns 当前交易开始的时间戳,如 explained in the documentation:

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.