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 1mSvzE-0002y9-AQ for pgsql-general@arkaria.postgresql.org; Wed, 22 Sep 2021 06:44:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mSvzD-0002TY-7r for pgsql-general@arkaria.postgresql.org; Wed, 22 Sep 2021 06:44:07 +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 1mSvzC-0002TP-SH for pgsql-general@lists.postgresql.org; Wed, 22 Sep 2021 06:44:06 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mSvz8-0006DT-KC for pgsql-general@postgresql.org; Wed, 22 Sep 2021 06:44:06 +0000 Received: by mail-ed1-x533.google.com with SMTP id co2so5851234edb.8 for ; Tue, 21 Sep 2021 23:44:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=9C6yBUjEVx/liqFGR5KUFo5YWoWDtjSlx6ClQRkhIhw=; b=eNQI2OUtarsYhWtfFPJjhfKUyHywkG54s41gHGTg/uaBB5bXydMY73vHOoEFC+9JGI f9+v5AuVlTH/mJZu7TBQa83gkA75I9L2WxezS6KyHoRmORbsfizMkiVvxXvspfrW0xbV NQMEbSraYuZImLdoAhIJdqi+WzxxfusfT2VonmBaZL+T2AesJKl/ZjWYtX0PRpkTtnV7 0lC3pV0fq5kG5soMus23+obNgtKF2PY5EqM8HGRpy8w1aQ4AUvezkbzw8cmRT0zkABbC ZRGYv9t3w+/DM9Wt14AwHDll57fbAE88EIWXuUAj3qXqfCWYl+rAtr/CNpoXTq6OSNDv upYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=9C6yBUjEVx/liqFGR5KUFo5YWoWDtjSlx6ClQRkhIhw=; b=QcP971vUk+h0p2HZEf+GvwctRxXVE7+/oQld1+WX7IY5Qz1Oh0qMOWLIA5Xron6LTV 6g5WFDiYWo5CrwtM0EnTVBhj5A77Dw2FcDzoYEWipeGDxWxTucXc68Arg63iBZlIVR+x ZHUWRtlkBSeWHaSQ7rMS/rbkAkqchnwjmfFES0N66ogqGaqxASfxbHqGvjrRLfQ6afKY rsOZXRz/l8xHYVraP7RTCirLru+HU6uPukEo3qEaXJMKm4yd5CNVDt6fatk3OKssbvpW WYKVRf53Xj4r/UkEQ2Y6SgVuBWUtGxx1WoobkWozZmU/sTMcOEXeGyAxmAHGpx0xlst0 wQBw== X-Gm-Message-State: AOAM532y6SyX/tTkVDFjs7MlHP4u9LV9jVBFWX0AY5qZ+pC0Sn1AUjKm UQlOTDWJG+fo4rbl39Z7qq52EUeN0UE= X-Google-Smtp-Source: ABdhPJzDsa/+F2QWzOQst1CbDcTMVnklyMFPfo8C9gm38PA+4oQwOK7HvdsuGpwFYyzxhhFQQxK9XQ== X-Received: by 2002:aa7:d649:: with SMTP id v9mr41108651edr.38.1632293040578; Tue, 21 Sep 2021 23:44:00 -0700 (PDT) Received: from cens-MBP.int.sunesis.si (n60h206.net.telprom.si. [178.216.60.206]) by smtp.gmail.com with ESMTPSA id m10sm539171ejx.76.2021.09.21.23.43.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Sep 2021 23:44:00 -0700 (PDT) Subject: Re: Timestamp with vs without time zone. To: Michael Lewis Cc: pgsql-general References: From: cen Message-ID: <39bdc659-cf20-a078-00cf-14a1bc5a842e@gmail.com> Date: Wed, 22 Sep 2021 08:43:58 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------97D094940AFD8705BE83C688" Content-Language: sl 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. --------------97D094940AFD8705BE83C688 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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. --------------97D094940AFD8705BE83C688 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


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.

--------------97D094940AFD8705BE83C688--