pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Lincoln Swaine-Moore <lswainemoore@gmail.com>
Cc: Marian Wendt <marian.wendt@yahoo.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Strategies for converting UTC data to local windows for arbitrary resolutions and timezones
Date: Wed, 4 Oct 2023 23:10:38 +0200
Message-ID: <952AE7B1-5911-4389-A253-783CD011446D@gmail.com> (raw)
In-Reply-To: <CABcidkLCfH7CPjywobHLq4YNVWd=mS2XWeU4O80qm=_Oqx3LsA@mail.gmail.com>
References: <CABcidkJzoCFTX1Bkn8aCSPgH-3BwR8MO8=uHY2r48s1ynNbbJQ@mail.gmail.com>
	<CAEfWYywLggL0UJbnQxsafqmkkHYzrU494iEDgHxC=JMdCUj5rQ@mail.gmail.com>
	<2582288.1696428710@sss.pgh.pa.us>
	<13eb1e62-9c00-4878-bb82-ef003eca884c@yahoo.com>
	<CABcidkLmpJ6GrW9ma6mQq1vVEjdoLxtxndfr9E-zJNCKa9bPVw@mail.gmail.com>
	<EB0DA992-5D41-470B-A521-41706A6E3EBA@gmail.com>
	<CABcidkLCfH7CPjywobHLq4YNVWd=mS2XWeU4O80qm=_Oqx3LsA@mail.gmail.com>


> On 4 Oct 2023, at 21:30, Lincoln Swaine-Moore <lswainemoore@gmail.com> wrote:
> 
> > What I do in such cases is to add an extra column with the UTC timestamp to serve as a linear scale to the local timestamps. That also helps with ordering buckets in reports and such during DST changes (especially the ones where an hour repeats).
> 
> > For hours and quarter hours I found it to be fairly convenient to base a view on a join between a date calendar and an (quarter of an) hour per UTC day table, but materialising that with some indexes may perform better (at the cost of disk space). I do materialise that currently, but our database server doesn’t have a lot of memory so I’m often not hitting the cache and performance suffers a bit (infrastructure is about to change for the better though).
> 
> That's an interesting idea, but I'm not sure I fully understand. Assuming you're aggregating data: what do you group by? For instance, at an hourly resolution, if you group by both the UTC timestamp and the local one, you might end up, say, dividing an hour-long bucket in two for time zones with half-hour-based offsets, no? 
> 
> Thanks for the detailed writeup! Definitely helpful to learn more about what people are using in production to handle this sort of thing.

Frankly, I haven’t had to deal with half-hour-based offsets since I got this idea. I’m using it with whole-hour-offsets, where it doesn’t affect bin boundaries.

I suppose you could enrich your data in a similar fashion by adding a (virtual) column with the (client) time zone offset, so you could group by local timestamp + offset. That’s not going to match index expressions though, I fear…

For sorting, UTC timestamps would probably still be a useful addition, but they’re simple to add by either converting back from the local timestamps or by taking the min and max of the UTC-based column on the above grouping. Both solutions require that offset, obviously.

Now of course there are only 2 hours a year where this happens. Our data scientists chose to ignore the problem for simplicity’s sake and be slightly off with their numbers on those dates.

Regards,

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






view thread (18+ messages)

Message-ID: <952AE7B1-5911-4389-A253-783CD011446D@gmail.com>
Permalink:  ../952AE7B1-5911-4389-A253-783CD011446D@gmail.com/
Also on:    postgresql.org/message-id/952AE7B1-5911-4389-A253-783CD011446D@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, lswainemoore@gmail.com, marian.wendt@yahoo.com
  Subject: Re: Strategies for converting UTC data to local windows for arbitrary resolutions and timezones
  In-Reply-To: <952AE7B1-5911-4389-A253-783CD011446D@gmail.com>
  References: <CABcidkJzoCFTX1Bkn8aCSPgH-3BwR8MO8=uHY2r48s1ynNbbJQ@mail.gmail.com> <CAEfWYywLggL0UJbnQxsafqmkkHYzrU494iEDgHxC=JMdCUj5rQ@mail.gmail.com> <2582288.1696428710@sss.pgh.pa.us> <13eb1e62-9c00-4878-bb82-ef003eca884c@yahoo.com> <CABcidkLmpJ6GrW9ma6mQq1vVEjdoLxtxndfr9E-zJNCKa9bPVw@mail.gmail.com> <EB0DA992-5D41-470B-A521-41706A6E3EBA@gmail.com> <CABcidkLCfH7CPjywobHLq4YNVWd=mS2XWeU4O80qm=_Oqx3LsA@mail.gmail.com> <952AE7B1-5911-4389-A253-783CD011446D@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox