Sub topic 7
Getting the current time
PostgreSQL has several functions that return the current time, and they do not all mean the same thing. Some return the time your transaction began. Others return the time on the server's clock right now. Picking the wrong one produces bugs such as identical timestamps on every row of a batch, "elapsed time" measurements that always read zero, or audit columns that look wrong. This chapter assumes you know timestamptz, the session TimeZone setting, and basic transaction control (BEGIN, COMMIT).
Three clocks: transaction, statement, and wall clock
PostgreSQL distinguishes three notions of "now":
- Transaction time is fixed when the transaction starts and does not change until it ends.
- Statement time is fixed when the server receives the current command from the client.
- Wall-clock time is the real time at the moment the function is called. It can change during a single statement.
The table lists the functions. Each one maps onto one of these clocks.
| Function | Return type | Value reflects |
|---|---|---|
| `now()` | `timestamptz` | Start of the current transaction |
| `CURRENT_TIMESTAMP` | `timestamptz` | Same as `now()` |
| `transaction_timestamp()` | `timestamptz` | Same as `now()` |
| `statement_timestamp()` | `timestamptz` | Start of the current statement |
| `clock_timestamp()` | `timestamptz` | Actual time at the moment of the call |
| `timeofday()` | `text` | Actual time, as a formatted string |
| `CURRENT_DATE` | `date` | Transaction start, as a date in the session zone |
| `CURRENT_TIME` | `timetz` | Transaction start, time of day with offset |
| `LOCALTIMESTAMP` | `timestamp` | Transaction start, wall clock in the session zone |
| `LOCALTIME` | `time` | Transaction start, time of day in the session zone |
now(), CURRENT_TIMESTAMP, and transaction_timestamp() are three spellings of the same value. CURRENT_TIMESTAMP, CURRENT_DATE, CURRENT_TIME, LOCALTIME, and LOCALTIMESTAMP are SQL-standard keywords, so you write them without parentheses. now() is a regular function and needs them.
now() is frozen for the transaction
The surprise that catches most people is that now() does not tick. Run this in psql:
BEGIN;
SELECT now() AS txn_start, clock_timestamp() AS wall_clock;
-- txn_start: 2026-11-27 21:20:24.799+00
-- wall_clock: 2026-11-27 21:20:24.801+00
SELECT pg_sleep(3);
SELECT now() AS txn_start, clock_timestamp() AS wall_clock;
-- txn_start: 2026-11-27 21:20:24.799+00 (unchanged)
-- wall_clock: 2026-11-27 21:20:27.804+00 (about 3 seconds later)
COMMIT;Outside an explicit transaction, each statement runs in its own implicit transaction. That is why SELECT now(); in psql seems to change from one command to the next. The values differ because each command is a separate transaction.
Freezing is deliberate. Every row written by a transaction gets the same now(), so you can say "these changes happened together." Two rows inserted in the same transaction compare as simultaneous. A function called in the middle of the transaction sees the same now() as the code that called it. This is also why now() is a good default for created_at columns: a parent row and its child rows share one timestamp.
The same behavior has a cost. A transaction that stays open for an hour, such as a long batch job or an application holding a connection in idle in transaction, writes timestamps from an hour ago. If you need the true time of each write, use clock_timestamp().
statement_timestamp() and clock_timestamp()
statement_timestamp() returns the time the server received the latest command message from the client. Inside a multi-statement transaction it advances with each statement, but it is constant within a single statement. It is useful when you want per-statement time without the cost of reading the clock for every row.
clock_timestamp() is volatile. PostgreSQL calls it every time it is evaluated, so it can return different values for different rows of the same query:
SELECT g,
now() AS txn_ts,
clock_timestamp() AS clock_ts
FROM generate_series(1, 3) AS g;
-- txn_ts is identical on all three rows.
-- clock_ts increases slightly from row to row.A typical use is measuring how long work took:
DO $$
DECLARE
t0 timestamptz := clock_timestamp();
BEGIN
PERFORM pg_sleep(1.5);
RAISE NOTICE 'elapsed: %', clock_timestamp() - t0;
END
$$;
-- NOTICE: elapsed: 00:00:01.50xxxxIf you had used now() for both readings, the result would be 00:00:00, because both would return the transaction start. timeofday() is the older text-returning equivalent of clock_timestamp(). Prefer clock_timestamp() in new code, since it returns a real timestamptz you can do arithmetic on.
Time zones and the "local" variants
All of these functions are instants, so the underlying moment does not depend on the session zone. What changes is how it is displayed and how the date-only and time-only forms are derived. CURRENT_DATE is a date in the session's TimeZone, so the same instant can give different dates in different zones:
-- Suppose the real time is 2026-11-27 21:20 UTC.
SET TimeZone = 'UTC';
SELECT current_date;
-- 2026-11-27
SET TimeZone = 'Pacific/Auckland'; -- UTC+13 at this time of year
SELECT current_date;
-- 2026-11-28This matters for jobs like "process everything for today." The meaning of "today" depends on the session zone, so pin the zone explicitly when the answer must be reproducible, either with SET TimeZone or by writing (now() AT TIME ZONE 'Europe/Paris')::date.
LOCALTIMESTAMP returns a timestamp without time zone holding the session's wall-clock reading. CURRENT_TIME returns timetz, a type the PostgreSQL documentation discourages because a time of day with an offset but no date cannot account for daylight saving time. Both exist for SQL-standard compatibility. In practice, use now() and convert with AT TIME ZONE when you need a wall-clock value.
You can request fewer fractional digits from the keyword forms:
SELECT CURRENT_TIMESTAMP(0), LOCALTIMESTAMP(3);
-- 2026-11-27 21:20:25+00 (rounded to whole seconds)
-- 2026-11-27 21:20:24.799 (milliseconds)The default precision is microseconds. Rounding rounds to the nearest value rather than truncating, so CURRENT_TIMESTAMP(0) can be up to half a second later than the real transaction start.
Using the current time in columns and queries
The standard pattern for a creation timestamp is a default:
CREATE TABLE audit_log (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
action text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);A default is evaluated at each insert, so it uses the inserting transaction's now(). Use DEFAULT clock_timestamp() only if rows inserted in one transaction must be ordered by real time.
Relative filters read naturally with now() and an interval:
SELECT *
FROM audit_log
WHERE created_at >= now() - interval '7 days';This is a rolling window: the last 168 hours. If you want "the last seven calendar days in a given zone," truncate to a local day boundary first, as covered in the chapters on date_trunc and range queries.
now() is STABLE, not IMMUTABLE. It cannot change within a single statement, but it differs between statements. Two consequences follow:
- You cannot use it in an index expression, a partial index predicate, or a generated column. A predicate such as
WHERE created_at > now() - interval '1 day'fails with an error saying functions in an index predicate must be markedIMMUTABLE. - A query that uses
now()can still use an ordinary index oncreated_at, because the planner evaluatesnow()once per statement and treats it as a constant.
Testing and reproducibility
Code that calls now() directly is hard to test, because the result changes every run and you cannot override the function. Two practical approaches:
- Pass the reference time as a parameter. A function with a signature like
report(as_of timestamptz DEFAULT now())behaves normally in production and is deterministic in tests. - In a test script, keep everything in one transaction, so
now()stays constant while you inspect results.
Also remember that now() reads the database server's clock, not the client's. If application servers and the database disagree on the time, timestamps stamped by the app and by now() will not compare cleanly. Prefer one source of truth, usually the database.
Common errors
- Measuring duration with
now(). Both readings are equal inside one transaction, so the result is zero. Useclock_timestamp(). - Expecting different values per row.
now()returns the same value for every row. Useclock_timestamp()if you need row-level time. - Stale timestamps in long transactions. An open transaction keeps its old
now(). Keep transactions short, or useclock_timestamp(). - Assuming
CURRENT_DATEis the UTC date. It follows the sessionTimeZone. - Putting
now()in an index predicate. It is not immutable. Filter at query time instead. - Storing
LOCALTIMESTAMPorCURRENT_TIME. These drop the zone information. Storetimestamptzfromnow().
Exercise
A batch job runs inside one transaction and inserts 10,000 rows, each with processed_at timestamptz DEFAULT now(). The job takes 40 seconds. The team wants processed_at to show roughly when each row was actually written, and wants to compute total elapsed time at the end. What should they change, and what would the elapsed-time calculation look like?
Show answer
Change the default to clock_timestamp(), because now() returns the transaction start for every row, so all 10,000 rows get the same value. For elapsed time, capture t0 := clock_timestamp() at the start and compute clock_timestamp() - t0 at the end. Using now() - t0 or now() - now() would give a result of zero or the time since the transaction began, not the real elapsed time.
Exercise
At 23:30 UTC on 2026-06-01, a session with TimeZone = 'Asia/Tokyo' runs SELECT current_date;. What date is returned, and why does the same query in a UTC session return a different one?
Show answer
Tokyo is UTC+9, so the local time is 08:30 on 2026-06-02 and current_date returns 2026-06-02. A UTC session returns 2026-06-01. current_date is derived from the transaction start instant, interpreted in the session TimeZone, so the same instant yields different dates in different zones.