pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: Niels Jespersen <NJN@dst.dk>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: where clauses including timstamptz and intervals
Date: Fri, 9 Apr 2021 12:24:00 +0200
Message-ID: <20210409102400.GA2310@depesz.com> (raw)
In-Reply-To: <df95450aee764d6fbedaf04613d17508@dst.dk>
References: <df95450aee764d6fbedaf04613d17508@dst.dk>

On Fri, Apr 09, 2021 at 07:24:54AM +0000, Niels Jespersen wrote:
> Hello all
> 
> Are these two queries exactly eqivalent? The table is partitioned on
> r_time, which is a timestamptz. The explain plans are not exactly the
> same. The first wants to scan a partition more than the latter. 
> 
> select f.xx from f 
> where f.r_time  >= '2020-10-01 00:00:00+00'::timestamptz
>   and f.r_time < ('2020-10-01 00:00:00+00'::timestamptz + interval '1 month');
> 
> select f.xx from f 
> where f.r_time  >= '2020-10-01 00:00:00+00'::timestamptz
>   and f.r_time < ('2020-11-01 00:00:00+00'::timestamptz);

It depends on timezone.

For example, in my timezone:

$ select ('2020-10-01 00:00:00+00'::timestamptz + interval '1 month'), '2020-11-01 00:00:00+00'::timestamptz;
        ?column?        │      timestamptz       
────────────────────────┼────────────────────────
 2020-11-01 02:00:00+01 │ 2020-11-01 01:00:00+01
(1 row)

Please note that there is 1 hour difference.

The reason is that somewhere in there we change time due to daylight
savings.

If I'd set timezone to UTC, suddently it's the same:

$ set timezone=UTC;
SET

$ select ('2020-10-01 00:00:00+00'::timestamptz + interval '1 month') = '2020-11-01 00:00:00+00'::timestamptz;
 ?column? 
──────────
 t
(1 row)

As usual - time is more complicated than one could expect.

Best regards,

depesz





view thread (6+ messages)  latest in thread

Message-ID: <20210409102400.GA2310@depesz.com>
Permalink:  ../20210409102400.GA2310@depesz.com/
Also on:    postgresql.org/message-id/20210409102400.GA2310@depesz.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: depesz@depesz.com, NJN@dst.dk
  Subject: Re: where clauses including timstamptz and intervals
  In-Reply-To: <20210409102400.GA2310@depesz.com>
  References: <df95450aee764d6fbedaf04613d17508@dst.dk> <20210409102400.GA2310@depesz.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