PostgreSQL Tutorial: CLOCK_TIMESTAMP Function

October 24, 2023

Summary: The PostgreSQL CLOCK_TIMESTAMP() function returns the current date and time with time zone, which is the time when the function executes.

Table of Contents

clock_timestamp() is a system function returning a timestamp representing the point-in-time the function was executed.

Usage

clock_timestamp ()  timestamp with time zone

clock_timestamp() returns the time and date current when the function was executed. In contrast to current_timestamp, which returns the time and date current at the start of the current transaction, the value returned by clock_timestamp() will change for each execution of the function.

Examples

Basic execution example for clock_timestamp():

postgres=# SELECT clock_timestamp();
        clock_timestamp        
-------------------------------
 2021-06-17 16:08:07.867682+01
(1 row)

The timestamp returned by clock_timestamp() will increase each time the function is executed within a query:

postgres=# SELECT
              clock_timestamp(),
              statement_timestamp(),
              current_timestamp,
              clock_timestamp()\gx
-[ RECORD 1 ]-------+------------------------------
clock_timestamp     | 2021-06-17 16:12:37.167085+01
statement_timestamp | 2021-06-17 16:12:37.166871+01
current_timestamp   | 2021-06-17 16:12:37.166871+01
clock_timestamp     | 2021-06-17 16:12:37.167086+01

In the above query, the values reported for statement_timestamp() and current_timestamp represent the point-in-time when statement execution started, and hence report an earlier time than the first invocation of clock_timestamp().

See more

PostgreSQL Tutorial: Date Functions

PostgreSQL Documentation: Date/Time Functions and Operators