pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: cen <cen.is.imba@gmail.com>
To: Michael Lewis <mlewis@entrata.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Timestamp with vs without time zone.
Date: Wed, 22 Sep 2021 08:43:58 +0200
Message-ID: <39bdc659-cf20-a078-00cf-14a1bc5a842e@gmail.com> (raw)
In-Reply-To: <CAHOFxGpHM8Ka+S2eTSMSR=TRA5DsQ95C+H7Ezrs5d_1UjD6HXQ@mail.gmail.com>
References: <CAGuHJrPKdrXGAFkzE4tXd3b5uKe47WCRdV3LNrYi_HBjYYa+OA@mail.gmail.com>
	<ae03895fc4ba57bce2195919aad40899c7d484b7.camel@cybertec.at>
	<CAGuHJrNeP8H-J9LD-pwO5gaNhSSKSgfn=oBKrH5-ifyoy+WgrA@mail.gmail.com>
	<fc4ba58a-1cfd-24b0-bf0a-7f784b369fbc@gmail.com>
	<CAHOFxGpHM8Ka+S2eTSMSR=TRA5DsQ95C+H7Ezrs5d_1UjD6HXQ@mail.gmail.com>


On 21. 09. 21 23:34, Michael Lewis wrote:
> Related to this current discussion and exchange of ideas... is there a 
> best practice for retrieving data in such a way as the rows are 
> localized to a timezone for where/group by purposes. That is, if I 
> have a table which has events, but those events belong to a tenant or 
> some entity that has a location which implies a timezone (or at least 
> an offset), is there a best way to write a query similar to the below? 
> Please forgive and overlook if there is some obvious syntax error, as 
> this is just a quick and dirty example. Might it make sense to store a 
> "localized" version of the timestamp *without* timezone on the event 
> record such that an index can be used for fast retrieval and even 
> grouping?
>
> select
> date_trunc( 'month', e.event_datetime AT TIMEZONE t.time_zone_nameĀ ) 
> AS event_date,
> count( e.id <http://e.id; )
> from events AS e
> join tenants AS t ON t.id <http://t.id; = e.tenant_id
> where e.event_datetime AT TIMEZONE t.time_zone_name >= 
> '01/01/2021'::DATE AND e.event_datetime AT TIMEZONE t.time_zone_name < 
> '09/01/2021'::DATE;


This is an interesting case. A simplified query example would be to 
"give me all events for this year".

I am not sure what the cost of shifting UTC is, probably not much, but 
depending on use case it might make sense to deconstruct into date and 
time for query optimization.

view thread (35+ messages)  latest in thread

Message-ID: <39bdc659-cf20-a078-00cf-14a1bc5a842e@gmail.com>
Permalink:  ../39bdc659-cf20-a078-00cf-14a1bc5a842e@gmail.com/
Also on:    postgresql.org/message-id/39bdc659-cf20-a078-00cf-14a1bc5a842e@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: cen.is.imba@gmail.com, mlewis@entrata.com
  Subject: Re: Timestamp with vs without time zone.
  In-Reply-To: <39bdc659-cf20-a078-00cf-14a1bc5a842e@gmail.com>
  References: <CAGuHJrPKdrXGAFkzE4tXd3b5uKe47WCRdV3LNrYi_HBjYYa+OA@mail.gmail.com> <ae03895fc4ba57bce2195919aad40899c7d484b7.camel@cybertec.at> <CAGuHJrNeP8H-J9LD-pwO5gaNhSSKSgfn=oBKrH5-ifyoy+WgrA@mail.gmail.com> <fc4ba58a-1cfd-24b0-bf0a-7f784b369fbc@gmail.com> <CAHOFxGpHM8Ka+S2eTSMSR=TRA5DsQ95C+H7Ezrs5d_1UjD6HXQ@mail.gmail.com> <39bdc659-cf20-a078-00cf-14a1bc5a842e@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