> This page location: Date Functions > STATEMENT_TIMESTAMP
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL STATEMENT_TIMESTAMP() Function

**Info:** The STATEMENT_TIMESTAMP() function works the same on any PostgreSQL database, so everything here carries over to whatever Postgres you run. If you're an enterprise looking for managed Postgres built for the AI era, [Lakebase](https://www.databricks.com/product/lakebase) delivers performance, security, and native integration with the Lakehouse. If you're a developer or startup who needs to ship and scale quickly, [Neon](https://neon.com) is the Postgres platform built for you.

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `STATEMENT_TIMESTAMP()` function to retrieve the start time of the current statement.

## Introduction to the PostgreSQL STATEMENT_TIMESTAMP() function

The `STATEMENT_TIMESTAMP()` function returns the start time of the current statement.

Here's the syntax of the `STATEMENT_TIMESTAMP()` function:

```sql
STATEMENT_TIMESTAMP()
```

The `STATEMENT_TIMESTAMP()` function doesn't accept any argument. It returns a value of the type `TIMESTAMP WITH TIME ZONE`, representing a [timestamp](../postgresql-tutorial/postgresql-timestamp) at the start of the current statement.

## PostgreSQL STATEMENT_TIMESTAMP() function examples

Let's take some examples of using the `STATEMENT_TIMESTAMP()` function.

### 1) Basic statement_timestamp() function example

The following statement uses the `STATEMENT_TIMESTAMP()` function to retrieve the start time of the current statement:

```sql
SELECT STATEMENT_TIMESTAMP();
```

Output:

```text
      statement_timestamp
-------------------------------
 2024-03-20 11:30:47.001021-07
(1 row)
```

The output indicates that the `STATEMENT_TIMESTAMP()` function returns a timestamp with a time zone of the start time when the statement is executed.

### 2) Using the statement_timestamp() within a transaction

The following example calls the `STATEMENT_TIMESTAMP()` function within a transaction multiple times and log the result into a table:

```sql
-- create a new table for logging
CREATE TABLE logs(
   id SERIAL PRIMARY KEY,
   started_at TIMESTAMP WITH TIME ZONE
);

-- start a transaction
BEGIN;

INSERT INTO logs(started_at) VALUES(statement_timestamp());
SELECT pg_sleep(3);

INSERT INTO logs(started_at) VALUES(statement_timestamp());
SELECT pg_sleep(3);

INSERT INTO logs(started_at) VALUES(statement_timestamp());
END;

-- retrieve data from the logs table
SELECT * FROM logs;
```

Output:

```text
 id |          started_at
----+-------------------------------
  1 | 2024-03-20 13:22:13.056783+07
  2 | 2024-03-20 13:22:16.228492+07
  3 | 2024-03-20 13:22:19.390211+07
(3 rows)
```

In this example, we use the `pg_sleep()` function to delay the execution of each [INSERT](../postgresql-tutorial/postgresql-insert) statement.

Since we invoke the `STATEMENT_TIMESTAMP()` function in its own SQL statement, it returns a timestamp differently with each call.

Notice that the `STATEMENT_TIMESTAMP()` function is unlike the `TRANSACTION_TIMESTAMP()` function which does not change with each statement. The `TRANSACTION_TIMESTAMP()` will return the same start time of the transaction.

### 3) Call the statement_timestamp() function multiple times within a statement

The following example calls the `STATEMENT_TIMESTAMP()` function multiple times within a single statement:

```sql
SELECT
  statement_timestamp(),
  pg_sleep(3),
  statement_timestamp(),
  pg_sleep(3),
  statement_timestamp();
```

Output:

```
-[ RECORD 1 ]-------+------------------------------
statement_timestamp | 2024-03-20 13:52:55.861004-07
pg_sleep            |
statement_timestamp | 2024-03-20 13:52:55.861004-07
pg_sleep            |
statement_timestamp | 2024-03-20 13:52:55.861004-07
```

Note that to display vertical results in psql, you execute the \x command first.

In this example, the `STATEMENT_TIMESTAMP()` function returns the same values for all three calls, even though we call the `pg_sleep()` to delay execution between each call.

It is important to notice that this behavior contrasts with the [`CLOCK_TIMESTAMP()`](https://neon.com/postgresql/date-functions/postgresql-clock_timestamp) function, which continues to change as it progresses through the statement.

## Summary

- Use the `STATEMENT_TIMESTAMP()` function to retrieve the start time of the current statement.

---

## Related docs (Date Functions)

- [AGE](https://neon.com/postgresql/date-functions/age)
- [AT TIME ZONE Operator](https://neon.com/postgresql/date-functions/at-time-zone)
- [CLOCK_TIMESTAMP](https://neon.com/postgresql/date-functions/clock_timestamp)
- [CURRENT_DATE](https://neon.com/postgresql/date-functions/current_date)
- [CURRENT_TIME](https://neon.com/postgresql/date-functions/current_time)
- [CURRENT_TIMESTAMP](https://neon.com/postgresql/date-functions/current_timestamp)
- [DATE_PART](https://neon.com/postgresql/date-functions/date_part)
- [DATE_TRUNC](https://neon.com/postgresql/date-functions/date_trunc)
- [EXTRACT](https://neon.com/postgresql/date-functions/extract)
- [ISFINITE](https://neon.com/postgresql/date-functions/isfinite)
- [JUSTIFY_DAYS](https://neon.com/postgresql/date-functions/justify_days)
- [JUSTIFY_HOURS](https://neon.com/postgresql/date-functions/justify_hours)
- [JUSTIFY_INTERVAL](https://neon.com/postgresql/date-functions/justify_interval)
- [LOCALTIME](https://neon.com/postgresql/date-functions/localtime)
- [LOCALTIMESTAMP](https://neon.com/postgresql/date-functions/localtimestamp)
- [MAKE_DATE](https://neon.com/postgresql/date-functions/make_date)
- [MAKE_INTERVAL](https://neon.com/postgresql/date-functions/make_interval)
- [MAKE_TIME](https://neon.com/postgresql/date-functions/make_time)
- [NOW](https://neon.com/postgresql/date-functions/now)
- [PG_SLEEP](https://neon.com/postgresql/date-functions/pg_sleep)
- [TIMEOFDAY](https://neon.com/postgresql/date-functions/timeofday)
- [TO_DATE](https://neon.com/postgresql/date-functions/to_date)
- [TO_TIMESTAMP](https://neon.com/postgresql/date-functions/to_timestamp)
