Sub topic 7
Getting the Current Time
Assume you already know SELECT, aliases, and basic PostgreSQL data types. This chapter focuses on choosing the right current-time function when transaction timing and clock accuracy matter.
PostgreSQL provides several ways to ask for the current date or time. The important difference is whether the value is fixed for a transaction, fixed for a statement, or read directly from the operating-system clock.
Transaction time and statement time
CURRENT_TIMESTAMP and now() return the timestamp at which the current transaction started. PostgreSQL keeps this value stable until the transaction ends. This behavior makes one transaction internally consistent.
statement_timestamp() returns the time at which the current statement began. In a normal one-statement transaction, it is usually the same as now(). In a longer transaction, later statements can have a newer statement timestamp.
BEGIN;
SELECT
now() AS transaction_time,
current_timestamp AS standard_transaction_time,
statement_timestamp() AS statement_time;
SELECT pg_sleep(2);
SELECT
now() AS transaction_time_again,
statement_timestamp() AS statement_time_again;
COMMIT;The second query still returns the same transaction time. Its statement time is about two seconds later because that statement started later.
CURRENT_TIMESTAMP is SQL-standard syntax. now() is PostgreSQL shorthand. Both return a timestamptz value, which represents an instant and is displayed using the session's TimeZone.
Reading the wall clock
clock_timestamp() reads the actual clock each time PostgreSQL evaluates it. It can change during a single statement.
This makes it useful for measuring elapsed work, but usually unsuitable for audit columns that should have one consistent value.
SELECT
statement_timestamp() AS before_wait,
clock_timestamp() AS clock_before_wait,
pg_sleep(2),
clock_timestamp() AS clock_after_wait;The two clock_timestamp() results should differ by approximately two seconds. The pg_sleep expression is included to make the difference visible.
| Function | Stable during | Return type | Typical use |
|---|---|---|---|
| `now()` | Transaction | `timestamptz` | Creation or transaction timestamps |
| `statement_timestamp()` | Statement | `timestamptz` | Statement timing |
| `clock_timestamp()` | No transaction guarantee | `timestamptz` | Measuring real elapsed time |
Current dates and local times
CURRENT_DATE returns the current date according to the session time zone. CURRENT_TIME returns a time with time-zone information. LOCALTIME and LOCALTIMESTAMP return values without time-zone information.
SET TIME ZONE 'America/New_York';
SELECT
current_date,
current_time,
localtime,
localtimestamp,
current_timestamp;The displayed date can change when the session TimeZone changes, even though the underlying instant does not. Use a named time zone when the result must represent a user's local calendar date.
Choosing a function
Use transaction-stable time for values that should agree within one unit of work. Use statement-stable time when each statement needs its own start time. Use clock_timestamp() only when observing the wall clock itself is the goal.
Exercise
Which function should you use when every row inserted by one transaction should receive a consistent creation time?
Show answer
Use CURRENT_TIMESTAMP or now(), because both remain stable for the transaction.