How can I get most recent time in a Postgres transaction



  • I noticed that Postgres caches the value of NOW() inside a transaction, e.g.:

    lev=# BEGIN;
    BEGIN
    lev=# SELECT now();
                  now
    -------------------------------
     2022-04-07 19:16:52.358923-07
    (1 row)
    

    lev=# SELECTpg_sleep(1);
    pg_sleep

    (1 row)

    lev=# SELECT now();
    now

    2022-04-07 19:16:52.358923-07
    (1 row)

    Is there a way to get up-to-date time inside a transaction for every call to NOW()?



  • https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT , now() (and the standard compliant current_timestamp) returns the time at the start of the transaction (and is an alias for transaction_timestamp())

    If you want something that is independent of the statement or transaction start, use clock_timestamp()


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2