Sub topic 3
What `timestamptz` Really Stores
Assume you know SELECT, INSERT, and CREATE TABLE, and that you understand UTC and UTC offsets. This chapter focuses on PostgreSQL's timestamptz type, which represents an instant on the global timeline.
An instant, not a time zone
Despite its name, timestamptz does not store a time zone name. PostgreSQL normalizes the input to a UTC instant. It does not remember whether the value originally used Europe/Paris, America/New_York, or a +02 offset.
When PostgreSQL displays the value, it converts that instant to the current session's TimeZone setting. Two sessions can therefore see different clock readings for the same stored value.
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 two displayed values identify the same instant. The original +02 offset is not preserved.
| Type | Meaning | Stores an instant? |
|---|---|---|
| `date` | A calendar date without a time | No |
| `timestamp` | A date and clock time without a zone | No |
| `timestamptz` | A date and clock time interpreted as an instant | Yes |
| `time` | A clock time without a date | No |
The session boundary
TimeZone is a per-session setting. It affects how PostgreSQL converts timestamptz values to text for output. It also affects input that does not include an offset.
sequenceDiagram
participant C as Client
participant P as PostgreSQL
C->>P: timestamptz with +02 offset
P->>P: Normalize to one UTC instant
P-->>C: Display using session TimeZoneAn input with an explicit offset is unambiguous:
SELECT TIMESTAMPTZ '2024-06-01 09:00:00+02' =
TIMESTAMPTZ '2024-06-01 07:00:00+00' AS same_instant;
-- tAn input without an offset is interpreted using the session time zone:
SET TimeZone = 'America/New_York';
SELECT TIMESTAMP '2024-06-01 09:00:00'::timestamptz;
-- 2024-06-01 09:00:00-04The plain timestamp means “9:00 on a clock.” The cast adds meaning by treating that clock time as New York time. If the session setting differs, the resulting instant differs too.
What `timestamptz` does not solve
timestamptz is appropriate for events such as payments, log entries, and messages, where the instant matters. It is not enough for a recurring local appointment whose original zone must be retained. For that case, store the instant and a separate zone identifier such as America/Los_Angeles.
Changing TimeZone changes presentation, not the stored instant. This makes it safe to use one timestamptz value across users in different regions, as long as each application handles display deliberately.
Exercise
A row contains 2024-06-01 07:00:00+00. What will a session with TimeZone = 'Asia/Tokyo' display, and does the stored instant change?
Show answer
It will display 2024-06-01 16:00:00+09. The stored instant does not change; only its presentation changes.