Sub topic 3
What timestamptz really stores
This chapter assumes you can run queries in psql, know basic CREATE TABLE and INSERT syntax, and know that UTC is a reference time and an offset like +02 is a distance from it. It builds on the overview of the date and time types. Here you will see what timestamptz holds on disk and why the same row can print differently in two sessions.
The name is misleading
timestamptz is short for timestamp with time zone. It does not store a time zone. It stores one point on the universal timeline, an instant, and nothing else. The offset or zone name you typed is used once, to work out which instant you meant, and is then discarded.
Internally the value is an 8-byte integer counting microseconds from 2000-01-01 00:00:00 UTC. The range is 4713 BC to 294276 AD, and the resolution is one microsecond. You can confirm the size:
SELECT pg_column_size(TIMESTAMPTZ '2024-06-01 07:00:00+00') AS bytes;
-- 8timestamp without time zone is also 8 bytes. The two types differ in how PostgreSQL interprets the number, not in how much space it takes.
| Type | What the stored number means | Input offset | Output depends on session TimeZone |
|---|---|---|---|
| `timestamptz` | An instant, counted from UTC | Used to compute the instant, then dropped | Yes |
| `timestamp` | A wall-clock reading with no zone attached | Silently ignored | No |
The conversion boundary
Conversion happens at the edges of the database, not inside it. Input is converted to UTC when it enters. Output is converted from UTC to the session's TimeZone setting when it leaves.
Here is the same row read from two sessions with different settings:
CREATE TABLE event (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
starts_at timestamptz NOT NULL
);
INSERT INTO event (name, starts_at)
VALUES ('launch', '2024-06-01 09:00:00+02');
SET TimeZone = 'America/New_York';
SELECT starts_at FROM event;
-- 2024-06-01 03:00:00-04
SET TimeZone = 'UTC';
SELECT starts_at FROM event;
-- 2024-06-01 07:00:00+00The row did not change between the two queries. The insert said 09:00 at +02, which is 07:00 UTC. New York was at UTC-4 on that date, so the same instant prints as 03:00. The original +02 is gone, and no query can recover it.
What happens to input
How the input is resolved depends on what the text contains.
- If the text has an explicit offset such as
+02or-05:30, PostgreSQL uses it to compute the instant. - If the text has a zone name such as
Europe/Paris, PostgreSQL applies that zone's rules for that date, including daylight saving time. - If the text has neither, PostgreSQL assumes the session
TimeZone.
The third case is the one that causes trouble. The same string means different instants in different sessions:
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-06-01 09:00:00' AS utc_session;
-- 2024-06-01 09:00:00+00
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-06-01 09:00:00' AS ny_session;
-- 2024-06-01 09:00:00-04These are two different instants, four hours apart, from identical text. When you load data from files or an application, include an explicit offset, or set TimeZone deliberately before the insert.
Comparison works on instants
Because every timestamptz is an instant, comparison, sorting, and equality ignore how the value was written:
SELECT TIMESTAMPTZ '2024-06-01 09:00:00+02'
= TIMESTAMPTZ '2024-06-01 07:00:00+00' AS same_instant;
-- tThis is the main benefit of the type. Indexes, ORDER BY, and range filters behave consistently no matter where the data came from. Subtracting two timestamptz values gives the real elapsed time as an interval.
Contrast with timestamp
timestamp without time zone keeps the wall-clock reading as typed. If you give it an offset, PostgreSQL ignores the offset without any warning:
SELECT TIMESTAMP '2024-06-01 09:00:00+02' AS ignored_offset;
-- 2024-06-01 09:00:00The +02 has disappeared. The result is a clock reading with no way to say which instant it was. That is correct for things that really are wall-clock values with no zone, such as a store's opening time of 09:00 or a birthday. It is a bug when the value was meant to be an instant, for example a log entry or a payment time.
The conversion rules also work in the other direction, which is where AT TIME ZONE comes in. That operator is covered in a later chapter. For now, remember that timestamp carries no zone, so there is nothing to convert it from until you say which zone it was.
The epoch view
You can look at the underlying instant as a plain number. extract(epoch FROM ...) returns seconds since 1970-01-01 00:00:00 UTC, and for timestamptz the answer does not depend on the session zone:
SET TimeZone = 'Asia/Tokyo';
SELECT extract(epoch FROM TIMESTAMPTZ '2024-06-01 07:00:00+00') AS epoch_seconds;
-- 1717225200
SET TimeZone = 'UTC';
SELECT extract(epoch FROM TIMESTAMPTZ '2024-06-01 07:00:00+00') AS epoch_seconds;
-- 1717225200The result is the same in both sessions because the instant is the same. For timestamp without time zone, extract(epoch ...) treats the clock reading as if it were UTC, so the number is only meaningful if you stored UTC clock readings in that column.
Where the session zone still leaks in
Output formatting is not the only place TimeZone matters. Any operation that needs a calendar, not just an instant, uses the session zone to pick the calendar. The clearest case is a cast to date:
SET TimeZone = 'UTC';
SELECT (TIMESTAMPTZ '2024-06-01 20:00:00+00')::date AS d;
-- 2024-06-01
SET TimeZone = 'Asia/Tokyo';
SELECT (TIMESTAMPTZ '2024-06-01 20:00:00+00')::date AS d;
-- 2024-06-02The instant is identical, but it is 20:00 on June 1 in London terms and 05:00 on June 2 in Tokyo. The same applies to extract(day FROM ...), extract(hour FROM ...), date_trunc, and to_char. A grouped report built on created_at::date will give different totals depending on who runs it. The fix is to state the zone you mean, either with SET TimeZone or with AT TIME ZONE.
What you lose, and how to keep it
Since the original offset is discarded, timestamptz cannot answer questions like "what time was it on the user's clock when they clicked?" If you need that, store it separately:
- Store the user's or venue's IANA zone name in a
textcolumn, for example'Europe/Paris'. You can then convert the stored instant at read time. - For future events that must stay at a local time even if the zone's rules change, store a
timestamp(the local reading) and the zone name, and compute the instant when needed. - Do not try to keep the offset as a number alone. An offset like
+02does not tell you whether the zone observes daylight saving time on another date.
Ambiguous and special values
A few inputs need care.
- During the autumn clock change, a local reading such as 01:30 occurs twice. During the spring change, some readings do not occur at all. If you give a string without an offset in such a window, PostgreSQL picks one interpretation by its rules. Add an explicit offset to remove the guesswork.
timestamptzaccepts the special valuesinfinityand-infinity, which sort after and before every real instant. They are useful as open-ended bounds, but they break arithmetic, so check for them before computing durations.- Precision can be declared as
timestamptz(n), withnfrom 0 to 6 fractional digits. Values are rounded to that precision on input, not truncated.
Putting it together
When you read a timestamptz value, ask two questions. First, which instant does it represent? That answer never changes after the insert. Second, which zone is the session using to show it? That answer can change from one connection to the next, and it controls the displayed clock time and any date-based operation. Most time zone bugs come from mixing those two questions.
Exercise
A session with TimeZone = 'UTC' runs INSERT INTO event (name, startsat) VALUES ('demo', '2024-06-01 12:00:00'). Another session with TimeZone = 'Asia/Tokyo' then runs SELECT startsat FROM event WHERE name = 'demo'. What does the second session see, and why? (Tokyo is UTC+9 all year.)
Show answer
It sees 2024-06-01 21:00:00+09. The insert had no offset, so the first session interpreted it as 12:00 UTC and stored that instant. The second session renders the same instant in its own zone, which is nine hours ahead. The stored value is unchanged. Only the display differs.