// dates & times reference

SQL clock_timestamp function

Returns the actual current time, re-read on every call, the only PostgreSQL timestamp function that moves inside a transaction.

类别
Dates & times
Returns
timestamptz
In this category
18 of 121

语法

clock_timestamp()
clock_timestamp() AT TIME ZONE 'UTC'

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支持

引擎Support备注
SQL Server Different name or syntax SYSDATETIME() is fixed per query but re-read by each statement in a batch
PostgreSQL 是 Supported under this name.
MySQL Different name or syntax SYSDATE() is the per-call clock; NOW() is fixed per statement
SQLite Different name or syntax 'now' is stable only within one sqlite3_step() call
Oracle Different name or syntax SYSTIMESTAMP is fixed per SQL statement; each PL/SQL call re-reads it

示例

PostgreSQL

SELECT clock_timestamp() AS tick_one,
       pg_sleep(0.5),
       clock_timestamp() AS tick_two;

MySQL

SELECT SYSDATE(6) AS tick_one, SLEEP(1), SYSDATE(6) AS tick_two;

值得了解

  • NOW() and transaction_timestamp() freeze for the whole transaction, statement_timestamp() for one statement; only clock_timestamp() advances between rows of one query.
  • For timing steps inside a PL/pgSQL function, use clock_timestamp(): the whole function runs in one transaction, so NOW() returns the same value at every step.
  • MySQL's SYSDATE() is the same idea but is unsafe with statement-based replication; NOW() is the replication-safe choice.

Questions about clock_timestamp

Why does NOW() not change inside my loop?

PostgreSQL freezes NOW() at transaction start, and a function or DO block runs inside one transaction. clock_timestamp() advances call to call.

What is MySQL's equivalent?

SYSDATE(): it evaluates per call, while NOW() is constant for the whole statement.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.