now() default values are all showing same timestamp

前端 未结 2 1442
天涯浪人
天涯浪人 2020-12-28 14:05

I have created my tables with a column (type: timestamp with timezone) and set its default value to now() (current_timestamp()).

I run a

相关标签:
2条回答
  • 2020-12-28 14:13

    That is expected and documented behaviour:

    From the manual:

    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.

    If you want something that changes each time you run a statement, you need to use statement_timestamp() or even clock_timestamp() (again see the description in the manual)

    0 讨论(0)
  • 2020-12-28 14:17

    now() and current_timestamp are STABLE functions returning the point in time when the transaction started as timestamptz.

    Consider one of the other options PostgreSQL offers, in particular statement_timestamp(). The manual:

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

    0 讨论(0)
提交回复
热议问题