为什么在对每一行执行时(使用 LATERAL JOIN)当前时间是常数?

Why is the current time constant when executing for every row (using a LATERAL JOIN)?

为什么在横向连接中包装 now() 函数时我可以看到相同的时间戳。

我制定了一个代码示例来举例说明:

SELECT *
FROM generate_series(1, 10000)
       LEFT JOIN LATERAL (
  SELECT now() AS now
  ) sub ON TRUE

结果相同的时间戳,例如(修剪):

1, 2020-07-22 08:48:55.038668
2, 2020-07-22 08:48:55.038668
3, 2020-07-22 08:48:55.038668
4, 2020-07-22 08:48:55.038668

我原以为每一行都包含一个新的时间戳(按递增顺序),因为 LATERAL 是逐行执行的。

这是a documented behavior(强调我的):

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.

这里的目的是保证给定交易中函数返回值的一致性。

如果您需要对每一行进行更改,请使用 clock_timestamp()