Konstantinos Gallis
Konstantinos Gallis

Reputation: 1140

Why timestamptz instead of timestamp in timescale?

Reading through the timescale docs, I've seen mentioning many times the following:

When you create a hypertable in Timescale, set the datatype for the time column as timestamptz and not timestamp.

I am wondering, why is this so important?

Does it improve the performance of the database or is it just a strong suggestion?

I am currently having a hypertable with column time whose type is timestamp and this seems to be working fine.

Upvotes: 1

Views: 799

Answers (1)

Zegarek
Zegarek

Reputation: 26302

It's not a technical limitation but rather a generally good advice.
There's no storage or performance difference between the two, it's just that if you use timestamptz from the start, you don't need to switch to it later, after you realise some of your sources operate in different time zones, and possibly after causing some problems by suggesting things coincided in time, while in reality they took place hours and kilometers apart.

It's well put on the PostgreSQL Don't Do This wiki:

timestamptz records a single moment in time. Despite what the name says it doesn't store a timestamp, just a point in time described as the number of microseconds since January 1st, 2000 in UTC. You can insert values in any timezone and it'll store the point in time that value describes. By default it will display times in your current timezone, but you can use at time zone to display it in other time zones.
Because it stores a point in time it will do the right thing with arithmetic involving timestamps entered in different timezones - including between timestamps from the same location on different sides of a daylight savings time change.

timestamp (also known as timestamp without time zone) doesn't do any of that, it just stores a date and time you give it. You can think of it being a picture of a calendar and a clock rather than a point in time. Without additional information - the timezone - you don't know what time it records. Because of that, arithmetic between timestamps from different locations or between timestamps from summer and winter may give the wrong answer.

So if what you want to store is a point in time, rather than a picture of a clock, use timestamptz.

The latest version of the Timescale documentation section you're referring to actually links that same exact wiki page:

Ensure that you set the datatype for the time column as timestamptz and not timestamp. For more information, see PostgreSQL timestamp.

Upvotes: 2

Related Questions