by doctor_eval · 5y ago · 0 comments
There is not enough room here to explain fully but “Timestamp *with* time zone” effectively means UTC time which is the closest equivalent in PG to Unix epoch time and almost certainly what most people want. It doesn’t actually store a time zone.
You’ll thank me later :)
The decision on which type to use IMO depends a lot more on the behaviour of the database driver of your programming language, and which one works better with the type.
If you're not writing an application that uses a db driver, but interacting with pg over psql manually, timestamptz is preferrable because a lot of pg functions works better with timestamptz. If you use timestamp, you would need to be using explicit timezone conversion and be consistent to avoid weird double timezone conversion issues, which makes it far more verbose.
Thanks, I would never have guessed that!
By reading the doc, it seems tome that timestamp without timezone should be used whenever the entirety of time operations are handled by the application. I'd argue that using timestamptz might actually cause bugs due to the conversion, often overlooked because everything is configured to use UTC.
Do you want PG to store the same value or different values?
If you answer 'same', you want with time zone.
Now your answer might be: "make sure your application doesn't do that" in which case your probably fine either way but then you need to make sure you do that correctly.
The problem is that a timestamp of “00:00 1 jan” may have a different UTC time depending on the timezone your client is in. So if your laptop is set to Melbourne time then the UTC time would be “14:00 31 Dec”. If your server is set to UTC then the code you deploy would treat it as 0000 UTC. The behaviour changes depending on environment.
This happens because PG silently converts timestamp to timestamptz for many time operations, so the final result will depend on the environment TZ, which you don’t necessarily control or set. From the perspective of the SQL expression, the behaviour becomes nondeterministic.
If you want to guarantee deterministic behaviour you should use timestamptz which does not require conversion.
Timestamp is really only useful IMO when you have a feature such as “enable this setting from 2pm local time wherever the user is”
Side note: As much as I love PG, the implicit casting between these two incompatible types is pretty baffling.
- moment in time, geo-aware. scalar + geo-value, such as TZ or UTC offset
I (mediocrely) wrote about this years ago: https://cdaringe.com/managing-application-dates
"timestamp without time zone default (now() at time zone 'utc')"
is fine as well and does make the time zone explicit in the type definition.
Let’s say I create a row at “12:34 5 April UTC”. If I now change client time zones and run a query, the epoch time will change to “12:34 5 April EST”. That’s probably not what you want.
The underlying issue is that most PG functions use timestamptz, and PG silently converts timestamp to timestamptz. This conversion uses the local TZ and can lead to nondeterministic behaviour from the perspective of the SQL expression.
Put another way, timestamptz is perfectly stable, timestamp can be nondeterministic, use timestamptz unless you are absolutely sure you know what you are doing.