STATEMENT_TIMESTAMP

Return the timestamp captured at the start of the current statement.

Category: datetimeReturns: TIMESTAMP WITH TIME ZONEDialect: PostgreSql

Syntax

STATEMENT_TIMESTAMP()

Description

## Overview Returns the current date and time (with time zone) captured at the start of the currently executing statement. Unlike NOW() and TRANSACTION_TIMESTAMP (which are fixed for the duration of the entire transaction), STATEMENT_TIMESTAMP produces a fresh value for each top-level statement. This makes it ideal for audit trails that need to distinguish 'when this row was written' from 'when the enclosing transaction started'. The value is stable within a single statement, so all row expressions in one statement see the same instant. ## Behavior - Returns a TIMESTAMP WITH TIME ZONE value. - Never returns NULL. - Stable within a statement; all references in a single statement return the same instant. - Advances between statements inside the same transaction. - In autocommit mode each statement is its own transaction, so STATEMENT_TIMESTAMP and TRANSACTION_TIMESTAMP converge. - Requires the function-call parentheses. ## Timezone handling - Carries the session time zone offset. - Use AT TIME ZONE for explicit zone conversion. ## Compatibility - Extension beyond the SQL standard. The standard provides only CURRENT_TIMESTAMP (transaction scope) and does not expose statement-level or call-level variants.

Examples

-- Timestamp at which the current statement began
SELECT STATEMENT_TIMESTAMP();
-- Side-by-side comparison with transaction-scope and call-scope timestamps
SELECT TRANSACTION_TIMESTAMP() AS txn_ts,
       STATEMENT_TIMESTAMP() AS stmt_ts,
       CLOCK_TIMESTAMP() AS clock_ts;
-- Per-statement audit logging
INSERT INTO security.audit.access_log (action, logged_at)
VALUES ('integrity_check', STATEMENT_TIMESTAMP());
-- Tag every row with the time the statement started executing
SELECT order_id, STATEMENT_TIMESTAMP() AS scanned_at
FROM commerce.sales.orders;
-- Verify that STATEMENT_TIMESTAMP advances between statements in a transaction
BEGIN;
SELECT STATEMENT_TIMESTAMP() AS first_stmt;
SELECT STATEMENT_TIMESTAMP() AS second_stmt;
COMMIT;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →