Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mT4jx-000258-N3 for pgsql-general@arkaria.postgresql.org; Wed, 22 Sep 2021 16:04:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mT4jw-0003TJ-GS for pgsql-general@arkaria.postgresql.org; Wed, 22 Sep 2021 16:04:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mT4jw-0003TA-42 for pgsql-general@lists.postgresql.org; Wed, 22 Sep 2021 16:04:56 +0000 Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mT4jt-0002pP-AE for pgsql-general@lists.postgresql.org; Wed, 22 Sep 2021 16:04:55 +0000 Received: by mail-io1-xd34.google.com with SMTP id d18so3966110iof.13 for ; Wed, 22 Sep 2021 09:04:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=KzOAqF6DtpviRrqKqGVhULMmrvCUTtarNSx3uKSZ6Qw=; b=lTDK/sm4A20muImR5an4TcZuEB+lPwoorV4dzUtLn6eeQryCutEDkl7fpZHba1ugYl PIqn8ig5sBJ7Op4Ncsy0gUY6ayg/d1R7QRsqvdJ3JiyXHkhUdQXdDq+cBv1mFyy6eZ3d ZBOdfpgSYxrxg6oyDhe+H9L9y4Jqr6LYZ+6SC7a0QZhMegdphNl6TJwig8RFS18w7Gds clmpJP6lsaCvSSwhrAyRzut6ZrNr/JY8LYycR87vbaRxo4c7UiEjryVAcYjz9XjGv5gi wclH9vTkD/yOwoHLfMkodb0S7nJ0VyxRIOmsrwT1S1E7lBppDIPDAaAc177/BqiJpdJH 7evA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=KzOAqF6DtpviRrqKqGVhULMmrvCUTtarNSx3uKSZ6Qw=; b=G6ZgXPOPnB/8MzdnMCTanuX47dU/6yFneTy7fbg6bUlgiG8PMhh3P8aqT6ms5+5QBW TLpyePeEHjgzOToSjnXc3CZcBOxn6qZr/R3jX6QKbM6dQgxpghRBuCg6phw+V25PkGON GEFL3E4AInyvAEu6lLm4jgtCiw08Svt902rGfgWw9GvK6cWu2zdTkVNkhjpmbdGTvJU2 xJOwcuQAnXlVY16GzA4mvMdfZQ+yXAZkWtyV3h1pFhf9pdHb1UdNwneXae6P5k8RkSE7 uvvMRLZ765M2s1Y8tcer1TdI0hMqgARx/+Rn7uWL43JlxnVTFGObjgKaD5ndQLayr7mQ k6HA== X-Gm-Message-State: AOAM533nqfMzfpetzuMsQCiXyX/GrQBKq8Z/voZSBYkUatCnunFlFirg apyOoAV9fHo2CbbXVhCqQrzBieRMkik= X-Google-Smtp-Source: ABdhPJwbs0xgK2kuYGRPXRf8LXtpHb4jVwG+BsYX9Sg1dH9dCnIWglsAEopNInMy3bcC71i01j+qlg== X-Received: by 2002:a02:630f:: with SMTP id j15mr500565jac.62.1632326691430; Wed, 22 Sep 2021 09:04:51 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:cd83:f518:3cde:6c52? ([2601:681:5500:dde0:cd83:f518:3cde:6c52]) by smtp.gmail.com with ESMTPSA id b12sm1138598ioj.55.2021.09.22.09.04.50 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Sep 2021 09:04:51 -0700 (PDT) Subject: Re: Timestamp with vs without time zone. To: pgsql-general@lists.postgresql.org References: <39bdc659-cf20-a078-00cf-14a1bc5a842e@gmail.com> From: Rob Sargent Message-ID: <75529275-01cc-eb90-69fd-65e470d5f30d@gmail.com> Date: Wed, 22 Sep 2021 10:04:49 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------5331D926BBD82F6026C7C9C3" Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------5331D926BBD82F6026C7C9C3 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 9/22/21 9:56 AM, Michael Lewis wrote: > On Wed, Sep 22, 2021 at 12:44 AM cen > wrote: > > 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 ) >> from events AS e >> join tenants AS t ON 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. > > > Does that solve anything? My concern is that for the events at the > begin and end of the where (or group by) we may want to include them > or exclude them depending on the time in that location. If I want to > know how many events happen between 8am and 5pm for each location in > California and New York for example, I'm not certain if a second > timestamp that is "localized" should be stored, or if there is some > better way to achieve that goal. At least for some data in the > evening, the date value will be different between UTC and local > timezone for that location. Just setting my session to an east coast > equivalent timezone when I pull the data still doesn't give me "what > are all the events that happened in California" accurately. The goal > is that the data is accurate for that location. Not as much a concern > if there are only a few hours apart, but if the user is running a > report across properties all over the world... what's the proper way > to ensure each date means the day for that location? If your event doesn't tell you its locale, all is lost. All events happen at some point in UTC-space.  The consumer of the event must adjust UTC according. --------------5331D926BBD82F6026C7C9C3 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 9/22/21 9:56 AM, Michael Lewis wrote:
On Wed, Sep 22, 2021 at 12:44 AM cen <cen.is.imba@gmail.com> wrote:

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 )
from events AS e
join tenants AS t ON 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.


Does that solve anything? My concern is that for the events at the begin and end of the where (or group by) we may want to include them or exclude them depending on the time in that location. If I want to know how many events happen between 8am and 5pm for each location in California and New York for example, I'm not certain if a second timestamp that is "localized" should be stored, or if there is some better way to achieve that goal. At least for some data in the evening, the date value will be different between UTC and local timezone for that location. Just setting my session to an east coast equivalent timezone when I pull the data still doesn't give me "what are all the events that happened in California" accurately. The goal is that the data is accurate for that location. Not as much a concern if there are only a few hours apart, but if the user is running a report across properties all over the world... what's the proper way to ensure each date means the day for that location?

If your event doesn't tell you its locale, all is lost.  
All events happen at some point in UTC-space.  The consumer of the event must adjust UTC according.

--------------5331D926BBD82F6026C7C9C3--