SQL transaction_timestamp function
Returns the time the current transaction began (identical to NOW()), with statement_timestamp() as the per-statement sibling.
- Categoría
- Dates & times
- Returns
timestamptz- In this category
- 19 of 121
Sintaxis
transaction_timestamp()
statement_timestamp()
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Different name or syntax | no function; sys.dm_tran_active_transactions.transaction_begin_time |
| PostgreSQL | Sí | Supported under this name. |
| MySQL | Different name or syntax | no function; information_schema.innodb_trx.trx_started |
| SQLite | Not built in | no transaction clock |
| Oracle | Different name or syntax | no function; V$TRANSACTION.START_DATE, which needs privileges |
Ejemplos
PostgreSQL
BEGIN; SELECT transaction_timestamp() AS txn_start; SELECT pg_sleep(2), transaction_timestamp() AS same_txn_start; COMMIT;
PostgreSQL
SELECT statement_timestamp() AS stmt_start FROM orders LIMIT 1;
Conviene saberlo
- Every statement in a long transaction sees the same transaction_timestamp(), so rows inserted minutes apart in one transaction get identical created_at values.
- statement_timestamp() resets on each statement, so it differs from transaction_timestamp() everywhere except the first statement.
- Elsewhere the clock functions freeze per statement at most. SQL Server exposes the start time only through sys.dm_tran_active_transactions, filtered on CURRENT_TRANSACTION_ID().
Questions about transaction_timestamp
Is transaction_timestamp different from NOW()?
No. They are aliases. Both freeze at transaction start; statement_timestamp() is the one that resets per statement.
Why would I want a frozen timestamp?
Consistency: every row a transaction writes gets one value, so a batch can be found again by its timestamp and nothing in it looks older than the rest.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
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.