HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

What is the reasoning behind PostgreSQL changing the timezone when changing a timestamp with time zone field?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
reasoningpostgresqlfieldthezonetimezonewhattimestampwithtime

Problem

I have a table with a "timestamp with time zone" column.

For one row, it had this value: 2021-06-24 00:00:00+02.

Then, I edited and saved it to be: 2021-06-24 11:00:00-05. Note that both the hour and the timezone has changed.

When I then re-retrieved the value, it was now: 2021-06-24 18:00:00+02.

In other words, it seems to "prefer" the timezone +02 and changes any updated values to be in that timezone, even though I changed the timezone to -05 (because that's the timezone Microsoft gave for their live event). That data is apparently "lost"; seen as unimportant.

Of course, the time is not wrong now, but it's changed in the sense that it isn't explicitly what I changed it to -- just the local timezone version of that time.

This confuses me. What is the reason it was designed like this? Wouldn't it be good to retain the timezone and inputted time for each field? Is it hidden/stored somewhere? If I really want that to be stored, do I have to manually make my own columns and handle this manually? For example, if I want to display something in my control panel that points out that this timestamp origins from this or that timezone, etc.?

Solution

timestamp with time zone does not quite meet the expectations of the SQL standard. It should more appropriately be called “absolute timestamp”.

It is stored as microseconds since 2000-01-01 00:00:00 UTC, and when converted to a string, it is converted to the time zone indicated by the current setting of the timezone parameter in your session.

If you need to retain the original time zone, you will have to store it in an additional column.

Context

StackExchange Database Administrators Q#294695, answer score: 4

Revisions (0)

No revisions yet.