HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1472 min read

timestamptz vs timestamp

the column that lies about what it stores

Databasesdatabases / data modeling & correctness

The bug report

Your signups-per-day chart says Nov 24 had 11 signups. The admin table, same data, lists 14. Nobody deleted anything. The query looks innocent:

SELECT date_trunc('day', created_at) AS day, count(*)
FROM signups
GROUP BY 1 ORDER BY 1;

Step 1 — look at the column, then the session

\d signups
 created_at | timestamp with time zone | not null

SHOW TimeZone;
 UTC

Your users are in Baghdad (UTC+3). Three of them signed up at 01:30 local — which is 22:30 the previous day in UTC. date_trunc bucketed them into Nov 23. The chart isn't wrong, it's answering a different question: UTC days.

What almost everyone assumes

"It's timestamp with time zone, so Postgres stored the zone. The row knows it came from Baghdad."

Actually

timestamptz stores no time zone at all. It is 8 bytes holding one UTC instant. The zone is used only at the two edges: on input, to convert your literal into UTC; on output, to render it in the session TimeZone. The type name describes behaviour, not storage.

timestamp (without tz) is the genuinely odd one: a wall-clock reading with no instant attached. 2026-11-24 01:30 — somewhere, someday. Two of them from different regions cannot be meaningfully compared or ordered.

The fix — convert first, truncate second

SELECT date_trunc('day', created_at AT TIME ZONE 'Asia/Baghdad') AS day,
       count(*)
FROM signups
GROUP BY 1 ORDER BY 1;

AT TIME ZONE applied to a timestamptz hands you back a timestamp: the wall clock in that zone. Truncate that. On PG14+ prefer the three-arg form, which stays DST-correct and returns timestamptz:

date_trunc('day', created_at, 'Asia/Baghdad')

Four traps that follow

  • AT TIME ZONE is a direction-flipper, overloaded on input type: tz → naive, naive → tz. Apply it twice and you have silently time-travelled.
  • Use IANA names ('Asia/Baghdad'), never '+03'. A fixed offset ignores DST, so every Europe/* report drifts an hour twice a year.
  • WHERE date_trunc('day', created_at, 'Asia/Baghdad') = '2026-11-24' cannot use your index on created_at. Write a half-open range instead — created_at >= '2026-11-24 00:00 Asia/Baghdad' AND created_at < '2026-11-25 00:00 Asia/Baghdad' — and the plain B-tree index works.
  • created_at + interval '1 day' and + interval '24 hours' are not the same value across a DST boundary. Calendar units respect the zone; clock units don't.

The rule of thumb

Store instants as timestamptz. Use naive timestamp only for things that genuinely have no instant — a 09:00 store opening time, a recurring alarm. Pin TimeZone to UTC on the server, keep the display zone in application config, and pass the reporting zone explicitly into every aggregate query.

Your exercise

Predict the output of both lines before running them (Berlin springs forward at 02:00 local on 2026-03-29):

SET TimeZone='UTC';
SELECT '2026-03-29 00:30+00'::timestamptz AT TIME ZONE 'Europe/Berlin';
SELECT '2026-03-29 01:30+00'::timestamptz AT TIME ZONE 'Europe/Berlin';

The inputs are one hour apart. How far apart are the results — and how many rows could a report bucketing 02:00–03:00 Berlin wall-clock possibly return on that date?