pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SV: where clauses including timstamptz and intervals
2+ messages / 1 participants
[nested] [flat]

* SV: where clauses including timstamptz and intervals
@ 2021-04-09 17:39 Niels Jespersen <NJN@dst.dk>
  2021-04-16 06:01 ` SV: where clauses including timstamptz and intervals Niels Jespersen <NJN@dst.dk>
  0 siblings, 1 reply; 2+ messages in thread

From: Niels Jespersen @ 2021-04-09 17:39 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

>-----Oprindelig meddelelse-----
>Fra: Tom Lane <tgl@sss.pgh.pa.us> 
>Emne: Re: where clauses including timstamptz and intervals
>
>Ron <ronljohnsonjr@gmail.com> writes:
>> On 4/9/21 5:24 AM, hubert depesz lubaczewski wrote:
>>> For example, in my timezone:
>>> 
>>> (1 row)
>>> 
>>> Please note that there is 1 hour difference.
>>> The reason is that somewhere in there we change time due to daylight 
>>> savings.
>
>> That looks like a flaw in how "month" is calculated.  Whether or not 
>> October is 744 hours (31 days x 24 hours/day) or 743 hours 
>> (subtracting the "fall back" hour), one month from 01-Oct-2020 is still 01-Nov-2020.
>
>No, it's more likely because the calculation was done in some time zone other than UTC, which (somewhat confusingly) is what the starting value was specified in.  Assuming Hubert meant EU rules, we have
>
>regression=# set timezone = 'Europe/Paris'; SET regression=# select '2020-10-01 00:00:00+00'::timestamptz;
>      timestamptz       
>------------------------
> 2020-10-01 02:00:00+02
>(1 row)
>
>regression=# select '2020-10-01 00:00:00+00'::timestamptz + interval '1 month';
>        ?column?        
>------------------------
> 2020-11-01 02:00:00+01
>(1 row)
>
>which looks to me like the addition did exactly what it's supposed to, ie same local time 1 month later.
>
>			regards, tom lane
>

I hope this sums it up. 

select ('2020-10-01 00:00:00+00'::timestamptz at time zone 'utc' + interval '1 month')             as c1,
       '2020-11-01 00:00:00+00'::timestamptz at time zone 'utc'                                    as c2,
       ('2020-10-01 00:00:00+00'::timestamptz at time zone 'utc' + interval '1 month') =
       ('2020-11-01 00:00:00+00'::timestamptz at time zone 'utc')                                  as c3,
       ('2020-10-01 00:00:00+00'::timestamptz at time zone 'Europe/Brussels' + interval '1 month') as c4,
       '2020-11-01 00:00:00+00'::timestamptz at time zone 'Europe/Brussels'                        as c5,
       ('2020-10-01 00:00:00+00'::timestamptz at time zone 'Europe/Brussels' + interval '1 month') =
       ('2020-11-01 00:00:00+00'::timestamptz at time zone 'Europe/Brussels')                      as c6;

returns 

         c1          |         c2          | c3 |         c4          |         c5          | c6
---------------------+---------------------+----+---------------------+---------------------+----
 2020-11-01 00:00:00 | 2020-11-01 00:00:00 | t  | 2020-11-01 02:00:00 | 2020-11-01 01:00:00 | f
(1 row)

The answer to my original problem behind my original question which is how to query using utc timing in order to hit exactly the (utc created) partition, without changing the session timezone, would be like this:

select f.* from f
where f.r_time at time zone 'utc' >= '2020-10-01 00:00:00+00'::timestamptz at time zone 'utc'
  and f.r_time at time zone 'utc' < ('2020-10-01 00:00:00+00'::timestamptz at time zone 'utc' + interval '1 month');

I think maybe it's better to just change the session timezone when this need arises. 

Thank you all for feedback. 

Regards Niels



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* SV: where clauses including timstamptz and intervals
  2021-04-09 17:39 SV: where clauses including timstamptz and intervals Niels Jespersen <NJN@dst.dk>
@ 2021-04-16 06:01 ` Niels Jespersen <NJN@dst.dk>
  0 siblings, 0 replies; 2+ messages in thread

From: Niels Jespersen @ 2021-04-16 06:01 UTC (permalink / raw)
  To: Francisco Olarte <folarte@peoplecall.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

>-----Oprindelig meddelelse-----
>Fra: Francisco Olarte <folarte@peoplecall.com> 
>Sendt: 10. april 2021 09:31
>Til: Niels Jespersen <NJN@dst.dk>
>
>Although it is equivalent I would suggest to use:
>
>f.r_time< ('2020-10-01 00:00:00+00'::timestamptz at time zone 'utc' + interval '1 month') at time zone 'utc' ;
>
>optimizer should treat both the same, but I'm no sure it can recognize it, and it may not see your condition is of the type "field < constant" to select index scans and similar things. Also, you can refactor your calculations into an stable function for greater clarity.

The plannes seems to be able to figure this out nicely. 
>
>> I think maybe it's better to just change the session timezone when this need arises.
>
>Maybe. Also, I do not see the need to query exact partitions by tstz range, but assuming you do it may be much easier to query the catalog for the partition and then query the partition. ( In many years of working with time-partitioned tables I have never got the need to build a query to hit exactly X partitions from times ).
>
I need to hit an exact partition, because the query makes some QA on data coming from a file that fits that exact timeslot. File and partition must match exactly because the file may be raplaced later and the partition truncated. 

Thank you for your insights.

Niels

>Francisco Olarte.


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2021-04-16 06:01 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-04-09 17:39 SV: where clauses including timstamptz and intervals Niels Jespersen <NJN@dst.dk>
2021-04-16 06:01 ` Niels Jespersen <NJN@dst.dk>

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