October 24, 2023
Summary: The PostgreSQL STATEMENT_TIMESTAMP()
function returns the current date and time with time zone, which is the time when the current statement starts.
Table of Contents
A function returning the point-in-time the current query started.
statement_timestamp()
is a system function returning a timestamp representing the point-in-time at which the current statement started.
Usage
statement_timestamp ( ) → timestamp with time zone
The value returned is the time of receipt of the latest command message from the client. If it is the first and only statement in a transaction, the value returned will be the same as the value returned by transaction_timestamp()
/ current_timestamp
.
Examples
Basic execution example for statement_timestamp()
:
postgres=# SELECT statement_timestamp();
statement_timestamp
-------------------------------
2021-06-17 17:51:41.791603+01
(1 row)
With a statement executed outside of an explicit transaction, the timestamp returned by statement_timestamp()
is identical to that returned by transaction_timestamp()
:
postgres=# SELECT
transaction_timestamp(),
statement_timestamp(),
clock_timestamp(),
clock_timestamp()\gx
-[ RECORD 1 ]---------+------------------------------
transaction_timestamp | 2021-06-17 18:06:45.891294+01
statement_timestamp | 2021-06-17 18:06:45.891294+01
clock_timestamp | 2021-06-17 18:06:45.891497+01
clock_timestamp | 2021-06-17 18:06:45.891497+01
The same query executed in an explicit transaction:
postgres=# BEGIN;
BEGIN
postgres=# SELECT
transaction_timestamp(),
statement_timestamp(),
clock_timestamp(),
clock_timestamp()\gx
-[ RECORD 1 ]---------+------------------------------
transaction_timestamp | 2021-06-17 18:09:28.23169+01
statement_timestamp | 2021-06-17 18:09:30.726471+01
clock_timestamp | 2021-06-17 18:09:30.726765+01
clock_timestamp | 2021-06-17 18:09:30.726766+01
See more
PostgreSQL Tutorial: Date Functions
PostgreSQL Documentation: Date/Time Functions and Operators